Linux: kmem_cache_create fix and cleanup
authorMarc Dionne <marc.c.dionne@gmail.com>
Fri, 9 Oct 2009 01:53:03 +0000 (21:53 -0400)
committerDerrick Brashear <shadow|account-1000005@unknown>
Fri, 9 Oct 2009 11:04:22 +0000 (04:04 -0700)
Fix the kmem_cache constructor function to match the current
expected prototype, and cleanup related code.  This has been wrong
for a while, but since we were just passing extra parameters the
only effect was to generate a warning.

- Add a new configure test to detect the new constructor function
signature
- Define the older versions of the constructor in osi_compat.h,
making them call the current version
- Move a few compatibility #defines to osi_compat.h
- Clean up the call to kmem_cache_create by introducing an inline
afs_kmem_cache_create in osi_compat.h

Reviewed-on: http://gerrit.openafs.org/621
Reviewed-by: Derrick Brashear <shadow@dementia.org>
Tested-by: Derrick Brashear <shadow@dementia.org>

acinclude.m4
src/afs/LINUX/osi_compat.h
src/afs/LINUX/osi_vfsops.c
src/cf/linux-test4.m4

index 275bc22..5a452c7 100644 (file)
@@ -895,6 +895,7 @@ case $AFS_SYSNAME in *_linux* | *_umlinux*)
                 LINUX_STRUCT_PROC_DIR_ENTRY_HAS_OWNER
                 LINUX_HAVE_KMEM_CACHE_T
                 LINUX_KMEM_CACHE_CREATE_TAKES_DTOR
+                LINUX_KMEM_CACHE_CREATE_CTOR_TAKES_VOID
                 LINUX_D_PATH_TAKES_STRUCT_PATH
                 LINUX_NEW_EXPORT_OPS
                 LINUX_CONFIG_H_EXISTS
index b0982a5..0ce841b 100644 (file)
@@ -89,4 +89,54 @@ grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
 }
 #endif
 
+extern void init_once(void *);
+#if defined(HAVE_KMEM_CACHE_T)
+static inline int
+init_once_kmem_cache_t(void * foo, kmem_cache_t * cachep, unsigned long flags) {
+    init_once(foo);
+    return 0;
+}
+#define init_once_func init_once_kmem_cache_t
+#elif defined(KMEM_CACHE_INIT)
+static inline int
+init_once_kmem_cache_init(struct kmem_cache * cachep, void * foo) {
+    init_once(foo);
+    return 0;
+}
+#define init_once_func init_once_kmem_cache_init
+#elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
+static inline int
+init_once_compat(void * foo, struct kmem_cache * cachep, unsigned long flags) {
+    init_once(foo);
+    return 0;
+}
+#define init_once_func init_once_compat
+#else
+#define init_once_func init_once
+#endif
+
+#ifndef SLAB_RECLAIM_ACCOUNT
+#define SLAB_RECLAIM_ACCOUNT 0
+#endif
+
+#if defined(HAVE_KMEM_CACHE_T)
+#define afs_kmem_cache_t kmem_cache_t
+#else
+#define afs_kmem_cache_t struct kmem_cache
+#endif
+
+static inline afs_kmem_cache_t *
+afs_kmem_cache_create(const char *cache_name, size_t size, size_t size2,
+                       unsigned long flags, void (*ctor)(void *)) {
+    return kmem_cache_create(cache_name, size, size2, flags, ctor
+#if defined(KMEM_CACHE_TAKES_DTOR)
+       , NULL
+#endif
+       );
+}
+
+#if defined(SLAB_KERNEL)
+#define GFP_KERNEL SLAB_KERNEL
+#endif
+
 #endif
index aa80111..bd4d603 100644 (file)
@@ -23,6 +23,7 @@
 #include "afs/afs_stats.h"
 #include <linux/smp_lock.h>
 
+#include "osi_compat.h"
 
 struct vcache *afs_globalVp = 0;
 struct vfs *afs_globalVFS = 0;
@@ -216,22 +217,14 @@ afs_notify_change(struct dentry *dp, struct iattr *iattrp)
 
 
 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
-#if defined(HAVE_KMEM_CACHE_T)
-static kmem_cache_t *afs_inode_cachep;
-#else
-struct kmem_cache *afs_inode_cachep;
-#endif
+static afs_kmem_cache_t *afs_inode_cachep;
 
 static struct inode *
 afs_alloc_inode(struct super_block *sb)
 {
     struct vcache *vcp;
 
-#if defined(SLAB_KERNEL)
-    vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
-#else
     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
-#endif
     if (!vcp)
        return NULL;
 
@@ -244,14 +237,8 @@ afs_destroy_inode(struct inode *inode)
     kmem_cache_free(afs_inode_cachep, inode);
 }
 
-static void
-#if defined(HAVE_KMEM_CACHE_T)
-init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
-#elif defined(KMEM_CACHE_INIT)
-init_once(struct kmem_cache * cachep, void * foo)
-#else
-init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
-#endif
+void
+init_once(void * foo)
 {
     struct vcache *vcp = (struct vcache *) foo;
 
@@ -265,21 +252,9 @@ init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
 int
 afs_init_inodecache(void)
 {
-#ifndef SLAB_RECLAIM_ACCOUNT
-#define SLAB_RECLAIM_ACCOUNT 0
-#endif
-
-#if defined(KMEM_CACHE_TAKES_DTOR)
-    afs_inode_cachep = kmem_cache_create("afs_inode_cache",
-                                        sizeof(struct vcache),
-                                        0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
-                                        init_once, NULL);
-#else
-    afs_inode_cachep = kmem_cache_create("afs_inode_cache",
-                                        sizeof(struct vcache),
-                                        0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
-                                        init_once);
-#endif
+    afs_inode_cachep = afs_kmem_cache_create("afs_inode_cache",
+               sizeof(struct vcache), 0,
+               SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func);
     if (afs_inode_cachep == NULL)
        return -ENOMEM;
     return 0;
index 6763f15..b9fd21f 100644 (file)
@@ -974,6 +974,24 @@ AC_DEFUN([LINUX_KMEM_CACHE_CREATE_TAKES_DTOR], [
       ac_cv_linux_kmem_cache_create_takes_dtor=no)])
   AC_MSG_RESULT($ac_cv_linux_kmem_cache_create_takes_dtor)])
 
+AC_DEFUN([LINUX_KMEM_CACHE_CREATE_CTOR_TAKES_VOID], [
+  AC_MSG_CHECKING([whether kmem_cache_create constructor function takes a void pointer argument])
+  AC_CACHE_VAL([ac_cv_linux_kmem_cache_create_ctor_takes_void], [
+    save_CPPFLAGS="$CPPFLAGS"
+    CPPFLAGS="$CPPFLAGS -Werror"
+    AC_TRY_KBUILD(
+[#include <linux/slab.h>],
+[void _ctor(void *v) { };
+kmem_cache_create(NULL, 0, 0, 0, _ctor);],
+      ac_cv_linux_kmem_cache_create_ctor_takes_void=yes,
+      ac_cv_linux_kmem_cache_create_ctor_takes_void=no)
+    CPPFLAGS="$save_CPPFLAGS"
+])
+  AC_MSG_RESULT($ac_cv_linux_kmem_cache_create_ctor_takes_void)
+  if test "x$ac_cv_linux_kmem_cache_create_ctor_takes_void" = "xyes"; then
+    AC_DEFINE([KMEM_CACHE_CTOR_TAKES_VOID], 1, [define if kmem_cache_create constructor function takes a single void pointer argument])
+  fi])
+
 AC_DEFUN([LINUX_FS_STRUCT_FOP_HAS_SENDFILE], [
   AC_MSG_CHECKING([for sendfile in struct file_operations])
   AC_CACHE_VAL([ac_cv_linux_fs_struct_fop_has_sendfile], [