Skip to content

Commit b8f9755

Browse files
jpirkodavem330
authored andcommitted
net: devlink: add port type spinlock
Add spinlock to protect port type and type_dev pointer consistency. Without that, userspace may see inconsistent type and type_dev combinations. Signed-off-by: Jiri Pirko <[email protected]> v1->v2: - rebased Signed-off-by: David S. Miller <[email protected]>
1 parent 2b239e7 commit b8f9755

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

include/net/devlink.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/gfp.h>
1717
#include <linux/list.h>
1818
#include <linux/netdevice.h>
19+
#include <linux/spinlock.h>
1920
#include <net/net_namespace.h>
2021
#include <uapi/linux/devlink.h>
2122

@@ -53,6 +54,9 @@ struct devlink_port {
5354
struct devlink *devlink;
5455
unsigned index;
5556
bool registered;
57+
spinlock_t type_lock; /* Protects type and type_dev
58+
* pointer consistency.
59+
*/
5660
enum devlink_port_type type;
5761
enum devlink_port_type desired_type;
5862
void *type_dev;

net/core/devlink.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/device.h>
2020
#include <linux/list.h>
2121
#include <linux/netdevice.h>
22+
#include <linux/spinlock.h>
2223
#include <rdma/ib_verbs.h>
2324
#include <net/netlink.h>
2425
#include <net/genetlink.h>
@@ -543,12 +544,14 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
543544
goto nla_put_failure;
544545
if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
545546
goto nla_put_failure;
547+
548+
spin_lock(&devlink_port->type_lock);
546549
if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
547-
goto nla_put_failure;
550+
goto nla_put_failure_type_locked;
548551
if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
549552
nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
550553
devlink_port->desired_type))
551-
goto nla_put_failure;
554+
goto nla_put_failure_type_locked;
552555
if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
553556
struct net_device *netdev = devlink_port->type_dev;
554557

@@ -557,22 +560,25 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
557560
netdev->ifindex) ||
558561
nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
559562
netdev->name)))
560-
goto nla_put_failure;
563+
goto nla_put_failure_type_locked;
561564
}
562565
if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
563566
struct ib_device *ibdev = devlink_port->type_dev;
564567

565568
if (ibdev &&
566569
nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
567570
ibdev->name))
568-
goto nla_put_failure;
571+
goto nla_put_failure_type_locked;
569572
}
573+
spin_unlock(&devlink_port->type_lock);
570574
if (devlink_nl_port_attrs_put(msg, devlink_port))
571575
goto nla_put_failure;
572576

573577
genlmsg_end(msg, hdr);
574578
return 0;
575579

580+
nla_put_failure_type_locked:
581+
spin_unlock(&devlink_port->type_lock);
576582
nla_put_failure:
577583
genlmsg_cancel(msg, hdr);
578584
return -EMSGSIZE;
@@ -5300,6 +5306,7 @@ int devlink_port_register(struct devlink *devlink,
53005306
devlink_port->devlink = devlink;
53015307
devlink_port->index = port_index;
53025308
devlink_port->registered = true;
5309+
spin_lock_init(&devlink_port->type_lock);
53035310
list_add_tail(&devlink_port->list, &devlink->port_list);
53045311
INIT_LIST_HEAD(&devlink_port->param_list);
53055312
mutex_unlock(&devlink->lock);
@@ -5330,8 +5337,10 @@ static void __devlink_port_type_set(struct devlink_port *devlink_port,
53305337
{
53315338
if (WARN_ON(!devlink_port->registered))
53325339
return;
5340+
spin_lock(&devlink_port->type_lock);
53335341
devlink_port->type = type;
53345342
devlink_port->type_dev = type_dev;
5343+
spin_unlock(&devlink_port->type_lock);
53355344
devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
53365345
}
53375346

0 commit comments

Comments
 (0)