Re: [PATCH v4] memcg: add charging of already allocated slab objects

From: Shakeel Butt
Date: Fri Sep 06 2024 - 15:04:49 EST


On Fri, Sep 06, 2024 at 07:28:56PM GMT, Vlastimil Babka wrote:
> On 9/6/24 19:19, Yosry Ahmed wrote:
> > [..]
> >> I felt it could be improved more, so ended up with this. Thoughts?
> >>
> >> /**
> >> * kmem_cache_charge - memcg charge an already allocated slab memory
> >> * @objp: address of the slab object to memcg charge
> >> * @gfpflags: describe the allocation context
> >> *
> >> * kmem_cache_charge allows charging a slab object to the current memcg,
> >> * primarily in cases where charging at allocation time might not be possible
> >> * because the target memcg is not known (i.e. softirq context)
> >> *
> >> * The objp should be pointer returned by the slab allocator functions like
> >> * kmalloc (with __GFP_ACCOUNT in flags) or kmem_cache_alloc. The memcg charge
> >
> > Aren't allocations done with kmalloc(__GFP_ACCOUNT) already accounted?
> > Why would we need to call kmem_cache_charge() for those?
>
> AFAIU current_obj_cgroup() returns NULL because we're in the interrupt
> context and no remote memcg context has been set. Thus the charging is
> skipped. The patch commit log describes such scenario for network receive.
> But in case of kmalloc() the allocation must have been still attempted with
> __GFP_ACCOUNT so a kmalloc-cg cache is used even if the charging fails.
>
> If there's another usage for kmem_cache_charge() where the memcg is
> available but we don't want to charge immediately on purpose (such as the
> Linus' idea for struct file), we might need to find another way to tell
> kmalloc() to use the kmalloc-cg cache but not charge immediately...
>

For the struct file, we already have a dedicated kmem_cache
(filp_cachep), so no additional handling would be needed. However in
future we might have cases where we want the kmalloc allocations to
happen from non-normal kmalloc caches and then we can add mechanism to
support such cases.