USB: replace __get_free_pages() with kmalloc()
Subsystems (3)
v2 cover USB: replace __get_free_pages() with kmalloc()
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.
Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/usb
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/
---
v2 changes:
- rephrase the sentence about unconditional kfree() to make it clear why it is safe
- don't include <linux/slab.h> in ohci-dbg
- fix name of debugfs directory
v1: https://patch.msgid.link/[email protected]
---
Mike Rapoport (Microsoft) (2):
usb: host: ohci-dbg: use kmalloc() for print buffer
usb: core: devices: use kmalloc() to allocate dump buffer
drivers/usb/core/devices.c | 7 ++++--- drivers/usb/host/ohci-dbg.c | 9 +++------ 2 files changed, 7 insertions(+), 9 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260616-b4-usb-b302cae4004d
Best regards,
Signature · 2 lines
Sincerely yours, Mike.
PATCH v2 1/2 usb: host: ohci-dbg: use kmalloc() for print buffer
ochi-dbg allocates buffers for formatting of various dump outputs.
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().
While on it, drop the NULL checks in debug_close(). buf is never NULL here because all the open handlers return -ENOMEM when alloc_buffer() fails, and kfree() can handle a NULL buf->page.
--- drivers/usb/host/ohci-dbg.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/usb/host/ohci-dbg.c +3 -6
@@ -683,7 +683,7 @@ static int fill_buffer(struct debug_buffer *buf)int ret;if (!buf->page) - buf->page = (char *)get_zeroed_page (GFP_KERNEL ); + buf->page =kzalloc (PAGE_SIZE ,GFP_KERNEL );if (!buf->page) { ret = -ENOMEM ;
@@ -729,11 +729,8 @@ static int debug_close(struct inode *inode, struct file *file) {struct debug_buffer *buf = file->private_data; -if (buf) { -if (buf->page) -free_page ((unsigned long )buf->page); -kfree (buf); - } +kfree (buf->page); +kfree (buf);return 0 ; }
Signature · 1 line
2.53.0
Re: [PATCH v2 1/2] usb: host: ohci-dbg: use kmalloc() for print buffer
Quoted 18 lines · Mike Rapoport (Microsoft)
ochi-dbg allocates buffers for formatting of various dump outputs.
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()withkzalloc()andfree_page()withkfree().
Quoted 7 lines
While on it, drop the NULL checks in
debug_close(). buf is never NULL here because all the open handlers return-ENOMEMwhenalloc_buffer()fails, andkfree()can handle a NULLbuf->page.Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
Incidentally, I filtered it out of my reply this time, but on this and previous submissions you have a CC: entry that says <[email protected]>, which is an invalid address (three 'p's instead of two). Is there a typo in one of your scripts or settings? It's a little surprising that you aren't constantly getting email error messages every time you send something out.
Alan Stern
Quoted 34 lines
drivers/usb/host/ohci-dbg.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)drivers/usb/host/ohci-dbg.c +3
-6@@ -683,7 +683,7 @@ static int fill_buffer(struct debug_buffer *buf)int ret;if (!buf->page) - buf->page = (char *)get_zeroed_page (GFP_KERNEL ); + buf->page =kzalloc (PAGE_SIZE ,GFP_KERNEL );if (!buf->page) { ret = -ENOMEM ;@@ -729,11 +729,8 @@ static int debug_close(struct inode *inode, struct file *file) {struct debug_buffer *buf = file->private_data; -if (buf) { -if (buf->page) -free_page ((unsigned long )buf->page); -kfree (buf); - } +kfree (buf->page); +kfree (buf);return 0 ; }Signature · 2 lines
-- 2.53.0
Re: [PATCH v2 1/2] usb: host: ohci-dbg: use kmalloc() for print buffer
Quoted 23 lines · Alan Stern
Quoted 22 lines · Mike Rapoport (Microsoft)
ochi-dbg allocates buffers for formatting of various dump outputs.
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()withkzalloc()andfree_page()withkfree().While on it, drop the NULL checks in
debug_close(). buf is never NULL here because all the open handlers return-ENOMEMwhenalloc_buffer()fails, andkfree()can handle a NULLbuf->page.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
Reviewed-by Alan Stern <[email protected]>
Thanks!
Quoted 6 lines
Incidentally, I filtered it out of my reply this time, but on this and previous submissions you have a CC: entry that says <[email protected]>, which is an invalid address (three 'p's instead of two). Is there a typo in one of your scripts or settings? It's a little surprising that you aren't constantly getting email error messages every time you send something out.
I'm gradually switching to b4 send and this looks like off-by-one in my manual editing of the addresses.
Quoted 1 line
Alan Stern
Signature · 2 lines
Sincerely yours, Mike.
PATCH v2 2/2 usb: core: devices: use kmalloc() to allocate dump buffer
In reply to Mike Rapoport (Microsoft)usb_device_dump() allocates a buffer for formatting /sys/kernel/debug/usb/devices output text.
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_free_pages() with kmalloc() and free_pages() with kfree().
--- drivers/usb/core/devices.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/core/devices.c +4 -3
@@ -37,6 +37,7 @@*/ #include <linux/fs.h> +#include <linux/slab.h> #include <linux/mm.h> #include <linux/gfp.h> #include <linux/usb.h>
@@ -408,7 +409,7 @@ static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes,return 0 ;/* allocate 2^1 pages = 8K (on i386); * should be more than enough for one device */ - pages_start = (char *)__get_free_pages (GFP_NOIO ,1 ); + pages_start =kmalloc (PAGE_SIZE <<1 ,GFP_NOIO );if (!pages_start)return -ENOMEM ;
@@ -479,7 +480,7 @@ static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes,if (length > *nbytes) length = *nbytes;if (copy_to_user (*buffer, pages_start + *skip_bytes, length)) { -free_pages ((unsigned long )pages_start,1 ); +kfree (pages_start);return -EFAULT ; } *nbytes -= length;
@@ -490,7 +491,7 @@ static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes, }else *skip_bytes -= length; -free_pages ((unsigned long )pages_start,1 ); +kfree (pages_start);/* Now look at all of this device's children. */ usb_hub_for_each_child (usbdev, chix, childdev) {
Signature · 1 line
2.53.0