Skip to content

Commit a3353ec

Browse files
kuba-moodavem330
authored andcommitted
netdevsim: move max vf config to dev
max_vfs is a strange little beast because the file hangs off of nsim's debugfs, but it configures a field in the bus device. Move it to dev.c, let's look at it as if the device driver was imposing VF limit based on FW info (like pci_sriov_set_totalvfs()). Again, when moving refactor the function not to hold the vfs lock pointlessly while parsing the input. Wrap the access from the read side in READ_ONCE() to appease concurrency checkers. Do not check if return value from snprintf() is negative... Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1c40107 commit a3353ec

File tree

3 files changed

+64
-82
lines changed

3 files changed

+64
-82
lines changed

drivers/net/netdevsim/bus.c

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -57,81 +57,6 @@ static struct device_attribute nsim_bus_dev_numvfs_attr =
5757
__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
5858
nsim_bus_dev_numvfs_store);
5959

60-
ssize_t nsim_bus_dev_max_vfs_read(struct file *file,
61-
char __user *data,
62-
size_t count, loff_t *ppos)
63-
{
64-
struct nsim_dev *nsim_dev = file->private_data;
65-
struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
66-
char buf[11];
67-
ssize_t len;
68-
69-
len = snprintf(buf, sizeof(buf), "%u\n", nsim_bus_dev->max_vfs);
70-
if (len < 0)
71-
return len;
72-
73-
return simple_read_from_buffer(data, count, ppos, buf, len);
74-
}
75-
76-
ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
77-
const char __user *data,
78-
size_t count, loff_t *ppos)
79-
{
80-
struct nsim_dev *nsim_dev = file->private_data;
81-
struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
82-
struct nsim_vf_config *vfconfigs;
83-
ssize_t ret;
84-
char buf[10];
85-
u32 val;
86-
87-
if (*ppos != 0)
88-
return 0;
89-
90-
if (count >= sizeof(buf))
91-
return -ENOSPC;
92-
93-
mutex_lock(&nsim_dev->vfs_lock);
94-
/* Reject if VFs are configured */
95-
if (nsim_bus_dev->num_vfs) {
96-
ret = -EBUSY;
97-
goto unlock;
98-
}
99-
100-
ret = copy_from_user(buf, data, count);
101-
if (ret) {
102-
ret = -EFAULT;
103-
goto unlock;
104-
}
105-
106-
buf[count] = '\0';
107-
ret = kstrtouint(buf, 10, &val);
108-
if (ret) {
109-
ret = -EIO;
110-
goto unlock;
111-
}
112-
113-
/* max_vfs limited by the maximum number of provided port indexes */
114-
if (val > NSIM_DEV_VF_PORT_INDEX_MAX - NSIM_DEV_VF_PORT_INDEX_BASE) {
115-
ret = -ERANGE;
116-
goto unlock;
117-
}
118-
119-
vfconfigs = kcalloc(val, sizeof(struct nsim_vf_config), GFP_KERNEL | __GFP_NOWARN);
120-
if (!vfconfigs) {
121-
ret = -ENOMEM;
122-
goto unlock;
123-
}
124-
125-
kfree(nsim_dev->vfconfigs);
126-
nsim_dev->vfconfigs = vfconfigs;
127-
nsim_bus_dev->max_vfs = val;
128-
*ppos += count;
129-
ret = count;
130-
unlock:
131-
mutex_unlock(&nsim_dev->vfs_lock);
132-
return ret;
133-
}
134-
13560
static ssize_t
13661
new_port_store(struct device *dev, struct device_attribute *attr,
13762
const char *buf, size_t count)

drivers/net/netdevsim/dev.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,70 @@ static const struct file_operations nsim_dev_trap_fa_cookie_fops = {
227227
.owner = THIS_MODULE,
228228
};
229229

230+
static ssize_t nsim_bus_dev_max_vfs_read(struct file *file, char __user *data,
231+
size_t count, loff_t *ppos)
232+
{
233+
struct nsim_dev *nsim_dev = file->private_data;
234+
char buf[11];
235+
ssize_t len;
236+
237+
len = scnprintf(buf, sizeof(buf), "%u\n",
238+
READ_ONCE(nsim_dev->nsim_bus_dev->max_vfs));
239+
240+
return simple_read_from_buffer(data, count, ppos, buf, len);
241+
}
242+
243+
static ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
244+
const char __user *data,
245+
size_t count, loff_t *ppos)
246+
{
247+
struct nsim_vf_config *vfconfigs;
248+
struct nsim_dev *nsim_dev;
249+
char buf[10];
250+
ssize_t ret;
251+
u32 val;
252+
253+
if (*ppos != 0)
254+
return 0;
255+
256+
if (count >= sizeof(buf))
257+
return -ENOSPC;
258+
259+
ret = copy_from_user(buf, data, count);
260+
if (ret)
261+
return -EFAULT;
262+
buf[count] = '\0';
263+
264+
ret = kstrtouint(buf, 10, &val);
265+
if (ret)
266+
return -EINVAL;
267+
268+
/* max_vfs limited by the maximum number of provided port indexes */
269+
if (val > NSIM_DEV_VF_PORT_INDEX_MAX - NSIM_DEV_VF_PORT_INDEX_BASE)
270+
return -ERANGE;
271+
272+
vfconfigs = kcalloc(val, sizeof(struct nsim_vf_config),
273+
GFP_KERNEL | __GFP_NOWARN);
274+
if (!vfconfigs)
275+
return -ENOMEM;
276+
277+
nsim_dev = file->private_data;
278+
mutex_lock(&nsim_dev->vfs_lock);
279+
/* Reject if VFs are configured */
280+
if (nsim_dev_get_vfs(nsim_dev)) {
281+
ret = -EBUSY;
282+
} else {
283+
swap(nsim_dev->vfconfigs, vfconfigs);
284+
WRITE_ONCE(nsim_dev->nsim_bus_dev->max_vfs, val);
285+
*ppos += count;
286+
ret = count;
287+
}
288+
mutex_unlock(&nsim_dev->vfs_lock);
289+
290+
kfree(vfconfigs);
291+
return ret;
292+
}
293+
230294
static const struct file_operations nsim_dev_max_vfs_fops = {
231295
.open = simple_open,
232296
.read = nsim_bus_dev_max_vfs_read,

drivers/net/netdevsim/netdevsim.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,6 @@ void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *fib_data);
317317
u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
318318
enum nsim_resource_id res_id, bool max);
319319

320-
ssize_t nsim_bus_dev_max_vfs_read(struct file *file,
321-
char __user *data,
322-
size_t count, loff_t *ppos);
323-
ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
324-
const char __user *data,
325-
size_t count, loff_t *ppos);
326-
327320
static inline bool nsim_dev_port_is_pf(struct nsim_dev_port *nsim_dev_port)
328321
{
329322
return nsim_dev_port->port_type == NSIM_DEV_PORT_TYPE_PF;

0 commit comments

Comments
 (0)