Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp446381pxb; Tue, 14 Sep 2021 00:47:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyroLuK5lkAVSMzO3rMFX+/uw7T77qwPU2XeYG/psxrVPSr8KdV7rUI/Xi1EhBMa4zs2jax X-Received: by 2002:a17:906:a08d:: with SMTP id q13mr17245098ejy.465.1631605627418; Tue, 14 Sep 2021 00:47:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631605627; cv=none; d=google.com; s=arc-20160816; b=ZWKcKNudWHHfXNQwVwdhLuTQBaoyiawOLoa7ZNR2jobKwZbEI9rj2LeYUBCP2xOlan VLzZEi8+XGT63sggk5FXM1i1RLMIat38aLVrxMfgsTYsm2AqjS/vI61gSc+oq9LsyfX3 qdVA7Ec0fPE6BycEN2yacr4MmEQuw/bJDSKDGeM2AXDmqffyrl+yPtfisWtsMfMy2NME xwqnR8gF5IBNyEKe9/19P2EpxpBiOu+uyUl2UjYD7fx/7s1OOTf6PW4FW2KYYuhjeikj Q2jlDeUzRw/gDgHRKUVadc5qEQQBREgf/dt52glmTz748CQAuTKeM1XfNSmKFcsf+PRn IWyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GTuclICje8xvheuCWw6eY5mtrJC1enFIQ8G/X086C4w=; b=n/OLf1C/Qzg+0mdbDnvifZ2pUqWqxPH+8LEXv4HiWCmGKEuXd13vSxvKiW9kPlQWrU 3Hov+Ggq6RTumaX5pCGw82qHoRdDomMD/tsoGjsE3sCofabfjv8few43O9jEbmOCS3Lc cfM5aQCY66sSw+NXpcLwsReSqLEmgx0uWrqHq//V5s4c0d4awP/NY1auUgTNwp701OQU ZFHOgj71bVecZspp0EGEhO6fBknz1PGybPnyAagQkZmjqA+cC/OJXnXfSOInpams5Ri4 erp9IRIXuamYmhVHC6LFQZcpzZ99RQR6fbxpwecdEeGXNwIxvAicFby4MdKwY4mwLjym PAFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=1+5SZrth; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q2si11495393ejx.184.2021.09.14.00.46.44; Tue, 14 Sep 2021 00:47:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=1+5SZrth; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237574AbhINHrw (ORCPT + 99 others); Tue, 14 Sep 2021 03:47:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233570AbhINHq6 (ORCPT ); Tue, 14 Sep 2021 03:46:58 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F93FC0611BF for ; Tue, 14 Sep 2021 00:41:41 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id n30so8659230pfq.5 for ; Tue, 14 Sep 2021 00:41:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GTuclICje8xvheuCWw6eY5mtrJC1enFIQ8G/X086C4w=; b=1+5SZrthq4E9CNTL9shy+XTzy4GfS0MhkZdeG8bD6fclynhHhj6SeOoRgLf3pFR4my BGdV0PSiF09qkvytKWXvDnXU/KOBFqL/mqyOd1sL+vo2Gj5NZHMeujnW4p/ySo1tTlxW 5MEgUiDUCrH+j8TmwGGLYiTNfhLcLs0t0Id5/t6xyCn302Mn4qKJClIS5r943Vjxj4R3 3ESMJEOYtM7rTAMfYaypQTmjrXvgsC2iUteRT+ora65DDVkEHvRsGjpmwEPEOxw8Zrdi fZ9RQ48GuD6pdi3pqwQ3fw6la2Ty26HoG1CaMfVQn3mCzfqq1EJZGL+pXOhOIh4oqFaO 8qtg== 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=GTuclICje8xvheuCWw6eY5mtrJC1enFIQ8G/X086C4w=; b=Fk9oyZtZ6KIFvZApmDruX8PeSLpZL2cIqzR0/QhVlpnBYPPxgRwuyypaBOMNjjH0b+ 5CQA5QORIpyDo/sly0dphuLgBnPyxJmWHXuukKu4zmJbeW84BjRzhJLEbPqYhWcS01uj SqnG0/jcij5KLQTlOCAhSUMzJ/QW8ReMEnsG2q5KEx0E1cZHSg0rSJzWYG5VyIOB5qdo jmWZUouo3ozGXFTwsAf4x/G0c+dC8nsQ6uLmxXdTii8E9m28V/msxnA+V5HPORd39DSI Pe5ZrLIyjWzUP+yqoDEW2wHyvLRH7rc5gROMLtDi8+CGqYtNacYp9xEJuhjDs5PdTObL VV7g== X-Gm-Message-State: AOAM532mY4h9yQP7hxbJv5RgalfX8Ma0YY9uUmgIB/N/4XY5mT2kkx4V 0525cJPuzKYDD/lmiuxQ2RIMHw== X-Received: by 2002:a63:1f24:: with SMTP id f36mr14690074pgf.6.1631605300457; Tue, 14 Sep 2021 00:41:40 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id s3sm9377839pfd.188.2021.09.14.00.41.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Sep 2021 00:41:39 -0700 (PDT) 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 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 v3 73/76] mm: memcontrol: reuse memory cgroup ID for kmem ID Date: Tue, 14 Sep 2021 15:29:35 +0800 Message-Id: <20210914072938.6440-74-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210914072938.6440-1-songmuchun@bytedance.com> References: <20210914072938.6440-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org 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 | 47 ++++++++-------------------------------------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 83add6c484b1..33f6ec4783f8 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 8e0cde19b648..e3a2e4d65cc5 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -356,23 +356,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 @@ -3520,10 +3503,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; @@ -3531,22 +3516,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; } @@ -3554,7 +3533,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; @@ -3567,16 +3545,7 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) parent = root_mem_cgroup; memcg_reparent_objcgs(memcg, parent); - - /* - * memcg_reparent_list_lrus() can change memcg->kmemcg_id. - * Cache it to @kmemcg_id. - */ - kmemcg_id = memcg->kmemcg_id; - memcg_reparent_list_lrus(memcg, parent); - - ida_free(&memcg_cache_ida, kmemcg_id); } #else static int memcg_online_kmem(struct mem_cgroup *memcg) @@ -5042,7 +5011,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; @@ -5070,7 +5039,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 -- 2.11.0