Whamcloud - gitweb
LU-18539 libcfs: introduce OBD_FREE_RCU() 87/57387/2
authorAlex Zhuravlev <bzzz@whamcloud.com>
Thu, 12 Dec 2024 06:06:01 +0000 (09:06 +0300)
committerOleg Drokin <green@whamcloud.com>
Wed, 22 Jan 2025 18:44:04 +0000 (18:44 +0000)
which can be used instead of direct call to kfree_rcu(), does
internal counting for memory leak detector and let us use
regular OBD_ALLOC().

Signed-off-by: Alex Zhuravlev <bzzz@whamcloud.com>
Change-Id: I7dfbfb0856440632ef56fbd1ab76e4b5b90b81e9
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57387
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Emoly Liu <emoly@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/include/obd_support.h
lustre/lod/lod_pool.c
lustre/lov/lov_pool.c
lustre/mdt/mdt_open.c
lustre/mgs/mgs_handler.c
lustre/obdclass/genops.c
lustre/obdclass/jobid.c
lustre/obdclass/local_storage.c
lustre/obdclass/lu_object.c
lustre/obdclass/obd_mount.c

index 9c0843d..32e7c0c 100644 (file)
@@ -933,6 +933,15 @@ do {                                                                             \
        }                                                                     \
 } while (0)
 
+#define OBD_FREE_RCU(ptr, size, list)                                  \
+do {                                                                   \
+       if (likely(ptr)) {                                              \
+               OBD_FREE_PRE(ptr, size, "kfreed_rcu");                  \
+               kfree_rcu(ptr, list);                                   \
+               POISON_PTR(ptr);                                        \
+       }                                                               \
+} while (0)
+
 #define OBD_FREE_LARGE(ptr, size)                                            \
 do {                                                                         \
        if (is_vmalloc_addr(ptr)) {                                           \
index 1b8923b..7489f76 100644 (file)
@@ -69,7 +69,7 @@ static void lod_pool_putref_free(struct kref *kref)
        LASSERT(pool->pool_proc_entry == NULL);
        lu_tgt_pool_free(&(pool->pool_rr.lqr_pool));
        lu_tgt_pool_free(&(pool->pool_obds));
-       kfree_rcu(pool, pool_rcu);
+       OBD_FREE_RCU(pool, sizeof(*pool), pool_rcu);
        EXIT;
 }
 
@@ -405,8 +405,7 @@ int lod_pool_new(struct obd_device *obd, char *poolname)
        if (strlen(poolname) > LOV_MAXPOOLNAME)
                RETURN(-ENAMETOOLONG);
 
-       /* OBD_ALLOC_* doesn't work with direct kfree_rcu use */
-       new_pool = kmalloc(sizeof(*new_pool), __GFP_ZERO);
+       OBD_ALLOC_PTR(new_pool);
        if (new_pool == NULL)
                RETURN(-ENOMEM);
 
index 6abc433..500cfe3 100644 (file)
@@ -96,7 +96,7 @@ static void lov_pool_putref_free(struct kref *kref)
        LASSERT(list_empty(&pool->pool_list));
        LASSERT(pool->pool_proc_entry == NULL);
        lu_tgt_pool_free(&(pool->pool_obds));
-       kfree_rcu(pool, pool_rcu);
+       OBD_FREE_RCU(pool, sizeof(*pool), pool_rcu);
        EXIT;
 }
 
@@ -365,8 +365,7 @@ int lov_pool_new(struct obd_device *obd, char *poolname)
        if (strlen(poolname) > LOV_MAXPOOLNAME)
                RETURN(-ENAMETOOLONG);
 
-       /* OBD_ALLOC doesn't work with direct use of kfree_rcu */
-       new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
+       OBD_ALLOC_PTR(new_pool);
        if (new_pool == NULL)
                RETURN(-ENOMEM);
 
index 703bbd3..691de2d 100644 (file)
@@ -103,8 +103,7 @@ void mdt_mfd_free(struct mdt_file_data *mfd)
 {
        LASSERT(refcount_read(&mfd->mfd_open_handle.h_ref) == 1);
        LASSERT(list_empty(&mfd->mfd_list));
-       OBD_FREE_PRE(mfd, sizeof(*mfd), "kfree_rcu");
-       kfree_rcu(mfd, mfd_open_handle.h_rcu);
+       OBD_FREE_RCU(mfd, sizeof(*mfd), mfd_open_handle.h_rcu);
 }
 
 static int mdt_create_data(struct mdt_thread_info *info,
index 9fbb4f4..a268de6 100644 (file)
@@ -1349,8 +1349,7 @@ static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
 
        dt_object_fini(&obj->mgo_obj);
        lu_object_header_fini(h);
-       OBD_FREE_PRE(obj, sizeof(*obj), "kfreed");
-       kfree_rcu(obj, mgo_header.loh_rcu);
+       OBD_FREE_RCU(obj, sizeof(*obj), mgo_header.loh_rcu);
 }
 
 static int mgs_object_print(const struct lu_env *env, void *cookie,
index f58cc2b..f7b0d74 100644 (file)
@@ -875,8 +875,7 @@ static void class_export_destroy(struct obd_export *exp)
        if (exp != obd->obd_self_export)
                class_decref(obd, "export", exp);
 
-       OBD_FREE_PRE(exp, sizeof(*exp), "kfree_rcu");
-       kfree_rcu(exp, exp_handle.h_rcu);
+       OBD_FREE_RCU(exp, sizeof(*exp), exp_handle.h_rcu);
        EXIT;
 }
 
index 3ae5ca6..017da36 100644 (file)
@@ -106,7 +106,7 @@ int jobid_set_current(char *jobid)
        int ret;
        int len = strlen(jobid);
 
-       sj = kmalloc(sizeof(*sj) + len + 1, GFP_KERNEL);
+       OBD_ALLOC(sj, sizeof(*sj) + len + 1);
        if (!sj)
                return -ENOMEM;
        rcu_read_lock();
@@ -125,7 +125,7 @@ int jobid_set_current(char *jobid)
 
        if (IS_ERR(origsj)) {
                put_pid(sj->sj_session);
-               kfree(sj);
+               OBD_FREE(sj, sizeof(*sj) + strlen(sj->sj_jobid) + 1);
                rcu_read_unlock();
                return PTR_ERR(origsj);
        }
@@ -135,13 +135,13 @@ int jobid_set_current(char *jobid)
                                      jobid_params);
        if (ret) {
                put_pid(sj->sj_session);
-               kfree(sj);
+               OBD_FREE(sj, sizeof(*sj) + strlen(sj->sj_jobid) + 1);
                rcu_read_unlock();
                return ret;
        }
        put_pid(origsj->sj_session);
        rcu_read_unlock();
-       kfree_rcu(origsj, sj_rcu);
+       OBD_FREE_RCU(origsj, sizeof(*sj) + strlen(origsj->sj_jobid) + 1, sj_rcu);
        jobid_prune_expedite();
 
        return 0;
@@ -152,7 +152,7 @@ static void jobid_free(void *vsj, void *arg)
        struct session_jobid *sj = vsj;
 
        put_pid(sj->sj_session);
-       kfree(sj);
+       OBD_FREE(sj, sizeof(*sj) + strlen(sj->sj_jobid) + 1);;
 }
 
 static void jobid_prune(struct work_struct *work);
@@ -181,7 +181,8 @@ static void jobid_prune(struct work_struct *work)
                                           &sj->sj_linkage,
                                           jobid_params) == 0) {
                        put_pid(sj->sj_session);
-                       kfree_rcu(sj, sj_rcu);
+                       OBD_FREE_RCU(sj, sizeof(*sj) + strlen(sj->sj_jobid) + 1,
+                                    sj_rcu);
                }
        }
        rhashtable_walk_stop(&iter);
index 23e02cb..695feec 100644 (file)
@@ -47,8 +47,7 @@ static void ls_object_free(const struct lu_env *env, struct lu_object *o)
 
        dt_object_fini(&obj->ls_obj);
        lu_object_header_fini(h);
-       OBD_FREE_PRE(obj, sizeof(*obj), "kfreed");
-       kfree_rcu(obj, ls_header.loh_rcu);
+       OBD_FREE_RCU(obj, sizeof(*obj), ls_header.loh_rcu);
 }
 
 static const struct lu_object_operations ls_lu_obj_ops = {
index 9477906..dc03289 100644 (file)
@@ -1306,8 +1306,7 @@ EXPORT_SYMBOL(lu_object_header_fini);
 void lu_object_header_free(struct lu_object_header *h)
 {
        lu_object_header_fini(h);
-       OBD_FREE_PRE(h, sizeof(*h), "kfreed");
-       kfree_rcu(h, loh_rcu);
+       OBD_FREE_RCU(h, sizeof(*h), loh_rcu);
 }
 EXPORT_SYMBOL(lu_object_header_free);
 
@@ -1941,8 +1940,7 @@ void lu_env_remove(struct lu_env *env)
                                     lu_env_rhash_params);
        if (lei && rhashtable_remove_fast(&lu_env_rhash, &lei->lei_linkage,
                                          lu_env_rhash_params) == 0) {
-               OBD_FREE_PRE(lei, sizeof(*lei), "kfreed");
-               kfree_rcu(lei, lei_rcu_head);
+               OBD_FREE_RCU(lei, sizeof(*lei), lei_rcu_head);
        }
 }
 EXPORT_SYMBOL(lu_env_remove);
index abde573..4d91da0 100644 (file)
@@ -1182,17 +1182,19 @@ static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
 
 static int lmd_parse_string(char **handle, char *ptr)
 {
+       int len;
+
        if (!handle || !ptr)
                return -EINVAL;
 
        OBD_FREE(*handle, strlen(*handle) + 1);
        *handle = NULL;
 
-       *handle = kstrdup(ptr, GFP_NOFS);
+       len = strlen(ptr);
+       OBD_ALLOC(*handle, len + 1);
        if (!*handle)
                return -ENOMEM;
-
-       OBD_ALLOC_POST(*handle, strlen(ptr) + 1, "kmalloced");
+       memcpy(*handle, ptr, len + 1);
 
        return 0;
 }