Skip to content

Commit a81ebe7

Browse files
MingLi-4davejiang
authored andcommitted
cxl/core: Use guard() to drop goto pattern of cxl_dpa_alloc()
In cxl_dpa_alloc(), some checking and operations need to be protected by a rwsem called cxl_dpa_rwsem, so there is a goto pattern in cxl_dpa_alloc() to release the rwsem. The goto pattern can be optimized by using guard() to hold the rwsem. Creating a new function called __cxl_dpa_alloc() to include all checking and operations needed to be protected by cxl_dpa_rwsem. Using guard(rwsem_write()) to hold cxl_dpa_rwsem at the beginning of the new function. 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/20250221012453.126366-6-ming.li@zohomail.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 16fe6ec commit a81ebe7

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

drivers/cxl/core/hdm.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -442,29 +442,25 @@ int cxl_dpa_set_mode(struct cxl_endpoint_decoder *cxled,
442442
return 0;
443443
}
444444

445-
int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, unsigned long long size)
445+
static int __cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, unsigned long long size)
446446
{
447447
struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
448448
resource_size_t free_ram_start, free_pmem_start;
449-
struct cxl_port *port = cxled_to_port(cxled);
450449
struct cxl_dev_state *cxlds = cxlmd->cxlds;
451450
struct device *dev = &cxled->cxld.dev;
452451
resource_size_t start, avail, skip;
453452
struct resource *p, *last;
454-
int rc;
455453

456-
down_write(&cxl_dpa_rwsem);
454+
guard(rwsem_write)(&cxl_dpa_rwsem);
457455
if (cxled->cxld.region) {
458456
dev_dbg(dev, "decoder attached to %s\n",
459457
dev_name(&cxled->cxld.region->dev));
460-
rc = -EBUSY;
461-
goto out;
458+
return -EBUSY;
462459
}
463460

464461
if (cxled->cxld.flags & CXL_DECODER_F_ENABLE) {
465462
dev_dbg(dev, "decoder enabled\n");
466-
rc = -EBUSY;
467-
goto out;
463+
return -EBUSY;
468464
}
469465

470466
for (p = cxlds->ram_res.child, last = NULL; p; p = p->sibling)
@@ -504,21 +500,24 @@ int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, unsigned long long size)
504500
skip = skip_end - skip_start + 1;
505501
} else {
506502
dev_dbg(dev, "mode not set\n");
507-
rc = -EINVAL;
508-
goto out;
503+
return -EINVAL;
509504
}
510505

511506
if (size > avail) {
512507
dev_dbg(dev, "%pa exceeds available %s capacity: %pa\n", &size,
513508
cxl_decoder_mode_name(cxled->mode), &avail);
514-
rc = -ENOSPC;
515-
goto out;
509+
return -ENOSPC;
516510
}
517511

518-
rc = __cxl_dpa_reserve(cxled, start, size, skip);
519-
out:
520-
up_write(&cxl_dpa_rwsem);
512+
return __cxl_dpa_reserve(cxled, start, size, skip);
513+
}
514+
515+
int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, unsigned long long size)
516+
{
517+
struct cxl_port *port = cxled_to_port(cxled);
518+
int rc;
521519

520+
rc = __cxl_dpa_alloc(cxled, size);
522521
if (rc)
523522
return rc;
524523

0 commit comments

Comments
 (0)