From nobody Thu Apr 9 18:00:23 2026 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15F3F1F8691 for ; Fri, 6 Mar 2026 07:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772783973; cv=none; b=qTlEmvK0FZICL/v6fzqQD3B3QnDZqWuCx21VLXFj5YF1mlUcd8oaIaXBzIllWpoAAcFFfs76Qeo+3WY8NqJau0BQg/SKERneIvzIw9H+jHtUcTNBkeWs+Nbi1Sl3FJBHGy8IgFuVXQ4aEz0eis30LpGZcs3wYuhoJazkMx9ma/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772783973; c=relaxed/simple; bh=4hSQIoVlgfInIBdA/n7QLjHycxFYgKrdYL0d5fUZqpE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lzvQiAgt7/BuTruBI5rhvxTHcHco1ySen2b57FrFmKGfK3mstAhjLPJWFcrVd/pzia3fDrmKwEE6MQvZUdsDGHGrn25SH2/58eDTKTBrtr6qo0buyZ1wZfobtmjW0L8aRrAOgkQFkHdWFU8OS3KGuGs5K38PBlSr8SXvp40cQA4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=shopee.com; spf=pass smtp.mailfrom=shopee.com; dkim=pass (2048-bit key) header.d=shopee.com header.i=@shopee.com header.b=JDhdAjNI; arc=none smtp.client-ip=209.85.216.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=shopee.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shopee.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shopee.com header.i=@shopee.com header.b="JDhdAjNI" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3567e2b4159so4895599a91.0 for ; Thu, 05 Mar 2026 23:59:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopee.com; s=shopee.com; t=1772783971; x=1773388771; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tACXMiPwwIjQ2HHSPAlpz5qHApNq0epbCchekjDNTPc=; b=JDhdAjNIcp0CUczN4JqREdepO1fsASz0OQfRexgovIbQQStYrtBs6Xgg8vj0XKdoIi N2XDl5b7gopmjbsEaD5uUClBKw26TysD7+01SZ7NPvRd0YWZgtZv/K8z/5cDZ7OGgTcA 2zgxj39rgD1kFym/+PnpW8IstnqmTODDiis4G6gFdYfPBcErn1c9pfp/CcyuIAUS8bT4 +OAKFiVkp0JYyl/7kkTfmsGUyhGW6KwndnGq4nH34dzBdgXplN6OBV7cXkdgiZbojSX+ D6FaaOIFas5mTwLzkAl62bC7GWUWT1lxGTQ9qxBD+yyRIgovgF38XpEvXHV9i3ErKAU7 h1FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772783971; x=1773388771; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=tACXMiPwwIjQ2HHSPAlpz5qHApNq0epbCchekjDNTPc=; b=oRikAMolrlifIqOSHjtqvDExsjkVb2p+63Ryr4uZRwmgKDcUM971RR/oW6dJveHfoy ekYy8LxuBEIhkJhNzeoP3CNOaVfDMf9h1op4WlsWGerRX8rUhcJU9fa1GC0/kjY6iBR8 PjZan2yeSbQVu00WzQi/xZIUagS5RGOUiFhwQKXuMnWJpG1+Yplp1NLU9Jl3XNWwTbsY nREwYVHaSLrg5fU20VoDWV1EOwakrJy4cj5jqkIDM5fHi8y8IJbFGOR7PlNiDCj8VLIC tQgWnnkhw1ufHo2HAXagbmBVxUc/zwqoCMe3o6r7mqVz5xJt8iUFaA/iebfzexKVXJab BCOw== X-Forwarded-Encrypted: i=1; AJvYcCUGQ/7J32vWH994T3x9x200yD0kofJAaqbeDO6K9P7H0MgxVER4Fj6wbVZg3Tz/Dql1s25t1iv7fOQLHHc=@vger.kernel.org X-Gm-Message-State: AOJu0YyhQ+AkHt0xJ6NT2UKRhNMxydgeunjJKuEeYLlQ6kPjWlBSfJLe ACztYTjk85AD+hMThJ28ZkBCeZOC1tUfg5V4dFvuK5gbKB5eKtP41fC5/DWdhZxNQR8= X-Gm-Gg: ATEYQzx3X/+Yx4gMi0D7tuiftbGbgi5i1OuKbzNSiiZy+AeRaCgMvMcyD1Ksj8CgaZF T0t1BRaehnb7K3MvdpYdTEJ28kh4+deE4anN1o9AwYddzkWisDJMed2Yvzl/mmM2RwpUWUkafST zUUsr8DBcY1HkC7+e6ltR0SghZou5udtgODFksUGIQHX2uzXr4tSH9BkxUDI7r8RT/K423u5Y+/ YGnYBOhRt5Id2FD+MdwrA8tKtEnStNzNS/1DSrZupHAcwNOGpd0+bv6Ypiq7cm8BgcqnKzHq7zC CkkyCjJlHXCZg770JNKVYdczTuUAhvyZICPXztJvHrIae6VoXlnjut5w1Gm2S8nClMJVlqnzg2B fRwJfZR2Is2yNRoeQctCo6sboMROGxJbC5lB49CR1YVIUsqTUu8F3YteOLge630x1F7cUM8PLjs turaBXCMNJUVW90RfnNdKMFVcuevjm1G/OpXPyqQ== X-Received: by 2002:a17:90b:384d:b0:359:9083:3661 with SMTP id 98e67ed59e1d1-359be2c9bf2mr1229924a91.15.1772783971285; Thu, 05 Mar 2026 23:59:31 -0800 (PST) Received: from localhost.localdomain ([147.136.157.2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-359bbaa0d83sm804787a91.0.2026.03.05.23.59.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Mar 2026 23:59:30 -0800 (PST) From: Haifeng Xu To: akpm@linux-foundation.org, david@fromorbit.com, roman.gushchin@linux.dev Cc: zhengqi.arch@bytedance.com, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Haifeng Xu Subject: [PATCH 1/3] mm: shrinker: introduce new function get_shrinker_id() Date: Fri, 6 Mar 2026 15:57:54 +0800 Message-ID: <20260306075757.198887-2-haifeng.xu@shopee.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260306075757.198887-1-haifeng.xu@shopee.com> References: <20260306075757.198887-1-haifeng.xu@shopee.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use get_shrinker_id() to retrieve the id of shrinker. No functional change. Signed-off-by: Haifeng Xu --- mm/shrinker.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mm/shrinker.c b/mm/shrinker.c index 7b61fc0ee78f..f0c6dfa026b0 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -193,6 +193,11 @@ static inline int calc_shrinker_id(int index, int offs= et) return index * SHRINKER_UNIT_BITS + offset; } =20 +static inline int get_shrinker_id(struct mem_cgroup *memcg, struct shrinke= r *shrinker) +{ + return shrinker->id; +} + void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) { if (shrinker_id >=3D 0 && memcg && !mem_cgroup_is_root(memcg)) { @@ -255,11 +260,13 @@ static long xchg_nr_deferred_memcg(int nid, struct sh= rinker *shrinker, struct shrinker_info *info; struct shrinker_info_unit *unit; long nr_deferred; + int id; =20 rcu_read_lock(); + id =3D get_shrinker_id(memcg, shrinker); info =3D rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); - unit =3D info->unit[shrinker_id_to_index(shrinker->id)]; - nr_deferred =3D atomic_long_xchg(&unit->nr_deferred[shrinker_id_to_offset= (shrinker->id)], 0); + unit =3D info->unit[shrinker_id_to_index(id)]; + nr_deferred =3D atomic_long_xchg(&unit->nr_deferred[shrinker_id_to_offset= (id)], 0); rcu_read_unlock(); =20 return nr_deferred; @@ -271,12 +278,14 @@ static long add_nr_deferred_memcg(long nr, int nid, s= truct shrinker *shrinker, struct shrinker_info *info; struct shrinker_info_unit *unit; long nr_deferred; + int id; =20 rcu_read_lock(); + id =3D get_shrinker_id(memcg, shrinker); info =3D rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); - unit =3D info->unit[shrinker_id_to_index(shrinker->id)]; + unit =3D info->unit[shrinker_id_to_index(id)]; nr_deferred =3D - atomic_long_add_return(nr, &unit->nr_deferred[shrinker_id_to_offset(shri= nker->id)]); + atomic_long_add_return(nr, &unit->nr_deferred[shrinker_id_to_offset(id)]= ); rcu_read_unlock(); =20 return nr_deferred; --=20 2.43.0 From nobody Thu Apr 9 18:00:23 2026 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F59D37269E for ; Fri, 6 Mar 2026 07:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772783977; cv=none; b=qEfatfncSejtrHl93UdScAnT0x4BWd10jnaTxq9ChByyvcYChIi3jtUYdAveYS7wWZ/OQNZ8u9++XkzoFRwvh8qFAlBHBlgbulmaElFBF88Yx5VijcexMEg2Orap1xkNJgzEnsdhO4/GEQQy4wPJ3am7G701AXyPPrhdF/SNJRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772783977; c=relaxed/simple; bh=pltg4uC7XwqcqY9tG4REqQt6mprsWW6WPe8QbZ/f6ZY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mrVsDY4dLpjICUNf5GHrMcYqg0KwLRmhQUcVUdJO037LLYiQ5AuSk1AHBNy3k38GSraoiKEVkNdEi25H/pQnH7SEPt+SB+GAxGevnV9Z9YlaN2IyjBN7wy0n2md/LGx6mj8iPJ/UmpsJUVAYtIO/J4nzYuVNIBua3ra2rWyQJqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=shopee.com; spf=pass smtp.mailfrom=shopee.com; dkim=pass (2048-bit key) header.d=shopee.com header.i=@shopee.com header.b=ITiqSfqu; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=shopee.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shopee.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shopee.com header.i=@shopee.com header.b="ITiqSfqu" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-3597c40a838so2323713a91.1 for ; Thu, 05 Mar 2026 23:59:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopee.com; s=shopee.com; t=1772783975; x=1773388775; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PiiIe69GsFq8HWnkBbJ0yBz0EjYwZDUtJMK8tMs0bEk=; b=ITiqSfquvmudpKmywGoWuJ8qp5wsIyeLx8fGY/KgAXMlMAUkUo6saEx0WjD1oJKIAl NEhoOJDPOOJ2nrFP30k/g+JGYTtdxLWLoaa9VyIYwf0zbSX4FaslTJqIgc98uk3uod51 DvjdmMgDndv2akJn1jX0ytXgKiiENnfnzEky36/vIYRE65zgI+tkreduW/tBCAHRqApo G1A9fGcREuDje1fzCUxSDKLJ9EAJWj1SNxGRlCd+nMo3Yj5a/5B/ZWk8+Ql8jFx9cyHj lqtTlfx5s6yMrog18Tx/mwqdJ7WoExGK9CgeAg3ualpT7+xw3QfbZcbAOXCemmRgW27d QAvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772783975; x=1773388775; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=PiiIe69GsFq8HWnkBbJ0yBz0EjYwZDUtJMK8tMs0bEk=; b=O734FO5PQJJ78f7OYZBsUqSuiT8ZOCSqv/bQBi/90yXp8ZoqVadfTyunlu2uVlQebZ UHlOSsAWqQbgI5f3UxD3nhoMaQn52isqMqxfWXKPmD1DloUIaUTZcOpwTPmvlt3c4/zd ctNY8EPKSwnYPoTVdRaMc4UcF0gkr3dCMNdYdQtlk2jPqN0dCLVpBeBTBri7HsVrcrTF GDYtLXAy6ZAcw2EscLaUm0+//O2bmawIMlSzJTO08cBmjIigdEEOAjI7eIlgKpNumvCR Vtt0suzaiCiWTQXGnyUR8zYyTme55+SgS/2cy5aYQSqdXLBypRs8ln2F9hQ8hwsjjf7J AM9g== X-Forwarded-Encrypted: i=1; AJvYcCUL3T5EGxx+UK879AZUvo25axhWNEEsg/6k5Xci1QmDmEocUKa2OUNWAnRGi7+Ka45vjCK06K7ptQ9Rgeo=@vger.kernel.org X-Gm-Message-State: AOJu0Yzb3VCgM9gZGo4O28kc/bQb6c/UV6PRIKrJwo27f1ee8inUQsk9 oJ/abqeKzjEkpz7izBR4XJtrxxRYr2l7rZvvTsIGLuHDP72ZHaXX3cWsHDqPqnfoXHs= X-Gm-Gg: ATEYQzx4O2/QQiKDnDF2RF1R5sDYD3w1+jz/gcolOmXPeEFR/zHfQz6bMcfk7vtmDp9 avxV0I7dATD9+mOSMsSaRaSmYEpIkahFJL0NAZYHylVGdwyFnNg+Frhcbg+pqL2GpRhImMcdkpr SdupGCDfqFoibOuQElvR23rCYPOJwJrWSrp4xdO7ye5QKCUj4nGrnqoL/CZfwhiUcnndm9Lxv6v WHNvm+MpvjNTo/lWdABGS4CJbThRBTvviuSI9fPxheErBhJmq7UN4PdK7f4uKBEd6YWNc+FeJpg GnB/JQIc4tyocT1wXGAlv7LjXGyPqseab3RtHGWeWAGlXw7SCwn8Lcwhn4UhOvtjDY0I8XpaXRc pZbNR6VOG2dAPEAAN499ZdroHLEKdomRBy4tghvDYimy6TX3cvBGX4+OHTY4P3uwOITp6+7+5px GnDHducd09V8rJ08Xo+WriSj/H+OBaUyfOR6wFj0bBgxqBUxlq X-Received: by 2002:a17:90b:3e45:b0:354:9b3f:710b with SMTP id 98e67ed59e1d1-359be32bc30mr1164370a91.19.1772783974785; Thu, 05 Mar 2026 23:59:34 -0800 (PST) Received: from localhost.localdomain ([147.136.157.2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-359bbaa0d83sm804787a91.0.2026.03.05.23.59.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Mar 2026 23:59:34 -0800 (PST) From: Haifeng Xu To: akpm@linux-foundation.org, david@fromorbit.com, roman.gushchin@linux.dev Cc: zhengqi.arch@bytedance.com, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Haifeng Xu Subject: [PATCH 2/3] mm: shrinker: optimize the allocation of shrinker_info when setting cgroup_memory_nokmem Date: Fri, 6 Mar 2026 15:57:55 +0800 Message-ID: <20260306075757.198887-3-haifeng.xu@shopee.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260306075757.198887-1-haifeng.xu@shopee.com> References: <20260306075757.198887-1-haifeng.xu@shopee.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/shrinker.h | 3 + mm/huge_memory.c | 21 ++++-- mm/shrinker.c | 135 ++++++++++++++++++++++++++++++++++----- 3 files changed, 138 insertions(+), 21 deletions(-) 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/huge_memory.c b/mm/huge_memory.c index 8e2746ea74ad..319349b5da5d 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4351,9 +4351,14 @@ void deferred_split_folio(struct folio *folio, bool = partially_mapped) memcg =3D folio_split_queue_memcg(folio, ds_queue); list_add_tail(&folio->_deferred_list, &ds_queue->split_queue); ds_queue->split_queue_len++; - if (memcg) - set_shrinker_bit(memcg, folio_nid(folio), - shrinker_id(deferred_split_shrinker)); + if (memcg) { + int id =3D deferred_split_shrinker->id; + + if (!memcg_kmem_online() && memcg !=3D root_mem_cgroup) + id =3D deferred_split_shrinker->nonslab_id; + + set_shrinker_bit(memcg, folio_nid(folio), id); + } } split_queue_unlock_irqrestore(ds_queue, flags); } @@ -4508,8 +4513,14 @@ void reparent_deferred_split_queue(struct mem_cgroup= *memcg) parent_ds_queue->split_queue_len +=3D ds_queue->split_queue_len; ds_queue->split_queue_len =3D 0; =20 - for_each_node(nid) - set_shrinker_bit(parent, nid, shrinker_id(deferred_split_shrinker)); + for_each_node(nid) { + int id =3D deferred_split_shrinker->id; + + if (!memcg_kmem_online() && parent !=3D root_mem_cgroup) + id =3D deferred_split_shrinker->nonslab_id; + + set_shrinker_bit(parent, nid, id); + } =20 unlock: spin_unlock(&parent_ds_queue->split_queue_lock); diff --git a/mm/shrinker.c b/mm/shrinker.c index f0c6dfa026b0..52ea0e6391af 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -12,6 +12,7 @@ DEFINE_MUTEX(shrinker_mutex); =20 #ifdef CONFIG_MEMCG static int shrinker_nr_max; +static int shrinker_nonslab_nr_max; =20 static inline int shrinker_unit_size(int nr_items) { @@ -78,15 +79,25 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) { int nid, ret =3D 0; int array_size =3D 0; + int alloc_nr_max; + + if (memcg_kmem_online()) { + alloc_nr_max =3D shrinker_nr_max; + } else { + if (memcg =3D=3D root_mem_cgroup) + alloc_nr_max =3D shrinker_nr_max; + else + alloc_nr_max =3D shrinker_nonslab_nr_max; + } =20 mutex_lock(&shrinker_mutex); - array_size =3D shrinker_unit_size(shrinker_nr_max); + array_size =3D shrinker_unit_size(alloc_nr_max); for_each_node(nid) { struct shrinker_info *info =3D kvzalloc_node(sizeof(*info) + array_size, GFP_KERNEL, nid); if (!info) goto err; - info->map_nr_max =3D shrinker_nr_max; + info->map_nr_max =3D alloc_nr_max; if (shrinker_unit_alloc(info, NULL, nid)) { kvfree(info); goto err; @@ -147,33 +158,47 @@ static int expand_one_shrinker_info(struct mem_cgroup= *memcg, int new_size, return 0; } =20 -static int expand_shrinker_info(int new_id) +static int expand_shrinker_info(int new_id, bool full, bool root) { int ret =3D 0; int new_nr_max =3D round_up(new_id + 1, SHRINKER_UNIT_BITS); int new_size, old_size =3D 0; struct mem_cgroup *memcg; + struct mem_cgroup *start =3D NULL; + int old_nr_max =3D shrinker_nr_max; =20 if (!root_mem_cgroup) goto out; =20 lockdep_assert_held(&shrinker_mutex); =20 + if (!full && !root) { + start =3D root_mem_cgroup; + old_nr_max =3D shrinker_nonslab_nr_max; + } + new_size =3D shrinker_unit_size(new_nr_max); - old_size =3D shrinker_unit_size(shrinker_nr_max); + old_size =3D shrinker_unit_size(old_nr_max); + + memcg =3D mem_cgroup_iter(NULL, start, NULL); + if (!memcg) + goto out; =20 - memcg =3D mem_cgroup_iter(NULL, NULL, NULL); do { ret =3D expand_one_shrinker_info(memcg, new_size, old_size, new_nr_max); - if (ret) { + if (ret || (root && memcg =3D=3D root_mem_cgroup)) { mem_cgroup_iter_break(NULL, memcg); goto out; } } while ((memcg =3D mem_cgroup_iter(NULL, memcg, NULL)) !=3D NULL); out: - if (!ret) - shrinker_nr_max =3D new_nr_max; + if (!ret) { + if (!full && !root) + shrinker_nonslab_nr_max =3D new_nr_max; + else + shrinker_nr_max =3D new_nr_max; + } =20 return ret; } @@ -195,7 +220,13 @@ static inline int calc_shrinker_id(int index, int offs= et) =20 static inline int get_shrinker_id(struct mem_cgroup *memcg, struct shrinke= r *shrinker) { - return shrinker->id; + int id =3D shrinker->id; + + if (!memcg_kmem_online() && (shrinker->flags & SHRINKER_NONSLAB) && + memcg !=3D root_mem_cgroup) + id =3D shrinker->nonslab_id; + + return id; } =20 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) @@ -217,6 +248,8 @@ void set_shrinker_bit(struct mem_cgroup *memcg, int nid= , int shrinker_id) } =20 static DEFINE_IDR(shrinker_idr); +static DEFINE_IDR(shrinker_nonslab_idr); + =20 static int shrinker_memcg_alloc(struct shrinker *shrinker) { @@ -230,10 +263,46 @@ static int shrinker_memcg_alloc(struct shrinker *shri= nker) if (id < 0) goto unlock; =20 - if (id >=3D shrinker_nr_max) { - if (expand_shrinker_info(id)) { - idr_remove(&shrinker_idr, id); - goto unlock; + shrinker->nonslab_id =3D -1; + + if (!mem_cgroup_kmem_disabled()) { + if (id >=3D shrinker_nr_max) { + /* expand shrinker info for all memory cgroups */ + if (expand_shrinker_info(id, true, false)) { + idr_remove(&shrinker_idr, id); + goto unlock; + } + } + } else { + /* + * If cgroup_memory_nokmem is set, every shrinker needs to be recorded in + * root memory cgroup because gloal slab shrink traverse all shrinkers. = For + * non-root memcgs, record shrinkers with SHRINKER_NONSLAB because memcg + * slab shrink only call non-slab shrinkers. + */ + if (id >=3D shrinker_nr_max) { + /* expand shrinker info for root memory cgroup only */ + if (expand_shrinker_info(id, false, true)) { + idr_remove(&shrinker_idr, id); + goto unlock; + } + } + + if (shrinker->flags & SHRINKER_NONSLAB) { + int nonslab_id; + + nonslab_id =3D idr_alloc(&shrinker_nonslab_idr, shrinker, 0, 0, GFP_KER= NEL); + if (nonslab_id < 0) + goto unlock; + + if (nonslab_id >=3D shrinker_nonslab_nr_max) { + /* expand shrinker info for non-root memory cgroups */ + if (expand_shrinker_info(nonslab_id, false, false)) { + idr_remove(&shrinker_nonslab_idr, id); + goto unlock; + } + } + shrinker->nonslab_id =3D nonslab_id; } } shrinker->id =3D id; @@ -252,6 +321,12 @@ static void shrinker_memcg_remove(struct shrinker *shr= inker) lockdep_assert_held(&shrinker_mutex); =20 idr_remove(&shrinker_idr, id); + + if (shrinker->flags & SHRINKER_NONSLAB) { + id =3D shrinker->nonslab_id; + if (id >=3D 0) + idr_remove(&shrinker_nonslab_idr, id); + } } =20 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, @@ -310,10 +385,33 @@ void reparent_shrinker_deferred(struct mem_cgroup *me= mcg) parent_info =3D shrinker_info_protected(parent, nid); for (index =3D 0; index < shrinker_id_to_index(child_info->map_nr_max); = index++) { child_unit =3D child_info->unit[index]; - parent_unit =3D parent_info->unit[index]; for (offset =3D 0; offset < SHRINKER_UNIT_BITS; offset++) { nr =3D atomic_long_read(&child_unit->nr_deferred[offset]); - atomic_long_add(nr, &parent_unit->nr_deferred[offset]); + + /* + * If memcg_kmem_online() is false, the non-root memcgs use + * nonslab_id but root memory cgroup use id. When reparenting + * shrinker info to it, must convert the nonslab_id to id. + */ + if (!memcg_kmem_online() && parent =3D=3D root_mem_cgroup) { + int id, p_index, p_off; + struct shrinker *shrinker; + + id =3D calc_shrinker_id(index, offset); + shrinker =3D idr_find(&shrinker_nonslab_idr, id); + if (shrinker) { + id =3D shrinker->id; + p_index =3D shrinker_id_to_index(id); + p_off =3D shrinker_id_to_offset(id); + + parent_unit =3D parent_info->unit[p_index]; + atomic_long_add(nr, + &parent_unit->nr_deferred[p_off]); + } + } else { + parent_unit =3D parent_info->unit[index]; + atomic_long_add(nr, &parent_unit->nr_deferred[offset]); + } } } } @@ -543,7 +641,12 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask,= int nid, int shrinker_id =3D calc_shrinker_id(index, offset); =20 rcu_read_lock(); - shrinker =3D idr_find(&shrinker_idr, shrinker_id); + + if (memcg_kmem_online()) + shrinker =3D idr_find(&shrinker_idr, shrinker_id); + else + shrinker =3D idr_find(&shrinker_nonslab_idr, shrinker_id); + if (unlikely(!shrinker || !shrinker_try_get(shrinker))) { clear_bit(offset, unit->map); rcu_read_unlock(); --=20 2.43.0 From nobody Thu Apr 9 18:00:23 2026 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECC3E372B3A for ; Fri, 6 Mar 2026 07:59:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772783980; cv=none; b=sXBT73bk/uJQkV2RGfk/GzBn+S5wbmjGGirmWlfH0MpP37tpwqRahqgrW6A0tQWBe5U9sTf5SyVi4yjlOqQC3JcOJux/hOb3Us4hbkIssJ452NwtdO4dJw5eSd1f26i/XNaez9Bxuyhd144xu6AMMz/9Vvs3cy61k7WVM8zutVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772783980; c=relaxed/simple; bh=ry3sehuoKFfPIQDr095OlRAfL4Ko8+wyhALrbyTEGEQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cZeSCJeUTRPnx7y4RW8Z2HF77d4HJeyKb4eBEWdq8vGmYcMh8/XmjXuSvaN/LPC/UK0fa2fcz2yKRefUqZJkwc5ZgnIHkeSS3TrFdf3ao162N9kjFv7ZSbhpx1GY0F5BvZit0pyckxuDl1dL+7LR7/lLXav64F0Dw98aXH3G8Bc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=shopee.com; spf=pass smtp.mailfrom=shopee.com; dkim=pass (2048-bit key) header.d=shopee.com header.i=@shopee.com header.b=GhDvtjpm; arc=none smtp.client-ip=209.85.216.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=shopee.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shopee.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shopee.com header.i=@shopee.com header.b="GhDvtjpm" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-3598b2318c2so3245703a91.3 for ; Thu, 05 Mar 2026 23:59:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopee.com; s=shopee.com; t=1772783978; x=1773388778; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VPUcTtKDdmEGo0dCBXR6iAhs14HBU2LRhPqQ4/x+vks=; b=GhDvtjpmk7QNMJNfhgH/GpTrPk+Vpq3RO2rbIrzgJZqBtvw43z3we1GMIvgtGGNjel eScxRvahcBbe7JPgGHoEb55jiAfUHrR8nuZP6Kd2eeJM4OEyBdgcnN3AVKcJQuZi7q+y CsChX2luwCexdDay7XraIFv60FGCp21VGM+6i7MARZlhOtBfjkch7pDusVDyiuhaOmF0 MZce8PBwyRkLudAK4es/i0xYrH7DYwkfWtA3XUbJ9noplOfvMQPvKDRbky9BNj2IQN9h MAAoUY3ZYGkA8q36mwghSwqrX1QYTN9Ts4AiGv2jYvaZE8xvjfewxs3Zu73as5E0pPpD W/Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772783978; x=1773388778; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=VPUcTtKDdmEGo0dCBXR6iAhs14HBU2LRhPqQ4/x+vks=; b=wsyB/Yn6CowOv3neQmSI/yaVM2lzkwLMUDYU1OdulvxeB7bjn1tSbqUMzezi6jNPD8 US6rfAlOSJZj7jJTwYZx99P6vjIlETBE2sw8V87vtMui7rhfm9d0K3yaYKJ6W2Ari/sR 9C095KV5oCZIBjKPUL7fUvgMiXEpCI4WnnFtYBzndeZzRoyF/z/+MaMK6FWP6+u+hJHk anBpxSN0XetF8ZxLmWnOz3S/qZMfxVJ4qwrk5QT7YJpIoCoJM6A8MJjusSyvBBcm9aUG NcBO/KsAC6QxHGJa6YrBv+rlfWX1tDpscqfI8ffFdLzUwlOIkLJyKnyNGfPp48gVcZhu tiXA== X-Forwarded-Encrypted: i=1; AJvYcCXIIm8E9Kiww7FK4eRuSG2tfrlQ2zbnC+PmHzc35Yiq3712yAB5VlB1fh6I1ZNAp+7HuNDAMntiTnqPdhI=@vger.kernel.org X-Gm-Message-State: AOJu0YzJusVuLr/xcFBo/1+bXKHEFMCRs2jeg+cGYlczEFxn35D8b0g/ /vQyUprNxzatOPPu/HZLun37rkBsOP/TVY5G+NX2/wAueH1fMGVa4r9o0pD5wJm21CHhe55GQQF o1cnQY90pEg== X-Gm-Gg: ATEYQzyV6PNxzYhaZQLUqG5aOpqrRdoEILGTAT0DbPT1UppnU2ZpoHegygU+rtOLBkc i7IS/CGKgv/0KoXinEE7sXCK7br4wBn6H92K3jlpOCJ8uxhVMoDYIeY968LTgdJb1fFRwxj8MjT 2WGvS0RflkxASyBGY6erffxlCnD169hADoXwH+zV9qOg8gwDGVW6S8JyXrw/rq/hZWGYEYRkAj2 NldtzJMAl6t4bgW9eAfjO4yWYei6uJtrzC97x7GT0G4qLDm+BVDWCko1v3nzbb6ZsRdKg8zuG9t NQuzgtzGMth4DGout4ZCbopWB4Ivu81NYnL03dtPJRIlWmGdvP57kHAuAzBrXUD5sI+k+PirzJ0 AaQauHUBM9H6wNs1KIQe610l5w9W7Gx11VcpYxWJyrV99TtsKDYG+XYfaSVAdGI16/g97mB6sls vs4ZpWdk5B+AZjKlJuNFQRccUsFaU8WgXJmAhPDw== X-Received: by 2002:a17:90b:3c90:b0:32e:1b1c:f8b8 with SMTP id 98e67ed59e1d1-359be375c6dmr1246016a91.26.1772783978384; Thu, 05 Mar 2026 23:59:38 -0800 (PST) Received: from localhost.localdomain ([147.136.157.2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-359bbaa0d83sm804787a91.0.2026.03.05.23.59.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Mar 2026 23:59:38 -0800 (PST) From: Haifeng Xu To: akpm@linux-foundation.org, david@fromorbit.com, roman.gushchin@linux.dev Cc: zhengqi.arch@bytedance.com, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Haifeng Xu Subject: [PATCH 3/3] mm: shrinker: remove unnecessary check in shrink_slab_memcg() Date: Fri, 6 Mar 2026 15:57:56 +0800 Message-ID: <20260306075757.198887-4-haifeng.xu@shopee.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260306075757.198887-1-haifeng.xu@shopee.com> References: <20260306075757.198887-1-haifeng.xu@shopee.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If memcg_kmem_online() is false, only non-slab shrinkers are recorded in the map, so remove the check. Signed-off-by: Haifeng Xu --- mm/shrinker.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/mm/shrinker.c b/mm/shrinker.c index 52ea0e6391af..c8b090e8a972 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -654,11 +654,6 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask,= int nid, } rcu_read_unlock(); =20 - /* Call non-slab shrinkers even though kmem is disabled */ - if (!memcg_kmem_online() && - !(shrinker->flags & SHRINKER_NONSLAB)) - continue; - ret =3D do_shrink_slab(&sc, shrinker, priority); if (ret =3D=3D SHRINK_EMPTY) { clear_bit(offset, unit->map); --=20 2.43.0