From nobody Wed Apr 8 02:51:31 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 026FB3630B0 for ; Wed, 11 Mar 2026 03:02:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773198178; cv=none; b=i1JmhecW8Sqsca9CSNzwE0y5C/I2yiBhQxclo5n5Sgt/M01eidBddG0Py1ACegiwC3mk461Nd+w7hDjRZaEfNGyflae1rh2qnWaV1J6vu+wrmJLDRWo+buQ+jDVAdsxebmnF6RKl7W6UZFTUDOyIJtHoZZbLY+ClQUegsH/yBOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773198178; c=relaxed/simple; bh=42Us+IqcaMdNlezkQsLqEZY9sOJKTa/yaa10PAZo22M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hY2LTSW0NQUcjt3nCpXm87cXRcVwYKfF/7SZc7y2eH547dexBk3pXSnPxWDeuuOyi3UyKbcwOtfFT3WVpdVQMBgNqzv7etkEOiMaZd3RHIVAGRo0PmA6cDg5o9GXuWC/0r5X98ABd9v32Ew1jJ7+omIxiizRKSEkjsrLb3sqh3w= 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=VkLU3d62; arc=none smtp.client-ip=209.85.214.180 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="VkLU3d62" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2a871daa98fso101099715ad.1 for ; Tue, 10 Mar 2026 20:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopee.com; s=shopee.com; t=1773198176; x=1773802976; 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=llSFVwpJUq7vN+RyIjB37GeQ61RCDZGes5EfnEaTg+0=; b=VkLU3d62KCsgLPKogNpxNAWu8PpOhcIW+EksCUtCaVlbljfBmZyBFig07bo4wq8Ysu 0zz4OTkZTYbr1O/LJ4GGzF3WC9Obbzuy8mJEf1i6qpBugCFd82VSKdPNfxSDByhQpMDf 9OKVo6689GVEILIirGDUDUNqfMss/2fXnmdFcsrk0k53IdzCBVH3A6TfHoubsqhqCWHw YWNydmioDQCXftCpTeZ9OqIfbUlFPMyXIXnxHpgbTmm5+GDqVjYK+zvlPeAM3mBMjkqt YOcIUCwdH180pM6GW83P3bJb/E7BXJNP/3n1ixwSaVs8E5o6Mg/LerpvmUyJsCmUE8An qYJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773198176; x=1773802976; 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=llSFVwpJUq7vN+RyIjB37GeQ61RCDZGes5EfnEaTg+0=; b=rJ8uG20DyPJmR1xYvKuHbMYMaAVjDN3YAECq87pGfBT1I4dkDKGXQoNeWHI0Pu2yy/ JFnKKqBrK5JNHOV6mBBU3eWrUbRwwEHrFANMbduhypyEiIt5KWaTZe5Xosic3XSQ0jwP B3R7nYzjC6fyuh3j66zsxr4OKNtK0CU2tHFVGHIqO7q9rIOX6dtBSHIEGzfIJkkMwT/z 8hPlD8DjyAC/AJeTxQ2kjXKzC21Onvprx/8cj+qa9zhfxpQTmRolTdtPFecQkPRc7Ror zrrFfTH5lN/aqagFElJz+iXGyR7Oyq9SaUCiDtsQBnG0h/iayxlxnC7IWq6wDndD0KXT aTdQ== X-Forwarded-Encrypted: i=1; AJvYcCVQP70uhFULfMTerXu/oKS0ReAYmEjQFRWM/zd1eOhHK5OJmQABMJ1w/tgopuYJX8jnCdGDSmhyFCE8kf0=@vger.kernel.org X-Gm-Message-State: AOJu0Ywtd697u6zOBB8eRjgeyQEFGablrBB4jLX2C6EUIS4lHd5JaJGS zNbye4QMa5YCZB+ZhJV92xFKZe+lFyuQlHjZNr2QMlIPxDi4cgFXj4t4Kd0QAx2xHc0= X-Gm-Gg: ATEYQzzobpoYBtYiOUqQfeQH+efY9zHZKV/Mmm8x+2+hqhtTpSArn0IDi0xEEljPwgs BNKAumlEMX3tFcKe+9V6guEVsUYuVSm9phx2a52ahDktqe6DelwKE/lyGLKwnFtLb17x0iojDPZ R9RnbzrExqaDkz3MGk0Ot7lFlnPSAHHKDCn+0ZPg8HnnHknjjWuMu4VEKWr/dcmkv1kIxbJxrED uHU3jPnSUutlICzTJurfk88zu8aSVZvBNDZ6KXhglwtcJb9O5AbSMcxqfFuGKaBy6BbQo2ut44e pUV3aOR4NJYs63UI0fzrUEhG3hfDVumiIOI1rzcPgjayN4PWtp7gaQpmd3JCEAEc3XrmJ2IiyZT DwqfagKUZMw3EmI3yZzclAUHDYmSZNSf3DZMOcNJ+HCTGwYBO9qwKkZmxvaIYW7b3Um86VHbsPH al9Fl2aFpi0P7FB5PHvnbFf2hu2Gj8v8T3836zUw== X-Received: by 2002:a17:903:18d:b0:2ae:3dd5:c69c with SMTP id d9443c01a7336-2aeae858e3fmr11351905ad.33.1773198176279; Tue, 10 Mar 2026 20:02:56 -0700 (PDT) Received: from localhost.localdomain ([147.136.157.1]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2aeae378974sm6251555ad.82.2026.03.10.20.02.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2026 20:02:55 -0700 (PDT) From: Haifeng Xu To: akpm@linux-foundation.org, david@fromorbit.com, roman.gushchin@linux.dev, hannes@cmpxchg.org, mhocko@kernel.org, shakeel.butt@linux.dev Cc: zhengqi.arch@bytedance.com, muchun.song@linux.dev, usama.arif@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Haifeng Xu Subject: [PATCH V3 3/4] mm: shrinker: optimize the allocation of shrinker_info when setting cgroup_memory_nokmem Date: Wed, 11 Mar 2026 11:02:34 +0800 Message-ID: <20260311030235.240953-4-haifeng.xu@shopee.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260311030235.240953-1-haifeng.xu@shopee.com> References: <20260311030235.240953-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/memcontrol.h | 8 ++- include/linux/shrinker.h | 3 + mm/shrinker.c | 117 +++++++++++++++++++++++++++++++++---- 3 files changed, 115 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 *m= emcg); =20 static inline int shrinker_id(struct mem_cgroup *memcg, struct shrinker *s= hrinker) { - 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; } #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..3137518bc746 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,26 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) { int nid, ret =3D 0; int array_size =3D 0; + int alloc_nr_max; =20 mutex_lock(&shrinker_mutex); - array_size =3D shrinker_unit_size(shrinker_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; + } + + 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 +159,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; } @@ -212,25 +238,58 @@ void set_shrinker_bit(struct mem_cgroup *memcg, int n= id, int shrinker_id) } =20 static DEFINE_IDR(shrinker_idr); +static DEFINE_IDR(shrinker_nonslab_idr); =20 static int shrinker_memcg_alloc(struct shrinker *shrinker) { int id, ret =3D -ENOMEM; + bool kmem_online; =20 if (mem_cgroup_disabled()) return -ENOSYS; =20 + kmem_online =3D memcg_kmem_online(); mutex_lock(&shrinker_mutex); id =3D idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); if (id < 0) goto unlock; =20 if (id >=3D shrinker_nr_max) { - if (expand_shrinker_info(id)) { + /* If memcg_kmem_online() returns true, expand shrinker + * info for all memcgs, otherwise, expand shrinker info + * for root memcg only + */ + if (expand_shrinker_info(id, kmem_online, !kmem_online)) { idr_remove(&shrinker_idr, id); goto unlock; } } + + shrinker->nonslab_id =3D -1; + /* + * If cgroup_memory_nokmem is set, record shrinkers with SHRINKER_NONSLAB + * because memcg slab shrink only call non-slab shrinkers. + */ + if (!kmem_online && shrinker->flags & SHRINKER_NONSLAB) { + int nonslab_id; + + nonslab_id =3D idr_alloc(&shrinker_nonslab_idr, shrinker, 0, 0, GFP_KERN= EL); + if (nonslab_id < 0) { + idr_remove(&shrinker_idr, id); + goto unlock; + } + + if (nonslab_id >=3D shrinker_nonslab_nr_max) { + /* expand shrinker info for non-root memcgs */ + if (expand_shrinker_info(nonslab_id, false, false)) { + idr_remove(&shrinker_idr, id); + idr_remove(&shrinker_nonslab_idr, nonslab_id); + goto unlock; + } + } + shrinker->nonslab_id =3D nonslab_id; + } + shrinker->id =3D id; ret =3D 0; unlock: @@ -247,6 +306,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, @@ -305,10 +370,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]); + } } } } @@ -538,7 +626,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