[PATCH v3 3/3] cxl/core: Using resource_contains_addr() to check address availability

Li Ming posted 3 patches 3 months ago
There is a newer version of this series
[PATCH v3 3/3] cxl/core: Using resource_contains_addr() to check address availability
Posted by Li Ming 3 months ago
Helper function resource_contains_addr() can be used to check if a
resource range contains an input address. Use it to replace all
code that checks whether a resource range contains the input
DPA/HPA/SPA.

Signed-off-by: Li Ming <ming.li@zohomail.com>
---
 drivers/cxl/core/edac.c   | 4 ++--
 drivers/cxl/core/memdev.c | 2 +-
 drivers/cxl/core/region.c | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/cxl/core/edac.c b/drivers/cxl/core/edac.c
index 1cf65b1538b9..a3c19ec8033f 100644
--- a/drivers/cxl/core/edac.c
+++ b/drivers/cxl/core/edac.c
@@ -1523,7 +1523,7 @@ static int cxl_mem_sparing_set_dpa(struct device *dev, void *drv_data, u64 dpa)
 	struct cxl_memdev *cxlmd = ctx->cxlmd;
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
 
-	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end)
+	if (!resource_contains_addr(&cxlds->dpa_res, dpa))
 		return -EINVAL;
 
 	ctx->dpa = dpa;
@@ -1892,7 +1892,7 @@ static int cxl_ppr_set_dpa(struct device *dev, void *drv_data, u64 dpa)
 	struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd;
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
 
-	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end)
+	if (!resource_contains_addr(&cxlds->dpa_res, dpa))
 		return -EINVAL;
 
 	cxl_ppr_ctx->dpa = dpa;
diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index f88a13adf7fa..4a5dc1d93af1 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -267,7 +267,7 @@ static int cxl_validate_poison_dpa(struct cxl_memdev *cxlmd, u64 dpa)
 		dev_dbg(cxlds->dev, "device has no dpa resource\n");
 		return -EINVAL;
 	}
-	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end) {
+	if (!resource_contains_addr(&cxlds->dpa_res, dpa)) {
 		dev_dbg(cxlds->dev, "dpa:0x%llx not in resource:%pR\n",
 			dpa, &cxlds->dpa_res);
 		return -EINVAL;
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 6e5e1460068d..79d8211f8ed0 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -2847,7 +2847,7 @@ static int __cxl_dpa_to_region(struct device *dev, void *arg)
 	if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
 		return 0;
 
-	if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
+	if (!resource_contains_addr(cxled->dpa_res, dpa))
 		return 0;
 
 	/*
@@ -2959,7 +2959,7 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 	if (cxlrd->hpa_to_spa)
 		hpa = cxlrd->hpa_to_spa(cxlrd, hpa);
 
-	if (hpa < p->res->start || hpa > p->res->end) {
+	if (!resource_contains_addr(p->res, hpa)) {
 		dev_dbg(&cxlr->dev,
 			"Addr trans fail: hpa 0x%llx not in region\n", hpa);
 		return ULLONG_MAX;
@@ -3499,7 +3499,7 @@ u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa)
 	xa_for_each(&endpoint->regions, index, iter) {
 		struct cxl_region_params *p = &iter->region->params;
 
-		if (p->res->start <= spa && spa <= p->res->end) {
+		if (resource_contains_addr(p->res, spa)) {
 			if (!p->cache_size)
 				return ~0ULL;
 
-- 
2.34.1
Re: [PATCH v3 3/3] cxl/core: Using resource_contains_addr() to check address availability
Posted by Alison Schofield 3 months ago
On Tue, Jul 08, 2025 at 01:15:36PM +0800, Li Ming wrote:
> Helper function resource_contains_addr() can be used to check if a
> resource range contains an input address. Use it to replace all
> code that checks whether a resource range contains the input
> DPA/HPA/SPA.
> 

with the cxl_ prefix update, you can add:

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


> Signed-off-by: Li Ming <ming.li@zohomail.com>
> ---
>  drivers/cxl/core/edac.c   | 4 ++--
>  drivers/cxl/core/memdev.c | 2 +-
>  drivers/cxl/core/region.c | 6 +++---
>  3 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/cxl/core/edac.c b/drivers/cxl/core/edac.c
> index 1cf65b1538b9..a3c19ec8033f 100644
> --- a/drivers/cxl/core/edac.c
> +++ b/drivers/cxl/core/edac.c
> @@ -1523,7 +1523,7 @@ static int cxl_mem_sparing_set_dpa(struct device *dev, void *drv_data, u64 dpa)
>  	struct cxl_memdev *cxlmd = ctx->cxlmd;
>  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
>  
> -	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end)
> +	if (!resource_contains_addr(&cxlds->dpa_res, dpa))
>  		return -EINVAL;
>  
>  	ctx->dpa = dpa;
> @@ -1892,7 +1892,7 @@ static int cxl_ppr_set_dpa(struct device *dev, void *drv_data, u64 dpa)
>  	struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd;
>  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
>  
> -	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end)
> +	if (!resource_contains_addr(&cxlds->dpa_res, dpa))
>  		return -EINVAL;
>  
>  	cxl_ppr_ctx->dpa = dpa;
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index f88a13adf7fa..4a5dc1d93af1 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -267,7 +267,7 @@ static int cxl_validate_poison_dpa(struct cxl_memdev *cxlmd, u64 dpa)
>  		dev_dbg(cxlds->dev, "device has no dpa resource\n");
>  		return -EINVAL;
>  	}
> -	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end) {
> +	if (!resource_contains_addr(&cxlds->dpa_res, dpa)) {
>  		dev_dbg(cxlds->dev, "dpa:0x%llx not in resource:%pR\n",
>  			dpa, &cxlds->dpa_res);
>  		return -EINVAL;
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 6e5e1460068d..79d8211f8ed0 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -2847,7 +2847,7 @@ static int __cxl_dpa_to_region(struct device *dev, void *arg)
>  	if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
>  		return 0;
>  
> -	if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
> +	if (!resource_contains_addr(cxled->dpa_res, dpa))
>  		return 0;
>  
>  	/*
> @@ -2959,7 +2959,7 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
>  	if (cxlrd->hpa_to_spa)
>  		hpa = cxlrd->hpa_to_spa(cxlrd, hpa);
>  
> -	if (hpa < p->res->start || hpa > p->res->end) {
> +	if (!resource_contains_addr(p->res, hpa)) {
>  		dev_dbg(&cxlr->dev,
>  			"Addr trans fail: hpa 0x%llx not in region\n", hpa);
>  		return ULLONG_MAX;
> @@ -3499,7 +3499,7 @@ u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa)
>  	xa_for_each(&endpoint->regions, index, iter) {
>  		struct cxl_region_params *p = &iter->region->params;
>  
> -		if (p->res->start <= spa && spa <= p->res->end) {
> +		if (resource_contains_addr(p->res, spa)) {
>  			if (!p->cache_size)
>  				return ~0ULL;
>  
> -- 
> 2.34.1
>
Re: [PATCH v3 3/3] cxl/core: Using resource_contains_addr() to check address availability
Posted by Dave Jiang 3 months ago

On 7/7/25 10:15 PM, Li Ming wrote:
> Helper function resource_contains_addr() can be used to check if a
> resource range contains an input address. Use it to replace all
> code that checks whether a resource range contains the input
> DPA/HPA/SPA.
> 
> Signed-off-by: Li Ming <ming.li@zohomail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
>  drivers/cxl/core/edac.c   | 4 ++--
>  drivers/cxl/core/memdev.c | 2 +-
>  drivers/cxl/core/region.c | 6 +++---
>  3 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/cxl/core/edac.c b/drivers/cxl/core/edac.c
> index 1cf65b1538b9..a3c19ec8033f 100644
> --- a/drivers/cxl/core/edac.c
> +++ b/drivers/cxl/core/edac.c
> @@ -1523,7 +1523,7 @@ static int cxl_mem_sparing_set_dpa(struct device *dev, void *drv_data, u64 dpa)
>  	struct cxl_memdev *cxlmd = ctx->cxlmd;
>  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
>  
> -	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end)
> +	if (!resource_contains_addr(&cxlds->dpa_res, dpa))
>  		return -EINVAL;
>  
>  	ctx->dpa = dpa;
> @@ -1892,7 +1892,7 @@ static int cxl_ppr_set_dpa(struct device *dev, void *drv_data, u64 dpa)
>  	struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd;
>  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
>  
> -	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end)
> +	if (!resource_contains_addr(&cxlds->dpa_res, dpa))
>  		return -EINVAL;
>  
>  	cxl_ppr_ctx->dpa = dpa;
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index f88a13adf7fa..4a5dc1d93af1 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -267,7 +267,7 @@ static int cxl_validate_poison_dpa(struct cxl_memdev *cxlmd, u64 dpa)
>  		dev_dbg(cxlds->dev, "device has no dpa resource\n");
>  		return -EINVAL;
>  	}
> -	if (dpa < cxlds->dpa_res.start || dpa > cxlds->dpa_res.end) {
> +	if (!resource_contains_addr(&cxlds->dpa_res, dpa)) {
>  		dev_dbg(cxlds->dev, "dpa:0x%llx not in resource:%pR\n",
>  			dpa, &cxlds->dpa_res);
>  		return -EINVAL;
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 6e5e1460068d..79d8211f8ed0 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -2847,7 +2847,7 @@ static int __cxl_dpa_to_region(struct device *dev, void *arg)
>  	if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
>  		return 0;
>  
> -	if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
> +	if (!resource_contains_addr(cxled->dpa_res, dpa))
>  		return 0;
>  
>  	/*
> @@ -2959,7 +2959,7 @@ u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
>  	if (cxlrd->hpa_to_spa)
>  		hpa = cxlrd->hpa_to_spa(cxlrd, hpa);
>  
> -	if (hpa < p->res->start || hpa > p->res->end) {
> +	if (!resource_contains_addr(p->res, hpa)) {
>  		dev_dbg(&cxlr->dev,
>  			"Addr trans fail: hpa 0x%llx not in region\n", hpa);
>  		return ULLONG_MAX;
> @@ -3499,7 +3499,7 @@ u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa)
>  	xa_for_each(&endpoint->regions, index, iter) {
>  		struct cxl_region_params *p = &iter->region->params;
>  
> -		if (p->res->start <= spa && spa <= p->res->end) {
> +		if (resource_contains_addr(p->res, spa)) {
>  			if (!p->cache_size)
>  				return ~0ULL;
>