From patchwork Mon Dec 13 16:53:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12674407 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AF92C433EF for ; Mon, 13 Dec 2021 17:02:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 756AD6B0073; Mon, 13 Dec 2021 11:56:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7066A6B0080; Mon, 13 Dec 2021 11:56:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57F876B0089; Mon, 13 Dec 2021 11:56:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay030.a.hostedemail.com [64.99.140.30]) by kanga.kvack.org (Postfix) with ESMTP id 49B116B0073 for ; Mon, 13 Dec 2021 11:56:39 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D449F207BD for ; Mon, 13 Dec 2021 16:56:27 +0000 (UTC) X-FDA: 78913374414.05.FBABEFC Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf17.hostedemail.com (Postfix) with ESMTP id 989EE4000F for ; Mon, 13 Dec 2021 16:56:22 +0000 (UTC) Received: by mail-pj1-f49.google.com with SMTP id nh10-20020a17090b364a00b001a69adad5ebso13842516pjb.2 for ; Mon, 13 Dec 2021 08:56:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yE80mqiok0lBzQsvKCygl5SYuh3BGa/YlTAclAjawz8=; b=WprY6MrF9pfD9yPre2IS9eUB2V7hXNkqMvFWWiLu3/WIs2ETn41uzacZR1n2xQejdu +j8kw8jEenoRuxuaD3VpAj9WBXjrANBkGSEgpbnLAWYmbsBSgz/KpISu2FOctnW+R6xC LVRS0wfF4wxy0iAbSPTFtLFlEQbyvS5ccd/Jf6etqlFGZ3sqUKq4Hls0e9uER6bJavEx v+RjkDiXPiE4AE46zYqyvcIxc3i0Hji92TU+JtKen/GOvaERHdEG1FWOAn9iov/2Ojih bLD+PJH7YsoIlMqHWC1pNzStchXV08YEG6DTXWxNORmRhW8xwJ7A8iA7LJRpVvgblxSS japw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yE80mqiok0lBzQsvKCygl5SYuh3BGa/YlTAclAjawz8=; b=D1EXaYnvrY/91zVNW6o3bRv67703rkbnMkQGdv8Ltdy519K5PY7LvUTliIkQPVE+8w nh/fyic+xMOZE+BHnCCKmnxVY+C1JhCx3zR7KGgbfErrBiGROgIrOyMH6Jf+Uudoaohs Ni0AzGCHdcnFrfV8qzPjeSNtonjRzbURT1EJIR0bBA4YmS8mukNTcnZPGkqbkoNtT0Q3 Cc5P3W1V5JnOxCrchNlloWzff1EuMmaGqb6PKlkoMJMa7AkkgvTFowONkkxhIDr/2QWE ea/m/cB0c8ZxnDHJ2UZe3DndetAOfYCjOW0CZ+4ehzAhBJSARFA41T5J0wSaZ4dtEL+i nm2A== X-Gm-Message-State: AOAM533fG5Cr8ZlAufR1qIQxHKG6xTJQ0/rHwP9mUqVB7mFmVhUC/yM9 bBCQsXM4+BKzjtcNvDuj9QK2wQ== X-Google-Smtp-Source: ABdhPJx5lBZ3oSuFgfk4GYVzH0w+u1AhyAcUmo6rwNGnKRgniT1jXiIidIHrL2i+URu2tl0zRvKPAA== X-Received: by 2002:a17:90b:17c4:: with SMTP id me4mr45311597pjb.15.1639414586138; Mon, 13 Dec 2021 08:56:26 -0800 (PST) Received: from localhost.localdomain ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id n11sm10430992pgp.15.2021.12.13.08.56.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Dec 2021 08:56:25 -0800 (PST) From: Muchun Song To: willy@infradead.org, akpm@linux-foundation.org, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, shakeelb@google.com, guro@fb.com, shy828301@gmail.com, alexs@kernel.org, richard.weiyang@gmail.com, david@fromorbit.com, trond.myklebust@hammerspace.com, anna.schumaker@netapp.com, jaegeuk@kernel.org, chao@kernel.org, kari.argillander@gmail.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org, zhengqi.arch@bytedance.com, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v4 14/17] mm: memcontrol: reuse memory cgroup ID for kmem ID Date: Tue, 14 Dec 2021 00:53:39 +0800 Message-Id: <20211213165342.74704-15-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20211213165342.74704-1-songmuchun@bytedance.com> References: <20211213165342.74704-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 989EE4000F Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=WprY6MrF; spf=pass (imf17.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam04 X-Stat-Signature: 44764stmr8ppx6bodr8nqiytuuronz8g X-HE-Tag: 1639414582-790063 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: There are two idrs being used by memory cgroup, one is for kmem ID, another is for memory cgroup ID. The maximum ID of both is 64Ki. Both of them can limit the total number of memory cgroups. Actually, we can reuse memory cgroup ID for kmem ID to simplify the code. Signed-off-by: Muchun Song --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 46 ++++++++-------------------------------------- 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 3fc437162add..7b472f805d77 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -56,6 +56,7 @@ struct mem_cgroup_reclaim_cookie { #ifdef CONFIG_MEMCG #define MEM_CGROUP_ID_SHIFT 16 +#define MEM_CGROUP_ID_MIN 1 #define MEM_CGROUP_ID_MAX USHRT_MAX struct mem_cgroup_id { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 28d6d2564f9d..04f75055f518 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -348,23 +348,6 @@ static void memcg_reparent_objcgs(struct mem_cgroup *memcg, } /* - * This will be used as a shrinker list's index. - * The main reason for not using cgroup id for this: - * this works better in sparse environments, where we have a lot of memcgs, - * but only a few kmem-limited. - */ -static DEFINE_IDA(memcg_cache_ida); - -/* - * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get - * this constant directly from cgroup, but it is understandable that this is - * better kept as an internal representation in cgroup.c. In any case, the - * cgrp_id space is not getting any smaller, and we don't have to necessarily - * increase ours as well if it increases. - */ -#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX - -/* * A lot of the calls to the cache allocation functions are expected to be * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are * conditional to this static branch, we'll have to allow modules that does @@ -3528,10 +3511,12 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, } #ifdef CONFIG_MEMCG_KMEM +#define MEM_CGROUP_KMEM_ID_MIN -1 +#define MEM_CGROUP_ID_DIFF (MEM_CGROUP_ID_MIN - MEM_CGROUP_KMEM_ID_MIN) + static int memcg_online_kmem(struct mem_cgroup *memcg) { struct obj_cgroup *objcg; - int memcg_id; if (cgroup_memory_nokmem) return 0; @@ -3539,22 +3524,16 @@ static int memcg_online_kmem(struct mem_cgroup *memcg) if (unlikely(mem_cgroup_is_root(memcg))) return 0; - memcg_id = ida_alloc_max(&memcg_cache_ida, MEMCG_CACHES_MAX_SIZE - 1, - GFP_KERNEL); - if (memcg_id < 0) - return memcg_id; - objcg = obj_cgroup_alloc(); - if (!objcg) { - ida_free(&memcg_cache_ida, memcg_id); + if (!objcg) return -ENOMEM; - } + objcg->memcg = memcg; rcu_assign_pointer(memcg->objcg, objcg); static_branch_enable(&memcg_kmem_enabled_key); - memcg->kmemcg_id = memcg_id; + memcg->kmemcg_id = memcg->id.id - MEM_CGROUP_ID_DIFF; return 0; } @@ -3562,7 +3541,6 @@ static int memcg_online_kmem(struct mem_cgroup *memcg) static void memcg_offline_kmem(struct mem_cgroup *memcg) { struct mem_cgroup *parent; - int kmemcg_id; if (cgroup_memory_nokmem) return; @@ -3577,20 +3555,12 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) memcg_reparent_objcgs(memcg, parent); /* - * memcg_reparent_list_lrus() can change memcg->kmemcg_id. - * Cache it to local @kmemcg_id. - */ - kmemcg_id = memcg->kmemcg_id; - - /* * After we have finished memcg_reparent_objcgs(), all list_lrus * corresponding to this cgroup are guaranteed to remain empty. * The ordering is imposed by list_lru_node->lock taken by * memcg_reparent_list_lrus(). */ memcg_reparent_list_lrus(memcg, parent); - - ida_free(&memcg_cache_ida, kmemcg_id); } #else static int memcg_online_kmem(struct mem_cgroup *memcg) @@ -5043,7 +5013,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void) return ERR_PTR(error); memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL, - 1, MEM_CGROUP_ID_MAX, + MEM_CGROUP_ID_MIN, MEM_CGROUP_ID_MAX, GFP_KERNEL); if (memcg->id.id < 0) { error = memcg->id.id; @@ -5071,7 +5041,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void) spin_lock_init(&memcg->event_list_lock); memcg->socket_pressure = jiffies; #ifdef CONFIG_MEMCG_KMEM - memcg->kmemcg_id = -1; + memcg->kmemcg_id = MEM_CGROUP_KMEM_ID_MIN; INIT_LIST_HEAD(&memcg->objcg_list); #endif #ifdef CONFIG_CGROUP_WRITEBACK