Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

locking/qrwlock: Better optimization for interrupt context readers

The qrwlock is fair in the process context, but becoming unfair when
in the interrupt context to support use cases like the tasklist_lock.

The current code isn't that well-documented on what happens when
in the interrupt context. The rspin_until_writer_unlock() will only
spin if the writer has gotten the lock. If the writer is still in the
waiting state, the increment in the reader count will cause the writer
to remain in the waiting state and the new interrupt context reader
will get the lock and return immediately. The current code, however,
does an additional read of the lock value which is not necessary as
the information has already been there in the fast path. This may
sometime cause an additional cacheline transfer when the lock is
highly contended.

This patch passes the lock value information gotten in the fast path
to the slow path to eliminate the additional read. It also documents
the action for the interrupt context readers more clearly.

Signed-off-by: Waiman Long <Waiman.Long@hp.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Douglas Hatch <doug.hatch@hp.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Scott J Norton <scott.norton@hp.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1434729002-57724-3-git-send-email-Waiman.Long@hp.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Waiman Long and committed by
Ingo Molnar
0e06e5be f7d71f20

+9 -8
+2 -2
include/asm-generic/qrwlock.h
··· 36 36 /* 37 37 * External function declarations 38 38 */ 39 - extern void queued_read_lock_slowpath(struct qrwlock *lock); 39 + extern void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts); 40 40 extern void queued_write_lock_slowpath(struct qrwlock *lock); 41 41 42 42 /** ··· 105 105 return; 106 106 107 107 /* The slowpath will decrement the reader count, if necessary. */ 108 - queued_read_lock_slowpath(lock); 108 + queued_read_lock_slowpath(lock, cnts); 109 109 } 110 110 111 111 /**
+7 -6
kernel/locking/qrwlock.c
··· 62 62 /** 63 63 * queued_read_lock_slowpath - acquire read lock of a queue rwlock 64 64 * @lock: Pointer to queue rwlock structure 65 + * @cnts: Current qrwlock lock value 65 66 */ 66 - void queued_read_lock_slowpath(struct qrwlock *lock) 67 + void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) 67 68 { 68 - u32 cnts; 69 - 70 69 /* 71 70 * Readers come here when they cannot get the lock without waiting 72 71 */ 73 72 if (unlikely(in_interrupt())) { 74 73 /* 75 - * Readers in interrupt context will spin until the lock is 76 - * available without waiting in the queue. 74 + * Readers in interrupt context will get the lock immediately 75 + * if the writer is just waiting (not holding the lock yet). 76 + * The rspin_until_writer_unlock() function returns immediately 77 + * in this case. Otherwise, they will spin until the lock 78 + * is available without waiting in the queue. 77 79 */ 78 - cnts = smp_load_acquire((u32 *)&lock->cnts); 79 80 rspin_until_writer_unlock(lock, cnts); 80 81 return; 81 82 }