From patchwork Mon Jun 3 12:05:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiu Jianfeng X-Patchwork-Id: 13683728 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 CD620C25B76 for ; Mon, 3 Jun 2024 12:12:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 770816B0096; Mon, 3 Jun 2024 08:12:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F7116B009A; Mon, 3 Jun 2024 08:12:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 431F56B0098; Mon, 3 Jun 2024 08:12:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 21C8F6B0095 for ; Mon, 3 Jun 2024 08:12:52 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C42F5A2523 for ; Mon, 3 Jun 2024 12:12:51 +0000 (UTC) X-FDA: 82189466142.02.2EDC4B9 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf15.hostedemail.com (Postfix) with ESMTP id 4A9CFA0003 for ; Mon, 3 Jun 2024 12:12:48 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of xiujianfeng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=xiujianfeng@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717416770; 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=CaVTNr4vMZ0wmkOABc7zKPlT7Xkb2fm4ZEmPQrQor48=; b=z9pd277FouivJjPsQgg39lMn1xcalEeTZb8GRi5ITT+xeO2i5kM/Jt7N2e5QxEIA7Xo2/J c/7pNZpwqQWBA7pERwg6FFUV2HAd1wQv+oJZlvdI4I+FHDfGBanjv7tBqPURW3Ea35J5Gk TSwFOMPWZXG6s277wUQg//mw5VeKB2s= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of xiujianfeng@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=xiujianfeng@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717416770; a=rsa-sha256; cv=none; b=n9Q9AHOV0oRESLY/AIEj9EC9JsZofra53P5qOdFyK/2i+W0YMzawEG9hpecWAdfm1rsS5/ 1Yr0wqnGCPGdsDuxhmyHznx5b0eFmEihEinQM6mtpffHs2GF/AviRVk5MJtEVzm95PnyAy lF2oNCvvvikiWOen0W2W4Tc0e1FAbGo= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4VtCF03SF1z1S92S; Mon, 3 Jun 2024 20:08:52 +0800 (CST) Received: from dggpeml500023.china.huawei.com (unknown [7.185.36.114]) by mail.maildlp.com (Postfix) with ESMTPS id 65CC0180060; Mon, 3 Jun 2024 20:12:46 +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; Mon, 3 Jun 2024 20:12:46 +0800 From: Xiu Jianfeng To: , CC: , Subject: [PATCH -next 2/3] mm/hugetlb_cgroup: prepare cftypes based on template Date: Mon, 3 Jun 2024 12:05:05 +0000 Message-ID: <20240603120506.1837322-3-xiujianfeng@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240603120506.1837322-1-xiujianfeng@huawei.com> References: <20240603120506.1837322-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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4A9CFA0003 X-Stat-Signature: 688f4whh7gxdy1m5nbs7y4kw4wy97483 X-Rspam-User: X-HE-Tag: 1717416768-530302 X-HE-Meta: U2FsdGVkX1+tHpG9qpusXFrV867mfqvP1qf99GfkMuio7jf2UozCoS6KZvsk94mdVbyFQT7lL5JPmCWHIsZ0P6J0SJxaiYN8h2YMbctA4omL3vNhcCDQKEtv/Z8MT1pSlZrcWDqS5mt6lCFBvjU4LIEejc6QJNuAW8ti8qKdmnxyQ7bjOZSrjnOL/4vxy/UfZbdYCjQkzml2fS19p/QT8aZwJRptBrgNmQ2rPy5auXU/+EY5I3D3AVWD+JEP9b0ohylrTK90HsjV2/a80TkxYzkdqRRPSipfsgQVD72dou+ZH1egR5mIyk1JoM2wnXYs0nueYZhkP4FiU46In/XslLS7Au1/twPSp9Qy5AXfp6/46JcmksAlE/9NSicf/srlrEVEi4/h+/iDDyiCm+WpeFsnvmx9hEVf7CHfq7iPkyxXINbo5kHQd3uUIkhwg9EKRtJl/zHAyH8YFK2l2DLRARt9Q5KuVqCOryX/iOXleRiqowpVNX+F2O0sqj/f841AiXxeAyNl5ehDyuZS3T6M2bHYUYZiDiMgksuRPiU4Tph8RsblaxNCPFiLY5JvqQz9dEL3N6g8hvkbc5DveGt/SbzMexuRi9nCPBSTZ2naFQhpOyFaZaPsILSfdDQwX96QrIpOI2YIFRTkX5jfoqJT9bH1npnHyjmta6GyxtFIREVzmYTe8iOr+1z52LeELf6ooXtf/mci3wG6H4XLDfOh26mnVAqqKA57vIVvks0nk48uhNlzwIBAaLihPCNKXUaJEcZLgoj2txihPpRmA239mFhWCtwlZYxSuEhiXxPkfmWd4MfvRwwGf96Op3aHt6lM66J5rT98UxiKG9DxIzb6DXH1oOs5/Jrvm8uHKoxK7WDIK6+A33IiwN7lgNZskFWByU5lv5RSprwQn4XoZCkhhCVJCpaSSQ15RntfUll3sDoBaWw7RiCBNTY2N6ygPC7HuTaDr824tFKx8/lsYU0 uW/P9ZxQ zoRcJiDrKZ9K75WyWjbclQE4vQqKBujFqjptv06cfVLt/GNTs20rGO2F1tI9EyQKEvs357Rvj+KNlG/8xgitxSYnVlI5LliXN1lnQkY5S2JXMZTBJq64tHNLEkcu/kHMmYbYgRuqytFUv3mzVnBB28G2WW8Es9l2VJJ2AH66q0fzQ8FCZpCdbMYQOdLG1OhvrbH7n 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 | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c index 45f94a869776..378f2353443f 100644 --- a/mm/hugetlb_cgroup.c +++ b/mm/hugetlb_cgroup.c @@ -27,7 +27,16 @@ #define MEMFILE_IDX(val) (((val) >> 16) & 0xffff) #define MEMFILE_ATTR(val) ((val) & 0xffff) +#define MEMFILE_OFFSET(t, m) (((offsetof(t, m) << 16) | sizeof_field(t, m))) +#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 +711,144 @@ 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 */ + if (tmpl->name) + snprintf(cft->name, MAX_CFTYPE_NAME, "%s.%s", buf, tmpl->name); + /* rebuild the private */ + if (tmpl->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 +920,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 +1000,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)); }