Re: [PATCH] memcg: protect concurrent access to mem_cgroup_idr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Aug 08, 2024 at 04:54:57AM GMT, Matthew Wilcox wrote:
> On Fri, Aug 02, 2024 at 04:58:22PM -0700, Shakeel Butt wrote:
> >  #define MEM_CGROUP_ID_MAX	((1UL << MEM_CGROUP_ID_SHIFT) - 1)
> >  static DEFINE_IDR(mem_cgroup_idr);
> > +static DEFINE_SPINLOCK(memcg_idr_lock);
> > +
> > +static int mem_cgroup_alloc_id(void)
> > +{
> > +	int ret;
> > +
> > +	idr_preload(GFP_KERNEL);
> > +	spin_lock(&memcg_idr_lock);
> > +	ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1,
> > +			GFP_NOWAIT);
> > +	spin_unlock(&memcg_idr_lock);
> > +	idr_preload_end();
> > +	return ret;
> > +}
> 
> You know, this works much better as an xarray:
> 
> static int mem_cgroup_alloc_id(void)
> {
> 	u32 id;
> 	int ret;
> 
> 	ret = xa_alloc(&mem_cgroup_ids, &id, XA_LIMIT(1, MEM_CGROUP_ID_MAX),
> 			GFP_KERNEL);
> 	if (ret < 0)
> 		return ret;
> 	return id;
> }
> 
> No messing around with preloading, the spinlock is built in and the MAX
> works the way you want it to.

Thanks. I would keep the current for the backports and change to xarray
from idr for next release. Please correct me if the following
alternatives are not correct.

1. You already mentioned idr_alloc() -> xa_alloc()
2. idr_remove() -> xa_erase()
3. idr_repalce() -> xa_cmpxchg()

thanks,
Shakeel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]     [Monitors]

  Powered by Linux