Analysis of Kernel Locking Mechanisms - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Analysis of Kernel Locking Mechanisms

Description:

Operate on a bit or an integer. Assembler or C functions dep. on arch ... Atomic Integer Operations. Bit Operations. Wait Queues. Capabilities and Restricted ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 18
Provided by: foss
Category:

less

Transcript and Presenter's Notes

Title: Analysis of Kernel Locking Mechanisms


1
  • Analysis of Kernel Locking Mechanisms
  • for Linux Kernel Programming
  • Dr. B. Thangaraju
  • Manager Talent Transformation
  • Wipro Technologies, Bangalore.

2
Locking Mechanisms
  • Kernel Control Path
  • Global Data
  • Shared Resources
  • Non-preemptive Kernel
  • Disable Interrupts
  • Atomic Operations
  • Locking Mechanism

3
Choices of Lock
  • Semaphore
  • Spin Lock
  • Integer and Bit Operations
  • Wait Queue
  • Capabilities and Restricted

  • Operations

4
Semaphore
  • Synchronization Tool
  • An Integer Number
  • P ( ) And V ( ) Operators
  • Busy Waiting
  • Types of Semaphore
  • Implementation
  • Expensive

5
Semaphore
  • include ltasm/semaphore.hgt
  • struct semaphore atomic_t count int sleepers
  • wait_queue_head_t wait
  • void sema_init (struct semaphore sem, int value
    )
  • int down (struct semaphore sem)
  • int down_interruptible (struct semaphore sem)
  • void up (struct semaphore sem)

6
Spin Lock
  • Little Locking Overhead
  • Ideal for Small Critical Section
  • Never Put a Process to Sleep
  • Spin in Busy-Wait Loop
  • include ltlinux/spinlock.hgt
  • typedef struct
  • volatile unsigned int lock
  • spinlock_t

7
Spin Lock- Functions
  • spin_lock_init ( spinlock_t lock)
  • spin_lock (spinlock_t lock)
  • spin_unlock ( spinlock_t lock)
  • spin_is_locked ( spinlock_t lock)
  • spin_trylock ( spinlock_t lock)
  • spin_lock_irqsave ( spinlock_t lock, unsigned
    long flags)
  • spin_lock_irqrestore ( spinlock_t lock, unsig
    long flags )
  • spin_lock_irq (/bh) (spinlock_t lock)
  • spin_unlock_irq (/bh) (spinlock_t lock)

8
Spin Lock reader and writer
  • rwlock_t my_lock RW_LOCK_UNLOCKED
  • read_lock (rwlock_t lock)
  • read_lock_irqsave (rwlock_t lock, unsigned long
    flags)
  • read_lock_irq (rwlock_t lock)
  • read_lock_bh (rwlock_t lock)
  • write_lock (rwlock_t lock)
  • write_lock_irqsave (rwlock_t lock, unsigned long
    flags)
  • write_lock_irq (rwlock_t lock)
  • write_lock_bh (rwlock_t lock)

9
Atomic Lock
  • Guaranteed to be an atomic even on SMP
  • Operate on a bit or an integer
  • Assembler or C functions dep. on arch
  • typedef volatile int counter
  • atomic_t
  • include ltasm/bitops.hgt
  • include ltasm/atomic.hgt

10
Atomic Integer Operations
  • void atomic_add (atomic_t i, atomic_t v)
  • void atomic_sub (atomic_t i, atomic_t v)
  • int atomic_sub_and_test (atomic_t i, atomic_t
    v)
  • void atomic_inc (atomic_t v)
  • void atomic_dec (atomic_t v)
  • int atomic_dec_and_test (atomic_t v)

11
Atomic bit Operations
  • void set_bit (int nr, void addr)
  • void clear_bit (int nr, void addr)
  • void change_bit (int nr, void addr)
  • test_bit (int nr, void addr)
  • int test_and_set_bit (int nr, void addr)
  • int test_and_clear_bit (int nr, void addr)
  • int test_and_change_bit (int nr, void addr)

12
Wait Queues
  • A queue of a processes
  • waiting for an event.
  • Used in
  • page allocator
  • kswapd kernel daemon
  • device drivers.
  • Many ways to ask a process to wait
  • Many ways to wakeup a process

13
Wait Queues - Functions
  • wait_queue_head_t my_queue //
    declaration
  • init_waitqueue_head (my_queue) //
    initialization
  • sleep_on (wait_queue_head_t queue)
  • interruptible_sleep_on (wait_queue_head_t
    queue)
  • sleep_on_timeout (wait_queue_head_t queue, long

  • timeout)
  • interruptible_sleep_on_timeout (wait_queue_head_t

  • queue, long timeout)
  • void wait_event (wait_queue_head_t queue, int
    condition)
  • int wait_event_interruptible (wait_queue_head_t
    queue,

  • int condition)

14
Wait Queues - Functions
  • Ways of Wake Up
  • wake_up (wait_queue_head_t queue)
  • wake_up_interruptible (wait_queue_head_t queue)
  • wake_up_sync (wait_queue_head_t queue)
  • wake_up_interruptible_sync (wait_queue_head_t
    queue)

15
Restricted Operations
  • Restricted for certain operations by capabilities
  • include ltlinux/capability.hgt
  • CAP_DAC_OVERRIDE
  • CAP_NET_ADMIN
  • CAP_SYS_MODULE
  • CAP_SYS_RAWIO
  • CAP_SYS_ADMIN
  • CAP_SYS_TTY_CONFIG

16
Summary
  • Locking Mechanisms
  • Semaphore
  • Spin Lock
  • Atomic Integer Operations
  • Bit Operations
  • Wait Queues
  • Capabilities and Restricted Operations

17
  • Thank You
Write a Comment
User Comments (0)
About PowerShow.com