From nobody Thu Apr 9 19:20:15 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