All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
To: linux-mm@kvack.org, akpm@linux-foundation.org
Cc: Wei Xu <weixugc@google.com>, Huang Ying <ying.huang@intel.com>,
	Greg Thelen <gthelen@google.com>, Yang Shi <shy828301@gmail.com>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Tim C Chen <tim.c.chen@intel.com>,
	Brice Goglin <brice.goglin@gmail.com>,
	Michal Hocko <mhocko@kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Hesham Almatary <hesham.almatary@huawei.com>,
	Dave Hansen <dave.hansen@intel.com>,
	Jonathan Cameron <Jonathan.Cameron@huawei.com>,
	Alistair Popple <apopple@nvidia.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Feng Tang <feng.tang@intel.com>,
	Jagdish Gediya <jvgediya@linux.ibm.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	David Rientjes <rientjes@google.com>,
	"Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
Subject: [PATCH v6 08/13] mm/demotion: Add support for memory tier creation from userspace
Date: Fri, 10 Jun 2022 19:22:24 +0530	[thread overview]
Message-ID: <20220610135229.182859-9-aneesh.kumar@linux.ibm.com> (raw)
In-Reply-To: <20220610135229.182859-1-aneesh.kumar@linux.ibm.com>

This patch adds support to create memory tiers with specific rank
value from userspace. To avoid race while creating memory tiers
/sys/devices/system/memtier/create_tier_from_rank file is provided.
Writing to this file with a specific rank value creates a new memory
tier with the specified rank value.

Memory tiers created from userspace gets destroyed when the memory
tier nodelist becomes empty.

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
---
 include/linux/memory-tiers.h |  3 +-
 mm/memory-tiers.c            | 74 ++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h
index e70f0040d845..52896f5970b7 100644
--- a/include/linux/memory-tiers.h
+++ b/include/linux/memory-tiers.h
@@ -15,7 +15,8 @@
 #define MEMORY_RANK_PMEM	100
 
 #define DEFAULT_MEMORY_TIER	MEMORY_TIER_DRAM
-#define MAX_MEMORY_TIERS  3
+#define MAX_STATIC_MEMORY_TIERS  3
+#define MAX_MEMORY_TIERS  (MAX_STATIC_MEMORY_TIERS + 2)
 
 extern bool numa_demotion_enabled;
 int node_create_and_set_memory_tier(int node, int tier);
diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c
index 7bfdfac4d43e..de3b7403ae6f 100644
--- a/mm/memory-tiers.c
+++ b/mm/memory-tiers.c
@@ -6,6 +6,7 @@
 #include <linux/memory-tiers.h>
 #include <linux/random.h>
 #include <linux/memory.h>
+#include <linux/idr.h>
 
 #include "internal.h"
 
@@ -126,9 +127,12 @@ static const struct attribute_group *memory_tier_dev_groups[] = {
 	NULL
 };
 
+static DEFINE_IDA(memtier_dev_id);
 static void memory_tier_device_release(struct device *dev)
 {
 	struct memory_tier *tier = to_memory_tier(dev);
+	if (tier->dev.id >= MAX_STATIC_MEMORY_TIERS)
+		ida_free(&memtier_dev_id, tier->dev.id);
 
 	kfree(tier);
 }
@@ -195,6 +199,17 @@ static struct memory_tier *register_memory_tier(unsigned int tier,
 	return memtier;
 }
 
+static void unregister_memory_tier(struct memory_tier *memtier)
+{
+	/*
+	 * Don't destroy static memory tiers.
+	 */
+	if (memtier->dev.id < MAX_STATIC_MEMORY_TIERS)
+		return;
+	list_del(&memtier->list);
+	device_unregister(&memtier->dev);
+}
+
 static struct memory_tier *__node_get_memory_tier(int node)
 {
 	struct memory_tier *memtier;
@@ -267,6 +282,10 @@ int node_create_and_set_memory_tier(int node, int tier)
 		node_set(node, current_tier->nodelist);
 		goto out;
 	}
+
+	if (nodes_empty(current_tier->nodelist))
+		unregister_memory_tier(current_tier);
+
 	establish_migration_targets();
 out:
 	mutex_unlock(&memory_tier_lock);
@@ -350,6 +369,9 @@ int node_reset_memory_tier(int node, int tier)
 		goto out;
 	}
 
+	if (nodes_empty(current_tier->nodelist))
+		unregister_memory_tier(current_tier);
+
 	establish_migration_targets();
 out:
 	mutex_unlock(&memory_tier_lock);
@@ -550,9 +572,61 @@ default_tier_show(struct device *dev, struct device_attribute *attr, char *buf)
 }
 static DEVICE_ATTR_RO(default_tier);
 
+static inline int memtier_alloc_id(void)
+{
+	return ida_alloc_range(&memtier_dev_id,
+			       MAX_STATIC_MEMORY_TIERS,
+			       MAX_MEMORY_TIERS - 1, GFP_KERNEL);
+}
+
+static ssize_t create_tier_from_rank_store(struct device *dev,
+					   struct device_attribute *attr,
+					   const char *buf, size_t count)
+{
+	int ret, tier, rank;
+	struct memory_tier *memtier;
+
+	ret = kstrtouint(buf, 10, &rank);
+	if (ret)
+		return ret;
+
+	if (rank == MEMORY_RANK_HBM_GPU ||
+	    rank == MEMORY_RANK_DRAM ||
+	    rank == MEMORY_RANK_PMEM)
+		return -EINVAL;
+
+	mutex_lock(&memory_tier_lock);
+	/*
+	 * We don't support multiple tiers with same rank value
+	 */
+	list_for_each_entry(memtier, &memory_tiers, list) {
+		if (memtier->rank == rank) {
+			ret = -EINVAL;
+			goto out;
+		}
+	}
+	tier = memtier_alloc_id();
+	if (tier < 0) {
+		ret = -ENOSPC;
+		goto out;
+	}
+	memtier = register_memory_tier(tier, rank);
+	if (IS_ERR(memtier)) {
+		ret = PTR_ERR(memtier);
+		goto out;
+	}
+
+	ret = count;
+out:
+	mutex_unlock(&memory_tier_lock);
+	return ret;
+}
+static DEVICE_ATTR_WO(create_tier_from_rank);
+
 static struct attribute *memory_tier_attrs[] = {
 	&dev_attr_max_tier.attr,
 	&dev_attr_default_tier.attr,
+	&dev_attr_create_tier_from_rank.attr,
 	NULL
 };
 
-- 
2.36.1



  parent reply	other threads:[~2022-06-10 13:54 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-10 13:52 [PATCH v6 00/13] mm/demotion: Memory tiers and demotion Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 01/13] mm/demotion: Add support for explicit memory tiers Aneesh Kumar K.V
2022-06-13  3:22   ` Ying Huang
2022-06-13  3:31     ` Aneesh Kumar K V
2022-06-13  5:30       ` Ying Huang
2022-06-13 13:16         ` Johannes Weiner
2022-06-13 13:28           ` Aneesh Kumar K V
2022-06-14  8:20         ` Aneesh Kumar K.V
2022-06-14 15:13           ` Davidlohr Bueso
2022-06-10 13:52 ` [PATCH v6 02/13] mm/demotion: Move memory demotion related code Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 03/13] mm/demotion: Return error on write to numa_demotion sysfs Aneesh Kumar K.V
2022-06-13  3:26   ` Ying Huang
2022-06-13  3:35     ` Aneesh Kumar K V
2022-06-13  5:33       ` Ying Huang
2022-06-13  5:48         ` Aneesh Kumar K V
2022-06-14  8:40           ` Ying Huang
2022-06-10 13:52 ` [PATCH v6 04/13] mm/demotion/dax/kmem: Set node's memory tier to MEMORY_TIER_PMEM Aneesh Kumar K.V
2022-06-13  6:59   ` Ying Huang
2022-06-13  7:05     ` Aneesh Kumar K V
2022-06-10 13:52 ` [PATCH v6 05/13] mm/demotion: Build demotion targets based on explicit memory tiers Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 06/13] mm/demotion: Expose memory tier details via sysfs Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 07/13] mm/demotion: Add per node memory tier attribute to sysfs Aneesh Kumar K.V
2022-06-10 13:52 ` Aneesh Kumar K.V [this message]
2022-06-10 13:52 ` [PATCH v6 09/13] mm/demotion: Add pg_data_t member to track node memory tier details Aneesh Kumar K.V
2022-06-13  7:07   ` Ying Huang
2022-06-10 13:52 ` [PATCH v6 10/13] mm/demotion: Demote pages according to allocation fallback order Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 11/13] mm/demotion: Update node_is_toptier to work with memory tiers Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 12/13] mm/demotion: Add documentation for memory tiering Aneesh Kumar K.V
2022-06-10 13:52 ` [PATCH v6 13/13] mm/demotion: Add sysfs ABI documentation Aneesh Kumar K.V

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220610135229.182859-9-aneesh.kumar@linux.ibm.com \
    --to=aneesh.kumar@linux.ibm.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=brice.goglin@gmail.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=dave@stgolabs.net \
    --cc=feng.tang@intel.com \
    --cc=gthelen@google.com \
    --cc=hesham.almatary@huawei.com \
    --cc=jvgediya@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=rientjes@google.com \
    --cc=shy828301@gmail.com \
    --cc=tim.c.chen@intel.com \
    --cc=weixugc@google.com \
    --cc=ying.huang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.