Re: [PATCH] lkdtm: use kmalloc() instead of __get_free_page

From: Arnd Bergmann

Date: Thu Jul 23 2026 - 05:06:34 EST


On Thu, Jul 23, 2026, at 10:45, David Laight wrote:
> On Wed, 22 Jul 2026 23:02:46 +0000
> Mahad Ibrahim <mahad.ibrahim.dev@xxxxxxxxx> wrote:
>
>> 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().
>
> 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