All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Pirko <jiri@resnulli.us>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, mlxsw@mellanox.com, idosch@mellanox.com,
	jakub.kicinski@netronome.com, f.fainelli@gmail.com,
	andrew@lunn.ch, vivien.didelot@gmail.com
Subject: [patch net-next v2 14/15] net: devlink: add port type spinlock
Date: Fri, 22 Mar 2019 17:56:35 +0100	[thread overview]
Message-ID: <20190322165636.1725-15-jiri@resnulli.us> (raw)
In-Reply-To: <20190322165636.1725-1-jiri@resnulli.us>

From: Jiri Pirko <jiri@mellanox.com>

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 <jiri@mellanox.com>
v1->v2:
- rebased
---
 include/net/devlink.h |  4 ++++
 net/core/devlink.c    | 17 +++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/include/net/devlink.h b/include/net/devlink.h
index 63de99e09f04..cb9b060033e1 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -16,6 +16,7 @@
 #include <linux/gfp.h>
 #include <linux/list.h>
 #include <linux/netdevice.h>
+#include <linux/spinlock.h>
 #include <net/net_namespace.h>
 #include <uapi/linux/devlink.h>
 
@@ -53,6 +54,9 @@ struct devlink_port {
 	struct devlink *devlink;
 	unsigned index;
 	bool registered;
+	spinlock_t type_lock; /* Protects type and type_dev
+			       * pointer consistency.
+			       */
 	enum devlink_port_type type;
 	enum devlink_port_type desired_type;
 	void *type_dev;
diff --git a/net/core/devlink.c b/net/core/devlink.c
index ff3e31010954..cf389e26a32d 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -19,6 +19,7 @@
 #include <linux/device.h>
 #include <linux/list.h>
 #include <linux/netdevice.h>
+#include <linux/spinlock.h>
 #include <rdma/ib_verbs.h>
 #include <net/netlink.h>
 #include <net/genetlink.h>
@@ -543,12 +544,14 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
 		goto nla_put_failure;
 	if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
 		goto nla_put_failure;
+
+	spin_lock(&devlink_port->type_lock);
 	if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
-		goto nla_put_failure;
+		goto nla_put_failure_type_locked;
 	if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
 	    nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
 			devlink_port->desired_type))
-		goto nla_put_failure;
+		goto nla_put_failure_type_locked;
 	if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
 		struct net_device *netdev = devlink_port->type_dev;
 
@@ -557,7 +560,7 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
 				 netdev->ifindex) ||
 		     nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
 				    netdev->name)))
-			goto nla_put_failure;
+			goto nla_put_failure_type_locked;
 	}
 	if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
 		struct ib_device *ibdev = devlink_port->type_dev;
@@ -565,14 +568,17 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
 		if (ibdev &&
 		    nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
 				   ibdev->name))
-			goto nla_put_failure;
+			goto nla_put_failure_type_locked;
 	}
+	spin_unlock(&devlink_port->type_lock);
 	if (devlink_nl_port_attrs_put(msg, devlink_port))
 		goto nla_put_failure;
 
 	genlmsg_end(msg, hdr);
 	return 0;
 
+nla_put_failure_type_locked:
+	spin_unlock(&devlink_port->type_lock);
 nla_put_failure:
 	genlmsg_cancel(msg, hdr);
 	return -EMSGSIZE;
@@ -5337,6 +5343,7 @@ int devlink_port_register(struct devlink *devlink,
 	devlink_port->devlink = devlink;
 	devlink_port->index = port_index;
 	devlink_port->registered = true;
+	spin_lock_init(&devlink_port->type_lock);
 	list_add_tail(&devlink_port->list, &devlink->port_list);
 	INIT_LIST_HEAD(&devlink_port->param_list);
 	mutex_unlock(&devlink->lock);
@@ -5367,8 +5374,10 @@ static void __devlink_port_type_set(struct devlink_port *devlink_port,
 {
 	if (WARN_ON(!devlink_port->registered))
 		return;
+	spin_lock(&devlink_port->type_lock);
 	devlink_port->type = type;
 	devlink_port->type_dev = type_dev;
+	spin_unlock(&devlink_port->type_lock);
 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
 }
 
-- 
2.17.2


  parent reply	other threads:[~2019-03-22 16:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-22 16:56 [patch net-next v2 00/15] devlink: small spring cleanup Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 01/15] net: devlink: add couple of missing mutex_destroy() calls Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 02/15] bnxt: add missing net/devlink.h include Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 03/15] dsa: " Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 04/15] bnxt: set devlink port attrs properly Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 05/15] bnxt: call devlink_port_type_eth_set() before port register Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 06/15] net: devlink: don't take devlink_mutex for devlink_compat_* Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 07/15] net: devlink: don't pass return value of __devlink_port_type_set() Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 08/15] mlxsw: Move devlink_port_attrs_set() call before register Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 09/15] dsa: move " Jiri Pirko
2019-03-22 20:47   ` Andrew Lunn
2019-03-22 16:56 ` [patch net-next v2 10/15] net: devlink: disallow port_attrs_set() to be called " Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 11/15] nfp: move devlink port type set after netdev registration Jiri Pirko
2019-03-23 20:31   ` Jakub Kicinski
2019-03-23 22:47     ` Jiri Pirko
2019-03-24  3:04       ` Jakub Kicinski
2019-03-24  8:01         ` Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 12/15] bnxt: set devlink port type after registration Jiri Pirko
2019-03-22 16:56 ` [patch net-next v2 13/15] net: devlink: warn on setting type on unregistered port Jiri Pirko
2019-03-22 16:56 ` Jiri Pirko [this message]
2019-03-22 16:56 ` [patch net-next v2 15/15] net: devlink: select NET_DEVLINK from drivers Jiri Pirko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190322165636.1725-15-jiri@resnulli.us \
    --to=jiri@resnulli.us \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=f.fainelli@gmail.com \
    --cc=idosch@mellanox.com \
    --cc=jakub.kicinski@netronome.com \
    --cc=mlxsw@mellanox.com \
    --cc=netdev@vger.kernel.org \
    --cc=vivien.didelot@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.