Skip to contentSkip to conversation

posix_acl: make posix_acl_to_xattr() alloc the buffer

PATCH Applied Miklos Szeredi 4 participants 5 messages Jan 15 – Jan 16

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 posix_acl: make posix_acl_to_xattr() alloc the buffer

Without exception all caller do that. So move the allocation into the helper.

This reduces boilerplate and removes unnecessary error checking.

---
Tested fuse only.

 fs/9p/acl.c                     | 16 ++---------
 fs/btrfs/acl.c                  | 10 ++-----
 fs/ceph/acl.c                   | 51 ++++++++++++++-------------------
 fs/fuse/acl.c                   | 12 +++-----
 fs/gfs2/acl.c                   | 13 ++-------
 fs/jfs/acl.c                    |  9 ++----
 fs/ntfs3/xattr.c                |  6 +---
 fs/orangefs/acl.c               |  8 +-----
 fs/posix_acl.c                  | 21 +++++++-------
 include/linux/posix_acl_xattr.h |  5 ++--
 10 files changed, 53 insertions(+), 98 deletions(-)
fs/9p/acl.c +3 -13
index 633da5e37299..ae7e7cf7523a 100644
@@ -167,17 +167,11 @@ int v9fs_iop_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		if (retval)
 			goto err_out;
 
-		size = posix_acl_xattr_size(acl->a_count);
-
-		value = kzalloc(size, GFP_NOFS);
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
 		if (!value) {
 			retval = -ENOMEM;
 			goto err_out;
 		}
-
-		retval = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (retval < 0)
-			goto err_out;
 	}
 
 	/*
@@ -257,13 +251,10 @@ static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
 		return 0;
 
 	/* Set a setxattr request to server */
-	size = posix_acl_xattr_size(acl->a_count);
-	buffer = kmalloc(size, GFP_KERNEL);
+	buffer = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL);
 	if (!buffer)
 		return -ENOMEM;
-	retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
-	if (retval < 0)
-		goto err_free_out;
+
 	switch (type) {
 	case ACL_TYPE_ACCESS:
 		name = XATTR_NAME_POSIX_ACL_ACCESS;
@@ -275,7 +266,6 @@ static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
 		BUG();
 	}
 	retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
-err_free_out:
 	kfree(buffer);
 	return retval;
 }
fs/btrfs/acl.c +3 -7
index c336e2ab7f8a..e55b686fe1ab 100644
@@ -57,7 +57,8 @@ struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu)
 int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 		    struct posix_acl *acl, int type)
 {
-	int ret, size = 0;
+	int ret;
+	size_t size = 0;
 	const char *name;
 	char AUTO_KFREE(value);
 
@@ -77,20 +78,15 @@ int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 	if (acl) {
 		unsigned int nofs_flag;
 
-		size = posix_acl_xattr_size(acl->a_count);
 		/*
 		 * We're holding a transaction handle, so use a NOFS memory
 		 * allocation context to avoid deadlock if reclaim happens.
 		 */
 		nofs_flag = memalloc_nofs_save();
-		value = kmalloc(size, GFP_KERNEL);
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL);
 		memalloc_nofs_restore(nofs_flag);
 		if (!value)
 			return -ENOMEM;
-
-		ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (ret < 0)
-			return ret;
 	}
 
 	if (trans)
fs/ceph/acl.c +22 -29

108 lines not on this page · raw message

fs/fuse/acl.c +4 -8
index 8f484b105f13..cbde6ac1add3 100644
@@ -122,20 +122,16 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		 * them to be refreshed the next time they are used,
 		 * and it also updates i_ctime.
 		 */
-		size_t size = posix_acl_xattr_size(acl->a_count);
+		size_t size;
 		void *value;
 
-		if (size > PAGE_SIZE)
-			return -E2BIG;
-
-		value = kmalloc(size, GFP_KERNEL);
+		value = posix_acl_to_xattr(fc->user_ns, acl, &size, GFP_KERNEL);
 		if (!value)
 			return -ENOMEM;
 
-		ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
-		if (ret < 0) {
+		if (size > PAGE_SIZE) {
 			kfree(value);
-			return ret;
+			return -E2BIG;
 		}
 
 		/*
fs/gfs2/acl.c +3 -10
index 443640e6fb9c..01789c23e31c 100644
@@ -83,21 +83,14 @@ struct posix_acl *gfs2_get_acl(struct inode *inode, int type, bool rcu)
 int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	int error;
-	size_t len;
-	char *data;
+	size_t len = 0;
+	char *data = 0;
 	const char *name = gfs2_acl_name(type);
 
 	if (acl) {
-		len = posix_acl_xattr_size(acl->a_count);
-		data = kmalloc(len, GFP_NOFS);
+		data = posix_acl_to_xattr(&init_user_ns, acl, &len, GFP_NOFS);
 		if (data == NULL)
 			return -ENOMEM;
-		error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
-		if (error < 0)
-			goto out;
-	} else {
-		data = NULL;
-		len = 0;
 	}
 
 	error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
fs/jfs/acl.c +2 -7
index 1de3602c98de..16b71a23ff1e 100644
@@ -61,7 +61,7 @@ static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
 {
 	char *ea_name;
 	int rc;
-	int size = 0;
+	size_t size = 0;
 	char *value = NULL;
 
 	switch (type) {
@@ -76,16 +76,11 @@ static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
 	}
 
 	if (acl) {
-		size = posix_acl_xattr_size(acl->a_count);
-		value = kmalloc(size, GFP_KERNEL);
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL);
 		if (!value)
 			return -ENOMEM;
-		rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (rc < 0)
-			goto out;
 	}
 	rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
-out:
 	kfree(value);
 
 	if (!rc)
fs/ntfs3/xattr.c +1 -5
index c93df55e98d0..37a69a75ce68 100644
@@ -641,13 +641,9 @@ static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
 		value = NULL;
 		flags = XATTR_REPLACE;
 	} else {
-		size = posix_acl_xattr_size(acl->a_count);
-		value = kmalloc(size, GFP_NOFS);
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
 		if (!value)
 			return -ENOMEM;
-		err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (err < 0)
-			goto out;
 		flags = 0;
 	}
 
fs/orangefs/acl.c +1 -7
index 5aefb705bcc8..a01ef0c1b1bf 100644
@@ -90,14 +90,9 @@ int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 		     type);
 
 	if (acl) {
-		size = posix_acl_xattr_size(acl->a_count);
-		value = kmalloc(size, GFP_KERNEL);
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL);
 		if (!value)
 			return -ENOMEM;
-
-		error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (error < 0)
-			goto out;
 	}
 
 	gossip_debug(GOSSIP_ACL_DEBUG,
@@ -111,7 +106,6 @@ int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 	 */
 	error = orangefs_inode_setxattr(inode, name, value, size, 0);
 
-out:
 	kfree(value);
 	if (!error)
 		set_cached_acl(inode, type, acl);
fs/posix_acl.c +11 -10
index 768f027c1428..4ef6f9d2b8d6 100644
@@ -829,19 +829,19 @@ EXPORT_SYMBOL (posix_acl_from_xattr);
 /*
  * Convert from in-memory to extended attribute representation.
  */
-int
+void *
 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
-		   void *buffer, size_t size)
+		   size_t *sizep, gfp_t gfp)
 {
-	struct posix_acl_xattr_header *ext_acl = buffer;
+	struct posix_acl_xattr_header *ext_acl;
 	struct posix_acl_xattr_entry *ext_entry;
-	int real_size, n;
+	size_t size;
+	int n;
 
-	real_size = posix_acl_xattr_size(acl->a_count);
-	if (!buffer)
-		return real_size;
-	if (real_size > size)
-		return -ERANGE;
+	size = posix_acl_xattr_size(acl->a_count);
+	ext_acl = kmalloc(size, gfp);
+	if (!ext_acl)
+		return NULL;
 
 	ext_entry = (void *)(ext_acl + 1);
 	ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
@@ -864,7 +864,8 @@ posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
 			break;
 		}
 	}
-	return real_size;
+	*sizep = size;
+	return ext_acl;
 }
 EXPORT_SYMBOL (posix_acl_to_xattr);
 
include/linux/posix_acl_xattr.h +3 -2
index e86f3b731da2..9e1892525eac 100644
@@ -44,8 +44,9 @@ posix_acl_from_xattr(struct user_namespace *user_ns, const void *value,
 }
 #endif
 
-int posix_acl_to_xattr(struct user_namespace *user_ns,
-		       const struct posix_acl *acl, void *buffer, size_t size);
+extern void *posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
+				size_t *sizep, gfp_t gfp);
+
 static inline const char *posix_acl_xattr_name(int type)
 {
 	switch (type) {
Signature · 1 line
2.52.0
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Miklos,

kernel test robot noticed the following build warnings:

[auto build test WARNING on brauner-vfs/vfs.all]
[also build test WARNING on kdave/for-next ceph-client/testing mszeredi-fuse/for-next gfs2/for-next linus/master v6.19-rc5 next-20260115] [cannot apply to ceph-client/for-linus kleikamp-shaggy/jfs-next ericvh-v9fs/for-next hubcap/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Miklos-Szeredi/posix_acl-make-posix_acl_to_xattr-alloc-the-buffer/20260115-202602
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20260115122341.556026-1-mszeredi%40redhat.com
patch subject: [PATCH] posix_acl: make posix_acl_to_xattr() alloc the buffer
config: arm-randconfig-r131-20260116 (https://download.01.org/0day-ci/archive/20260116/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260116/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> fs/gfs2/acl.c:87:22: sparse: sparse: Using plain integer as NULL pointer

vim +87 fs/gfs2/acl.c

    82	
    83	int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
    84	{
    85		int error;
    86		size_t len = 0;
  > 87		char *data = 0;
    88		const char *name = gfs2_acl_name(type);
    89	
    90		if (acl) {
    91			data = posix_acl_to_xattr(&init_user_ns, acl, &len, GFP_NOFS);
    92			if (data == NULL)
    93				return -ENOMEM;
    94		}
    95	
    96		error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
    97		if (error)
    98			goto out;
    99		set_cached_acl(inode, type, acl);

100 out:
101 kfree(data);
102 return error;
103 }
104

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Miklos Szeredi

Nice cleanup.

Quoted 44 lines
fs/ceph/acl.c +4 -8
index 1564eacc253d..34e853fdd0a9 100644
@@ -90,7 +90,8 @@ struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
 int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		 struct posix_acl *acl, int type)
 {
-	int ret = 0, size = 0;
+	int ret = 0;
+	size_t size = 0;
 	const char *name = NULL;
 	char *value = NULL;
 	struct iattr newattrs;
@@ -126,16 +127,11 @@ int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 	}
 
 	if (acl) {
-		size = posix_acl_xattr_size(acl->a_count);
-		value = kmalloc(size, GFP_NOFS);
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
 		if (!value) {
 			ret = -ENOMEM;
 			goto out;
 		}
-
-		ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (ret < 0)
-			goto out_free;
 	}
 
 	if (new_mode != old_mode) {
@@ -172,7 +168,7 @@ int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
 	struct posix_acl *acl, *default_acl;
 	size_t val_size1 = 0, val_size2 = 0;
 	struct ceph_pagelist *pagelist = NULL;
-	void *tmp_buf = NULL;
+	void *tmp_buf1 = NULL, *tmp_buf2 = NULL;
 	int err;
 
 	err = posix_acl_create(dir, mode, &default_acl, &acl);
@@ -192,15 +188,6 @@ int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
 	if (!default_acl && !acl)
 		return 0;

fs/ceph/acl.c int ceph_pre_init_acls(struct inode *dir, umode_t *mode, · line 203

Quoted 10 lines
fs/ceph/acl.c +0 -9
 
-	if (acl)
-		val_size1 = posix_acl_xattr_size(acl->a_count);
-	if (default_acl)
-		val_size2 = posix_acl_xattr_size(default_acl->a_count);
-
-	err = -ENOMEM;
-	tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
-	if (!tmp_buf)
-		goto out_err;

The ENOMEM needs to be retained otherwise you return 0 on ceph_pagelist_alloc_failure afaict.

I'll fix that up as well.

Quoted 3 lines
fs/ceph/acl.c +0 -0
 	pagelist = ceph_pagelist_alloc(GFP_KERNEL);
 	if (!pagelist)
 		goto out_err;
maintainer Applied
#3 lore raw
In reply to Miklos Szeredi
Quoted 6 lines · Miklos Szeredi

Without exception all caller do that. So move the allocation into the helper.

This reduces boilerplate and removes unnecessary error checking.

Applied to the vfs-7.0.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.0.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.0.misc

[1/1] posix_acl: make posix_acl_to_xattr() alloc the buffer

      https://git.kernel.org/vfs/vfs/c/6cbfdf89470e
#4 lore raw
In reply to Miklos Szeredi
Quoted 25 lines · Miklos Szeredi
fs/btrfs/acl.c +2 -3
index c336e2ab7f8a..e55b686fe1ab 100644
@@ -57,7 +57,8 @@ struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu)
  int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
  		    struct posix_acl *acl, int type)
  {
-	int ret, size = 0;
+	int ret;
+	size_t size = 0;
  	const char *name;
  	char AUTO_KFREE(value);
 
@@ -77,20 +78,15 @@ int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
  	if (acl) {
  		unsigned int nofs_flag;
 
-		size = posix_acl_xattr_size(acl->a_count);
  		/*
  		 * We're holding a transaction handle, so use a NOFS memory
  		 * allocation context to avoid deadlock if reclaim happens.
  		 */
  		nofs_flag = memalloc_nofs_save();
-		value = kmalloc(size, GFP_KERNEL);

fs/btrfs/acl.c int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode, · line 91

Quoted 10 lines
fs/btrfs/acl.c +1 -4
+		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL);
  		memalloc_nofs_restore(nofs_flag);
  		if (!value)
  			return -ENOMEM;
-
-		ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
-		if (ret < 0)
-			return ret;
  	}
 

Stupid question, does this work with the AUTO_KFREE() decalration of "value" here? I know that the free is called once value gets out of scope, but before this patch the allocation for value was done in __btrfs_set_acl() as well.

And I think posix_acl_to_xattr() should document the need to free the buffer, for any potential new users to come.

Thanks,

    Johannes