summaryrefslogtreecommitdiff
path: root/fs/nfsd
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-02-20 23:49:23 -0800
committerKees Cook <kees@kernel.org>2026-02-21 01:02:28 -0800
commit69050f8d6d075dc01af7a5f2f550a8067510366f (patch)
treebb265f94d9dfa7876c06a5d9f88673d496a15341 /fs/nfsd
parentd39a1d7486d98668dd34aaa6732aad7977c45f5a (diff)
treewide: Replace kmalloc with kmalloc_obj for non-scalar types
This is the result of running the Coccinelle script from scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to avoid scalar types (which need careful case-by-case checking), and instead replace kmalloc-family calls that allocate struct or union object instances: Single allocations: kmalloc(sizeof(TYPE), ...) are replaced with: kmalloc_obj(TYPE, ...) Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...) are replaced with: kmalloc_objs(TYPE, COUNT, ...) Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...) are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...) (where TYPE may also be *VAR) The resulting allocations no longer return "void *", instead returning "TYPE *". Signed-off-by: Kees Cook <kees@kernel.org>
Diffstat (limited to 'fs/nfsd')
-rw-r--r--fs/nfsd/blocklayout.c6
-rw-r--r--fs/nfsd/blocklayoutxdr.c4
-rw-r--r--fs/nfsd/export.c11
-rw-r--r--fs/nfsd/filecache.c2
-rw-r--r--fs/nfsd/flexfilelayout.c4
-rw-r--r--fs/nfsd/nfs4callback.c4
-rw-r--r--fs/nfsd/nfs4idmap.c2
-rw-r--r--fs/nfsd/nfs4proc.c9
-rw-r--r--fs/nfsd/nfs4recover.c16
-rw-r--r--fs/nfsd/nfs4state.c34
-rw-r--r--fs/nfsd/nfs4xdr.c6
-rw-r--r--fs/nfsd/nfsctl.c4
12 files changed, 48 insertions, 54 deletions
diff --git a/fs/nfsd/blocklayout.c b/fs/nfsd/blocklayout.c
index afa16d7a8013..e3553ccb6ab4 100644
--- a/fs/nfsd/blocklayout.c
+++ b/fs/nfsd/blocklayout.c
@@ -131,7 +131,7 @@ nfsd4_block_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode,
* layouts, so make sure to zero the whole structure.
*/
nfserr = nfserrno(-ENOMEM);
- bl = kzalloc(struct_size(bl, extents, nr_extents_max), GFP_KERNEL);
+ bl = kzalloc_flex(*bl, extents, nr_extents_max, GFP_KERNEL);
if (!bl)
goto out_error;
bl->nr_extents = nr_extents_max;
@@ -208,7 +208,7 @@ nfsd4_block_get_device_info_simple(struct super_block *sb,
struct pnfs_block_deviceaddr *dev;
struct pnfs_block_volume *b;
- dev = kzalloc(struct_size(dev, volumes, 1), GFP_KERNEL);
+ dev = kzalloc_flex(*dev, volumes, 1, GFP_KERNEL);
if (!dev)
return -ENOMEM;
gdp->gd_device = dev;
@@ -319,7 +319,7 @@ nfsd4_block_get_device_info_scsi(struct super_block *sb,
const struct pr_ops *ops;
int ret;
- dev = kzalloc(struct_size(dev, volumes, 1), GFP_KERNEL);
+ dev = kzalloc_flex(*dev, volumes, 1, GFP_KERNEL);
if (!dev)
return -ENOMEM;
gdp->gd_device = dev;
diff --git a/fs/nfsd/blocklayoutxdr.c b/fs/nfsd/blocklayoutxdr.c
index 196ef4245604..3c145146a0c8 100644
--- a/fs/nfsd/blocklayoutxdr.c
+++ b/fs/nfsd/blocklayoutxdr.c
@@ -164,7 +164,7 @@ nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
if (len != expected)
return nfserr_bad_xdr;
- iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
+ iomaps = kzalloc_objs(*iomaps, nr_iomaps, GFP_KERNEL);
if (!iomaps)
return nfserr_delay;
@@ -258,7 +258,7 @@ nfsd4_scsi_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
if (len != expected)
return nfserr_bad_xdr;
- iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
+ iomaps = kzalloc_objs(*iomaps, nr_iomaps, GFP_KERNEL);
if (!iomaps)
return nfserr_delay;
diff --git a/fs/nfsd/export.c b/fs/nfsd/export.c
index 09fe268fe2c7..8fb394afc3f5 100644
--- a/fs/nfsd/export.c
+++ b/fs/nfsd/export.c
@@ -234,7 +234,7 @@ static inline void expkey_update(struct cache_head *cnew,
static struct cache_head *expkey_alloc(void)
{
- struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
+ struct svc_expkey *i = kmalloc_obj(*i, GFP_KERNEL);
if (i)
return &i->h;
else
@@ -479,9 +479,8 @@ fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
if (fsloc->locations_count == 0)
return 0;
- fsloc->locations = kcalloc(fsloc->locations_count,
- sizeof(struct nfsd4_fs_location),
- GFP_KERNEL);
+ fsloc->locations = kzalloc_objs(struct nfsd4_fs_location,
+ fsloc->locations_count, GFP_KERNEL);
if (!fsloc->locations)
return -ENOMEM;
for (i=0; i < fsloc->locations_count; i++) {
@@ -871,11 +870,11 @@ static void export_update(struct cache_head *cnew, struct cache_head *citem)
static struct cache_head *svc_export_alloc(void)
{
- struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
+ struct svc_export *i = kmalloc_obj(*i, GFP_KERNEL);
if (!i)
return NULL;
- i->ex_stats = kmalloc(sizeof(*(i->ex_stats)), GFP_KERNEL);
+ i->ex_stats = kmalloc_obj(*(i->ex_stats), GFP_KERNEL);
if (!i->ex_stats) {
kfree(i);
return NULL;
diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c
index 93798575b807..6b4eeb28c88b 100644
--- a/fs/nfsd/filecache.c
+++ b/fs/nfsd/filecache.c
@@ -899,7 +899,7 @@ nfsd_alloc_fcache_disposal(void)
{
struct nfsd_fcache_disposal *l;
- l = kmalloc(sizeof(*l), GFP_KERNEL);
+ l = kmalloc_obj(*l, GFP_KERNEL);
if (!l)
return NULL;
spin_lock_init(&l->lock);
diff --git a/fs/nfsd/flexfilelayout.c b/fs/nfsd/flexfilelayout.c
index 0f1a35400cd5..a65d55bcdc02 100644
--- a/fs/nfsd/flexfilelayout.c
+++ b/fs/nfsd/flexfilelayout.c
@@ -36,7 +36,7 @@ nfsd4_ff_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode,
* Zero it out for the stateid - don't want junk in there!
*/
error = -ENOMEM;
- fl = kzalloc(sizeof(*fl), GFP_KERNEL);
+ fl = kzalloc_obj(*fl, GFP_KERNEL);
if (!fl)
goto out_error;
args->lg_content = fl;
@@ -86,7 +86,7 @@ nfsd4_ff_proc_getdeviceinfo(struct super_block *sb, struct svc_rqst *rqstp,
u16 port;
char addr[INET6_ADDRSTRLEN];
- da = kzalloc(sizeof(struct pnfs_ff_device_addr), GFP_KERNEL);
+ da = kzalloc_obj(struct pnfs_ff_device_addr, GFP_KERNEL);
if (!da)
return nfserrno(-ENOMEM);
diff --git a/fs/nfsd/nfs4callback.c b/fs/nfsd/nfs4callback.c
index e00b2aea8da2..88782df7c3a1 100644
--- a/fs/nfsd/nfs4callback.c
+++ b/fs/nfsd/nfs4callback.c
@@ -1378,7 +1378,7 @@ void nfsd41_cb_referring_call(struct nfsd4_callback *cb,
}
}
if (!found) {
- rcl = kmalloc(sizeof(*rcl), GFP_KERNEL);
+ rcl = kmalloc_obj(*rcl, GFP_KERNEL);
if (!rcl)
return;
memcpy(rcl->rcl_sessionid.data, sessionid->data,
@@ -1397,7 +1397,7 @@ void nfsd41_cb_referring_call(struct nfsd4_callback *cb,
}
}
if (!found) {
- rc = kmalloc(sizeof(*rc), GFP_KERNEL);
+ rc = kmalloc_obj(*rc, GFP_KERNEL);
if (!rc)
goto out;
rc->rc_sequenceid = seqno;
diff --git a/fs/nfsd/nfs4idmap.c b/fs/nfsd/nfs4idmap.c
index c319c31b0f64..6415b847a939 100644
--- a/fs/nfsd/nfs4idmap.c
+++ b/fs/nfsd/nfs4idmap.c
@@ -97,7 +97,7 @@ ent_put(struct kref *ref)
static struct cache_head *
ent_alloc(void)
{
- struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
+ struct ent *e = kmalloc_obj(*e, GFP_KERNEL);
if (e)
return &e->h;
else
diff --git a/fs/nfsd/nfs4proc.c b/fs/nfsd/nfs4proc.c
index 37ab3a69c4b6..7dbb1dc184db 100644
--- a/fs/nfsd/nfs4proc.c
+++ b/fs/nfsd/nfs4proc.c
@@ -451,7 +451,7 @@ do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, stru
int accmode;
__be32 status;
- *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
+ *resfh = kmalloc_obj(struct svc_fh, GFP_KERNEL);
if (!*resfh)
return nfserr_jukebox;
fh_init(*resfh, NFS4_FHSIZE);
@@ -1630,7 +1630,7 @@ static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
__be32 status = 0;
*nsui = NULL;
- work = kzalloc(sizeof(*work), GFP_KERNEL);
+ work = kzalloc_obj(*work, GFP_KERNEL);
try_again:
spin_lock(&nn->nfsd_ssc_lock);
list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
@@ -2160,7 +2160,7 @@ nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
memcpy(&copy->fh, &cstate->current_fh.fh_handle,
sizeof(struct knfsd_fh));
if (nfsd4_copy_is_async(copy)) {
- async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
+ async_copy = kzalloc_obj(struct nfsd4_copy, GFP_KERNEL);
if (!async_copy)
goto out_err;
async_copy->cp_nn = nn;
@@ -2171,7 +2171,8 @@ nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
if (atomic_inc_return(&nn->pending_async_copies) >
(int)rqstp->rq_pool->sp_nrthreads)
goto out_dec_async_copy_err;
- async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
+ async_copy->cp_src = kmalloc_obj(*async_copy->cp_src,
+ GFP_KERNEL);
if (!async_copy->cp_src)
goto out_dec_async_copy_err;
if (!nfs4_init_copy_state(nn, copy))
diff --git a/fs/nfsd/nfs4recover.c b/fs/nfsd/nfs4recover.c
index 1e6b2dd47ba7..87de08c7f405 100644
--- a/fs/nfsd/nfs4recover.c
+++ b/fs/nfsd/nfs4recover.c
@@ -203,7 +203,7 @@ nfsd4_build_namelist(struct dir_context *__ctx, const char *name, int namlen,
if (namlen != HEXDIR_LEN - 1)
return true;
- entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
+ entry = kmalloc_obj(struct name_list, GFP_KERNEL);
if (entry == NULL)
return false;
memcpy(entry->name, name, HEXDIR_LEN - 1);
@@ -478,9 +478,8 @@ nfs4_legacy_state_init(struct net *net)
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
int i;
- nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
- sizeof(struct list_head),
- GFP_KERNEL);
+ nn->reclaim_str_hashtbl = kmalloc_objs(struct list_head,
+ CLIENT_HASH_SIZE, GFP_KERNEL);
if (!nn->reclaim_str_hashtbl)
return -ENOMEM;
@@ -898,7 +897,7 @@ __nfsd4_init_cld_pipe(struct net *net)
if (nn->cld_net)
return 0;
- cn = kzalloc(sizeof(*cn), GFP_KERNEL);
+ cn = kzalloc_obj(*cn, GFP_KERNEL);
if (!cn) {
ret = -ENOMEM;
goto err;
@@ -960,7 +959,7 @@ alloc_cld_upcall(struct nfsd_net *nn)
struct cld_upcall *new, *tmp;
struct cld_net *cn = nn->cld_net;
- new = kzalloc(sizeof(*new), GFP_KERNEL);
+ new = kzalloc_obj(*new, GFP_KERNEL);
if (!new)
return new;
@@ -1356,9 +1355,8 @@ nfs4_cld_state_init(struct net *net)
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
int i;
- nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
- sizeof(struct list_head),
- GFP_KERNEL);
+ nn->reclaim_str_hashtbl = kmalloc_objs(struct list_head,
+ CLIENT_HASH_SIZE, GFP_KERNEL);
if (!nn->reclaim_str_hashtbl)
return -ENOMEM;
diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index f5cb067a1e50..fb5b2ff4201e 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -299,7 +299,7 @@ find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
nbl = find_blocked_lock(lo, fh, nn);
if (!nbl) {
- nbl = kmalloc(sizeof(*nbl), GFP_KERNEL);
+ nbl = kmalloc_obj(*nbl, GFP_KERNEL);
if (nbl) {
INIT_LIST_HEAD(&nbl->nbl_list);
INIT_LIST_HEAD(&nbl->nbl_lru);
@@ -974,7 +974,7 @@ struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
{
struct nfs4_cpntf_state *cps;
- cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
+ cps = kzalloc_obj(struct nfs4_cpntf_state, GFP_KERNEL);
if (!cps)
return NULL;
cps->cpntf_time = ktime_get_boottime_seconds();
@@ -2032,7 +2032,7 @@ static struct nfsd4_slot *nfsd4_alloc_slot(struct nfsd4_channel_attrs *fattrs,
size = fattrs->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ ?
0 : fattrs->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
- slot = kzalloc(struct_size(slot, sl_data, size), gfp);
+ slot = kzalloc_flex(*slot, sl_data, size, gfp);
if (!slot)
return NULL;
slot->sl_index = index;
@@ -2047,7 +2047,7 @@ static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
struct nfsd4_slot *slot;
int i;
- new = kzalloc(sizeof(*new), GFP_KERNEL);
+ new = kzalloc_obj(*new, GFP_KERNEL);
if (!new)
return NULL;
xa_init(&new->se_slots);
@@ -2108,7 +2108,7 @@ static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
{
struct nfsd4_conn *conn;
- conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
+ conn = kmalloc_obj(struct nfsd4_conn, GFP_KERNEL);
if (!conn)
return NULL;
svc_xprt_get(rqstp->rq_xprt);
@@ -2357,9 +2357,8 @@ static struct nfs4_client *alloc_client(struct xdr_netobj name,
xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
if (clp->cl_name.data == NULL)
goto err_no_name;
- clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
- sizeof(struct list_head),
- GFP_KERNEL);
+ clp->cl_ownerstr_hashtbl = kmalloc_objs(struct list_head,
+ OWNER_HASH_SIZE, GFP_KERNEL);
if (!clp->cl_ownerstr_hashtbl)
goto err_no_hashtbl;
clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
@@ -3309,7 +3308,7 @@ static struct nfs4_client *create_client(struct xdr_netobj name,
free_client(clp);
return NULL;
}
- clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
+ clp->cl_ra = kzalloc_obj(*clp->cl_ra, GFP_KERNEL);
if (!clp->cl_ra) {
free_client(clp);
return NULL;
@@ -8823,7 +8822,7 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp,
static inline struct nfs4_client_reclaim *
alloc_reclaim(void)
{
- return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
+ return kmalloc_obj(struct nfs4_client_reclaim, GFP_KERNEL);
}
bool
@@ -8960,19 +8959,16 @@ static int nfs4_state_create_net(struct net *net)
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
int i;
- nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
- sizeof(struct list_head),
- GFP_KERNEL);
+ nn->conf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE,
+ GFP_KERNEL);
if (!nn->conf_id_hashtbl)
goto err;
- nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
- sizeof(struct list_head),
- GFP_KERNEL);
+ nn->unconf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE,
+ GFP_KERNEL);
if (!nn->unconf_id_hashtbl)
goto err_unconf_id;
- nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
- sizeof(struct list_head),
- GFP_KERNEL);
+ nn->sessionid_hashtbl = kmalloc_objs(struct list_head,
+ SESSION_HASH_SIZE, GFP_KERNEL);
if (!nn->sessionid_hashtbl)
goto err_sessionid;
diff --git a/fs/nfsd/nfs4xdr.c b/fs/nfsd/nfs4xdr.c
index 5172dbd0cb05..c96d31b7846e 100644
--- a/fs/nfsd/nfs4xdr.c
+++ b/fs/nfsd/nfs4xdr.c
@@ -124,7 +124,7 @@ svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, size_t len)
{
struct svcxdr_tmpbuf *tb;
- tb = kmalloc(struct_size(tb, buf, len), GFP_KERNEL);
+ tb = kmalloc_flex(*tb, buf, len, GFP_KERNEL);
if (!tb)
return NULL;
tb->next = argp->to_free;
@@ -2184,7 +2184,7 @@ nfsd4_decode_copy(struct nfsd4_compoundargs *argp, union nfsd4_op_u *u)
if (status)
return status;
- ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
+ ns_dummy = kmalloc_obj(struct nl4_server, GFP_KERNEL);
if (ns_dummy == NULL)
return nfserr_jukebox;
for (i = 0; i < count - 1; i++) {
@@ -3956,7 +3956,7 @@ nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
}
if ((attrmask[0] & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) &&
!fhp) {
- tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
+ tempfh = kmalloc_obj(struct svc_fh, GFP_KERNEL);
status = nfserr_jukebox;
if (!tempfh)
goto out;
diff --git a/fs/nfsd/nfsctl.c b/fs/nfsd/nfsctl.c
index 89fe2c0e8d44..777b7bb9aeca 100644
--- a/fs/nfsd/nfsctl.c
+++ b/fs/nfsd/nfsctl.c
@@ -477,7 +477,7 @@ static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
return strlen(buf);
}
- nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
+ nthreads = kzalloc_objs(int, npools, GFP_KERNEL);
rv = -ENOMEM;
if (nthreads == NULL)
goto out_free;
@@ -1596,7 +1596,7 @@ int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
mutex_lock(&nfsd_mutex);
- nthreads = kcalloc(nrpools, sizeof(int), GFP_KERNEL);
+ nthreads = kzalloc_objs(int, nrpools, GFP_KERNEL);
if (!nthreads) {
ret = -ENOMEM;
goto out_unlock;