Skip to contentSkip to conversation

lkdtm: use kmalloc() instead of __get_free_page

PATCH Applied Mahad Ibrahim 5 participants 12 messages Jul 22 – Aug 21

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

PATCH 1/1 lkdtm: use kmalloc() instead of __get_free_page

lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a temporary buffer, perform copy_from_user to get the crashtype name, strim() to strip whitespace and find_crashtype to find the corresponding crashtype that is being requested.

The lkdtm_debugfs_read uses __get_free_page to allocate a temporary buffer to store all the available crashtypes, and then copy it to userspace.

The buffers that are allocated can be allocated with kmalloc as there is nothing special that requires a struct page, or the page allocator.

kmalloc() additionally provides a better API that doesn't require ugly casts which obfuscate the code and kfree does not need to know the size of the freed object.

Replace use of __get_free_page() with kmalloc().

---
 drivers/misc/lkdtm/core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/misc/lkdtm/core.c +8 -8
index ededa32d6744..01bebcb33bd4 100644
@@ -236,11 +236,11 @@ static ssize_t lkdtm_debugfs_entry(struct file *f,
 	if (count >= PAGE_SIZE)
 		return -EINVAL;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 	if (copy_from_user(buf, user_buf, count)) {
-		free_page((unsigned long) buf);
+		kfree(buf);
 		return -EFAULT;
 	}
 	/* NULL-terminate and remove enter */
@@ -248,7 +248,7 @@ static ssize_t lkdtm_debugfs_entry(struct file *f,
 	strim(buf);
 
 	crashtype = find_crashtype(buf);
-	free_page((unsigned long)buf);
+	kfree(buf);
 
 	if (!crashtype)
 		return -EINVAL;
@@ -271,7 +271,7 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
 	ssize_t out;
 	char *buf;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (buf == NULL)
 		return -ENOMEM;
 
@@ -290,7 +290,7 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
 
 	out = simple_read_from_buffer(user_buf, count, off,
 				      buf, n);
-	free_page((unsigned long) buf);
+	kfree(buf);
 
 	return out;
 }
@@ -313,11 +313,11 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf,
 	if (count < 1)
 		return -EINVAL;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 	if (copy_from_user(buf, user_buf, count)) {
-		free_page((unsigned long) buf);
+		kfree(buf);
 		return -EFAULT;
 	}
 	/* NULL-terminate and remove enter */
@@ -325,7 +325,7 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf,
 	strim(buf);
 
 	crashtype = find_crashtype(buf);
-	free_page((unsigned long) buf);
+	kfree(buf);
 	if (!crashtype)
 		return -EINVAL;
 
Signature · 1 line
2.54.0
#1 lore raw

For context, this is part of the broader __get_free_page() -> kmalloc() transition Mike Rapoport is performing. I noticed it on the mailing lists and found this site which wasn't covered.

Some background:

[1] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/
[2] https://lore.kernel.org/linux-scsi/[email protected]/T/#t

I apologize for this messy reply. I thought I had added the context, but I didn't.

Best regards,
Mahad Ibrahim

maintainer
#2 lore raw
Quoted 11 lines · Mahad Ibrahim

For context, this is part of the broader __get_free_page() -> kmalloc() transition Mike Rapoport is performing. I noticed it on the mailing lists and found this site which wasn't covered.

Some background:

[1] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/
[2] https://lore.kernel.org/linux-scsi/[email protected]/T/#t

I apologize for this messy reply. I thought I had added the context, but I didn't.

Do you want this to land via my tree or via the series Mike is doing?

Signature · 1 line
Kees Cook
#3 lore raw

Hello Kees,

Your tree would be better.

There are no dependencies, nor is drivers/misc/lkdtm/ in Mike's gfp-to-kmalloc branches, so there would be nothing to conflict with.

Thank you,
Mahad Ibrahim

#4 lore raw
In reply to Kees Cook

Hi Kees,

Quoted 7 lines · Kees Cook
Quoted 6 lines · Mahad Ibrahim

For context, this is part of the broader __get_free_page() -> kmalloc() transition Mike Rapoport is performing. I noticed it on the mailing lists and found this site which wasn't covered.

Some background:

Quoted 7 lines
Quoted 5 lines

[1] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/
[2] https://lore.kernel.org/linux-scsi/[email protected]/T/#t

I apologize for this messy reply. I thought I had added the context, but I didn't.

Do you want this to land via my tree or via the series Mike is doing?

I'm not actually maintaining a tree for this, I'm sending patches to the subsystem maintainers, so your tree would be great.

Quoted 2 lines
Signature · 1 line
Kees Cook
Signature · 2 lines
Sincerely yours,
Mike.
#5 lore raw
In reply to Mahad Ibrahim
Quoted 9 lines · On Wed, 22 Jul 2026 23:02:46 +0000 Mahad Ibrahim <[email protected]>…

lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a temporary buffer, perform copy_from_user to get the crashtype name, strim() to strip whitespace and find_crashtype to find the corresponding crashtype that is being requested.

The lkdtm_debugfs_read uses __get_free_page to allocate a temporary buffer to store all the available crashtypes, and then copy it to userspace.

Quoted 8 lines

The buffers that are allocated can be allocated with kmalloc as there is nothing special that requires a struct page, or the page allocator.

kmalloc() additionally provides a better API that doesn't require ugly casts which obfuscate the code and kfree does not need to know the size of the freed object.

Replace use of __get_free_page() with kmalloc().

None of those buffers need to be PAGE_SIZE.
Even the sanity limit for overlong requests could be 4k.
The longest 'crashtype->name' is probably about 32 characters, so could probably go on stack.

And can't this code use the sysfs/kernfs wrappers?

David

Quoted 74 lines
---
 drivers/misc/lkdtm/core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/misc/lkdtm/core.c +8 -8
index ededa32d6744..01bebcb33bd4 100644
@@ -236,11 +236,11 @@ static ssize_t lkdtm_debugfs_entry(struct file *f,
 	if (count >= PAGE_SIZE)
 		return -EINVAL;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 	if (copy_from_user(buf, user_buf, count)) {
-		free_page((unsigned long) buf);
+		kfree(buf);
 		return -EFAULT;
 	}
 	/* NULL-terminate and remove enter */
@@ -248,7 +248,7 @@ static ssize_t lkdtm_debugfs_entry(struct file *f,
 	strim(buf);
 
 	crashtype = find_crashtype(buf);
-	free_page((unsigned long)buf);
+	kfree(buf);
 
 	if (!crashtype)
 		return -EINVAL;
@@ -271,7 +271,7 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
 	ssize_t out;
 	char *buf;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (buf == NULL)
 		return -ENOMEM;
 
@@ -290,7 +290,7 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
 
 	out = simple_read_from_buffer(user_buf, count, off,
 				      buf, n);
-	free_page((unsigned long) buf);
+	kfree(buf);
 
 	return out;
 }
@@ -313,11 +313,11 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf,
 	if (count < 1)
 		return -EINVAL;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 	if (copy_from_user(buf, user_buf, count)) {
-		free_page((unsigned long) buf);
+		kfree(buf);
 		return -EFAULT;
 	}
 	/* NULL-terminate and remove enter */
@@ -325,7 +325,7 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf,
 	strim(buf);
 
 	crashtype = find_crashtype(buf);
-	free_page((unsigned long) buf);
+	kfree(buf);
 	if (!crashtype)
 		return -EINVAL;
 
maintainer
#6 lore raw
Quoted 19 lines · David Laight
Quoted 16 lines · On Wed, 22 Jul 2026 23:02:46 +0000 Mahad Ibrahim <[email protected]>…

lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a temporary buffer, perform copy_from_user to get the crashtype name, strim() to strip whitespace and find_crashtype to find the corresponding crashtype that is being requested.

The lkdtm_debugfs_read uses __get_free_page to allocate a temporary buffer to store all the available crashtypes, and then copy it to userspace.

The buffers that are allocated can be allocated with kmalloc as there is nothing special that requires a struct page, or the page allocator.

kmalloc() additionally provides a better API that doesn't require ugly casts which obfuscate the code and kfree does not need to know the size of the freed object.

Quoted 6 lines
Quoted 1 line

Replace use of __get_free_page() with kmalloc().

None of those buffers need to be PAGE_SIZE.
Even the sanity limit for overlong requests could be 4k.
The longest 'crashtype->name' is probably about 32 characters, so could probably go on stack.

It is a straightforward change with a very low risk of regression, so I see nothing wrong with this version.

If we wanted to improve readability here, I would suggest using strndup_user() to fold ten lines into one.

Arnd

#7 lore raw

Hello Arnd,

Thank you for the feedback and review.

Your suggestion would replace the write paths; if Kees would prefer, I can send it as a v2, if not I can do it as a follow up patch.

Thank you,
Mahad Ibrahim

#8 lore raw
In reply to David Laight
Quoted 19 lines · David Laight
Quoted 16 lines · On Wed, 22 Jul 2026 23:02:46 +0000 Mahad Ibrahim <[email protected]>…

lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a temporary buffer, perform copy_from_user to get the crashtype name, strim() to strip whitespace and find_crashtype to find the corresponding crashtype that is being requested.

The lkdtm_debugfs_read uses __get_free_page to allocate a temporary buffer to store all the available crashtypes, and then copy it to userspace.

The buffers that are allocated can be allocated with kmalloc as there is nothing special that requires a struct page, or the page allocator.

kmalloc() additionally provides a better API that doesn't require ugly casts which obfuscate the code and kfree does not need to know the size of the freed object.

Quoted 6 lines
Quoted 1 line

Replace use of __get_free_page() with kmalloc().

None of those buffers need to be PAGE_SIZE.
Even the sanity limit for overlong requests could be 4k.
The longest 'crashtype->name' is probably about 32 characters, so could probably go on stack.

As Arnd said, this is straightforward change, while changing actual allocation size is more intrusive patch and you can send it if you think it's worth the churn.

Quoted 3 lines

And can't this code use the sysfs/kernfs wrappers?

David

Signature · 2 lines
Sincerely yours,
Mike.
Acked-by on v1 1/1
#9 lore raw
In reply to Mahad Ibrahim
Quoted 12 lines · Mahad Ibrahim

lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a temporary buffer, perform copy_from_user to get the crashtype name, strim() to strip whitespace and find_crashtype to find the corresponding crashtype that is being requested.

The lkdtm_debugfs_read uses __get_free_page to allocate a temporary buffer to store all the available crashtypes, and then copy it to userspace.

The buffers that are allocated can be allocated with kmalloc as there is nothing special that requires a struct page, or the page allocator.

Quoted 7 lines

kmalloc() additionally provides a better API that doesn't require ugly casts which obfuscate the code and kfree does not need to know the size of the freed object.

Replace use of __get_free_page() with kmalloc().

Quoted 3 lines
---
 drivers/misc/lkdtm/core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Signature · 2 lines
Sincerely yours,
Mike.
#10 lore raw
In reply to Mahad Ibrahim

Gentle ping.

Mike Rapoport acked this on-list and said he'd prefer that it goes via your tree. No outstanding review comments.

Boot-tested on 7.2.0-rc4+. Read and write paths tested, clean dmesg.

Thank you,
Mahad Ibrahim

maintainer Applied
#11 lore raw
In reply to Mahad Ibrahim
Quoted 10 lines · Mahad Ibrahim

lkdtm_debugfs_entry and direct_entry use __get_free_page to allocate a temporary buffer, perform copy_from_user to get the crashtype name, strim() to strip whitespace and find_crashtype to find the corresponding crashtype that is being requested.

The lkdtm_debugfs_read uses __get_free_page to allocate a temporary buffer to store all the available crashtypes, and then copy it to userspace.

[...]

Applied to for-next/hardening, thanks!

[1/1] lkdtm: use kmalloc() instead of __get_free_page

      https://git.kernel.org/kees/c/c7ca1fa293ad

Take care,

Signature · 1 line
Kees Cook