Skip to content

Commit 5ec6759

Browse files
MingLi-4davejiang
authored andcommitted
cxl/region: Drop goto pattern of construct_region()
Some operations need to be protected by the cxl_region_rwsem in construct_region(). Currently, construct_region() uses down_write() and up_write() for the cxl_region_rwsem locking, so there is a goto pattern after down_write() invoked to release cxl_region_rwsem. construct region() can be optimized to remove the goto pattern. The changes are creating a new function called __construct_region() which will include all checking and operations protected by the cxl_region_rwsem, and using guard(rwsem_write) to replace down_write() and up_write() in __construct_region(). Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Reviewed-by: Alison Schofield <alison.schofield@intel.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Reviewed-by: Ira Weiny <ira.weiny@intel.com> Acked-by: Davidlohr Bueso <dave@stgolabs.net> Signed-off-by: Li Ming <ming.li@zohomail.com> Link: https://patch.msgid.link/20250221013205.126419-1-ming.li@zohomail.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 9e7b7ab commit 5ec6759

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

drivers/cxl/core/region.c

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,49 +3216,31 @@ static int match_region_by_range(struct device *dev, const void *data)
32163216
return 0;
32173217
}
32183218

3219-
/* Establish an empty region covering the given HPA range */
3220-
static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
3221-
struct cxl_endpoint_decoder *cxled)
3219+
static int __construct_region(struct cxl_region *cxlr,
3220+
struct cxl_root_decoder *cxlrd,
3221+
struct cxl_endpoint_decoder *cxled)
32223222
{
32233223
struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3224-
struct cxl_port *port = cxlrd_to_port(cxlrd);
32253224
struct range *hpa = &cxled->cxld.hpa_range;
32263225
struct cxl_region_params *p;
3227-
struct cxl_region *cxlr;
32283226
struct resource *res;
32293227
int rc;
32303228

3231-
do {
3232-
cxlr = __create_region(cxlrd, cxled->mode,
3233-
atomic_read(&cxlrd->region_id));
3234-
} while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
3235-
3236-
if (IS_ERR(cxlr)) {
3237-
dev_err(cxlmd->dev.parent,
3238-
"%s:%s: %s failed assign region: %ld\n",
3239-
dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3240-
__func__, PTR_ERR(cxlr));
3241-
return cxlr;
3242-
}
3243-
3244-
down_write(&cxl_region_rwsem);
3229+
guard(rwsem_write)(&cxl_region_rwsem);
32453230
p = &cxlr->params;
32463231
if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
32473232
dev_err(cxlmd->dev.parent,
32483233
"%s:%s: %s autodiscovery interrupted\n",
32493234
dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
32503235
__func__);
3251-
rc = -EBUSY;
3252-
goto err;
3236+
return -EBUSY;
32533237
}
32543238

32553239
set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
32563240

32573241
res = kmalloc(sizeof(*res), GFP_KERNEL);
3258-
if (!res) {
3259-
rc = -ENOMEM;
3260-
goto err;
3261-
}
3242+
if (!res)
3243+
return -ENOMEM;
32623244

32633245
*res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
32643246
dev_name(&cxlr->dev));
@@ -3281,7 +3263,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
32813263

32823264
rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
32833265
if (rc)
3284-
goto err;
3266+
return rc;
32853267

32863268
dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
32873269
dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
@@ -3290,14 +3272,39 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
32903272

32913273
/* ...to match put_device() in cxl_add_to_region() */
32923274
get_device(&cxlr->dev);
3293-
up_write(&cxl_region_rwsem);
32943275

3295-
return cxlr;
3276+
return 0;
3277+
}
32963278

3297-
err:
3298-
up_write(&cxl_region_rwsem);
3299-
devm_release_action(port->uport_dev, unregister_region, cxlr);
3300-
return ERR_PTR(rc);
3279+
/* Establish an empty region covering the given HPA range */
3280+
static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
3281+
struct cxl_endpoint_decoder *cxled)
3282+
{
3283+
struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3284+
struct cxl_port *port = cxlrd_to_port(cxlrd);
3285+
struct cxl_region *cxlr;
3286+
int rc;
3287+
3288+
do {
3289+
cxlr = __create_region(cxlrd, cxled->mode,
3290+
atomic_read(&cxlrd->region_id));
3291+
} while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
3292+
3293+
if (IS_ERR(cxlr)) {
3294+
dev_err(cxlmd->dev.parent,
3295+
"%s:%s: %s failed assign region: %ld\n",
3296+
dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3297+
__func__, PTR_ERR(cxlr));
3298+
return cxlr;
3299+
}
3300+
3301+
rc = __construct_region(cxlr, cxlrd, cxled);
3302+
if (rc) {
3303+
devm_release_action(port->uport_dev, unregister_region, cxlr);
3304+
return ERR_PTR(rc);
3305+
}
3306+
3307+
return cxlr;
33013308
}
33023309

33033310
int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)

0 commit comments

Comments
 (0)