[RFC PATCH 1/7] sysfs interface for the boundary of movable zone

From: kaiyang2
Date: Tue Mar 19 2024 - 22:42:46 EST


From: Kaiyang Zhao <kaiyang2@xxxxxxxxxx>

Exports the pfn and memory block id for boundary

Signed-off-by: Kaiyang Zhao <zh_kaiyang@xxxxxxxxxxx>
---
drivers/base/memory.c | 2 +-
drivers/base/node.c | 32 ++++++++++++++++++++++++++++++++
include/linux/memory.h | 1 +
3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index b456ac213610..281b229d7019 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -55,7 +55,7 @@ static inline unsigned long memory_block_id(unsigned long section_nr)
return section_nr / sections_per_block;
}

-static inline unsigned long pfn_to_block_id(unsigned long pfn)
+unsigned long pfn_to_block_id(unsigned long pfn)
{
return memory_block_id(pfn_to_section_nr(pfn));
}
diff --git a/drivers/base/node.c b/drivers/base/node.c
index b46db17124f3..f29ce07565ba 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -486,6 +486,37 @@ static ssize_t node_read_meminfo(struct device *dev,
#undef K
static DEVICE_ATTR(meminfo, 0444, node_read_meminfo, NULL);

+static ssize_t node_read_movable_zone(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ int len = 0;
+ struct zone *unmovable_zone;
+ unsigned long movable_start_pfn, unmovable_end_pfn;
+ unsigned long movable_start_block_id, unmovable_end_block_id;
+
+ movable_start_pfn = NODE_DATA(dev->id)->node_zones[ZONE_MOVABLE].zone_start_pfn;
+ movable_start_block_id = pfn_to_block_id(movable_start_pfn);
+
+ if (populated_zone(&(NODE_DATA(dev->id)->node_zones[ZONE_NORMAL])))
+ unmovable_zone = &(NODE_DATA(dev->id)->node_zones[ZONE_NORMAL]);
+ else
+ unmovable_zone = &(NODE_DATA(dev->id)->node_zones[ZONE_DMA32]);
+
+ unmovable_end_pfn = zone_end_pfn(unmovable_zone);
+ unmovable_end_block_id = pfn_to_block_id(unmovable_end_pfn);
+
+ len = sysfs_emit_at(buf, len,
+ "movable_zone_start_pfn %lu\n"
+ "movable_zone_start_block_id %lu\n"
+ "unmovable_zone_end_pfn %lu\n"
+ "unmovable_zone_end_block_id %lu\n",
+ movable_start_pfn, movable_start_block_id,
+ unmovable_end_pfn, unmovable_end_block_id);
+
+ return len;
+}
+static DEVICE_ATTR(movable_zone, 0444, node_read_movable_zone, NULL);
+
static ssize_t node_read_numastat(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -565,6 +596,7 @@ static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);

static struct attribute *node_dev_attrs[] = {
&dev_attr_meminfo.attr,
+ &dev_attr_movable_zone.attr,
&dev_attr_numastat.attr,
&dev_attr_distance.attr,
&dev_attr_vmstat.attr,
diff --git a/include/linux/memory.h b/include/linux/memory.h
index 31343566c221..17a92a5c1ae5 100644
--- a/include/linux/memory.h
+++ b/include/linux/memory.h
@@ -92,6 +92,7 @@ struct memory_block {
int arch_get_memory_phys_device(unsigned long start_pfn);
unsigned long memory_block_size_bytes(void);
int set_memory_block_size_order(unsigned int order);
+unsigned long pfn_to_block_id(unsigned long pfn);

/* These states are exposed to userspace as text strings in sysfs */
#define MEM_ONLINE (1<<0) /* exposed to userspace */
--
2.40.1