]> git.openfabrics.org - ~emulex/for-vlad/old/compat.git/commitdiff
compat: backport kfree_rcu
authorHauke Mehrtens <hauke@hauke-m.de>
Tue, 10 May 2011 11:10:15 +0000 (13:10 +0200)
committerLuis R. Rodriguez <lrodriguez@atheros.com>
Tue, 10 May 2011 21:50:41 +0000 (14:50 -0700)
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
include/linux/compat-2.6.40.h

index 42f9feeb53a92e02feea051191fa8f445e70fb88..9ec8c1b10bf346a98c5fd2606bdb7e035ca6220e 100644 (file)
@@ -5,6 +5,8 @@
 
 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,40))
 
+#include <linux/rcupdate.h>
+
 /*
  * This is not part of The 2.6.37 kernel yet but we
  * we use it to optimize the backport code we
 #define br_port_exists(dev)    (dev->br_port)
 #endif
 
+static __always_inline bool __is_kfree_rcu_offset(unsigned long offset)
+{
+       return offset < 4096;
+}
+
+static __always_inline
+void __kfree_rcu(struct rcu_head *head, unsigned long offset)
+{
+       typedef void (*rcu_callback)(struct rcu_head *);
+
+       /* This causes build failures with some kernel versions at positions
+        * where this methods is not used at all.
+        */
+       /* BUILD_BUG_ON(!__builtin_constant_p(offset)); */
+
+       /* See the kfree_rcu() header comment. */
+       /* BUILD_BUG_ON(!__is_kfree_rcu_offset(offset)); */
+
+       call_rcu(head, (rcu_callback)offset);
+}
+
+static inline void __rcu_reclaim(struct rcu_head *head)
+{
+       unsigned long offset = (unsigned long)head->func;
+
+       if (__is_kfree_rcu_offset(offset))
+               kfree((void *)head - offset);
+       else
+               head->func(head);
+}
+
+/**
+ * kfree_rcu() - kfree an object after a grace period.
+ * @ptr:       pointer to kfree
+ * @rcu_head:  the name of the struct rcu_head within the type of @ptr.
+ *
+ * Many rcu callbacks functions just call kfree() on the base structure.
+ * These functions are trivial, but their size adds up, and furthermore
+ * when they are used in a kernel module, that module must invoke the
+ * high-latency rcu_barrier() function at module-unload time.
+ *
+ * The kfree_rcu() function handles this issue.  Rather than encoding a
+ * function address in the embedded rcu_head structure, kfree_rcu() instead
+ * encodes the offset of the rcu_head structure within the base structure.
+ * Because the functions are not allowed in the low-order 4096 bytes of
+ * kernel virtual memory, offsets up to 4095 bytes can be accommodated.
+ * If the offset is larger than 4095 bytes, a compile-time error will
+ * be generated in __kfree_rcu().  If this error is triggered, you can
+ * either fall back to use of call_rcu() or rearrange the structure to
+ * position the rcu_head structure into the first 4096 bytes.
+ *
+ * Note that the allowable offset might decrease in the future, for example,
+ * to allow something like kmem_cache_free_rcu().
+ */
+#define kfree_rcu(ptr, rcu_head)                                       \
+       __kfree_rcu(&((ptr)->rcu_head), offsetof(typeof(*(ptr)), rcu_head))
+
+
 #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,40)) */
 
 #endif /* LINUX_26_40_COMPAT_H */