The slides cover the various kernel synchronization mechanisms such as Atomic operations, Mutex, Semaphores, reader/writer semaphores. Waiting mechanisms such as wait queues. Waiting on the multiple file descriptors with select & poll
1. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Kernel Process Management
2. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What to Expect
●
Need for synchronization
●
Atomic Variables
●
Mutex
●
Semaphore
●
Spinlocks
●
Making process wait
●
Basic wait mechanism
●
Wait queues
●
Poll & Select
3. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Need for Synchronization
●
Multiple tasks run in the system at the same
●
Might be sharing the resources
●
May result in memory leak or data corruption
●
Avoiding the global variables could resolve the
issue
●
If can’t be avoided, need to use the
synchronization mechanism
4. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
●
Operations that gurantee indivisibility &
uninterruptibility
●
CPU instructions that provide the atomic read-
modify-write operations on a memory location
●
Handy for manipulation of shared data
atomically in the multithreaded scenario
●
Linux kernel uses this for reference counting in
shared data structures, wait-notify flags
Atomic Operations
5. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Atomic Integer Operations
●
Generic atomic operation interfaces include support for integer
& bitwise operations
●
Integer operations operate on atomic_t & atomic64_t
●
Operations
●
ATOMIC_INIT(i)
●
atomic_read(v)
●
atomic_set(v, i)
●
atomic_add(init i, atomic_t *v)
●
atomic_sub(int i, atomic_t *v)
●
atomic_inc(atomic_t *v)
●
atomic_dec(atomic_t *v)
8. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Semaphore
●
Counter
– Maintains the count of the resources
●
Two operations
– up/post
– down/wait
●
Process calling the down/wait blocks if the value is 0
10. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Reader/Writer Semaphore
●
#include <linux/rwsem.h>
●
struct rw_semaphore
●
Allows one writer or unlimited number of readers to hold the semaphore
●
Initialization
– void int_rwsem(struct rw_semaphore *rwsem)
●
Operations for readers
– void down_read(struct rw_semaphore *rwsem)
– int down_read_trylock(struct rw_semaphore *rwsem)
– void up_read(struct rw_semaphore *rwsem)
●
Operations for writers
– void down_write(struct rw_semaphore *rwsem)
– int down_write_trylock(struct rw_semaphore *rwsem)
– void up_write(struct rw_semaphore *rwsem)
11. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spinlocks
●
Two operations
– Lock
– Unlock
●
Difference from the mutex
– Doesn’t puts the process to sleep, if lock is
not available
– Instead spins
– That’s why the name
13. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spinlock APIs...
●
spin_lock_irqsave(&my_lock, flags)
– Disables interrupts on the local processor
– Interrupt state is saved in flags
●
spin_lock_irq(&my_slock)
– Disables interrupts, but doesn't keeps track of flags
●
spin_lock_bh
– Disables software interrupts only
14. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spin unlock APIs
●
void spin_unlock_irqrestore(&my_slock, flags)
●
Unlocks & restores the irqs
●
void spin_unlock_irq(&my_slock)
●
void spin_unlock_bh(&my_slock)
15. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Kernel Threads
●
Basic unit of CPU utilization
●
Also, known as Light weight process
●
Implemented as POSIX threads in user space,
commonly known as pthread
●
Multiple threads share the same address space
in a process
●
Communication is usually through global
variables
16. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Kernel Threads...
●
Exists in Kernel space
●
Used to perform some background tasks
●
Are descended from kthreadd
●
Can be viewed in user space with command
– ps -fx or ps -mx
●
Examples
– khubd
– ksoftirqd
– kworker
17. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Creating a Kernel thread
●
#include <kthread.h>
●
kthread_create(int (*fn)(void *data), const char name[],
...)
●
Parameters
– fn – The function that the thread has to execute
– data – The ‘data’ to the passed to the function
– name – The name by which the thread will be
recognized in the kernel
●
Returns
– Pointer to the structure of type task_struct
18. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waking Up & Stopping a Thread
●
wake_up_process(struct task_struct *)
– Functiion passed to the kthread_create() gets
executed
– Parameters
●
Pointer to struct of type task_struct
●
kthread_stop(struct task_struct *)
– Waits for the thread to stop
– Parameters
●
Pointer to structure of type task_struct
– Returns
●
Result of the thread function
19. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Running a Thread
●
kthread_run(int (*function)(void *data), void
*data, const char name[])
– Creates & starts the threads
– Parameters
●
function – the function that the thread has
to execute
●
data – The ‘data’ to be passed to the
functions
●
name – The name by which the thread
would be recognized in the kernel
20. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Making Process Wait
●
While dealing with hardware, process needs to
wait for an event:
– Specified amount of time
– Data from file I/O
– Waiting on semaphore or mutex
●
Task moves out of run queue and waits for an
event
●
Task can be in TASK_INTERRUPTIBLE or
TASK_UNINTERRUPTIBLE state
21. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Basic wait mechanism
●
schedule()
– Used by the processes to voluntarily
relinquish the CPU
– Trigger the scheduler
●
Set_current_state(state)
– Sets the state of the process to:
●
TASK_INTERRUPTIBLE
●
TASK_UNINTERRUPTIBLE
●
TASK_RUNNING
22. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waking up
●
wake_up_process(struct task_struct *ts)
– Wakes up the process & changes the
process state to TASK_RUNNING
23. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Wait Queues
●
Queue of sleeping processes waiting for an event
●
#include <linux/wait.h>
●
Data structure
– wait_queue_head_t
●
Created statically
– DECLARE_WAIT_QUEUE_HEAD(wait_queue_na
me)
●
Created dynamically
– wait_queue_head_t my_queue
– init_waitqueue_head(&my_queue)
24. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Variants for wait
●
Wait_event(queue, condition)
●
Wait_event_interruptible(queue, condition)
●
Wait_event_timeout(queue, condition, timeout)
●
Wait_event_interruptible_timeout(queue,
condition, timeout)
25. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waking up the Process
●
Wake up family of functions
●
wake_up(wait_queue_head_t *)
– Wakes up all the processes waiting on the
queue
●
wakes_up_interruptible(wait_queue_head_t *)
– Wakes up only the processes performing the
interruptible sleep
26. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waiting using wait queue entry
●
Intialize wait queue entry
– Statically
●
DEFINE_WAIT(wait_queue)
– Dynamically
●
wait_queue_t wait_queue
●
init_wait(&wait_queue)
●
Add an entry to the queue
– Void prepare_to_wait()
●
Parameters
– Pointer to wait_queue_head_t
– Pointer to wait_queue_t
– State
●
Clean up
– Void finish_wait()
●
Pointer to wait_queue_head_t
●
Pointer to wait_queue_t
27. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Thundering herd problem
●
‘wake_up’ awakens all the processes waiting on
a wait queue
●
Only one will succeed in obtaining the desired
resource & rest will to sleep
●
For large number of processes on wait queue,
this ‘thundering herd’ may degrade the system
performance
28. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Exclusive wait
●
Exclusive waiters get added to the end of the
wait queue
●
wake_up on the wait queue stops after
awakening the first exclusive waiter
●
void prepare_to_wait_exclusive() sets the
‘exclusive’ flag in wait queue
●
Can’t be performed using wait_event()
29. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Poll & select
●
Allows an application to wait on more than one
inputs or outputs
●
Application blocks, if no data is available
●
Example, hyperterminal/minicom needs to wait
on user inputs as well as serial data
●
Multiple system calls for same functionality
– poll – System V solution
– select – BSD unix
– epoll – for supporting thousand’s of file
descriptors
30. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Driver support for select & poll
●
#include <linux/poll.h>
●
Support in driver is provided through poll method
– unsigned int (*poll) (struct file *filp, poll_table *wait)
●
Adds the wait_queues to the poll_table that can
indicate the change in the poll status
– Void poll_wait(struct file *, wait_queue_head_t *,
poll_table *)
●
Returns a bit mask for the operations that can be
performed without blocking
●
POLLIN, POLLRDNORM, POLLWRNORM
31. @ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What all did we learn?
●
Need for synchronization
●
Atomic Variables
●
Mutex
●
Semaphore
●
Spinlocks
●
Making process wait
●
Basic wait mechanism
●
Wait queues
●
Poll & Select