Skip to content

Commit 8850643

Browse files
davejiangweiny2
authored andcommitted
nvdimm: Clean up __nd_ioctl() and remove gotos
Utilize scoped based resource management to clean up the code and and remove gotos for the __nd_ioctl() function. Change allocation of 'buf' to use kvzalloc() in order to use vmalloc() memory when needed and also zero out the allocated memory. Signed-off-by: Dave Jiang <dave.jiang@intel.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Ira Weiny <ira.weiny@intel.com>
1 parent 0020839 commit 8850643

File tree

1 file changed

+22
-43
lines changed

1 file changed

+22
-43
lines changed

drivers/nvdimm/bus.c

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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>
@@ -1029,14 +1029,12 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
10291029
unsigned int cmd = _IOC_NR(ioctl_cmd);
10301030
struct device *dev = &nvdimm_bus->dev;
10311031
void __user *p = (void __user *) arg;
1032-
char *out_env = NULL, *in_env = NULL;
10331032
const char *cmd_name, *dimm_name;
10341033
u32 in_len = 0, out_len = 0;
10351034
unsigned int func = cmd;
10361035
unsigned long cmd_mask;
10371036
struct nd_cmd_pkg pkg;
10381037
int rc, i, cmd_rc;
1039-
void *buf = NULL;
10401038
u64 buf_len = 0;
10411039

10421040
if (nvdimm) {
@@ -1095,7 +1093,7 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
10951093
}
10961094

10971095
/* process an input envelope */
1098-
in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1096+
char *in_env __free(kfree) = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
10991097
if (!in_env)
11001098
return -ENOMEM;
11011099
for (i = 0; i < desc->in_num; i++) {
@@ -1105,17 +1103,14 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11051103
if (in_size == UINT_MAX) {
11061104
dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
11071105
__func__, dimm_name, cmd_name, i);
1108-
rc = -ENXIO;
1109-
goto out;
1106+
return -ENXIO;
11101107
}
11111108
if (in_len < ND_CMD_MAX_ENVELOPE)
11121109
copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
11131110
else
11141111
copy = 0;
1115-
if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
1116-
rc = -EFAULT;
1117-
goto out;
1118-
}
1112+
if (copy && copy_from_user(&in_env[in_len], p + in_len, copy))
1113+
return -EFAULT;
11191114
in_len += in_size;
11201115
}
11211116

@@ -1127,11 +1122,9 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11271122
}
11281123

11291124
/* process an output envelope */
1130-
out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1131-
if (!out_env) {
1132-
rc = -ENOMEM;
1133-
goto out;
1134-
}
1125+
char *out_env __free(kfree) = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
1126+
if (!out_env)
1127+
return -ENOMEM;
11351128

11361129
for (i = 0; i < desc->out_num; i++) {
11371130
u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
@@ -1141,17 +1134,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11411134
if (out_size == UINT_MAX) {
11421135
dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
11431136
dimm_name, cmd_name, i);
1144-
rc = -EFAULT;
1145-
goto out;
1137+
return -EFAULT;
11461138
}
11471139
if (out_len < ND_CMD_MAX_ENVELOPE)
11481140
copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
11491141
else
11501142
copy = 0;
11511143
if (copy && copy_from_user(&out_env[out_len],
11521144
p + in_len + out_len, copy)) {
1153-
rc = -EFAULT;
1154-
goto out;
1145+
return -EFAULT;
11551146
}
11561147
out_len += out_size;
11571148
}
@@ -1160,30 +1151,25 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11601151
if (buf_len > ND_IOCTL_MAX_BUFLEN) {
11611152
dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
11621153
cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
1163-
rc = -EINVAL;
1164-
goto out;
1154+
return -EINVAL;
11651155
}
11661156

1167-
buf = vmalloc(buf_len);
1168-
if (!buf) {
1169-
rc = -ENOMEM;
1170-
goto out;
1171-
}
1157+
void *buf __free(kvfree) = kvzalloc(buf_len, GFP_KERNEL);
1158+
if (!buf)
1159+
return -ENOMEM;
11721160

1173-
if (copy_from_user(buf, p, buf_len)) {
1174-
rc = -EFAULT;
1175-
goto out;
1176-
}
1161+
if (copy_from_user(buf, p, buf_len))
1162+
return -EFAULT;
11771163

1178-
device_lock(dev);
1179-
nvdimm_bus_lock(dev);
1164+
guard(device)(dev);
1165+
guard(nvdimm_bus)(dev);
11801166
rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
11811167
if (rc)
1182-
goto out_unlock;
1168+
return rc;
11831169

11841170
rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
11851171
if (rc < 0)
1186-
goto out_unlock;
1172+
return rc;
11871173

11881174
if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
11891175
struct nd_cmd_clear_error *clear_err = buf;
@@ -1193,16 +1179,9 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
11931179
}
11941180

11951181
if (copy_to_user(p, buf, buf_len))
1196-
rc = -EFAULT;
1182+
return -EFAULT;
11971183

1198-
out_unlock:
1199-
nvdimm_bus_unlock(dev);
1200-
device_unlock(dev);
1201-
out:
1202-
kfree(in_env);
1203-
kfree(out_env);
1204-
vfree(buf);
1205-
return rc;
1184+
return 0;
12061185
}
12071186

12081187
enum nd_ioctl_mode {

0 commit comments

Comments
 (0)