Re: [PATCH 2/2] lib/raid6: use kmalloc() in raid6_select_algo()
From: David Laight
Date: Wed May 27 2026 - 04:38:52 EST
On Tue, 26 May 2026 17:38:24 +0300
Mike Rapoport <rppt@xxxxxxxxxx> wrote:
> On Wed, May 20, 2026 at 02:06:57PM +0100, David Laight wrote:
> > On Wed, 20 May 2026 11:17:52 +0300
> > "Mike Rapoport (Microsoft)" <rppt@xxxxxxxxxx> wrote:
> >
> > > raid6_select_algo() allocates an order 3 (8 pages) buffer that is used
> > > as a scratch area for selection of the best algorithm.
> >
> > Should this code really be using a 4k buffer rather than a PAGE_SIZE one?
>
> Why?
Why a PAGE_SIZE buffer at all?
The real data will either be file-system block/fragment or raid stripe sized.
Neither is directly related to the memory system page size.
This actually a valid question for pretty much all of these changes.
Most architectures use 4k pages (probably because 3-level page tables
fit nicely into a 32bit word and it gave a reasonable number of pages
for 1980s systems) but some use much larger pages; I think 64k and 256k
both get used.
(IIRC m68k hardware uses 2k pages, but Linux uses them in pairs.)
Larger pages reduce allocation costs, TLB pressure and memory overhead.
But can waste memory especially for mmap() of small files.
Most of these functions just want 'a reasonable sized buffer'.
Possibly requesting whole pages was originally cheaper.
But I also suspect people have written PAGE_SIZE as a name for 4096.
(Much the same as they forget that some systems have 256 byte cache lines.)
For these changes I think I'd at least add a note in the commit message
when nothing external relies in the size of the buffer and that it could
be changed to 4k.
-- David
>
> > -- David
> >
> > >
> > > For such large allocations kmalloc() would fall back to alloc_pages() but
> > > still kmalloc() is a better API as it does not require unnecessary
> > > castings and may provide more debugging possibilities.
> > >
> > > Replace __get_free_pages() call with kmalloc().
> > >
> > > Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@xxxxxxxxxx
> > > Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
> > > ---
> > > lib/raid6/algos.c | 6 +++---
> > > 1 file changed, 3 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/lib/raid6/algos.c b/lib/raid6/algos.c
> > > index 799e0e5eac26..89e627c62e30 100644
> > > --- a/lib/raid6/algos.c
> > > +++ b/lib/raid6/algos.c
> > > @@ -12,6 +12,7 @@
> > > */
> > >
> > > #include <linux/raid/pq.h>
> > > +#include <linux/slab.h>
> > > #ifndef __KERNEL__
> > > #include <sys/mman.h>
> > > #include <stdio.h>
> > > @@ -129,7 +130,6 @@ const struct raid6_recov_calls *const raid6_recov_algos[] = {
> > > #endif
> > >
> > > #define RAID6_TEST_DISKS 8
> > > -#define RAID6_TEST_DISKS_ORDER 3
> > >
> > > static inline const struct raid6_recov_calls *raid6_choose_recov(void)
> > > {
> > > @@ -250,7 +250,7 @@ int __init raid6_select_algo(void)
> > > int i, cycle;
> > >
> > > /* prepare the buffer and fill it circularly with gfmul table */
> > > - disk_ptr = (char *)__get_free_pages(GFP_KERNEL, RAID6_TEST_DISKS_ORDER);
> > > + disk_ptr = kmalloc(PAGE_SIZE * RAID6_TEST_DISKS, GFP_KERNEL);
> > > if (!disk_ptr) {
> > > pr_err("raid6: Yikes! No memory available.\n");
> > > return -ENOMEM;
> > > @@ -275,7 +275,7 @@ int __init raid6_select_algo(void)
> > > /* select raid recover functions */
> > > rec_best = raid6_choose_recov();
> > >
> > > - free_pages((unsigned long)disk_ptr, RAID6_TEST_DISKS_ORDER);
> > > + kfree(disk_ptr);
> > >
> > > return gen_best && rec_best ? 0 : -EINVAL;
> > > }
> > >
> >
> >
>