From: "Koralahalli Channabasappa, Smita" <Smita.KoralahalliChannabasappa@amd.com>
To: "Zhijian Li (Fujitsu)" <lizhijian@fujitsu.com>,
"linux-cxl@vger.kernel.org" <linux-cxl@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"nvdimm@lists.linux.dev" <nvdimm@lists.linux.dev>,
"linux-fsdevel@vger.kernel.org" <linux-fsdevel@vger.kernel.org>,
"linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>,
Jonathan Cameron <jonathan.cameron@huawei.com>,
Dave Jiang <dave.jiang@intel.com>,
Alison Schofield <alison.schofield@intel.com>,
Vishal Verma <vishal.l.verma@intel.com>,
Ira Weiny <ira.weiny@intel.com>,
Dan Williams <dan.j.williams@intel.com>,
Matthew Wilcox <willy@infradead.org>, Jan Kara <jack@suse.cz>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Len Brown <len.brown@intel.com>, Pavel Machek <pavel@kernel.org>,
Li Ming <ming.li@zohomail.com>,
Jeff Johnson <jeff.johnson@oss.qualcomm.com>,
Ying Huang <huang.ying.caritas@gmail.com>,
"Xingtao Yao (Fujitsu)" <yaoxt.fnst@fujitsu.com>,
Peter Zijlstra <peterz@infradead.org>,
Greg KH <gregkh@linuxfoundation.org>,
Nathan Fontenot <nathan.fontenot@amd.com>,
Terry Bowman <terry.bowman@amd.com>,
Robert Richter <rrichter@amd.com>,
Benjamin Cheatham <benjamin.cheatham@amd.com>,
PradeepVineshReddy Kodamati <PradeepVineshReddy.Kodamati@amd.com>
Subject: Re: [PATCH v4 5/7] cxl/region: Introduce SOFT RESERVED resource removal on region teardown
Date: Fri, 6 Jun 2025 10:53:22 -0700 [thread overview]
Message-ID: <8e077620-b1aa-4c51-9f66-58a9739aeb75@amd.com> (raw)
In-Reply-To: <3e045950-9ffb-4a9b-9793-958a16db0c33@fujitsu.com>
On 6/5/2025 9:16 PM, Zhijian Li (Fujitsu) wrote:
>
> The term "region teardown" in the subject line appears inaccurate.
>
>
> As I understand, cxl_region_softreserv_update() should work only
> when the region is ready, but the current logic only guarantees that the *memdev* is ready.
>
> Is there truly no timing gap between region readiness and memdev readiness?
> If this assumption is true, could we document this relationship in both the commit log and code comments?
>
>
> Thanks
> Zhijian
I think you're right in pointing this out.
We cannot guarantee region readiness unless there's an explicit ordering
or dependency ensuring that cxl_region_probe() always completes after or
synchronously with memdev probing and wait_for_device_probe().
I don't believe we currently have such a guarantee.
I was considering adding a null check for cxlr->params.res in
__cxl_region_softreserv_update(), along with a comment like below:
static int __cxl_region_softreserv_update(struct resource *soft,
void *_cxlr)
{
struct cxl_region *cxlr = _cxlr;
struct resource *res = cxlr->params.res;
/*
* Skip if the region has not yet been fully initialized.
*
* This code assumes all cxl_region devices have completed
* probing before this function runs, and that params.res is
* valid.
*/
if (!res)
return 0;
..
..
}
This would prevent a null dereference and make the assumption around
region readiness more explicit.
That said, I'd appreciate input from Terry, Nathan, or others on whether
there's a better way to guarantee that region creation has completed by
the time this function is invoked.
Thanks
Smita
>
>
> On 04/06/2025 06:19, Smita Koralahalli wrote:
>> Reworked from a patch by Alison Schofield <alison.schofield@intel.com>
>>
>> Previously, when CXL regions were created through autodiscovery and their
>> resources overlapped with SOFT RESERVED ranges, the soft reserved resource
>> remained in place after region teardown. This left the HPA range
>> unavailable for reuse even after the region was destroyed.
>>
>> Enhance the logic to reliably remove SOFT RESERVED resources associated
>> with a region, regardless of alignment or hierarchy in the iomem tree.
>>
>> Link: https://lore.kernel.org/linux-cxl/29312c0765224ae76862d59a17748c8188fb95f1.1692638817.git.alison.schofield@intel.com/
>> Co-developed-by: Alison Schofield <alison.schofield@intel.com>
>> Signed-off-by: Alison Schofield <alison.schofield@intel.com>
>> Co-developed-by: Terry Bowman <terry.bowman@amd.com>
>> Signed-off-by: Terry Bowman <terry.bowman@amd.com>
>> Signed-off-by: Smita Koralahalli <Smita.KoralahalliChannabasappa@amd.com>
>> ---
>> drivers/cxl/acpi.c | 2 +
>> drivers/cxl/core/region.c | 151 ++++++++++++++++++++++++++++++++++++++
>> drivers/cxl/cxl.h | 5 ++
>> 3 files changed, 158 insertions(+)
>>
>> diff --git a/drivers/cxl/acpi.c b/drivers/cxl/acpi.c
>> index 978f63b32b41..1b1388feb36d 100644
>> --- a/drivers/cxl/acpi.c
>> +++ b/drivers/cxl/acpi.c
>> @@ -823,6 +823,8 @@ static void cxl_softreserv_mem_work_fn(struct work_struct *work)
>> * and cxl_mem drivers are loaded.
>> */
>> wait_for_device_probe();
>> +
>> + cxl_region_softreserv_update();
>> }
>> static DECLARE_WORK(cxl_sr_work, cxl_softreserv_mem_work_fn);
>>
>> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
>> index 109b8a98c4c7..3a5ca44d65f3 100644
>> --- a/drivers/cxl/core/region.c
>> +++ b/drivers/cxl/core/region.c
>> @@ -3443,6 +3443,157 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
>> }
>> EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
>>
>> +static int add_soft_reserved(resource_size_t start, resource_size_t len,
>> + unsigned long flags)
>> +{
>> + struct resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
>> + int rc;
>> +
>> + if (!res)
>> + return -ENOMEM;
>> +
>> + *res = DEFINE_RES_MEM_NAMED(start, len, "Soft Reserved");
>> +
>> + res->desc = IORES_DESC_SOFT_RESERVED;
>> + res->flags = flags;
>> + rc = insert_resource(&iomem_resource, res);
>> + if (rc) {
>> + kfree(res);
>> + return rc;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static void remove_soft_reserved(struct cxl_region *cxlr, struct resource *soft,
>> + resource_size_t start, resource_size_t end)
>> +{
>> + struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
>> + resource_size_t new_start, new_end;
>> + int rc;
>> +
>> + /* Prevent new usage while removing or adjusting the resource */
>> + guard(mutex)(&cxlrd->range_lock);
>> +
>> + /* Aligns at both resource start and end */
>> + if (soft->start == start && soft->end == end)
>> + goto remove;
>> +
>> + /* Aligns at either resource start or end */
>> + if (soft->start == start || soft->end == end) {
>> + if (soft->start == start) {
>> + new_start = end + 1;
>> + new_end = soft->end;
>> + } else {
>> + new_start = soft->start;
>> + new_end = start - 1;
>> + }
>> +
>> + rc = add_soft_reserved(new_start, new_end - new_start + 1,
>> + soft->flags);
>> + if (rc)
>> + dev_warn(&cxlr->dev, "cannot add new soft reserved resource at %pa\n",
>> + &new_start);
>> +
>> + /* Remove the original Soft Reserved resource */
>> + goto remove;
>> + }
>> +
>> + /*
>> + * No alignment. Attempt a 3-way split that removes the part of
>> + * the resource the region occupied, and then creates new soft
>> + * reserved resources for the leading and trailing addr space.
>> + */
>> + new_start = soft->start;
>> + new_end = soft->end;
>> +
>> + rc = add_soft_reserved(new_start, start - new_start, soft->flags);
>> + if (rc)
>> + dev_warn(&cxlr->dev, "cannot add new soft reserved resource at %pa\n",
>> + &new_start);
>> +
>> + rc = add_soft_reserved(end + 1, new_end - end, soft->flags);
>> + if (rc)
>> + dev_warn(&cxlr->dev, "cannot add new soft reserved resource at %pa + 1\n",
>> + &end);
>> +
>> +remove:
>> + rc = remove_resource(soft);
>> + if (rc)
>> + dev_warn(&cxlr->dev, "cannot remove soft reserved resource %pr\n",
>> + soft);
>> +}
>> +
>> +/*
>> + * normalize_resource
>> + *
>> + * The walk_iomem_res_desc() returns a copy of a resource, not a reference
>> + * to the actual resource in the iomem_resource tree. As a result,
>> + * __release_resource() which relies on pointer equality will fail.
>> + *
>> + * This helper walks the children of the resource's parent to find and
>> + * return the original resource pointer that matches the given resource's
>> + * start and end addresses.
>> + *
>> + * Return: Pointer to the matching original resource in iomem_resource, or
>> + * NULL if not found or invalid input.
>> + */
>> +static struct resource *normalize_resource(struct resource *res)
>> +{
>> + if (!res || !res->parent)
>> + return NULL;
>> +
>> + for (struct resource *res_iter = res->parent->child;
>> + res_iter != NULL; res_iter = res_iter->sibling) {
>> + if ((res_iter->start == res->start) &&
>> + (res_iter->end == res->end))
>> + return res_iter;
>> + }
>> +
>> + return NULL;
>> +}
>> +
>> +static int __cxl_region_softreserv_update(struct resource *soft,
>> + void *_cxlr)
>> +{
>> + struct cxl_region *cxlr = _cxlr;
>> + struct resource *res = cxlr->params.res;
>> +
>> + /* Skip non-intersecting soft-reserved regions */
>> + if (soft->end < res->start || soft->start > res->end)
>> + return 0;
>> +
>> + soft = normalize_resource(soft);
>> + if (!soft)
>> + return -EINVAL;
>> +
>> + remove_soft_reserved(cxlr, soft, res->start, res->end);
>> +
>> + return 0;
>> +}
>> +
>> +int cxl_region_softreserv_update(void)
>> +{
>> + struct device *dev = NULL;
>> +
>> + while ((dev = bus_find_next_device(&cxl_bus_type, dev))) {
>> + struct device *put_dev __free(put_device) = dev;
>> + struct cxl_region *cxlr;
>> +
>> + if (!is_cxl_region(dev))
>> + continue;
>> +
>> + cxlr = to_cxl_region(dev);
>> +
>> + walk_iomem_res_desc(IORES_DESC_SOFT_RESERVED,
>> + IORESOURCE_MEM, 0, -1, cxlr,
>> + __cxl_region_softreserv_update);
>> + }
>> +
>> + return 0;
>> +}
>> +EXPORT_SYMBOL_NS_GPL(cxl_region_softreserv_update, "CXL");
>> +
>> u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa)
>> {
>> struct cxl_region_ref *iter;
>> diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
>> index 1ba7d39c2991..fc39c4b24745 100644
>> --- a/drivers/cxl/cxl.h
>> +++ b/drivers/cxl/cxl.h
>> @@ -859,6 +859,7 @@ struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev);
>> int cxl_add_to_region(struct cxl_port *root,
>> struct cxl_endpoint_decoder *cxled);
>> struct cxl_dax_region *to_cxl_dax_region(struct device *dev);
>> +int cxl_region_softreserv_update(void);
>> u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa);
>> #else
>> static inline bool is_cxl_pmem_region(struct device *dev)
>> @@ -878,6 +879,10 @@ static inline struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
>> {
>> return NULL;
>> }
>> +static inline int cxl_region_softreserv_update(void)
>> +{
>> + return 0;
>> +}
>> static inline u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint,
>> u64 spa)
>> {
next prev parent reply other threads:[~2025-06-06 17:53 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-03 22:19 [PATCH v4 0/7] Add managed SOFT RESERVE resource handling Smita Koralahalli
2025-06-03 22:19 ` [PATCH v4 1/7] cxl/region: Avoid null pointer dereference in is_cxl_region() Smita Koralahalli
2025-06-03 23:49 ` Dave Jiang
2025-06-04 19:56 ` Nathan Fontenot
2025-06-25 15:23 ` Robert Richter
2025-06-03 22:19 ` [PATCH v4 2/7] cxl/core: Remove CONFIG_CXL_SUSPEND and always build suspend.o Smita Koralahalli
2025-06-09 11:02 ` Jonathan Cameron
2025-06-09 23:25 ` Koralahalli Channabasappa, Smita
2025-06-10 9:38 ` Jonathan Cameron
2025-06-03 22:19 ` [PATCH v4 3/7] cxl/pci: Add pci_loaded tracking to mark PCI driver readiness Smita Koralahalli
2025-06-04 9:29 ` Zhijian Li (Fujitsu)
2025-06-03 22:19 ` [PATCH v4 4/7] cxl/acpi: Add background worker to wait for cxl_pci and cxl_mem probe Smita Koralahalli
2025-06-03 23:45 ` Dave Jiang
2025-06-04 18:31 ` Koralahalli Channabasappa, Smita
2025-06-04 9:40 ` Zhijian Li (Fujitsu)
2025-06-04 14:35 ` Dave Jiang
2025-06-05 2:18 ` Zhijian Li (Fujitsu)
2025-06-03 22:19 ` [PATCH v4 5/7] cxl/region: Introduce SOFT RESERVED resource removal on region teardown Smita Koralahalli
2025-06-06 4:16 ` Zhijian Li (Fujitsu)
2025-06-06 17:53 ` Koralahalli Channabasappa, Smita [this message]
2025-06-09 12:54 ` Jonathan Cameron
2025-06-10 1:25 ` Koralahalli Channabasappa, Smita
2025-06-10 9:37 ` Jonathan Cameron
2025-06-25 15:20 ` Robert Richter
2025-07-09 1:14 ` Alison Schofield
2025-06-03 22:19 ` [PATCH v4 6/7] dax/hmem: Save the DAX HMEM platform device pointer Smita Koralahalli
2025-06-05 16:54 ` Dave Jiang
2025-06-06 8:11 ` Zhijian Li (Fujitsu)
2025-06-06 20:09 ` Nathan Fontenot
2025-06-03 22:19 ` [PATCH v4 7/7] cxl/dax: Defer DAX consumption of SOFT RESERVED resources until after CXL region creation Smita Koralahalli
2025-06-09 13:01 ` Jonathan Cameron
2025-06-13 2:12 ` Zhijian Li (Fujitsu)
2025-07-10 4:22 ` Koralahalli Channabasappa, Smita
2025-07-10 8:18 ` Zhijian Li (Fujitsu)
2025-07-15 16:27 ` Alison Schofield
2025-07-15 18:19 ` Koralahalli Channabasappa, Smita
2025-07-15 18:45 ` Alison Schofield
2025-06-04 8:43 ` [PATCH v4 0/7] Add managed SOFT RESERVE resource handling Zhijian Li (Fujitsu)
2025-06-04 18:59 ` Koralahalli Channabasappa, Smita
2025-06-16 1:00 ` Zhijian Li (Fujitsu)
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=8e077620-b1aa-4c51-9f66-58a9739aeb75@amd.com \
--to=smita.koralahallichannabasappa@amd.com \
--cc=PradeepVineshReddy.Kodamati@amd.com \
--cc=alison.schofield@intel.com \
--cc=benjamin.cheatham@amd.com \
--cc=dan.j.williams@intel.com \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=gregkh@linuxfoundation.org \
--cc=huang.ying.caritas@gmail.com \
--cc=ira.weiny@intel.com \
--cc=jack@suse.cz \
--cc=jeff.johnson@oss.qualcomm.com \
--cc=jonathan.cameron@huawei.com \
--cc=len.brown@intel.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=lizhijian@fujitsu.com \
--cc=ming.li@zohomail.com \
--cc=nathan.fontenot@amd.com \
--cc=nvdimm@lists.linux.dev \
--cc=pavel@kernel.org \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=rrichter@amd.com \
--cc=terry.bowman@amd.com \
--cc=vishal.l.verma@intel.com \
--cc=willy@infradead.org \
--cc=yaoxt.fnst@fujitsu.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
all inboxes | Powered by JetHome®