From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBC66382F29 for ; Tue, 10 Mar 2026 11:05:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773140747; cv=none; b=YNGtNNHo7s9Pw6QbcLl7Bn39xw/rjEgAY7B0SwmdF/dxQrL4VGlQwQI8nn3X3V13X/W/Ou0aO3R1pN8xdluGKCEgZwNIOcwBOA3DKFULuIuQWxD/jZlVvmoQQRhCiCZYAyWblMOlAL03AZNcm5+5DQEalm5aCoSNGJP96/VBefM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773140747; c=relaxed/simple; bh=ba/kSg4gXuq0pvryrbJnJHfMmpCgziMvHd8sy+b0EoA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GvHuv3Wdm5RC0SyT5JGrX7BFUmwMiHUld8FnqdMAMU9fCurgSQhZBfEfBKc6BaB2oypbgAO8+cxEFUnmW89vfeC71eUMADF8CruQyiKheozTP/6Z/keWKpi+h1OBWvz+rDwhYExBv5ju/WwdG6tZE5fB1lQUcl4DbSn9JLdvFfw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=N2aZCT3r; arc=none smtp.client-ip=95.215.58.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="N2aZCT3r" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1773140742; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4akV6MaGHmSnJk/SzTCTQAyiJw3QkRpB6h4z0N/jqq8=; b=N2aZCT3rN2A3bOZSlbE7ME1QU04ADUX5sf4VTjCoc9fD9LpEnEWln0NYOQ3yekfQsRaAYm ZJtjwe4EN6xrYEh8/Rvej1KTyC4MqGuZ00a6hifeCnDtBYLGJ/RSow0VzgweOWvqn17XDT 11zMIsUABN8SKCd0hAR4CLeT0cOIR3I= From: Usama Arif To: Haifeng Xu Cc: Usama Arif , akpm@linux-foundation.org, david@fromorbit.com, roman.gushchin@linux.dev, zhengqi.arch@bytedance.com, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH V2 3/4] mm: shrinker: optimize the allocation of shrinker_info when setting cgroup_memory_nokmem Date: Tue, 10 Mar 2026 04:05:35 -0700 Message-ID: <20260310110536.3474739-1-usama.arif@linux.dev> In-Reply-To: <20260310031250.289851-4-haifeng.xu@shopee.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On Tue, 10 Mar 2026 11:12:49 +0800 Haifeng Xu wrote: > When kmem is disabled, memcg slab shrink only call non-slab shrinkers, > so just allocates shrinker info for non-slab shrinkers to non-root memcgs. > > Therefore, if memcg_kmem_online is true, all things keep same as before. > Otherwise, root memcg allocates id from shrinker_idr to identify each > shrinker and non-root memcgs use nonslab_id to identify non-slab shrinkers. > The size of shrinkers_info in non-root memcgs can be very low because the > number of shrinkers marked as SHRINKER_NONSLAB | SHRINKER_MEMCG_AWARE is > few. Also, the time spending in expand_shrinker_info() can reduce a lot. > > When setting shrinker bit or updating nr_deferred, use nonslab_id for > non-root memcgs if the shrinker is marked as SHRINKER_NONSLAB. > > Signed-off-by: Haifeng Xu > --- > include/linux/memcontrol.h | 8 ++- > include/linux/shrinker.h | 3 + > mm/shrinker.c | 116 +++++++++++++++++++++++++++++++++---- > 3 files changed, 114 insertions(+), 13 deletions(-) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index ce7b5101bc02..3edd6211aed2 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1804,7 +1804,13 @@ void reparent_shrinker_deferred(struct mem_cgroup *memcg); > > static inline int shrinker_id(struct mem_cgroup *memcg, struct shrinker *shrinker) > { > - return shrinker->id; > + int id = shrinker->id; > + > + if (!memcg_kmem_online() && (shrinker->flags & SHRINKER_NONSLAB) && > + memcg != root_mem_cgroup) > + id = shrinker->nonslab_id; > + > + return id; > } > #else > #define mem_cgroup_sockets_enabled 0 > diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h > index 1a00be90d93a..df53008ed8b5 100644 > --- a/include/linux/shrinker.h > +++ b/include/linux/shrinker.h > @@ -107,6 +107,9 @@ struct shrinker { > #ifdef CONFIG_MEMCG > /* ID in shrinker_idr */ > int id; > + > + /* ID in shrinker_nonslab_idr */ > + int nonslab_id; > #endif > #ifdef CONFIG_SHRINKER_DEBUG > int debugfs_id; > diff --git a/mm/shrinker.c b/mm/shrinker.c > index 61dbb6afae52..68ea2d49495c 100644 > --- a/mm/shrinker.c > +++ b/mm/shrinker.c > @@ -12,6 +12,7 @@ DEFINE_MUTEX(shrinker_mutex); > > #ifdef CONFIG_MEMCG > static int shrinker_nr_max; > +static int shrinker_nonslab_nr_max; > > static inline int shrinker_unit_size(int nr_items) > { > @@ -78,15 +79,25 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) > { > int nid, ret = 0; > int array_size = 0; > + int alloc_nr_max; > + > + if (memcg_kmem_online()) { > + alloc_nr_max = shrinker_nr_max; > + } else { > + if (memcg == root_mem_cgroup) > + alloc_nr_max = shrinker_nr_max; > + else > + alloc_nr_max = shrinker_nonslab_nr_max; > + } > > mutex_lock(&shrinker_mutex); Hello! The patch reads shrinker_nonslab_nr_max and shrinker_nr_max before acquiring shrinker_mutex. The original code read shrinker_nr_max UNDER the lock. Both variables are modified under shrinker_mutex by concurrent shrinker registrations. A stale read could cause alloc_shrinker_info() to allocate an undersized shrinker_info, leading to out-of-bounds access in set_shrinker_bit() or the nr_deferred functions.