Skip to contentSkip to conversation

security: replace __get_free_pages() call with kmalloc()

PATCH Applied Mike Rapoport (Microsoft) 2 participants 7 messages May 20 – May 27

Subsystems (4)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover security: replace __get_free_pages() call with kmalloc()

This is a (tiny) part of larger work of replacing page allocator calls with kmalloc:

Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/security

---
Mike Rapoport (Microsoft) (3):
selinux: use k[mz]alloc() to allocate temporary buffers
selinux: hooks: use __getname() to allocate path buffer
apparmor: replace get_zeroed_page() with kzalloc()

 security/apparmor/apparmorfs.c |  5 +++--
 security/selinux/hooks.c       |  4 ++--
 security/selinux/selinuxfs.c   | 12 ++++++------
 3 files changed, 11 insertions(+), 10 deletions(-)

---
base-commit: 5d6919055dec134de3c40167a490f33c74c12581
change-id: 20260520-security-6cdd60da7129

Best regards,

Signature · 2 lines
Sincerely yours,
Mike.
#1 lore raw

PATCH 1/3 selinux: use k[mz]alloc() to allocate temporary buffers

Several functions in selinuxfs.c allocate temporary buffers using __get_free_page() or get_zeroed_page().

These buffers are used either to store a string generated by snprintf() (in sel_make_bools()) or to copy data from user (sel_read_avc_hash_stats() and sel_read_sidtab_hash_stats()).

Such usage does not require struct page access and it is better to allocate these buffers with kzalloc()/kmalloc() that provide better scalability and more debugging possibilities.

Replace use of get_zeroed_page() with kzalloc() and usage of __get_free_page() with kmalloc().

---
 security/selinux/selinuxfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
security/selinux/selinuxfs.c +6 -6
index 25ca7d714014..e7b884eedf80 100644
@@ -1244,7 +1244,7 @@ static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_
 	char **names, *page;
 	u32 i, num;
 
-	page = (char *)get_zeroed_page(GFP_KERNEL);
+	page = kzalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!page)
 		return -ENOMEM;
 
@@ -1290,7 +1290,7 @@ static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_
 		ret = sel_attach_file(bool_dir, names[i], inode);
 	}
 out:
-	free_page((unsigned long)page);
+	kfree(page);
 	return ret;
 }
 
@@ -1349,14 +1349,14 @@ static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
 	char *page;
 	ssize_t length;
 
-	page = (char *)__get_free_page(GFP_KERNEL);
+	page = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!page)
 		return -ENOMEM;
 
 	length = avc_get_hash_stats(page);
 	if (length >= 0)
 		length = simple_read_from_buffer(buf, count, ppos, page, length);
-	free_page((unsigned long)page);
+	kfree(page);
 
 	return length;
 }
@@ -1367,7 +1367,7 @@ static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
 	char *page;
 	ssize_t length;
 
-	page = (char *)__get_free_page(GFP_KERNEL);
+	page = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!page)
 		return -ENOMEM;
 
@@ -1375,7 +1375,7 @@ static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
 	if (length >= 0)
 		length = simple_read_from_buffer(buf, count, ppos, page,
 						length);
-	free_page((unsigned long)page);
+	kfree(page);
 
 	return length;
 }
Signature · 1 line
2.53.0
#3 lore raw

PATCH 2/3 selinux: hooks: use __getname() to allocate path buffer

In reply to Mike Rapoport (Microsoft)

selinux_genfs_get_sid() allocates memory for a path with __get_free_page() although there is a dedicated helper for allocation of file paths:
__getname().

Replace __get_free_page() for allocation of a path buffer with __getname().

---
 security/selinux/hooks.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/selinux/hooks.c +2 -2
index 0f704380a8c8..05b84b3781e0 100644
@@ -1336,7 +1336,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 	struct super_block *sb = dentry->d_sb;
 	char *buffer, *path;
 
-	buffer = (char *)__get_free_page(GFP_KERNEL);
+	buffer = __getname();
 	if (!buffer)
 		return -ENOMEM;
 
@@ -1361,7 +1361,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 			rc = 0;
 		}
 	}
-	free_page((unsigned long)buffer);
+	__putname(buffer);
 	return rc;
 }
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH 3/3 apparmor: replace get_zeroed_page() with kzalloc()

In reply to Mike Rapoport (Microsoft)

multi_transaction_new() allocates memory with get_zeroed_page() and uses it as struct multi_transaction.

The usage of that structure does not require struct page access and it is better to allocate multi_transaction objects with kzalloc() that provides better scalability and more debugging possibilities.

Replace use of get_zeroed_page() with kzalloc().

---
 security/apparmor/apparmorfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
security/apparmor/apparmorfs.c +3 -2
index ededaf46f3ca..e5c99c71e7ca 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/ctype.h>
+#include <linux/slab.h>
 #include <linux/security.h>
 #include <linux/vmalloc.h>
 #include <linux/init.h>
@@ -904,7 +905,7 @@ static void multi_transaction_kref(struct kref *kref)
 	struct multi_transaction *t;
 
 	t = container_of(kref, struct multi_transaction, count);
-	free_page((unsigned long) t);
+	kfree(t);
 }
 
 static struct multi_transaction *
@@ -947,7 +948,7 @@ static struct multi_transaction *multi_transaction_new(struct file *file,
 	if (size > MULTI_TRANSACTION_LIMIT - 1)
 		return ERR_PTR(-EFBIG);
 
-	t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
+	t = kzalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!t)
 		return ERR_PTR(-ENOMEM);
 	kref_init(&t->count);
Signature · 1 line
2.53.0
maintainer Applied
#2 lore raw

Re: [PATCH 1/3] selinux: use k[mz]alloc() to allocate temporary buffers

In reply to Mike Rapoport (Microsoft)
Quoted 12 lines · On May 20, 2026 "Mike Rapoport (Microsoft)" <[email protected]> wrote:

Several functions in selinuxfs.c allocate temporary buffers using __get_free_page() or get_zeroed_page().

These buffers are used either to store a string generated by snprintf() (in sel_make_bools()) or to copy data from user (sel_read_avc_hash_stats() and sel_read_sidtab_hash_stats()).

Such usage does not require struct page access and it is better to allocate these buffers with kzalloc()/kmalloc() that provide better scalability and more debugging possibilities.

Quoted 8 lines

Replace use of get_zeroed_page() with kzalloc() and usage of __get_free_page() with kmalloc().

---
 security/selinux/selinuxfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

I suspect if we look closer we can probably also trim some of those allocations to less then a page, but that can be work for another day.

Merged into selinux/dev, thanks Mike.

--
paul-moore.com

maintainer Applied
#4 lore raw

Re: [PATCH 2/3] selinux: hooks: use __getname() to allocate path buffer

In reply to Mike Rapoport (Microsoft)
Quoted 11 lines · On May 20, 2026 "Mike Rapoport (Microsoft)" <[email protected]> wrote:

selinux_genfs_get_sid() allocates memory for a path with __get_free_page() although there is a dedicated helper for allocation of file paths:
__getname().

Replace __get_free_page() for allocation of a path buffer with __getname().

---
 security/selinux/hooks.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Merged into selinux/dev, thanks.

--
paul-moore.com

maintainer Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] apparmor: replace get_zeroed_page() with kzalloc()

In reply to Mike Rapoport (Microsoft)
Quoted 8 lines · On May 20, 2026 "Mike Rapoport (Microsoft)" <[email protected]> wrote:

multi_transaction_new() allocates memory with get_zeroed_page() and uses it as struct multi_transaction.

The usage of that structure does not require struct page access and it is better to allocate multi_transaction objects with kzalloc() that provides better scalability and more debugging possibilities.

Quoted 7 lines

Replace use of get_zeroed_page() with kzalloc().

---
 security/apparmor/apparmorfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

I'll leave this for John and/or Georgia to review and ultimately decide to merge, but it looks okay to me.

--
paul-moore.com