Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()

From: Kent Overstreet
Date: Thu May 18 2023 - 15:11:00 EST


On Thu, May 18, 2023 at 12:01:26PM -0700, Song Liu wrote:
> On Thu, May 18, 2023 at 9:58 AM Kent Overstreet
> <kent.overstreet@xxxxxxxxx> wrote:
> >
> > On Thu, May 18, 2023 at 09:33:20AM -0700, Song Liu wrote:
> > > I am working on patches based on the discussion in [1]. I am planning to
> > > send v1 for review in a week or so.
> >
> > For reference, here's my own (early, but functioning :) slab allocator:
> >
> > Look forward to comparing!
> > -->--
> > From 6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976 Mon Sep 17 00:00:00 2001
> > From: Kent Overstreet <kent.overstreet@xxxxxxxxx>
> > Date: Wed, 17 May 2023 01:22:06 -0400
> > Subject: [PATCH] mm: jit/text allocator
> >
> > This provides a new, very simple slab allocator for jit/text, i.e. bpf,
> > ftrace trampolines, or bcachefs unpack functions.
> >
> > With this API we can avoid ever mapping pages both writeable and
> > executable (not implemented in this patch: need to tweak
> > module_alloc()), and it also supports sub-page sized allocations.
> >
> > Signed-off-by: Kent Overstreet <kent.overstreet@xxxxxxxxx>
>
> [...]
>
> > +static void *jit_cache_alloc(void *buf, size_t len, struct jit_cache *cache)
> > +{
> > + struct jit_slab *s =
> > + list_first_entry_or_null(&cache->partial, struct jit_slab, list) ?:
> > + jit_slab_alloc(cache);
> > + unsigned obj_idx, nr_allocated;
> > +
> > + if (!s)
> > + return NULL;
> > +
> > + obj_idx = find_first_zero_bit(s->objs_allocated, cache->objs_per_slab);
> > +
> > + BUG_ON(obj_idx >= cache->objs_per_slab);
> > + __set_bit(obj_idx, s->objs_allocated);
> > +
> > + nr_allocated = bitmap_weight(s->objs_allocated, s->cache->objs_per_slab);
> > +
> > + if (nr_allocated == s->cache->objs_per_slab) {
> > + list_del_init(&s->list);
> > + } else if (nr_allocated == 1) {
> > + list_del(&s->list);
> > + list_add(&s->list, &s->cache->partial);
> > + }
> > +
> > + return s->executably_mapped + (obj_idx << cache->obj_size_bits);
> > +}
>
> IIUC, "len" is ignored in jit_cache_alloc(), so it can only handle
> <=16 byte allocations?

len is a redundant parameter (good catch); at that point we've picked a
cache for the specific allocation size.

Since there's multiple caches for each power of two size, it can handle
allocations up to PAGE_SIZE.