Re: [PATCH 08/10] kfence: Remove mention of PG_slab

Marco Elver
2025-06-10T13:23:57Z

On Mon, 9 Jun 2025 at 17:02, Matthew Wilcox <willy@infradead.org> wrote:
>
> On Mon, Jun 09, 2025 at 03:33:41PM +0200, Vlastimil Babka wrote:
> > On 6/7/25 00:22, Matthew Wilcox (Oracle) wrote:
> > > Improve the documentation slightly, assuming I understood it correctly.
> >
> > Assuming I understood it correctly, this is going to be fun part of
> > splitting struct slab from struct page. It gets __kfence_pool from memblock
> > allocator and then makes the corresponding struct pages look like slab
> > pages. Maybe it will be possible to simplify things so it won't have to
> > allocate struct slab for each page...
>
> I've been looking at this and I'm not sure I understand it correctly
> either. Perhaps the kfence people can weigh in. It seems like the
> kfence pages are being marked as slab pages, but not being assigned to
> any particular slab cache?

They are marked as slab pages, because in kfree() there's the test for
folio_test_slab(..), which goes to free_large_kmalloc() if it's not a
slab page. But the kfence pool pages cannot be deallocated, given we
want to reuse them due to the particular layout of the pool (object
pages interleaved with guard pages, freed pages are only marked not
present to catch use-after-free).

But besides that they aren't real slab caches, given it's all managed
by kfence (each page can host at most 1 object, but those objects may
be of sizes up to PAGE_SIZE).

Some of this could be solved by adding more is_kfence_address()
checks, but that adds more branches in hot paths and more complexity
since some of the accounting and hooks are naturally shared with the
current design.

> Perhaps the right thing to do will be to allocate slabs for kfence
> objects. Or kfence objects get their own memdesc type. It's hard to
> say at this point. My plan was to disable kfence (along with almost
> everything else) when CONFIG_PAGE_DIET is enabled, and then someone
> who understands what's going on can come in and do the necessary to
> re-enable it.

Assuming it's not a new default, and if this new feature disables most
slab debugging anyway, this appears reasonable.

> > > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> > > ---
> > > mm/kfence/core.c | 4 ++--
> > > 1 file changed, 2 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> > > index 102048821c22..0ed3be100963 100644
> > > --- a/mm/kfence/core.c
> > > +++ b/mm/kfence/core.c
> > > @@ -605,8 +605,8 @@ static unsigned long kfence_init_pool(void)
> > > pages = virt_to_page(__kfence_pool);
> > >
> > > /*
> > > - * Set up object pages: they must have PG_slab set, to avoid freeing
> > > - * these as real pages.
> > > + * Set up object pages: they must have PGTY_slab set to avoid freeing
> > > + * them as real pages.

Acked-by: Marco Elver <elver@google.com>