From patchwork Tue Aug 30 08:17:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 12958913 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 53DBEECAAD4 for ; Tue, 30 Aug 2022 08:18:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE9A994000A; Tue, 30 Aug 2022 04:18:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D7E88940007; Tue, 30 Aug 2022 04:18:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC5FE94000A; Tue, 30 Aug 2022 04:18:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A9797940007 for ; Tue, 30 Aug 2022 04:18:11 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7DAB11A0CFE for ; Tue, 30 Aug 2022 08:18:11 +0000 (UTC) X-FDA: 79855556382.10.ED5FB59 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by imf08.hostedemail.com (Postfix) with ESMTP id 16AB7160026 for ; Tue, 30 Aug 2022 08:18:09 +0000 (UTC) Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27U8E5Xx005811; Tue, 30 Aug 2022 08:17:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : mime-version : content-type : content-transfer-encoding; s=pp1; bh=7u5zUT4sbhunsdX1IerE3lK6WYlfWvuAJsPf7w9nKMQ=; b=N5yHOXVT5L4M58PxkIZNEAwC0L8mQ5JJXfC3CddIwHJpghI5E2Tp2FM34y0Zk0RT+gep 7aaC4+KDhZSThSoFm7IxiM0e9xwVn2LEGIFLDPbk4k5ghl5zZLGqfzsM5uznjp25ypQ6 /RHX7paAI/ZC/lpd7nudcCWe8Uz4rm7BaO27FcRBYGkURa4w27MKpLGp3JDI3ub+rCDK Ql5AiGKBVfMWNvS8lEAd78REZCMp4Ntawk1UPIyr0mecOR2qO6F0bcaERFNxw0lK0yMb 9+1OBZHzQfbxEzDeDNDJmMI/wIaMxhMsEfo8Cd2EZ8ZP7msRuVVH9OygBPxP1BO68x45 sw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3j9euu03mc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Aug 2022 08:17:47 +0000 Received: from m0098421.ppops.net (m0098421.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 27U8FjET011658; Tue, 30 Aug 2022 08:17:47 GMT Received: from ppma03wdc.us.ibm.com (ba.79.3fa9.ip4.static.sl-reverse.com [169.63.121.186]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3j9euu03kv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Aug 2022 08:17:46 +0000 Received: from pps.filterd (ppma03wdc.us.ibm.com [127.0.0.1]) by ppma03wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 27U85aFP007095; Tue, 30 Aug 2022 08:17:46 GMT Received: from b01cxnp22035.gho.pok.ibm.com (b01cxnp22035.gho.pok.ibm.com [9.57.198.25]) by ppma03wdc.us.ibm.com with ESMTP id 3j7aw9af3r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Aug 2022 08:17:46 +0000 Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp22035.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 27U8Hj9q60752342 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Aug 2022 08:17:45 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C1AECAC05B; Tue, 30 Aug 2022 08:17:45 +0000 (GMT) Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EE706AC059; Tue, 30 Aug 2022 08:17:39 +0000 (GMT) Received: from skywalker.ibmuc.com (unknown [9.43.77.247]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTP; Tue, 30 Aug 2022 08:17:39 +0000 (GMT) From: "Aneesh Kumar K.V" To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: Wei Xu , Huang Ying , Yang Shi , Davidlohr Bueso , Tim C Chen , Michal Hocko , Linux Kernel Mailing List , Hesham Almatary , Dave Hansen , Jonathan Cameron , Alistair Popple , Dan Williams , Johannes Weiner , jvgediya.oss@gmail.com, Bharata B Rao , "Aneesh Kumar K.V" Subject: [PATCH v3 updated] mm/demotion: Expose memory tier details via sysfs Date: Tue, 30 Aug 2022 13:47:36 +0530 Message-Id: <20220830081736.119281-1-aneesh.kumar@linux.ibm.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: C89u5Jk2lA5Mnp_Zc2FvrOmMqfgxWrtI X-Proofpoint-ORIG-GUID: SNnAyl334nShH01UtFGTrk74QoGg2Iwm X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-30_04,2022-08-25_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 priorityscore=1501 bulkscore=0 suspectscore=0 adultscore=0 lowpriorityscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 impostorscore=0 mlxscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208300036 ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=N5yHOXVT; spf=pass (imf08.hostedemail.com: domain of aneesh.kumar@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=aneesh.kumar@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661847490; 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: references:dkim-signature; bh=7u5zUT4sbhunsdX1IerE3lK6WYlfWvuAJsPf7w9nKMQ=; b=WTqNiMogPUs6jKXeS5TryVtzMUbPIdnGoFUmPhhv2afJpgz3IE6cRIk+aTI5QZ5SFDF0aP JwfjVBEXhpA+81gdrl8akx8tog+qDc3TIHLTVW/dpkmv/2h1DuiDDMBFqCpLNLiY0rrK6g JPLv+IdUey01RKh19jNnngkLB5da5vM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661847490; a=rsa-sha256; cv=none; b=70cFwBh9xJGLeqr67woCiIW24elBREE5CPPfS5ce8C7jcn4ZVPeJCoFv3kdHq3oZb0p+IE p9WOw4LBPn87QTmspU2jdzyGTEHZ5mEbi/IpvKIsRcQDc5zrTpvgOoIlDjOcKnx5WGKttC US0wBN5Wg6e3mj2m/hVyke8P9sY5t1c= X-Stat-Signature: 7fo6pnb9g9bem3macritiqd78tpir3ns X-Rspamd-Queue-Id: 16AB7160026 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=N5yHOXVT; spf=pass (imf08.hostedemail.com: domain of aneesh.kumar@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=aneesh.kumar@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1661847489-334752 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: This patch adds /sys/devices/virtual/memory_tiering/ where all memory tier related details can be found. All allocated memory tiers will be listed there as /sys/devices/virtual/memory_tiering/memory_tierN/ The nodes which are part of a specific memory tier can be listed via /sys/devices/virtual/memory_tiering/memory_tierN/nodes A directory hierarchy looks like :/sys/devices/virtual/memory_tiering$ tree memory_tier4/ memory_tier4/ ├── nodes ├── subsystem -> ../../../../bus/memory_tiering └── uevent All toptier nodes are listed via /sys/devices/virtual/memory_tiering/toptier_nodes :/sys/devices/virtual/memory_tiering$ cat toptier_nodes 0,2 :/sys/devices/virtual/memory_tiering$ cat memory_tier4/nodes 0,2 Signed-off-by: Aneesh Kumar K.V --- Changes from v2: * update macro to static inline * Fix build error with CONFIG_MIGRATION disabled * drop abstract_distance * update commit message .../ABI/testing/sysfs-kernel-mm-memory-tiers | 35 ++++ mm/memory-tiers.c | 154 +++++++++++++++--- 2 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-kernel-mm-memory-tiers diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-memory-tiers b/Documentation/ABI/testing/sysfs-kernel-mm-memory-tiers new file mode 100644 index 000000000000..55051fcf5502 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-kernel-mm-memory-tiers @@ -0,0 +1,35 @@ +What: /sys/devices/virtual/memory_tiering/ +Date: August 2022 +Contact: Linux memory management mailing list +Description: A collection of all the memory tiers allocated. + + Individual memory tier details are contained in subdirectories + named by the abstract distance of the memory tier. + + /sys/devices/virtual/memory_tiering/memory_tierN/ + + +What: /sys/devices/virtual/memory_tiering/memory_tierN/ + /sys/devices/virtual/memory_tiering/memory_tierN/nodes +Date: August 2022 +Contact: Linux memory management mailing list +Description: Directory with details of a specific memory tier + + This is the directory containing information about a particular + memory tier, memtierN, where N is derived based on abstract distance. + + A smaller value of N implies a higher (faster) memory tier in the + hierarchy. + + nodes: NUMA nodes that are part of this memory tier. + + +What: /sys/devices/virtual/memory_tiering/toptier_nodes +Date: August 2022 +Contact: Linux memory management mailing list +Description: Toptier node mask + + A toptier is defined as the memory tier from which memory promotion + is not done by the kernel. + + toptier_nodes: Union of NUMA nodes that are part of each toptier. diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index c82eb0111383..33673ed9b3dc 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -19,6 +19,7 @@ struct memory_tier { * adistance_start .. adistance_start + MEMTIER_CHUNK_SIZE */ int adistance_start; + struct device dev; /* All the nodes that are part of all the lower memory tiers. */ nodemask_t lower_tier_mask; }; @@ -36,6 +37,12 @@ static DEFINE_MUTEX(memory_tier_lock); static LIST_HEAD(memory_tiers); static struct node_memory_type_map node_memory_types[MAX_NUMNODES]; static struct memory_dev_type *default_dram_type; + +static struct bus_type memory_tier_subsys = { + .name = "memory_tiering", + .dev_name = "memory_tier", +}; + #ifdef CONFIG_MIGRATION static int top_tier_adistance; /* @@ -98,8 +105,63 @@ static int top_tier_adistance; static struct demotion_nodes *node_demotion __read_mostly; #endif /* CONFIG_MIGRATION */ +static inline struct memory_tier *to_memory_tier(struct device *device) +{ + return container_of(device, struct memory_tier, dev); +} + +static __always_inline nodemask_t get_memtier_nodemask(struct memory_tier *memtier) +{ + nodemask_t nodes = NODE_MASK_NONE; + struct memory_dev_type *memtype; + + list_for_each_entry(memtype, &memtier->memory_types, tier_sibiling) + nodes_or(nodes, nodes, memtype->nodes); + + return nodes; +} + +static void memory_tier_device_release(struct device *dev) +{ + struct memory_tier *tier = to_memory_tier(dev); + /* + * synchronize_rcu in clear_node_memory_tier makes sure + * we don't have rcu access to this memory tier. + */ + kfree(tier); +} + +static ssize_t nodes_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + int ret; + nodemask_t nmask; + + mutex_lock(&memory_tier_lock); + nmask = get_memtier_nodemask(to_memory_tier(dev)); + ret = sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&nmask)); + mutex_unlock(&memory_tier_lock); + return ret; +} +static DEVICE_ATTR_RO(nodes); + +static struct attribute *memtier_dev_attrs[] = { + &dev_attr_nodes.attr, + NULL +}; + +static const struct attribute_group memtier_dev_group = { + .attrs = memtier_dev_attrs, +}; + +static const struct attribute_group *memtier_dev_groups[] = { + &memtier_dev_group, + NULL +}; + static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memtype) { + int ret; bool found_slot = false; struct memory_tier *memtier, *new_memtier; int adistance = memtype->adistance; @@ -123,15 +185,14 @@ static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memty list_for_each_entry(memtier, &memory_tiers, list) { if (adistance == memtier->adistance_start) { - list_add(&memtype->tier_sibiling, &memtier->memory_types); - return memtier; + goto link_memtype; } else if (adistance < memtier->adistance_start) { found_slot = true; break; } } - new_memtier = kmalloc(sizeof(struct memory_tier), GFP_KERNEL); + new_memtier = kzalloc(sizeof(struct memory_tier), GFP_KERNEL); if (!new_memtier) return ERR_PTR(-ENOMEM); @@ -142,8 +203,23 @@ static struct memory_tier *find_create_memory_tier(struct memory_dev_type *memty list_add_tail(&new_memtier->list, &memtier->list); else list_add_tail(&new_memtier->list, &memory_tiers); - list_add(&memtype->tier_sibiling, &new_memtier->memory_types); - return new_memtier; + + new_memtier->dev.id = adistance >> MEMTIER_CHUNK_BITS; + new_memtier->dev.bus = &memory_tier_subsys; + new_memtier->dev.release = memory_tier_device_release; + new_memtier->dev.groups = memtier_dev_groups; + + ret = device_register(&new_memtier->dev); + if (ret) { + list_del(&memtier->list); + put_device(&memtier->dev); + return ERR_PTR(ret); + } + memtier = new_memtier; + +link_memtype: + list_add(&memtype->tier_sibiling, &memtier->memory_types); + return memtier; } static struct memory_tier *__node_get_memory_tier(int node) @@ -275,17 +351,6 @@ static void disable_all_demotion_targets(void) synchronize_rcu(); } -static __always_inline nodemask_t get_memtier_nodemask(struct memory_tier *memtier) -{ - nodemask_t nodes = NODE_MASK_NONE; - struct memory_dev_type *memtype; - - list_for_each_entry(memtype, &memtier->memory_types, tier_sibiling) - nodes_or(nodes, nodes, memtype->nodes); - - return nodes; -} - /* * Find an automatic demotion target for all memory * nodes. Failing here is OK. It might just indicate @@ -433,11 +498,7 @@ static struct memory_tier *set_node_memory_tier(int node) static void destroy_memory_tier(struct memory_tier *memtier) { list_del(&memtier->list); - /* - * synchronize_rcu in clear_node_memory_tier makes sure - * we don't have rcu access to this memory tier. - */ - kfree(memtier); + device_unregister(&memtier->dev); } static bool clear_node_memory_tier(int node) @@ -564,11 +625,60 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self, return notifier_from_errno(0); } +#ifdef CONFIG_MIGRATION +static ssize_t toptier_nodes_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + int ret; + nodemask_t nmask, top_tier_mask = NODE_MASK_NONE; + struct memory_tier *memtier = to_memory_tier(dev); + + mutex_lock(&memory_tier_lock); + list_for_each_entry(memtier, &memory_tiers, list) { + if (memtier->adistance_start > top_tier_adistance) + break; + nmask = get_memtier_nodemask(memtier); + nodes_or(top_tier_mask, top_tier_mask, nmask); + } + + ret = sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&top_tier_mask)); + mutex_unlock(&memory_tier_lock); + return ret; +} +#else +static ssize_t toptier_nodes_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + nodemask_t top_tier_mask = node_states[N_MEMORY]; + + return sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&top_tier_mask)); +} +#endif +static DEVICE_ATTR_RO(toptier_nodes); + +static struct attribute *memtier_subsys_attrs[] = { + &dev_attr_toptier_nodes.attr, + NULL +}; + +static const struct attribute_group memtier_subsys_group = { + .attrs = memtier_subsys_attrs, +}; + +static const struct attribute_group *memtier_subsys_groups[] = { + &memtier_subsys_group, + NULL +}; + static int __init memory_tier_init(void) { - int node; + int ret, node; struct memory_tier *memtier; + ret = subsys_virtual_register(&memory_tier_subsys, memtier_subsys_groups); + if (ret) + panic("%s() failed to register memory tier subsystem\n", __func__); + #ifdef CONFIG_MIGRATION node_demotion = kcalloc(nr_node_ids, sizeof(struct demotion_nodes), GFP_KERNEL);