s390/char: replace page allocator calls with k[mz]alloc()
Subsystems (4)
cover s390/char: replace page allocator calls with k[mz]alloc()
This the next episode of conversion of page allocator calls to kmalloc family :)
This series covers s390 character drivers other than SCLP.
= tl;dr cover letter copied from the previous posts:
This is a (small) part of larger work of replacing page allocator calls with kmalloc.
My initial intention a few month ago was to remove ugly casts [1], but then willy pointed out that Linus objected to something like this [2] and it looks like more than a decade old technical debt.
Largely, anything that doesn't need struct page (or a memdesc in the future) should just use kmalloc() or kvmalloc() to allocate memory. kmalloc() guarantees alignment, physical contiguity and working virt_to_phys() and beside nicer API that returns void * on alloc and doesn't require to know the allocation size on free, kmalloc() provides better debugging capabilities than page allocator.
Another thing is that touching these allocation sites gives the reviewers opportunity to see if a PAGE_SIZE buffer is actually needed or maybe another size is appropriate.
For larger allocations that don't need physically contiguous memory kvmalloc() can be a better option that __get_free_pages() because under memory pressure it's is easier to allocate several order-0 pages than a physically contiguous chunk with the same number of pages.
And last, but not least, removing needless calls to page allocator should help with memdesc (aka project folio) conversion. There will be way less places to audit to see if the user was actually using struct page.
The patches are deliberately kept small, so that every conversion can be reviewed, and if needed reverted, on its own.
Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/s390-char
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/
---
Mike Rapoport (Microsoft) (5):
s390/zcore: Replace __get_free_page() with kmalloc() s390/vmur: Replace __get_free_page() with kmalloc() s390/vmlogrdr: Replace get_zeroed_page() with kzalloc() s390/uvdevice: Replace get_zeroed_page() with kzalloc() s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
drivers/s390/char/diag_ftp.c | 5 +++-- drivers/s390/char/hmcdrv_cache.c | 18 +++++++++--------- drivers/s390/char/hmcdrv_ftp.c | 14 +++++++------- drivers/s390/char/uvdevice.c | 4 ++-- drivers/s390/char/vmlogrdr.c | 4 ++-- drivers/s390/char/vmur.c | 8 ++++---- drivers/s390/char/zcore.c | 8 ++++---- 7 files changed, 31 insertions(+), 30 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260907-s390-char-ready-29b03a1bf781
--
Sincerely yours,
Mike.
PATCH 1/5 s390/zcore: Replace __get_free_page() with kmalloc()
zcore_reipl_init() allocates buffers that keep a copy of the IPL block and of the OS info structure of the crashed system.
These buffers can be allocated with kmalloc() as there's nothing special about them to go directly to the page allocator.
kmalloc() provides a better API that does not require ugly casts and kfree() does not need to know the size of the freed object.
Performance difference between kmalloc() and __get_free_pages() is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of __get_free_page() with kmalloc() and free_page() with kfree().
--- drivers/s390/char/zcore.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/s390/char/zcore.c +4 -4
@@ -224,7 +224,7 @@ static int __init zcore_reipl_init(void)return rc;if (ipib_info.ipib ==0 )return 0 ; - zcore_ipl_block = (void *)__get_free_page (GFP_KERNEL ); + zcore_ipl_block =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!zcore_ipl_block)return -ENOMEM ;if (ipib_info.ipib < sclp.hsa_size)
@@ -235,7 +235,7 @@ static int __init zcore_reipl_init(void)if (rc || (__force u32 )csum_partial (zcore_ipl_block, zcore_ipl_block->hdr.len,0 ) != ipib_info.checksum) {TRACE ("Checksum does not match\n" ); -free_page ((unsigned long ) zcore_ipl_block); +kfree (zcore_ipl_block); zcore_ipl_block =NULL ; }/*
@@ -244,7 +244,7 @@ static int __init zcore_reipl_init(void)* to continue dump processing, considering that os_info could be * corrupted on the panicked system. */ - os_info = (void *)__get_free_page (GFP_KERNEL ); + os_info =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!os_info)return -ENOMEM ; rc =memcpy_hsa_kernel (&os_info_addr, __LC_OS_INFO,sizeof (os_info_addr));
@@ -266,7 +266,7 @@ static int __init zcore_reipl_init(void) os_info_flags =0 ; } out: -free_page ((unsigned long )os_info); +kfree (os_info);return 0 ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/5] s390/zcore: Replace __get_free_page() with kmalloc()
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
zcore_reipl_init()allocates buffers that keep a copy of the IPL block and of the OS info structure of the crashed system.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
Quoted 8 lines
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of
__get_free_page()withkmalloc()andfree_page()withkfree().
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/5] s390/zcore: Replace __get_free_page() with kmalloc()
In reply to Mike Rapoport (Microsoft)Quoted 16 lines · Mike Rapoport (Microsoft)
zcore_reipl_init()allocates buffers that keep a copy of the IPL block and of the OS info structure of the crashed system.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Quoted 8 lines
Replace use of
__get_free_page()withkmalloc()andfree_page()withkfree().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/s390/char/zcore.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
PATCH 2/5 s390/vmur: Replace __get_free_page() with kmalloc()
In reply to Mike Rapoport (Microsoft)diag14_read() and verify_uri_device() allocate the buffers that receive the spool file data returned by z/VM DIAGNOSE 0x14.
These buffers can be allocated with kmalloc() as there's nothing special about them to go directly to the page allocator.
kmalloc() provides a better API that does not require ugly casts and kfree() does not need to know the size of the freed object.
Performance difference between kmalloc() and __get_free_pages() is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of __get_free_page() with kmalloc() and free_page() with kfree().
--- drivers/s390/char/vmur.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/s390/char/vmur.c +4 -4
@@ -529,7 +529,7 @@ static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,return rc; len =min ((size_t )PAGE_SIZE , count); - buf = (char *)__get_free_page (GFP_KERNEL |GFP_DMA ); + buf =kmalloc (PAGE_SIZE ,GFP_KERNEL |GFP_DMA );if (!buf)return -ENOMEM ;
@@ -556,7 +556,7 @@ static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count, *offs += copied; rc = copied; fail: -free_page ((unsigned long ) buf); +kfree (buf);return rc; }
@@ -621,7 +621,7 @@ static int verify_uri_device(struct urdev *urd) }/* open file on virtual reader */ - buf = (char *)__get_free_page (GFP_KERNEL |GFP_DMA ); + buf =kmalloc (PAGE_SIZE ,GFP_KERNEL |GFP_DMA );if (!buf) { rc = -ENOMEM ; goto fail_free_fcb;
@@ -641,7 +641,7 @@ static int verify_uri_device(struct urdev *urd) rc =0 ; fail_free_buf: -free_page ((unsigned long ) buf); +kfree (buf); fail_free_fcb:kfree (fcb);return rc;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/5] s390/vmur: Replace __get_free_page() with kmalloc()
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
diag14_read()andverify_uri_device()allocate the buffers that receive the spool file data returned by z/VM DIAGNOSE0x14.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
Quoted 8 lines
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of
__get_free_page()withkmalloc()andfree_page()withkfree().
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/5] s390/vmur: Replace __get_free_page() with kmalloc()
In reply to Mike Rapoport (Microsoft)Quoted 16 lines · Mike Rapoport (Microsoft)
diag14_read()andverify_uri_device()allocate the buffers that receive the spool file data returned by z/VM DIAGNOSE0x14.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Quoted 8 lines
Replace use of
__get_free_page()withkmalloc()andfree_page()withkfree().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/s390/char/vmur.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
PATCH 3/5 s390/vmlogrdr: Replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)vmlogrdr_init() allocates the buffers used to receive z/VM system service records via IUCV.
These buffers can be allocated with kmalloc() as there's nothing special about them to go directly to the page allocator.
kmalloc() provides a better API that does not require ugly casts and kfree() does not need to know the size of the freed object.
Performance difference between kmalloc() and __get_free_pages() is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of get_zeroed_page() with kzalloc() and free_page() with kfree().
--- drivers/s390/char/vmlogrdr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/s390/char/vmlogrdr.c +2 -2
@@ -793,7 +793,7 @@ static void vmlogrdr_cleanup(void) }for (i=0 ; i <MAXMINOR ; ++i ) {vmlogrdr_unregister_device (&sys_ser[i]); -free_page ((unsigned long )sys_ser[i].buffer); +kfree (sys_ser[i].buffer); }vmlogrdr_unregister_driver ();if (vmlogrdr_major) {
@@ -826,7 +826,7 @@ static int __init vmlogrdr_init(void) goto cleanup;for (i=0 ; i <MAXMINOR ; ++i ) { - sys_ser[i].buffer = (char *)get_zeroed_page (GFP_KERNEL |GFP_DMA ); + sys_ser[i].buffer =kzalloc (PAGE_SIZE ,GFP_KERNEL |GFP_DMA );if (!sys_ser[i].buffer) { rc = -ENOMEM ;break ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 3/5] s390/vmlogrdr: Replace get_zeroed_page() with kzalloc()
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
vmlogrdr_init()allocates the buffers used to receive z/VM system service records via IUCV.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
Quoted 8 lines
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/5] s390/vmlogrdr: Replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)Quoted 16 lines · Mike Rapoport (Microsoft)
vmlogrdr_init()allocates the buffers used to receive z/VM system service records via IUCV.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Quoted 8 lines
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/s390/char/vmlogrdr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
PATCH 4/5 s390/uvdevice: Replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)uvio_list_secrets() allocates the buffer that receives the list of secrets returned by the Ultravisor.
This buffer can be allocated with kmalloc() as there's nothing special about it to go directly to the page allocator.
kmalloc() provides a better API that does not require ugly casts and kfree() does not need to know the size of the freed object.
Performance difference between kmalloc() and __get_free_pages() is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of get_zeroed_page() with kzalloc() and free_page() with kfree().
--- drivers/s390/char/uvdevice.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/s390/char/uvdevice.c +2 -2
@@ -371,13 +371,13 @@ static int uvio_list_secrets(struct uvio_ioctl_cb *uv_ioctl) uv_ioctl->argument_len % UVIO_LIST_SECRETS_LEN != 0) return -EINVAL; - zpage = (void *)get_zeroed_page (GFP_KERNEL ); + zpage =kzalloc (PAGE_SIZE ,GFP_KERNEL );if (!zpage)return -ENOMEM ; rc =uvio_get_list (zpage, uv_ioctl); -free_page ((unsigned long )zpage); +kfree (zpage);return rc; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 4/5] s390/uvdevice: Replace get_zeroed_page() with kzalloc()
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
uvio_list_secrets()allocates the buffer that receives the list of secrets returned by the Ultravisor.This buffer can be allocated with
kmalloc()as there's nothing special about it to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
Quoted 8 lines
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 4/5] s390/uvdevice: Replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)Quoted 16 lines · Mike Rapoport (Microsoft)
uvio_list_secrets()allocates the buffer that receives the list of secrets returned by the Ultravisor.This buffer can be allocated with
kmalloc()as there's nothing special about it to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Quoted 8 lines
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/s390/char/uvdevice.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
PATCH 5/5 s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
In reply to Mike Rapoport (Microsoft)diag_ftp_cmd() allocates the DIAGNOSE 0x2C4 parameter list used for HMC file transfers under z/VM, hmcdrv_ftp_probe() and hmcdrv_ftp_cmd() allocate the data buffers used for the transfers and hmcdrv_cache_startup() allocates the buffer that caches the contents of the last file read from the HMC drive.
These buffers can be allocated with kmalloc() as there's nothing special about them to go directly to the page allocator.
kmalloc() provides a better API that does not require ugly casts and kfree() does not need to know the size of the freed object.
Performance difference between kmalloc() and __get_free_pages() is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.
For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of get_zeroed_page() and __get_free_pages() with kzalloc() and kmalloc() and free_page[s]() with kfree().
Round transfer and cache buffers up to a page for the required 4K alignment, without rounding to a power-of-two number of pages. Reject unsupported sizes before rounding.
--- drivers/s390/char/diag_ftp.c | 5 +++-- drivers/s390/char/hmcdrv_cache.c | 18 +++++++++--------- drivers/s390/char/hmcdrv_ftp.c | 14 +++++++------- 3 files changed, 19 insertions(+), 18 deletions(-)
drivers/s390/char/diag_ftp.c +3 -2
@@ -10,6 +10,7 @@#define pr_fmt (fmt) "hmcdrv: " fmt#include <linux/kernel.h> +#include <linux/slab.h> #include <linux/mm.h> #include <linux/irq.h> #include <linux/wait.h>
@@ -152,7 +153,7 @@ ssize_t diag_ftp_cmd(const struct hmcdrv_ftp_cmdspec *ftp, size_t *fsize)#endif init_completion (&diag_ftp_rx_complete); - ldfpl = (void *)get_zeroed_page (GFP_KERNEL |GFP_DMA ); + ldfpl =kzalloc (PAGE_SIZE ,GFP_KERNEL |GFP_DMA );if (!ldfpl) { len = -ENOMEM ; goto out;
@@ -208,7 +209,7 @@ ssize_t diag_ftp_cmd(const struct hmcdrv_ftp_cmdspec *ftp, size_t *fsize) } out_free: -free_page ((unsigned long ) ldfpl); +kfree (ldfpl); out:return len; }
drivers/s390/char/hmcdrv_cache.c +9 -9
@@ -11,6 +11,7 @@#include <linux/kernel.h> #include <linux/mm.h> +#include <linux/slab.h> #include <linux/jiffies.h> #include "hmcdrv_ftp.h"
@@ -42,7 +43,6 @@ struct hmcdrv_cache_entry {size_t len; }; -static int hmcdrv_cache_order;/* cache allocated page order */ static struct hmcdrv_cache_entry hmcdrv_cache_file = { .fsize =SIZE_MAX ,
@@ -214,11 +214,14 @@ ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp,*/ int hmcdrv_cache_startup (size_t cachesize) { +if (cachesize >KMALLOC_MAX_SIZE ) +return -ENOMEM ; +if (cachesize >0 ) {/* perform caching ? */ - hmcdrv_cache_order =get_order (cachesize); +/* the buffer must be 4k aligned, hence round up to page size */ hmcdrv_cache_file.content = - (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , - hmcdrv_cache_order); +kmalloc (round_up (cachesize,PAGE_SIZE ), +GFP_KERNEL |GFP_DMA );if (!hmcdrv_cache_file.content) { pr_err("Allocating the requested cache size of %zu bytes failed\n",
@@ -239,11 +242,8 @@ int hmcdrv_cache_startup(size_t cachesize)*/ void hmcdrv_cache_shutdown (void ) { -if (hmcdrv_cache_file.content) { -free_pages ((unsigned long ) hmcdrv_cache_file.content, - hmcdrv_cache_order); - hmcdrv_cache_file.content =NULL ; - } +kfree (hmcdrv_cache_file.content); + hmcdrv_cache_file.content =NULL ; hmcdrv_cache_file.id =HMCDRV_FTP_NOOP ; hmcdrv_cache_file.fsize =LLONG_MAX ;
drivers/s390/char/hmcdrv_ftp.c +7 -7
@@ -194,7 +194,7 @@ int hmcdrv_ftp_probe(void) .len = PAGE_SIZE }; - ftp.buf = (void *)get_zeroed_page (GFP_KERNEL |GFP_DMA ); + ftp.buf =kzalloc (PAGE_SIZE ,GFP_KERNEL |GFP_DMA );if (!ftp.buf)return -ENOMEM ;
@@ -218,7 +218,7 @@ int hmcdrv_ftp_probe(void)break ; }/* switch */ out: -free_page ((unsigned long ) ftp.buf); +kfree (ftp.buf);return rc; }EXPORT_SYMBOL (hmcdrv_ftp_probe);
@@ -238,16 +238,16 @@ EXPORT_SYMBOL(hmcdrv_ftp_probe);ssize_t hmcdrv_ftp_cmd (char __kernel *cmd,loff_t offset,char __user *buf,size_t len) { -int order; -struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};ssize_t retlen =hmcdrv_ftp_parse (cmd, &ftp);if (retlen)return retlen; - order =get_order (ftp.len); - ftp.buf = (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , order); +if (!ftp.len || ftp.len >KMALLOC_MAX_SIZE ) +return -ENOMEM ; + + ftp.buf =kmalloc (round_up (ftp.len,PAGE_SIZE ),GFP_KERNEL |GFP_DMA );if (!ftp.buf)return -ENOMEM ;
@@ -280,7 +280,7 @@ ssize_t hmcdrv_ftp_cmd(char __kernel *cmd, loff_t offset,break ; } -free_pages ((unsigned long ) ftp.buf, order); +kfree (ftp.buf);return retlen; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 5/5] s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] s390/hmcdrv: zero-length requests rejected with -ENOMEM in hmcdrv_ftp_cmd() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] s390/hmcdrv: zero-length requests rejected with -ENOMEM in hmcdrv_ftp_cmd()
--
commit 64ed22d8db2caf381431154b54f669d68274ef30
Author: Mike Rapoport (Microsoft) <[email protected]>
s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
This commit replaces get_zeroed_page(), __get_free_pages(), and free_page[s]() in the s390 hmcdrv driver with kzalloc(), kmalloc(), and kfree(), rounding buffer sizes up to PAGE_SIZE.
Quoted 4 lines
drivers/s390/char/hmcdrv_ftp.c +0
-0
[ ... ]
Quoted 6 lines
drivers/s390/char/hmcdrv_ftp.c +0
-2@@ -238,16 +238,16 @@ EXPORT_SYMBOL(hmcdrv_ftp_probe);ssize_t hmcdrv_ftp_cmd (char __kernel *cmd,loff_t offset,char __user *buf,size_t len) { -int order; -
drivers/s390/char/hmcdrv_ftp.c EXPORT_SYMBOL(hmcdrv_ftp_probe); · line 248
Quoted 10 lines
drivers/s390/char/hmcdrv_ftp.c +2
-2struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};ssize_t retlen =hmcdrv_ftp_parse (cmd, &ftp);if (retlen)return retlen; - order =get_order (ftp.len); - ftp.buf = (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , order); +if (!ftp.len || ftp.len >KMALLOC_MAX_SIZE ) +return -ENOMEM ;
[Severity: Medium]
Does rejecting !ftp.len here break zero-length commands such as HMCDRV_FTP_DELETE?
Previously, get_order(0) returned 0 and __get_free_pages() allocated one page while leaving ftp.len set to 0, allowing a 0-byte read() or write() on /dev/hmcdrv (via hmcdrv_dev_transfer() -> hmcdrv_ftp_cmd()) to execute commands that carry no payload. With this check, those requests fail with -ENOMEM.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Heiko Carstens
maintainerRe: [PATCH 5/5] s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
In reply to Mike Rapoport (Microsoft)Quoted 33 lines · Mike Rapoport (Microsoft)
diag_ftp_cmd()allocates the DIAGNOSE0x2C4parameter list used for HMC file transfers under z/VM,hmcdrv_ftp_probe()andhmcdrv_ftp_cmd()allocate the data buffers used for the transfers andhmcdrv_cache_startup()allocates the buffer that caches the contents of the last file read from the HMC drive.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of
get_zeroed_page()and__get_free_pages()withkzalloc()andkmalloc()and free_page[s]() withkfree().Round transfer and cache buffers up to a page for the required 4K alignment, without rounding to a power-of-two number of pages. Reject unsupported sizes before rounding.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/s390/char/diag_ftp.c | 5 +++-- drivers/s390/char/hmcdrv_cache.c | 18 +++++++++--------- drivers/s390/char/hmcdrv_ftp.c | 14 +++++++------- 3 files changed, 19 insertions(+), 18 deletions(-)
...
drivers/s390/char/hmcdrv_cache.c ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp, · line 218
Quoted 4 lines
int
hmcdrv_cache_startup(size_t cachesize)
{drivers/s390/char/hmcdrv_cache.c +2
-0+if (cachesize >KMALLOC_MAX_SIZE ) +return -ENOMEM ;
What is the reason for this check? I can see that this seems to be a common pattern, however the buddy allocator would warn as well for allocations larger than MAX_PAGE_ORDER if I'm not mistaken.
drivers/s390/char/hmcdrv_cache.c ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp, · line 224
Quoted 9 lines
drivers/s390/char/hmcdrv_cache.c +4
-3+if (cachesize >0 ) {/* perform caching ? */ - hmcdrv_cache_order =get_order (cachesize); +/* the buffer must be 4k aligned, hence round up to page size */ hmcdrv_cache_file.content = - (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , - hmcdrv_cache_order); +kmalloc (round_up (cachesize,PAGE_SIZE ), +GFP_KERNEL |GFP_DMA );
...and that check wasn't here before.
Quoted 10 lines
drivers/s390/char/hmcdrv_ftp.c +0
-2@@ -238,16 +238,16 @@ EXPORT_SYMBOL(hmcdrv_ftp_probe);ssize_t hmcdrv_ftp_cmd (char __kernel *cmd,loff_t offset,char __user *buf,size_t len) { -int order; -
drivers/s390/char/hmcdrv_ftp.c EXPORT_SYMBOL(hmcdrv_ftp_probe); · line 248
Quoted 10 lines
drivers/s390/char/hmcdrv_ftp.c +2
-2struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};ssize_t retlen =hmcdrv_ftp_parse (cmd, &ftp);if (retlen)return retlen; - order =get_order (ftp.len); - ftp.buf = (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , order); +if (!ftp.len || ftp.len >KMALLOC_MAX_SIZE ) +return -ENOMEM ;
Same here, and as far as I can tell the Sashiko finding seems to be valid as well, even though it looks to me as if this worked only by accident. Also, as far as I can see, the reported "delete" command is currently not used at all from the only existing user space application that I'm aware of. But let's not change this, please.
Re: [PATCH 5/5] s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
Quoted 37 lines · Heiko Carstens
Quoted 33 lines · Mike Rapoport (Microsoft)
diag_ftp_cmd()allocates the DIAGNOSE0x2C4parameter list used for HMC file transfers under z/VM,hmcdrv_ftp_probe()andhmcdrv_ftp_cmd()allocate the data buffers used for the transfers andhmcdrv_cache_startup()allocates the buffer that caches the contents of the last file read from the HMC drive.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Performance difference between
kmalloc()and__get_free_pages()is not measurable as both allocators take an object/page from a per-CPU list for fast path allocations.For the slow path the performance is anyway determined by the amount of reclaim involved rather than by what allocator is used.
Replace use of
get_zeroed_page()and__get_free_pages()withkzalloc()andkmalloc()and free_page[s]() withkfree().Round transfer and cache buffers up to a page for the required 4K alignment, without rounding to a power-of-two number of pages. Reject unsupported sizes before rounding.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/s390/char/diag_ftp.c | 5 +++-- drivers/s390/char/hmcdrv_cache.c | 18 +++++++++--------- drivers/s390/char/hmcdrv_ftp.c | 14 +++++++------- 3 files changed, 19 insertions(+), 18 deletions(-)...
Quoted 8 lines
drivers/s390/char/hmcdrv_cache.c ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp, · line 218
Quoted 4 lines
int
hmcdrv_cache_startup(size_t cachesize)
{drivers/s390/char/hmcdrv_cache.c +2
-0+if (cachesize >KMALLOC_MAX_SIZE ) +return -ENOMEM ;What is the reason for this check? I can see that this seems to be a common pattern, however the buddy allocator would warn as well for allocations larger than
MAX_PAGE_ORDERif I'm not mistaken.
Since cachesize is a module parameter there's a very corner case when it could have value within a page of ULONG_MAX. Then round_up() wraps to 0 and kmalloc(0) returns ZERO_SIZE_PTR rather than NULL and does not warn.
The module would load with a bogus cache instead of failing as it does now.
While it's really unlikely to happen in practice I'm sure Sashiko will be unhappy without this check :)
Quoted 28 lines
drivers/s390/char/hmcdrv_cache.c ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp, · line 224
Quoted 9 lines
drivers/s390/char/hmcdrv_cache.c +4
-3+if (cachesize >0 ) {/* perform caching ? */ - hmcdrv_cache_order =get_order (cachesize); +/* the buffer must be 4k aligned, hence round up to page size */ hmcdrv_cache_file.content = - (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , - hmcdrv_cache_order); +kmalloc (round_up (cachesize,PAGE_SIZE ), +GFP_KERNEL |GFP_DMA );...and that check wasn't here before.
Quoted 16 lines
drivers/s390/char/hmcdrv_ftp.c +0
-2@@ -238,16 +238,16 @@ EXPORT_SYMBOL(hmcdrv_ftp_probe);ssize_t hmcdrv_ftp_cmd (char __kernel *cmd,loff_t offset,char __user *buf,size_t len) { -int order; -struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};ssize_t retlen =hmcdrv_ftp_parse (cmd, &ftp);if (retlen)return retlen;
Quoted 10 lines
drivers/s390/char/hmcdrv_ftp.c EXPORT_SYMBOL(hmcdrv_ftp_probe); · line 248
Quoted 4 lines
drivers/s390/char/hmcdrv_ftp.c +2
-2- order =get_order (ftp.len); - ftp.buf = (void *)__get_free_pages (GFP_KERNEL |GFP_DMA , order); +if (!ftp.len || ftp.len >KMALLOC_MAX_SIZE ) +return -ENOMEM ;Same here, and as far as I can tell the Sashiko finding seems to be valid as well, even though it looks to me as if this worked only by accident. Also, as far as I can see, the reported "delete" command is currently not used at all from the only existing user space application that I'm aware of. But let's not change this, please.
Sashiko is wrong about get_order(0) returning 0, it actually returns 52, so __get_free_pages() returns NULL and we end up with -ENOMEM anyway :)
So it's either keep if (!ftp.len) and let it fail for "delete" like it does today or have
size = round_up(ftp.len, PAGE_SIZE) ? : PAGE_SIZE;
and allocate at least a page for ftp.len == 0.
What's your preference?
The ftp.len > KMALLOC_MAX_SIZE check seems bogus indeed.
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 5/5] s390/hmcdrv: Replace page allocator calls with k[mz]alloc()
Quoted 9 lines · Mike Rapoport
Quoted 8 lines
drivers/s390/char/hmcdrv_cache.c ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp, · line 218
Quoted 4 lines
int
hmcdrv_cache_startup(size_t cachesize)
{drivers/s390/char/hmcdrv_cache.c +2
-0+if (cachesize >KMALLOC_MAX_SIZE ) +return -ENOMEM ;What is the reason for this check? I can see that this seems to be a common pattern, however the buddy allocator would warn as well for allocations larger than
MAX_PAGE_ORDERif I'm not mistaken.
Quoted 8 lines
Since cachesize is a module parameter there's a very corner case when it could have value within a page of
ULONG_MAX. Thenround_up()wraps to 0 and kmalloc(0) returnsZERO_SIZE_PTRrather than NULL and does not warn.The module would load with a bogus cache instead of failing as it does now.
While it's really unlikely to happen in practice I'm sure Sashiko will be unhappy without this check :)
Ok, then let's keep that.
Quoted 11 lines
Quoted 8 lines
drivers/s390/char/hmcdrv_ftp.c EXPORT_SYMBOL(hmcdrv_ftp_probe); · line 248
Quoted 2 lines
drivers/s390/char/hmcdrv_ftp.c +2
-0+if (!ftp.len || ftp.len >KMALLOC_MAX_SIZE ) +return -ENOMEM ;Same here, and as far as I can tell the Sashiko finding seems to be valid as well, even though it looks to me as if this worked only by accident. Also, as far as I can see, the reported "delete" command is currently not used at all from the only existing user space application that I'm aware of. But let's not change this, please.
Sashiko is wrong about
get_order(0)returning 0, it actually returns 52, so__get_free_pages()returns NULL and we end up with-ENOMEManyway :)
Yes, indeed.
Quoted 8 lines
So it's either keep if (!ftp.len) and let it fail for "delete" like it does today or have
size = round_up(ftp.len, PAGE_SIZE) ? : PAGE_SIZE;and allocate at least a page for ftp.len == 0.
What's your preference?
Then let's keep the code as it is.
Quoted 1 line
The ftp.len >
KMALLOC_MAX_SIZEcheck seems bogus indeed.
This can be removed when applying - no need to send a new version.
Quoted 60 lines · Mike Rapoport (Microsoft)
This the next episode of conversion of page allocator calls to kmalloc family :)
This series covers s390 character drivers other than SCLP.
= tl;dr cover letter copied from the previous posts:
This is a (small) part of larger work of replacing page allocator calls with kmalloc.
My initial intention a few month ago was to remove ugly casts [1], but then willy pointed out that Linus objected to something like this [2] and it looks like more than a decade old technical debt.
Largely, anything that doesn't need struct page (or a memdesc in the future) should just use
kmalloc()orkvmalloc()to allocate memory.kmalloc()guarantees alignment, physical contiguity and workingvirt_to_phys()and beside nicer API that returns void * on alloc and doesn't require to know the allocation size on free,kmalloc()provides better debugging capabilities than page allocator.Another thing is that touching these allocation sites gives the reviewers opportunity to see if a
PAGE_SIZEbuffer is actually needed or maybe another size is appropriate.For larger allocations that don't need physically contiguous memory
kvmalloc()can be a better option that__get_free_pages()because under memory pressure it's is easier to allocate several order-0 pages than a physically contiguous chunk with the same number of pages.And last, but not least, removing needless calls to page allocator should help with memdesc (aka project folio) conversion. There will be way less places to audit to see if the user was actually using struct page.
The patches are deliberately kept small, so that every conversion can be reviewed, and if needed reverted, on its own.
Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/s390-char[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/---
Mike Rapoport (Microsoft) (5):
s390/zcore: Replace__get_free_page()withkmalloc()s390/vmur: Replace__get_free_page()withkmalloc()s390/vmlogrdr: Replaceget_zeroed_page()withkzalloc()s390/uvdevice: Replaceget_zeroed_page()withkzalloc()s390/hmcdrv: Replace page allocator calls with k[mz]alloc()drivers/s390/char/diag_ftp.c | 5 +++-- drivers/s390/char/hmcdrv_cache.c | 18 +++++++++--------- drivers/s390/char/hmcdrv_ftp.c | 14 +++++++------- drivers/s390/char/uvdevice.c | 4 ++-- drivers/s390/char/vmlogrdr.c | 4 ++-- drivers/s390/char/vmur.c | 8 ++++---- drivers/s390/char/zcore.c | 8 ++++---- 7 files changed, 31 insertions(+), 30 deletions(-)
Quoted 2 lines
Sincerely yours,
Mike.
Applied, thanks!