[PATCH 2/3] Introduce percpu rw semaphores

From: Mikulas Patocka
Date: Sat Jul 28 2012 - 12:42:02 EST


Introduce percpu rw semaphores

When many CPUs are locking a rw semaphore for read concurrently, cache
line bouncing occurs. When a CPU acquires rw semaphore for read, the
CPU writes to the cache line holding the semaphore. Consequently, the
cache line is being moved between CPUs and this slows down semaphore
acquisition.

This patch introduces new percpu rw semaphores. They are functionally
identical to existing rw semaphores, but locking the percpu rw semaphore
for read is faster and locking for write is slower.

The percpu rw semaphore is implemented as a percpu array of rw
semaphores, each semaphore for one CPU. When some thread needs to lock
the semaphore for read, only semaphore on the current CPU is locked for
read. When some thread needs to lock the semaphore for write, semaphores
for all CPUs are locked for write. This avoids cache line bouncing.

Note that the thread that is locking percpu rw semaphore may be
rescheduled, it doesn't cause bug, but cache line bouncing occurs in
this case.

Signed-off-by: Mikulas Patocka <mpatocka@xxxxxxxxxx>

---
include/linux/percpu-rwsem.h | 77 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 77 insertions(+)

Index: linux-3.5-fast/include/linux/percpu-rwsem.h
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ linux-3.5-fast/include/linux/percpu-rwsem.h 2012-07-28 18:41:05.000000000 +0200
@@ -0,0 +1,77 @@
+#ifndef _LINUX_PERCPU_RWSEM_H
+#define _LINUX_PERCPU_RWSEM_H
+
+#include <linux/rwsem.h>
+#include <linux/percpu.h>
+
+#ifndef CONFIG_SMP
+
+#define percpu_rw_semaphore rw_semaphore
+#define percpu_rwsem_ptr int
+#define percpu_down_read(x) (down_read(x), 0)
+#define percpu_up_read(x, y) up_read(x)
+#define percpu_down_write down_write
+#define percpu_up_write up_write
+#define percpu_init_rwsem(x) (({init_rwsem(x);}), 0)
+#define percpu_free_rwsem(x) do { } while (0)
+
+#else
+
+struct percpu_rw_semaphore {
+ struct rw_semaphore __percpu *s;
+};
+
+typedef struct rw_semaphore *percpu_rwsem_ptr;
+
+static inline percpu_rwsem_ptr percpu_down_read(struct percpu_rw_semaphore *sem)
+{
+ struct rw_semaphore *s = __this_cpu_ptr(sem->s);
+ down_read(s);
+ return s;
+}
+
+static inline void percpu_up_read(struct percpu_rw_semaphore *sem, percpu_rwsem_ptr s)
+{
+ up_read(s);
+}
+
+static inline void percpu_down_write(struct percpu_rw_semaphore *sem)
+{
+ int cpu;
+ for_each_possible_cpu(cpu) {
+ struct rw_semaphore *s = per_cpu_ptr(sem->s, cpu);
+ down_write(s);
+ }
+}
+
+static inline void percpu_up_write(struct percpu_rw_semaphore *sem)
+{
+ int cpu;
+ for_each_possible_cpu(cpu) {
+ struct rw_semaphore *s = per_cpu_ptr(sem->s, cpu);
+ up_write(s);
+ }
+}
+
+static inline int percpu_init_rwsem(struct percpu_rw_semaphore *sem)
+{
+ int cpu;
+ sem->s = alloc_percpu(struct rw_semaphore);
+ if (unlikely(!sem->s))
+ return -ENOMEM;
+ for_each_possible_cpu(cpu) {
+ struct rw_semaphore *s = per_cpu_ptr(sem->s, cpu);
+ init_rwsem(s);
+ }
+ return 0;
+}
+
+static inline void percpu_free_rwsem(struct percpu_rw_semaphore *sem)
+{
+ free_percpu(sem->s);
+ sem->s = NULL; /* catch use after free bugs */
+}
+
+#endif
+
+#endif
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/