Skip to content

Commit ba9dac9

Browse files
committed
Merge tag 'libnvdimm-for-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm
Pull libnvdimm updates from Ira Weiny: "Primarily bug fixes. Dave introduced the usage of cleanup.h a bit late in the cycle to help with the new label work required within CXL [1] nvdimm: - Return -ENOMEM if devm_kcalloc() fails in ndtest_probe() - Clean up __nd_ioctl() and remove gotos - Remove duplicate linux/slab.h header - Introduce guard() for nvdimm_bus_lock - Use str_plural() to simplify the code ACPI: - NFIT: Fix incorrect ndr_desc being reportedin dev_err message" Link: https://lore.kernel.org/all/[email protected]/ [1] * tag 'libnvdimm-for-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm: nvdimm: Remove duplicate linux/slab.h header nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe() nvdimm: Clean up __nd_ioctl() and remove gotos nvdimm: Introduce guard() for nvdimm_bus_lock ACPI: NFIT: Fix incorrect ndr_desc being reportedin dev_err message nvdimm: Use str_plural() to simplify the code
2 parents 169c9d0 + 5c34f2b commit ba9dac9

File tree

16 files changed

+229
-297
lines changed

16 files changed

+229
-297
lines changed

drivers/acpi/nfit/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,7 @@ static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
26372637
if (ndr_desc->target_node == NUMA_NO_NODE) {
26382638
ndr_desc->target_node = phys_to_target_node(spa->address);
26392639
dev_info(acpi_desc->dev, "changing target node from %d to %d for nfit region [%pa-%pa]",
2640-
NUMA_NO_NODE, ndr_desc->numa_node, &res.start, &res.end);
2640+
NUMA_NO_NODE, ndr_desc->target_node, &res.start, &res.end);
26412641
}
26422642

26432643
/*

drivers/nvdimm/badrange.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ void nvdimm_badblocks_populate(struct nd_region *nd_region,
278278
}
279279
nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
280280

281-
nvdimm_bus_lock(&nvdimm_bus->dev);
281+
guard(nvdimm_bus)(&nvdimm_bus->dev);
282282
badblocks_populate(&nvdimm_bus->badrange, bb, range);
283-
nvdimm_bus_unlock(&nvdimm_bus->dev);
284283
}
285284
EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate);

drivers/nvdimm/btt_devs.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ static ssize_t sector_size_store(struct device *dev,
5050
struct nd_btt *nd_btt = to_nd_btt(dev);
5151
ssize_t rc;
5252

53-
device_lock(dev);
54-
nvdimm_bus_lock(dev);
53+
guard(device)(dev);
54+
guard(nvdimm_bus)(dev);
5555
rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
5656
btt_lbasize_supported);
5757
dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
5858
buf[len - 1] == '\n' ? "" : "\n");
59-
nvdimm_bus_unlock(dev);
60-
device_unlock(dev);
6159

6260
return rc ? rc : len;
6361
}
@@ -93,13 +91,10 @@ static ssize_t namespace_show(struct device *dev,
9391
struct device_attribute *attr, char *buf)
9492
{
9593
struct nd_btt *nd_btt = to_nd_btt(dev);
96-
ssize_t rc;
9794

98-
nvdimm_bus_lock(dev);
99-
rc = sprintf(buf, "%s\n", nd_btt->ndns
95+
guard(nvdimm_bus)(dev);
96+
return sprintf(buf, "%s\n", nd_btt->ndns
10097
? dev_name(&nd_btt->ndns->dev) : "");
101-
nvdimm_bus_unlock(dev);
102-
return rc;
10398
}
10499

105100
static ssize_t namespace_store(struct device *dev,
@@ -108,13 +103,11 @@ static ssize_t namespace_store(struct device *dev,
108103
struct nd_btt *nd_btt = to_nd_btt(dev);
109104
ssize_t rc;
110105

111-
device_lock(dev);
112-
nvdimm_bus_lock(dev);
106+
guard(device)(dev);
107+
guard(nvdimm_bus)(dev);
113108
rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
114109
dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
115110
buf[len - 1] == '\n' ? "" : "\n");
116-
nvdimm_bus_unlock(dev);
117-
device_unlock(dev);
118111

119112
return rc;
120113
}
@@ -351,9 +344,8 @@ int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
351344
return -ENODEV;
352345
}
353346

354-
nvdimm_bus_lock(&ndns->dev);
355-
btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
356-
nvdimm_bus_unlock(&ndns->dev);
347+
scoped_guard(nvdimm_bus, &ndns->dev)
348+
btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
357349
if (!btt_dev)
358350
return -ENOMEM;
359351
btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);

drivers/nvdimm/bus.c

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
66
#include <linux/libnvdimm.h>
77
#include <linux/sched/mm.h>
8-
#include <linux/vmalloc.h>
8+
#include <linux/slab.h>
99
#include <linux/uaccess.h>
1010
#include <linux/module.h>
1111
#include <linux/blkdev.h>
1212
#include <linux/fcntl.h>
1313
#include <linux/async.h>
1414
#include <linux/ndctl.h>
1515
#include <linux/sched.h>
16-
#include <linux/slab.h>
1716
#include <linux/cpu.h>
1817
#include <linux/fs.h>
1918
#include <linux/io.h>
@@ -64,17 +63,15 @@ static struct module *to_bus_provider(struct device *dev)
6463

6564
static void nvdimm_bus_probe_start(struct nvdimm_bus *nvdimm_bus)
6665
{
67-
nvdimm_bus_lock(&nvdimm_bus->dev);
66+
guard(nvdimm_bus)(&nvdimm_bus->dev);
6867
nvdimm_bus->probe_active++;
69-
nvdimm_bus_unlock(&nvdimm_bus->dev);
7068
}
7169

7270
static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus)
7371
{
74-
nvdimm_bus_lock(&nvdimm_bus->dev);
72+
guard(nvdimm_bus)(&nvdimm_bus->dev);
7573
if (--nvdimm_bus->probe_active == 0)
7674
wake_up(&nvdimm_bus->wait);
77-
nvdimm_bus_unlock(&nvdimm_bus->dev);
7875
}
7976

8077
static int nvdimm_bus_probe(struct device *dev)
@@ -1031,14 +1028,12 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
10311028
unsigned int cmd = _IOC_NR(ioctl_cmd);
10321029
struct device *dev = &nvdimm_bus->dev;
10331030
void __user *p = (void __user *) arg;
1034-
char *out_env = NULL, *in_env = NULL;
10351031
const char *cmd_name, *dimm_name;
10361032
u32 in_len = 0, out_len = 0;
10371033
unsigned int func = cmd;
10381034
unsigned long cmd_mask;
10391035
struct nd_cmd_pkg pkg;
10401036
int rc, i, cmd_rc;
1041-
void *buf = NULL;
10421037
u64 buf_len = 0;
10431038

10441039
if (nvdimm) {
@@ -1097,7 +1092,7 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
10971092
}
10981093

10991094
/* process an input envelope */
1100-
in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1095+
char *in_env __free(kfree) = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
11011096
if (!in_env)
11021097
return -ENOMEM;
11031098
for (i = 0; i < desc->in_num; i++) {
@@ -1107,17 +1102,14 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11071102
if (in_size == UINT_MAX) {
11081103
dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
11091104
__func__, dimm_name, cmd_name, i);
1110-
rc = -ENXIO;
1111-
goto out;
1105+
return -ENXIO;
11121106
}
11131107
if (in_len < ND_CMD_MAX_ENVELOPE)
11141108
copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
11151109
else
11161110
copy = 0;
1117-
if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
1118-
rc = -EFAULT;
1119-
goto out;
1120-
}
1111+
if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
1112+
return -EFAULT;
11211113
in_len += in_size;
11221114
}
11231115

@@ -1129,11 +1121,9 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11291121
}
11301122

11311123
/* process an output envelope */
1132-
out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1133-
if (!out_env) {
1134-
rc = -ENOMEM;
1135-
goto out;
1136-
}
1124+
char *out_env __free(kfree) = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1125+
if (!out_env)
1126+
return -ENOMEM;
11371127

11381128
for (i = 0; i < desc->out_num; i++) {
11391129
u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
@@ -1143,17 +1133,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11431133
if (out_size == UINT_MAX) {
11441134
dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
11451135
dimm_name, cmd_name, i);
1146-
rc = -EFAULT;
1147-
goto out;
1136+
return -EFAULT;
11481137
}
11491138
if (out_len < ND_CMD_MAX_ENVELOPE)
11501139
copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
11511140
else
11521141
copy = 0;
11531142
if (copy && copy_from_user(&out_env[out_len],
11541143
p + in_len + out_len, copy)) {
1155-
rc = -EFAULT;
1156-
goto out;
1144+
return -EFAULT;
11571145
}
11581146
out_len += out_size;
11591147
}
@@ -1162,30 +1150,25 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11621150
if (buf_len > ND_IOCTL_MAX_BUFLEN) {
11631151
dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
11641152
cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1165-
rc = -EINVAL;
1166-
goto out;
1153+
return -EINVAL;
11671154
}
11681155

1169-
buf = vmalloc(buf_len);
1170-
if (!buf) {
1171-
rc = -ENOMEM;
1172-
goto out;
1173-
}
1156+
void *buf __free(kvfree) = kvzalloc(buf_len, GFP_KERNEL);
1157+
if (!buf)
1158+
return -ENOMEM;
11741159

1175-
if (copy_from_user(buf, p, buf_len)) {
1176-
rc = -EFAULT;
1177-
goto out;
1178-
}
1160+
if (copy_from_user(buf, p, buf_len))
1161+
return -EFAULT;
11791162

1180-
device_lock(dev);
1181-
nvdimm_bus_lock(dev);
1163+
guard(device)(dev);
1164+
guard(nvdimm_bus)(dev);
11821165
rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
11831166
if (rc)
1184-
goto out_unlock;
1167+
return rc;
11851168

11861169
rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
11871170
if (rc < 0)
1188-
goto out_unlock;
1171+
return rc;
11891172

11901173
if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
11911174
struct nd_cmd_clear_error *clear_err = buf;
@@ -1195,16 +1178,9 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11951178
}
11961179

11971180
if (copy_to_user(p, buf, buf_len))
1198-
rc = -EFAULT;
1181+
return -EFAULT;
11991182

1200-
out_unlock:
1201-
nvdimm_bus_unlock(dev);
1202-
device_unlock(dev);
1203-
out:
1204-
kfree(in_env);
1205-
kfree(out_env);
1206-
vfree(buf);
1207-
return rc;
1183+
return 0;
12081184
}
12091185

12101186
enum nd_ioctl_mode {

drivers/nvdimm/claim.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ void nd_detach_ndns(struct device *dev,
3434

3535
if (!ndns)
3636
return;
37-
get_device(&ndns->dev);
38-
nvdimm_bus_lock(&ndns->dev);
37+
38+
struct device *ndev __free(put_device) = get_device(&ndns->dev);
39+
guard(nvdimm_bus)(ndev);
3940
__nd_detach_ndns(dev, _ndns);
40-
nvdimm_bus_unlock(&ndns->dev);
41-
put_device(&ndns->dev);
4241
}
4342

4443
bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,

drivers/nvdimm/core.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@ static void nvdimm_map_put(void *data)
141141
struct nvdimm_map *nvdimm_map = data;
142142
struct nvdimm_bus *nvdimm_bus = nvdimm_map->nvdimm_bus;
143143

144-
nvdimm_bus_lock(&nvdimm_bus->dev);
144+
guard(nvdimm_bus)(&nvdimm_bus->dev);
145145
kref_put(&nvdimm_map->kref, nvdimm_map_release);
146-
nvdimm_bus_unlock(&nvdimm_bus->dev);
147146
}
148147

149148
/**
@@ -158,13 +157,13 @@ void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset,
158157
{
159158
struct nvdimm_map *nvdimm_map;
160159

161-
nvdimm_bus_lock(dev);
162-
nvdimm_map = find_nvdimm_map(dev, offset);
163-
if (!nvdimm_map)
164-
nvdimm_map = alloc_nvdimm_map(dev, offset, size, flags);
165-
else
166-
kref_get(&nvdimm_map->kref);
167-
nvdimm_bus_unlock(dev);
160+
scoped_guard(nvdimm_bus, dev) {
161+
nvdimm_map = find_nvdimm_map(dev, offset);
162+
if (!nvdimm_map)
163+
nvdimm_map = alloc_nvdimm_map(dev, offset, size, flags);
164+
else
165+
kref_get(&nvdimm_map->kref);
166+
}
168167

169168
if (!nvdimm_map)
170169
return NULL;

drivers/nvdimm/dax_devs.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
104104
return -ENODEV;
105105
}
106106

107-
nvdimm_bus_lock(&ndns->dev);
108-
nd_dax = nd_dax_alloc(nd_region);
109-
dax_dev = nd_dax_devinit(nd_dax, ndns);
110-
nvdimm_bus_unlock(&ndns->dev);
111-
if (!dax_dev)
112-
return -ENOMEM;
107+
scoped_guard(nvdimm_bus, &ndns->dev) {
108+
nd_dax = nd_dax_alloc(nd_region);
109+
dax_dev = nd_dax_devinit(nd_dax, ndns);
110+
if (!dax_dev)
111+
return -ENOMEM;
112+
}
113113
pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
114114
nd_pfn = &nd_dax->nd_pfn;
115115
nd_pfn->pfn_sb = pfn_sb;

drivers/nvdimm/dimm.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ static void nvdimm_remove(struct device *dev)
117117
{
118118
struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
119119

120-
nvdimm_bus_lock(dev);
121-
dev_set_drvdata(dev, NULL);
122-
nvdimm_bus_unlock(dev);
120+
scoped_guard(nvdimm_bus, dev)
121+
dev_set_drvdata(dev, NULL);
123122
put_ndd(ndd);
124123
}
125124

0 commit comments

Comments
 (0)