From: Alex Zhuravlev Date: Thu, 12 Dec 2024 06:06:01 +0000 (+0300) Subject: LU-18539 libcfs: introduce OBD_FREE_RCU() X-Git-Tag: 2.16.52~73 X-Git-Url: https://git.whamcloud.com/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F87%2F57387%2F2;p=fs%2Flustre-release.git LU-18539 libcfs: introduce OBD_FREE_RCU() 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 Change-Id: I7dfbfb0856440632ef56fbd1ab76e4b5b90b81e9 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57387 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Emoly Liu Reviewed-by: Oleg Drokin --- diff --git a/lustre/include/obd_support.h b/lustre/include/obd_support.h index 9c0843d..32e7c0c 100644 --- a/lustre/include/obd_support.h +++ b/lustre/include/obd_support.h @@ -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)) { \ diff --git a/lustre/lod/lod_pool.c b/lustre/lod/lod_pool.c index 1b8923b..7489f76 100644 --- a/lustre/lod/lod_pool.c +++ b/lustre/lod/lod_pool.c @@ -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); diff --git a/lustre/lov/lov_pool.c b/lustre/lov/lov_pool.c index 6abc433..500cfe3 100644 --- a/lustre/lov/lov_pool.c +++ b/lustre/lov/lov_pool.c @@ -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); diff --git a/lustre/mdt/mdt_open.c b/lustre/mdt/mdt_open.c index 703bbd3..691de2d 100644 --- a/lustre/mdt/mdt_open.c +++ b/lustre/mdt/mdt_open.c @@ -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, diff --git a/lustre/mgs/mgs_handler.c b/lustre/mgs/mgs_handler.c index 9fbb4f4..a268de6 100644 --- a/lustre/mgs/mgs_handler.c +++ b/lustre/mgs/mgs_handler.c @@ -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, diff --git a/lustre/obdclass/genops.c b/lustre/obdclass/genops.c index f58cc2b..f7b0d74 100644 --- a/lustre/obdclass/genops.c +++ b/lustre/obdclass/genops.c @@ -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; } diff --git a/lustre/obdclass/jobid.c b/lustre/obdclass/jobid.c index 3ae5ca6..017da36 100644 --- a/lustre/obdclass/jobid.c +++ b/lustre/obdclass/jobid.c @@ -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); diff --git a/lustre/obdclass/local_storage.c b/lustre/obdclass/local_storage.c index 23e02cb..695feec 100644 --- a/lustre/obdclass/local_storage.c +++ b/lustre/obdclass/local_storage.c @@ -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 = { diff --git a/lustre/obdclass/lu_object.c b/lustre/obdclass/lu_object.c index 9477906..dc03289 100644 --- a/lustre/obdclass/lu_object.c +++ b/lustre/obdclass/lu_object.c @@ -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); diff --git a/lustre/obdclass/obd_mount.c b/lustre/obdclass/obd_mount.c index abde573..4d91da0 100644 --- a/lustre/obdclass/obd_mount.c +++ b/lustre/obdclass/obd_mount.c @@ -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; }