Linux CXL
 help / color / mirror / Atom feed
From: "Cheatham, Benjamin" <benjamin.cheatham@amd.com>
To: Gregory Price <gourry@gourry.net>, <linux-cxl@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <kernel-team@meta.com>,
	<dave@stgolabs.net>, <jonathan.cameron@huawei.com>,
	<dave.jiang@intel.com>, <alison.schofield@intel.com>,
	<vishal.l.verma@intel.com>, <ira.weiny@intel.com>,
	<dan.j.williams@intel.com>, David Hildenbrand <david@kernel.org>,
	Hannes Reinecke <hare@suse.de>
Subject: Re: [PATCH 6/6] cxl/sysram: disallow onlining in ZONE_NORMAL if state is movable only
Date: Mon, 12 Jan 2026 15:11:05 -0600	[thread overview]
Message-ID: <9c975c63-2668-4283-a326-292e50bfbfec@amd.com> (raw)
In-Reply-To: <20260112163514.2551809-7-gourry@gourry.net>

On 1/12/2026 10:35 AM, Gregory Price wrote:
> If state is set to online (default to ZONE_MOVABLE), the user intends
> for this memory to either refuse non-movable allocations, and/or intends
> to preserve the hot-unpluggability of this memory.  However, any admin
> can write `offline` and `online` to the memory block controller and
> bring that memory online in ZONE_NORMAL.

Is it the expectation that the user will never want to change the zone from
MOVABLE to NORMAL? I can't think of a reason someone would want to off the top
of my head, but I also can't think of a reason to restrict it either.

> 
> Register a memory_notify callback that disallows onlining the block into
> ZONE_NORMAL if the default state of the controller is ZONE_MOVABLE.
> 
> If an actor attempts to online the block into ZONE_NORMAL, it will fail,
> but if it attempts to online into either NORMAL or MOVABLE, only MOVABLE
> will be allowed and it will succeed.

I'm not sure you need this paragraph. I think it's a logical conclusion of the above
that if someone attempts to online the memory as NORMAL or MOVABLE it'll only be onlined
as MOVABLE.
> 
> Suggested-by: David Hildenbrand <david@kernel.org>
> Suggested-by: Hannes Reinecke <hare@suse.de>
> Link: https://lore.kernel.org/linux-mm/39533aa8-ca78-41a8-b005-9202ce53e3ae@kernel.org/
> Signed-off-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/memctrl/sysram_region.c | 138 +++++++++++++++++++++--
>  1 file changed, 127 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/cxl/core/memctrl/sysram_region.c b/drivers/cxl/core/memctrl/sysram_region.c
> index 2e2d9b59a725..71e39d725dc5 100644
> --- a/drivers/cxl/core/memctrl/sysram_region.c
> +++ b/drivers/cxl/core/memctrl/sysram_region.c
> @@ -2,6 +2,7 @@
>  /* Copyright(c) 2026 Meta Inc. All rights reserved. */
>  #include <linux/memremap.h>
>  #include <linux/memory.h>
> +#include <linux/mmzone.h>
>  #include <linux/module.h>
>  #include <linux/device.h>
>  #include <linux/slab.h>
> @@ -23,6 +24,14 @@ struct cxl_sysram_data {
>  	const char *res_name;
>  	int mgid;
>  	struct resource *res;
> +	struct range range;
> +	struct notifier_block memory_notifier;
> +	/*
> +	 * Last online type requested by user via state sysfs or auto-online.
> +	 * Used to enforce zone consistency when memory blocks are onlined.
> +	 * MMOP_OFFLINE means no online preference has been set yet.
> +	 */
> +	int last_online_type;
>  };
>  
>  static DEFINE_MUTEX(cxl_memory_type_lock);
> @@ -158,7 +167,58 @@ static int cxl_sysram_offline_memory(struct range *range)
>  	return rc;
>  }
>  
> -static int cxl_sysram_auto_online(struct device *dev, struct range *range)
> +/*
> + * Memory notifier callback to enforce zone consistency.
> + *
> + * When the user (or auto-online) requests memory to be onlined into
> + * ZONE_MOVABLE, reject any subsequent attempts to online memory blocks
> + * from this region into a different zone (e.g., ZONE_NORMAL). This prevents
> + * accidental zone mixing which could lead to memory fragmentation and
> + * offlining failures.
> + */
> +static int cxl_sysram_memory_notify_cb(struct notifier_block *nb,
> +				       unsigned long action, void *arg)
> +{
> +	struct cxl_sysram_data *data = container_of(nb, struct cxl_sysram_data,
> +						    memory_notifier);
> +	struct memory_notify *mhp = arg;
> +	unsigned long start_phys = PFN_PHYS(mhp->start_pfn);
> +	unsigned long size = PFN_PHYS(mhp->nr_pages);
> +	struct page *page;
> +
> +	if (action != MEM_GOING_ONLINE)
> +		return NOTIFY_DONE;
> +
> +	/* Check if this memory block overlaps with our region */
> +	if (start_phys + size <= data->range.start ||
> +	    start_phys > data->range.end)
> +		return NOTIFY_DONE;
> +
> +	/*
> +	 * If no online preference has been set (MMOP_OFFLINE), allow any zone.
> +	 * Also allow if the preference wasn't for ZONE_MOVABLE.
> +	 */
> +	if (data->last_online_type != MMOP_ONLINE_MOVABLE)
> +		return NOTIFY_DONE;
> +
> +	/*
> +	 * The zone has already been assigned to the pages at this point
> +	 * via move_pfn_range_to_zone() before MEM_GOING_ONLINE is sent.
> +	 * Check if it's ZONE_MOVABLE as expected.
> +	 */
> +	page = pfn_to_page(mhp->start_pfn);
> +
> +	if (!is_zone_movable_page(page)) {
> +		pr_warn("CXL sysram: rejecting online to non-movable zone for range %#lx-%#lx (expected ZONE_MOVABLE)\n",
> +			start_phys, start_phys + size - 1);
> +		return NOTIFY_BAD;
> +	}
> +
> +	return NOTIFY_OK;
> +}
> +
> +static int cxl_sysram_auto_online(struct device *dev, struct range *range,
> +				  struct cxl_sysram_data *data)
>  {
>  	int online_type;
>  	int rc;
> @@ -173,6 +233,9 @@ static int cxl_sysram_auto_online(struct device *dev, struct range *range)
>  	else
>  		online_type = MMOP_ONLINE_MOVABLE;
>  
> +	/* Record the auto-online type for zone enforcement */
> +	data->last_online_type = online_type;
> +
>  	rc = lock_device_hotplug_sysfs();
>  	if (rc)
>  		return rc;
> @@ -187,17 +250,43 @@ static int cxl_sysram_auto_online(struct device *dev, struct range *range)
>  	return rc;
>  }
>  
> +static ssize_t state_show(struct device *dev,
> +			  struct device_attribute *attr, char *buf)
> +{
> +	struct cxl_sysram_data *data;
> +
> +	data = dev_get_drvdata(dev);
> +	if (!data)
> +		return -ENODEV;
> +
> +	switch (data->last_online_type) {
> +	case MMOP_ONLINE_MOVABLE:
> +		return sysfs_emit(buf, "online\n");
> +	case MMOP_ONLINE_KERNEL:
> +		return sysfs_emit(buf, "online_normal\n");
> +	case MMOP_OFFLINE:
> +	default:

You're missing the MMOP_ONLINE case. In that case the memory would be reported as "offline", which
I doubt is the intention.

Thanks,
Ben
> +		return sysfs_emit(buf, "offline\n");
> +	}
> +}
> +
>  static ssize_t state_store(struct device *dev,
>  			   struct device_attribute *attr,
>  			   const char *buf, size_t len)
>  {
>  	struct cxl_region *cxlr = to_cxl_region(dev);
> +	struct cxl_sysram_data *data;
>  	struct range range;
> +	int online_type = MMOP_OFFLINE;
>  	int rc;
>  
>  	if (!cxlr)
>  		return -ENODEV;
>  
> +	data = dev_get_drvdata(dev);
> +	if (!data)
> +		return -ENODEV;
> +
>  	rc = cxl_sysram_range(cxlr, &range);
>  	if (rc)
>  		return rc;
> @@ -206,23 +295,30 @@ static ssize_t state_store(struct device *dev,
>  	if (rc)
>  		return rc;
>  
> -	if (sysfs_streq(buf, "online"))
> -		rc = cxl_sysram_online_memory(&range, MMOP_ONLINE_MOVABLE);
> -	else if (sysfs_streq(buf, "online_normal"))
> -		rc = cxl_sysram_online_memory(&range, MMOP_ONLINE);
> -	else if (sysfs_streq(buf, "offline"))
> +	if (sysfs_streq(buf, "online")) {
> +		online_type = MMOP_ONLINE_MOVABLE;
> +		rc = cxl_sysram_online_memory(&range, online_type);
> +	} else if (sysfs_streq(buf, "online_normal")) {
> +		online_type = MMOP_ONLINE;
> +		rc = cxl_sysram_online_memory(&range, online_type);
> +	} else if (sysfs_streq(buf, "offline")) {
>  		rc = cxl_sysram_offline_memory(&range);
> -	else
> +	} else {
>  		rc = -EINVAL;
> +	}
>  
>  	unlock_device_hotplug();
>  
>  	if (rc)
>  		return rc;
>  
> +	/* Record the online type for zone enforcement on success */
> +	if (online_type != MMOP_OFFLINE)
> +		data->last_online_type = online_type;
> +
>  	return len;
>  }
> -static DEVICE_ATTR_WO(state);
> +static DEVICE_ATTR_RW(state);
>  
>  static ssize_t hotplug_store(struct device *dev,
>  			     struct device_attribute *attr,
> @@ -274,6 +370,11 @@ static void cxl_sysram_unregister(void *_data)
>  		.end = data->res->end
>  	};
>  
> +	unregister_memory_notifier(&data->memory_notifier);
> +
> +	range.start = data->res->start;
> +	range.end = data->res->end;
> +
>  	/* We have one shot for removal, otherwise it's stuck til reboot */
>  	if (!offline_and_remove_memory(range.start, range_len(&range))) {
>  		remove_resource(data->res);
> @@ -334,6 +435,10 @@ int devm_cxl_add_sysram_region(struct cxl_region *cxlr)
>  		goto err_data;
>  	}
>  
> +	/* Initialize range and online type tracking */
> +	data->range = range;
> +	data->last_online_type = MMOP_OFFLINE;
> +
>  	data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
>  	if (!data->res_name) {
>  		rc = -ENOMEM;
> @@ -373,11 +478,20 @@ int devm_cxl_add_sysram_region(struct cxl_region *cxlr)
>  	dev_dbg(dev, "%s: added %llu bytes as System RAM\n", dev_name(dev),
>  		(unsigned long long)total_len);
>  
> -	rc = cxl_sysram_auto_online(dev, &range);
> +	/* Set drvdata early so auto_online can access it */
> +	dev_set_drvdata(dev, data);
> +
> +	/* Register memory notifier for zone enforcement */
> +	data->memory_notifier.notifier_call = cxl_sysram_memory_notify_cb;
> +	data->memory_notifier.priority = CXL_CALLBACK_PRI;
> +	rc = register_memory_notifier(&data->memory_notifier);
> +	if (rc)
> +		goto err_notifier;
> +
> +	rc = cxl_sysram_auto_online(dev, &range, data);
>  	if (rc)
>  		goto err_auto_online;
>  
> -	dev_set_drvdata(dev, data);
>  	rc = devm_device_add_group(dev, &cxl_sysram_region_group);
>  	if (rc)
>  		goto err_add_group;
> @@ -385,9 +499,11 @@ int devm_cxl_add_sysram_region(struct cxl_region *cxlr)
>  	return devm_add_action_or_reset(dev, cxl_sysram_unregister, data);
>  
>  err_add_group:
> -	dev_set_drvdata(dev, NULL);
>  err_auto_online:
>  	/* if this fails, memory cannot be removed from the system until reboot */
> +	unregister_memory_notifier(&data->memory_notifier);
> +err_notifier:
> +	dev_set_drvdata(dev, NULL);
>  	remove_memory(range.start, range_len(&range));
>  err_add_memory:
>  	remove_resource(res);


  reply	other threads:[~2026-01-12 21:11 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20260113093758epcas5p10cc9749a657b8e4d32db75b8b973b67d@epcas5p1.samsung.com>
2026-01-12 16:35 ` [PATCH 0/6] CXL: Introduce memory controller abstraction and sysram controller Gregory Price
2026-01-12 16:35   ` [PATCH 1/6] drivers/cxl: add cxl_memctrl_mode and region->memctrl Gregory Price
2026-01-12 20:59     ` dan.j.williams
2026-01-12 22:25       ` Gregory Price
2026-01-13 18:00       ` Dave Jiang
2026-01-13 20:07         ` Gregory Price
2026-01-14 16:36         ` dan.j.williams
2026-01-12 21:10     ` Cheatham, Benjamin
2026-01-12 22:34       ` Gregory Price
2026-01-14 17:18     ` Jonathan Cameron
2026-01-14 18:25       ` Gregory Price
2026-01-14 18:36         ` Jonathan Cameron
2026-01-12 16:35   ` [PATCH 2/6] cxl: add sysram_region memory controller Gregory Price
2026-01-12 20:00     ` David Hildenbrand (Red Hat)
2026-01-12 22:43       ` Gregory Price
2026-01-12 21:10     ` dan.j.williams
2026-01-12 22:47       ` Gregory Price
2026-01-12 21:10     ` Cheatham, Benjamin
2026-01-12 22:55       ` Gregory Price
2026-01-13 22:34         ` Cheatham, Benjamin
2026-01-12 16:35   ` [PATCH 3/6] cxl/core/region: move pmem memctrl logic into memctrl/pmem_region Gregory Price
2026-01-12 21:10     ` Cheatham, Benjamin
2026-01-12 22:58       ` Gregory Price
2026-01-13  9:12         ` Neeraj Kumar
2026-01-12 16:35   ` [PATCH 4/6] cxl: add CONFIG_CXL_REGION_CTRL_AUTO_* build config options Gregory Price
2026-01-12 21:10     ` Cheatham, Benjamin
2026-01-12 23:05       ` Gregory Price
2026-01-13  4:31         ` dan.j.williams
2026-01-13 13:55           ` Gregory Price
2026-01-12 16:35   ` [PATCH 5/6] cxl: add CXL_REGION_SYSRAM_DEFAULT_* build options Gregory Price
2026-01-12 21:11     ` Cheatham, Benjamin
2026-01-12 23:07       ` Gregory Price
2026-01-12 16:35   ` [PATCH 6/6] cxl/sysram: disallow onlining in ZONE_NORMAL if state is movable only Gregory Price
2026-01-12 21:11     ` Cheatham, Benjamin [this message]
2026-01-12 23:14       ` Gregory Price
2026-01-13 22:35         ` Cheatham, Benjamin
2026-01-13  9:37   ` [PATCH 0/6] CXL: Introduce memory controller abstraction and sysram controller Neeraj Kumar
2026-01-13 13:33     ` Gregory Price
2026-01-15 18:43   ` Alejandro Lucero Palau
2026-01-15 18:56     ` Gregory Price

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=9c975c63-2668-4283-a326-292e50bfbfec@amd.com \
    --to=benjamin.cheatham@amd.com \
    --cc=alison.schofield@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=david@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hare@suse.de \
    --cc=ira.weiny@intel.com \
    --cc=jonathan.cameron@huawei.com \
    --cc=kernel-team@meta.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=vishal.l.verma@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox