What is the best Linux kernel locking mechanism for a particular scenario?

I need to solve the lock problem for this scenario:

  • Multiprocessor system.
  • All CPUs use a common (software) resource.
  • Read-only access to the resource is very common. (Processing incoming network packets)
  • Recording access is much less common. (Quite a lot of configuration changes).

I am currently using the mechanism read_lock_bh, write_lock_bh(spinlocks). The problem is that the more processors, the more I get soft locks in the recording context.

I read the concurrency chapter in this book , but I couldn’t understand whether the reader or writer would get priority when using spin locks.

So the questions are:

  • Does the Linux spinlock mechanism give priority to the reader / writer / none of them?
  • Is there a better mechanism I can use to avoid these soft locks in my script, or maybe a way to give priority to a writer whenever he tries to get a lock using my current solution?

Thanks Nir

+5
source share
3 answers

Here's a direct quote from Essential Linux Device Drivers , which may be what you are looking for. It seems that the part related to RCU at the end may be what you are interested in.

Read / write locks

concurrency - -. , , , . . - :

rwlock_t myrwlock = RW_LOCK_UNLOCKED;

read_lock(&myrwlock);             /* Acquire reader lock */
/* ... Critical Region ... */
read_unlock(&myrwlock);           /* Release lock */

, , . :

rwlock_t myrwlock = RW_LOCK_UNLOCKED;

write_lock(&myrwlock);            /* Acquire writer lock */
/* ... Critical Region ... */
write_unlock(&myrwlock); /* Release lock */

IPX, net/ipx/ipx_route.c, - . - ipx_routes_lock IPX . . , . , , .

, irq, read_lock_irqsave(), read_lock_irqrestore(), write_lock_irqsave() write_lock_irqrestore(). -.

seqlocks , 2.6, -, . , , . jiffies_64, . , . - , , :

u64 get_jiffies_64(void) /* Defined in kernel/time.c */
{
   unsigned long seq;
   u64 ret;
   do {
      seq = read_seqbegin(&xtime_lock);
      ret = jiffies_64;
   } while (read_seqretry(&xtime_lock, seq));
   return ret;
}

, write_seqlock() write_sequnlock().

2.6 , Read-Copy Update (RCU), , , . , . . , . . , RCU , , , . RCU include/linux/rcupdate.h. Documentation/RCU/*.

RCU fs/dcache.c. Linux ( dentry), ( inode) ( ). , , , . , dcache, . dcache , dcache, dcache RCU.

+5
+3

If the work you do while holding the lock is small, you can try a regular non-reader mutex. It is more efficient.

0
source

All Articles