Skip to content

Commit 6e1d219

Browse files
djbwdavejiang
authored andcommitted
cxl/mem: Convert devm_cxl_add_memdev() to scope-based-cleanup
In preparation for adding more setup steps, convert the current implementation to scope-based cleanup. The cxl_memdev_shutdown() is only required after cdev_device_add(). With that moved to a helper function it precludes the need to add scope-based-handler for that cleanup if devm_add_action_or_reset() fails. Cc: Smita Koralahalli <Smita.KoralahalliChannabasappa@amd.com> Reviewed-by: Alison Schofield <alison.schofield@intel.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Reviewed-by: Ben Cheatham <benjamin.cheatham@amd.com> Tested-by: Alejandro Lucero <alucerop@amd.com> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com> Link: https://patch.msgid.link/20251216005616.3090129-5-dan.j.williams@intel.com Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent ae201a0 commit 6e1d219

1 file changed

Lines changed: 44 additions & 26 deletions

File tree

drivers/cxl/core/memdev.c

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,52 +1050,70 @@ static const struct file_operations cxl_memdev_fops = {
10501050
.llseek = noop_llseek,
10511051
};
10521052

1053+
/*
1054+
* Activate ioctl operations, no cxl_memdev_rwsem manipulation needed as this is
1055+
* ordered with cdev_add() publishing the device.
1056+
*/
1057+
static int cxlmd_add(struct cxl_memdev *cxlmd, struct cxl_dev_state *cxlds)
1058+
{
1059+
int rc;
1060+
1061+
cxlmd->cxlds = cxlds;
1062+
cxlds->cxlmd = cxlmd;
1063+
1064+
rc = cdev_device_add(&cxlmd->cdev, &cxlmd->dev);
1065+
if (rc) {
1066+
/*
1067+
* The cdev was briefly live, shutdown any ioctl operations that
1068+
* saw that state.
1069+
*/
1070+
cxl_memdev_shutdown(&cxlmd->dev);
1071+
return rc;
1072+
}
1073+
1074+
return 0;
1075+
}
1076+
1077+
DEFINE_FREE(put_cxlmd, struct cxl_memdev *,
1078+
if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
1079+
1080+
static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
1081+
{
1082+
int rc;
1083+
1084+
rc = devm_add_action_or_reset(cxlmd->cxlds->dev, cxl_memdev_unregister,
1085+
cxlmd);
1086+
if (rc)
1087+
return ERR_PTR(rc);
1088+
1089+
return cxlmd;
1090+
}
1091+
10531092
/*
10541093
* Core helper for devm_cxl_add_memdev() that wants to both create a device and
10551094
* assert to the caller that upon return cxl_mem::probe() has been invoked.
10561095
*/
10571096
struct cxl_memdev *__devm_cxl_add_memdev(struct device *host,
10581097
struct cxl_dev_state *cxlds)
10591098
{
1060-
struct cxl_memdev *cxlmd;
10611099
struct device *dev;
1062-
struct cdev *cdev;
10631100
int rc;
10641101

1065-
cxlmd = cxl_memdev_alloc(cxlds, &cxl_memdev_fops);
1102+
struct cxl_memdev *cxlmd __free(put_cxlmd) =
1103+
cxl_memdev_alloc(cxlds, &cxl_memdev_fops);
10661104
if (IS_ERR(cxlmd))
10671105
return cxlmd;
10681106

10691107
dev = &cxlmd->dev;
10701108
rc = dev_set_name(dev, "mem%d", cxlmd->id);
10711109
if (rc)
1072-
goto err;
1073-
1074-
/*
1075-
* Activate ioctl operations, no cxl_memdev_rwsem manipulation
1076-
* needed as this is ordered with cdev_add() publishing the device.
1077-
*/
1078-
cxlmd->cxlds = cxlds;
1079-
cxlds->cxlmd = cxlmd;
1080-
1081-
cdev = &cxlmd->cdev;
1082-
rc = cdev_device_add(cdev, dev);
1083-
if (rc)
1084-
goto err;
1110+
return ERR_PTR(rc);
10851111

1086-
rc = devm_add_action_or_reset(host, cxl_memdev_unregister, cxlmd);
1112+
rc = cxlmd_add(cxlmd, cxlds);
10871113
if (rc)
10881114
return ERR_PTR(rc);
1089-
return cxlmd;
10901115

1091-
err:
1092-
/*
1093-
* The cdev was briefly live, shutdown any ioctl operations that
1094-
* saw that state.
1095-
*/
1096-
cxl_memdev_shutdown(dev);
1097-
put_device(dev);
1098-
return ERR_PTR(rc);
1116+
return cxl_memdev_autoremove(no_free_ptr(cxlmd));
10991117
}
11001118
EXPORT_SYMBOL_FOR_MODULES(__devm_cxl_add_memdev, "cxl_mem");
11011119

0 commit comments

Comments
 (0)