From patchwork Wed Jun 12 09:24:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiu Jianfeng X-Patchwork-Id: 13694698 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 5396EC27C53 for ; Wed, 12 Jun 2024 09:32:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA2D16B009D; Wed, 12 Jun 2024 05:32:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9715C6B00A2; Wed, 12 Jun 2024 05:32:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B7106B00A0; Wed, 12 Jun 2024 05:32:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5D8336B009D for ; Wed, 12 Jun 2024 05:32:02 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0D667815E9 for ; Wed, 12 Jun 2024 09:32:02 +0000 (UTC) X-FDA: 82221720084.15.E3A120A Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf28.hostedemail.com (Postfix) with ESMTP id 6B581C001F for ; Wed, 12 Jun 2024 09:31:58 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of xiujianfeng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=xiujianfeng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718184720; a=rsa-sha256; cv=none; b=2E0nfz9OgOxOV3B5Ibitx3ldEyoNKpDgIknM8iPa8PYvVbgs+MXC9W0/2Eyhz0zi8NXA4z yX/qwv/dP0dU8xqA3YcsbwKmAoYzcbYXincoBjpfhVsmMP0qmXs9xjg98rwMyqtAyaRSee gNLCbs2q9qkUon4jUEZCfmYPavV6U6o= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of xiujianfeng@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=xiujianfeng@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718184720; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yLDVjqKUdhYXt13w2CHRbblonLP3q/2GvJYLU9Xtj74=; b=cMTRcxk+9hNKvOVsxnR3hgsoftYLpa06S+UKQ5PT+c/fVhZ1o3tJf7aa1zzflKxz0KFsgE p6vXEfB784up4AuqfzMNMYLGJJpQnbZry4Q5bvUBSV6inpxUlQqqHzmbuJU+xNQO49jcg5 KqskVInYgHiCIzI5UVLkH6XQiSq9jV4= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4VzgHX67B7z1HDNP; Wed, 12 Jun 2024 17:30:00 +0800 (CST) Received: from dggpeml500023.china.huawei.com (unknown [7.185.36.114]) by mail.maildlp.com (Postfix) with ESMTPS id D16DD1400D3; Wed, 12 Jun 2024 17:31:55 +0800 (CST) Received: from hulk-vt.huawei.com (10.67.174.26) by dggpeml500023.china.huawei.com (7.185.36.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Wed, 12 Jun 2024 17:31:55 +0800 From: Xiu Jianfeng To: , CC: , , Subject: [PATCH v3 -next 2/3] mm/hugetlb_cgroup: prepare cftypes based on template Date: Wed, 12 Jun 2024 09:24:08 +0000 Message-ID: <20240612092409.2027592-3-xiujianfeng@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240612092409.2027592-1-xiujianfeng@huawei.com> References: <20240612092409.2027592-1-xiujianfeng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.26] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500023.china.huawei.com (7.185.36.114) X-Stat-Signature: 9ihsasoz6o8oi5irp84gto85h3fz7ezb X-Rspamd-Queue-Id: 6B581C001F X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1718184718-800030 X-HE-Meta: U2FsdGVkX19vFRpC5oaKhN6MWvOuy8dHqcp6FwYD3HuTXcggsTOM3pXTtAKOB/Wh8GOR2qHIeRG517i4/c1d/Eq2M4zUEA51a8wqP9HlUzB5tnAJW1aMZtdsBWUDSih4yUQe5MKMq9lXrQ6xETYs0UD5bvDomcO//hAKlOMJ1GZnoqbR/Ye/E8+m3bT3fXBdy+Z/GKNFXyeyVUPOhMZaHAcJ3Gd4CxJAsei4ojWHVAIuHiqPXkSPt/C64c+K5FxsEj8is5gWxjDxIZR6VNdXa1i0z2OH9QbGd8degPATD2B2BDg2rlhYtw4Aov5do4YaS/Wv4o9BU95wDk7BqqpO1Lx9pJ9KUrotLy3a0MXJT0yT9aBefLMu06CSLGjvY03ww386JksIGR51MrhkQ9ke/8a76ukJtG1hcvszULjjaf/Pbnfkg79g3odPPO/yR9ufr2PbfqQYsPI/JUv6pj9zGy0w9BB+Okcwx26c6mWb0jemL74N4CLoo5XYCHducTA5ZODUWm/Ujn3hFrZvYHrTVZNMd0NDVBHPmriNBZfugsXRY9w+rJMAW4GbO0joD0plCN/KjEUnX/RlBTNjOXYHCk40YPPiz6LJKqxNheRkGKmINBcp3XMQINYaCE/E/RW4bBv9q2gXk77elv1Cd5wGEd0X1yEH9aUWsvyiiflR3ofQxChjNN1jqq2BSevAFhD7b+dR4KZ6IuLIy47RpSK+Pt/aZsKE9R7K/ZBV/OC4KtuJyECKEEXlvUlP5uKcTqr2OJOFhhejN526Ea5mf5KQjQV491Cx/bs1lynlKV1mQQb3f75gti2Pxkytx87nGm2mZ9tEews8q+LzuGDdHFEs7/SEhlMgPp83oebgBqO/ig146Yt09FHvU7I9y4oN4Cnd5F4acpYFAzwM7HbcBgNFXUWdKbBCLLKc+q5bgXcny8tCSVoXrUrtgaMMr1I7NTjZ8LvyOov7akKH1wlfduX sWJJ55vA EOGNdkbpYn5oytC6KciaFKL/q4qEkZKrRO5ktfOdyws5yr9Z0icMFSdox8spjXKI8e93BTY0EqBz9QRvOGHLBLXa3x45mSrUwC371dYnxv120MDIl9dSJFjY7eCtsCFgKfcdjlB/te2HSjCIDxNOB//cTakDXPiLQ8QNW03/55AcSI4JtFF1/GHuwBtdcubmjhdcr 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: List-Subscribe: List-Unsubscribe: Unlike other cgroup subsystems, the hugetlb cgroup does not provide a static array of cftype that explicitly displays the properties, handling functions, etc., of each file. Instead, it dynamically creates the attribute of cftypes based on the hstate during the startup procedure. This reduces the readability of the code. To fix this issue, introduce two templates of cftypes, and rebuild the attributes according to the hstate to make it ready to be added to cgroup framework. Signed-off-by: Xiu Jianfeng --- mm/hugetlb_cgroup.c | 156 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c index 45f94a869776..43aae8f88d5f 100644 --- a/mm/hugetlb_cgroup.c +++ b/mm/hugetlb_cgroup.c @@ -27,7 +27,17 @@ #define MEMFILE_IDX(val) (((val) >> 16) & 0xffff) #define MEMFILE_ATTR(val) ((val) & 0xffff) +/* Use t->m[0] to encode the offset */ +#define MEMFILE_OFFSET(t, m0) (((offsetof(t, m0) << 16) | sizeof_field(t, m0))) +#define MEMFILE_OFFSET0(val) (((val) >> 16) & 0xffff) +#define MEMFILE_FIELD_SIZE(val) ((val) & 0xffff) + +#define DFL_TMPL_SIZE ARRAY_SIZE(hugetlb_dfl_tmpl) +#define LEGACY_TMPL_SIZE ARRAY_SIZE(hugetlb_legacy_tmpl) + static struct hugetlb_cgroup *root_h_cgroup __read_mostly; +static struct cftype *dfl_files; +static struct cftype *legacy_files; static inline struct page_counter * __hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx, @@ -702,12 +712,142 @@ static int hugetlb_events_local_show(struct seq_file *seq, void *v) return __hugetlb_events_show(seq, true); } +static struct cftype hugetlb_dfl_tmpl[] = { + { + .name = "max", + .private = RES_LIMIT, + .seq_show = hugetlb_cgroup_read_u64_max, + .write = hugetlb_cgroup_write_dfl, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "rsvd.max", + .private = RES_RSVD_LIMIT, + .seq_show = hugetlb_cgroup_read_u64_max, + .write = hugetlb_cgroup_write_dfl, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "current", + .private = RES_USAGE, + .seq_show = hugetlb_cgroup_read_u64_max, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "rsvd.current", + .private = RES_RSVD_USAGE, + .seq_show = hugetlb_cgroup_read_u64_max, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "events", + .seq_show = hugetlb_events_show, + .file_offset = MEMFILE_OFFSET(struct hugetlb_cgroup, events_file[0]), + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "events.local", + .seq_show = hugetlb_events_local_show, + .file_offset = MEMFILE_OFFSET(struct hugetlb_cgroup, events_local_file[0]), + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "numa_stat", + .seq_show = hugetlb_cgroup_read_numa_stat, + .flags = CFTYPE_NOT_ON_ROOT, + }, + /* don't need terminator here */ +}; + +static struct cftype hugetlb_legacy_tmpl[] = { + { + .name = "limit_in_bytes", + .private = RES_LIMIT, + .read_u64 = hugetlb_cgroup_read_u64, + .write = hugetlb_cgroup_write_legacy, + }, + { + .name = "rsvd.limit_in_bytes", + .private = RES_RSVD_LIMIT, + .read_u64 = hugetlb_cgroup_read_u64, + .write = hugetlb_cgroup_write_legacy, + }, + { + .name = "usage_in_bytes", + .private = RES_USAGE, + .read_u64 = hugetlb_cgroup_read_u64, + }, + { + .name = "rsvd.usage_in_bytes", + .private = RES_RSVD_USAGE, + .read_u64 = hugetlb_cgroup_read_u64, + }, + { + .name = "max_usage_in_bytes", + .private = RES_MAX_USAGE, + .write = hugetlb_cgroup_reset, + .read_u64 = hugetlb_cgroup_read_u64, + }, + { + .name = "rsvd.max_usage_in_bytes", + .private = RES_RSVD_MAX_USAGE, + .write = hugetlb_cgroup_reset, + .read_u64 = hugetlb_cgroup_read_u64, + }, + { + .name = "failcnt", + .private = RES_FAILCNT, + .write = hugetlb_cgroup_reset, + .read_u64 = hugetlb_cgroup_read_u64, + }, + { + .name = "rsvd.failcnt", + .private = RES_RSVD_FAILCNT, + .write = hugetlb_cgroup_reset, + .read_u64 = hugetlb_cgroup_read_u64, + }, + { + .name = "numa_stat", + .seq_show = hugetlb_cgroup_read_numa_stat, + }, + /* don't need terminator here */ +}; + +static void __init +hugetlb_cgroup_cfttypes_init(struct hstate *h, struct cftype *cft, + struct cftype *tmpl, int tmpl_size) +{ + char buf[32]; + int i, idx = hstate_index(h); + + /* format the size */ + mem_fmt(buf, sizeof(buf), huge_page_size(h)); + + for (i = 0; i < tmpl_size; cft++, tmpl++, i++) { + *cft = *tmpl; + /* rebuild the name */ + snprintf(cft->name, MAX_CFTYPE_NAME, "%s.%s", buf, tmpl->name); + /* rebuild the private */ + cft->private = MEMFILE_PRIVATE(idx, tmpl->private); + /* rebuild the file_offset */ + if (tmpl->file_offset) { + unsigned int offset = tmpl->file_offset; + + cft->file_offset = MEMFILE_OFFSET0(offset) + + MEMFILE_FIELD_SIZE(offset) * idx; + } + } +} + static void __init __hugetlb_cgroup_file_dfl_init(int idx) { char buf[32]; struct cftype *cft; struct hstate *h = &hstates[idx]; + hugetlb_cgroup_cfttypes_init(h, dfl_files + idx * DFL_TMPL_SIZE, + hugetlb_dfl_tmpl, DFL_TMPL_SIZE); + /* format the size */ mem_fmt(buf, sizeof(buf), huge_page_size(h)); @@ -779,6 +919,9 @@ static void __init __hugetlb_cgroup_file_legacy_init(int idx) struct cftype *cft; struct hstate *h = &hstates[idx]; + hugetlb_cgroup_cfttypes_init(h, legacy_files + idx * LEGACY_TMPL_SIZE, + hugetlb_legacy_tmpl, LEGACY_TMPL_SIZE); + /* format the size */ mem_fmt(buf, sizeof(buf), huge_page_size(h)); @@ -856,10 +999,23 @@ static void __init __hugetlb_cgroup_file_init(int idx) __hugetlb_cgroup_file_legacy_init(idx); } +static void __init __hugetlb_cgroup_file_pre_init(void) +{ + int cft_count; + + cft_count = hugetlb_max_hstate * DFL_TMPL_SIZE + 1; /* add terminator */ + dfl_files = kcalloc(cft_count, sizeof(struct cftype), GFP_KERNEL); + BUG_ON(!dfl_files); + cft_count = hugetlb_max_hstate * LEGACY_TMPL_SIZE + 1; /* add terminator */ + legacy_files = kcalloc(cft_count, sizeof(struct cftype), GFP_KERNEL); + BUG_ON(!legacy_files); +} + void __init hugetlb_cgroup_file_init(void) { struct hstate *h; + __hugetlb_cgroup_file_pre_init(); for_each_hstate(h) __hugetlb_cgroup_file_init(hstate_index(h)); }