All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Ido Schimmel <idosch@mellanox.com>,
	Jiri Pirko <jiri@mellanox.com>,
	"David S. Miller" <davem@davemloft.net>
Subject: [PATCH 5.3 03/48] devlink: disallow reload operation during device cleanup
Date: Tue, 19 Nov 2019 06:19:23 +0100	[thread overview]
Message-ID: <20191119050949.815080213@linuxfoundation.org> (raw)
In-Reply-To: <20191119050946.745015350@linuxfoundation.org>

From: Jiri Pirko <jiri@mellanox.com>

[ Upstream commit 5a508a254bed9a2e36a5fb96c9065532a6bf1e9c ]

There is a race between driver code that does setup/cleanup of device
and devlink reload operation that in some drivers works with the same
code. Use after free could we easily obtained by running:

while true; do
        echo "0000:00:10.0" >/sys/bus/pci/drivers/mlxsw_spectrum2/bind
        devlink dev reload pci/0000:00:10.0 &
        echo "0000:00:10.0" >/sys/bus/pci/drivers/mlxsw_spectrum2/unbind
done

Fix this by enabling reload only after setup of device is complete and
disabling it at the beginning of the cleanup process.

Reported-by: Ido Schimmel <idosch@mellanox.com>
Fixes: 2d8dc5bbf4e7 ("devlink: Add support for reload")
Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/net/ethernet/mellanox/mlx4/main.c  |    3 ++
 drivers/net/ethernet/mellanox/mlxsw/core.c |    6 +++-
 drivers/net/netdevsim/dev.c                |    2 +
 include/net/devlink.h                      |    3 ++
 net/core/devlink.c                         |   39 ++++++++++++++++++++++++++++-
 5 files changed, 51 insertions(+), 2 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx4/main.c
+++ b/drivers/net/ethernet/mellanox/mlx4/main.c
@@ -3982,6 +3982,7 @@ static int mlx4_init_one(struct pci_dev
 		goto err_params_unregister;
 
 	devlink_params_publish(devlink);
+	devlink_reload_enable(devlink);
 	pci_save_state(pdev);
 	return 0;
 
@@ -4093,6 +4094,8 @@ static void mlx4_remove_one(struct pci_d
 	struct devlink *devlink = priv_to_devlink(priv);
 	int active_vfs = 0;
 
+	devlink_reload_disable(devlink);
+
 	if (mlx4_is_slave(dev))
 		persist->interface_state |= MLX4_INTERFACE_STATE_NOWAIT;
 
--- a/drivers/net/ethernet/mellanox/mlxsw/core.c
+++ b/drivers/net/ethernet/mellanox/mlxsw/core.c
@@ -1128,8 +1128,10 @@ __mlxsw_core_bus_device_register(const s
 	if (err)
 		goto err_thermal_init;
 
-	if (mlxsw_driver->params_register)
+	if (mlxsw_driver->params_register) {
 		devlink_params_publish(devlink);
+		devlink_reload_enable(devlink);
+	}
 
 	return 0;
 
@@ -1191,6 +1193,8 @@ void mlxsw_core_bus_device_unregister(st
 {
 	struct devlink *devlink = priv_to_devlink(mlxsw_core);
 
+	if (!reload)
+		devlink_reload_disable(devlink);
 	if (mlxsw_core->reload_fail) {
 		if (!reload)
 			/* Only the parts that were not de-initialized in the
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -297,6 +297,7 @@ nsim_dev_create(struct nsim_bus_dev *nsi
 	if (err)
 		goto err_debugfs_exit;
 
+	devlink_reload_enable(devlink);
 	return nsim_dev;
 
 err_debugfs_exit:
@@ -314,6 +315,7 @@ static void nsim_dev_destroy(struct nsim
 {
 	struct devlink *devlink = priv_to_devlink(nsim_dev);
 
+	devlink_reload_disable(devlink);
 	nsim_bpf_dev_exit(nsim_dev);
 	nsim_dev_debugfs_exit(nsim_dev);
 	devlink_unregister(devlink);
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -35,6 +35,7 @@ struct devlink {
 	struct device *dev;
 	possible_net_t _net;
 	struct mutex lock;
+	u8 reload_enabled:1;
 	char priv[0] __aligned(NETDEV_ALIGN);
 };
 
@@ -594,6 +595,8 @@ struct ib_device;
 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
 int devlink_register(struct devlink *devlink, struct device *dev);
 void devlink_unregister(struct devlink *devlink);
+void devlink_reload_enable(struct devlink *devlink);
+void devlink_reload_disable(struct devlink *devlink);
 void devlink_free(struct devlink *devlink);
 int devlink_port_register(struct devlink *devlink,
 			  struct devlink_port *devlink_port,
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -2677,7 +2677,7 @@ static int devlink_nl_cmd_reload(struct
 	struct devlink *devlink = info->user_ptr[0];
 	int err;
 
-	if (!devlink->ops->reload)
+	if (!devlink->ops->reload || !devlink->reload_enabled)
 		return -EOPNOTSUPP;
 
 	err = devlink_resources_validate(devlink, NULL, info);
@@ -5559,6 +5559,8 @@ EXPORT_SYMBOL_GPL(devlink_register);
 void devlink_unregister(struct devlink *devlink)
 {
 	mutex_lock(&devlink_mutex);
+	WARN_ON(devlink_reload_supported(devlink) &&
+		devlink->reload_enabled);
 	devlink_notify(devlink, DEVLINK_CMD_DEL);
 	list_del(&devlink->list);
 	mutex_unlock(&devlink_mutex);
@@ -5566,6 +5568,41 @@ void devlink_unregister(struct devlink *
 EXPORT_SYMBOL_GPL(devlink_unregister);
 
 /**
+ *	devlink_reload_enable - Enable reload of devlink instance
+ *
+ *	@devlink: devlink
+ *
+ *	Should be called at end of device initialization
+ *	process when reload operation is supported.
+ */
+void devlink_reload_enable(struct devlink *devlink)
+{
+	mutex_lock(&devlink_mutex);
+	devlink->reload_enabled = true;
+	mutex_unlock(&devlink_mutex);
+}
+EXPORT_SYMBOL_GPL(devlink_reload_enable);
+
+/**
+ *	devlink_reload_disable - Disable reload of devlink instance
+ *
+ *	@devlink: devlink
+ *
+ *	Should be called at the beginning of device cleanup
+ *	process when reload operation is supported.
+ */
+void devlink_reload_disable(struct devlink *devlink)
+{
+	mutex_lock(&devlink_mutex);
+	/* Mutex is taken which ensures that no reload operation is in
+	 * progress while setting up forbidded flag.
+	 */
+	devlink->reload_enabled = false;
+	mutex_unlock(&devlink_mutex);
+}
+EXPORT_SYMBOL_GPL(devlink_reload_disable);
+
+/**
  *	devlink_free - Free devlink instance resources
  *
  *	@devlink: devlink



  parent reply	other threads:[~2019-11-19  5:22 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-19  5:19 [PATCH 5.3 00/48] 5.3.12-stable review Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 01/48] scsi: core: Handle drivers which set sg_tablesize to zero Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 02/48] ax88172a: fix information leak on short answers Greg Kroah-Hartman
2019-11-19  5:19 ` Greg Kroah-Hartman [this message]
2019-11-19  5:19 ` [PATCH 5.3 04/48] ipmr: Fix skb headroom in ipmr_get_route() Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 05/48] mlxsw: core: Enable devlink reload only on probe Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 06/48] net: gemini: add missed free_netdev Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 07/48] net/smc: fix fastopen for non-blocking connect() Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 08/48] net: usb: qmi_wwan: add support for Foxconn T77W968 LTE modules Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 09/48] slip: Fix memory leak in slip_open error path Greg Kroah-Hartman
2019-11-19  7:43   ` Oliver Hartkopp
2019-11-19  7:55     ` Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 10/48] tcp: remove redundant new line from tcp_event_sk_skb Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 11/48] dpaa2-eth: free already allocated channels on probe defer Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 12/48] devlink: Add method for time-stamp on reporters dump Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 13/48] net/smc: fix refcount non-blocking connect() -part 2 Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 14/48] ALSA: usb-audio: Fix missing error check at mixer resolution test Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 15/48] ALSA: usb-audio: not submit urb for stopped endpoint Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 16/48] ALSA: usb-audio: Fix incorrect NULL check in create_yamaha_midi_quirk() Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 17/48] ALSA: usb-audio: Fix incorrect size check for processing/extension units Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 18/48] Btrfs: fix log context list corruption after rename exchange operation Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 19/48] cgroup: freezer: call cgroup_enter_frozen() with preemption disabled in ptrace_stop() Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 20/48] Input: ff-memless - kill timer in destroy() Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 21/48] Input: synaptics-rmi4 - fix video buffer size Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 22/48] Input: synaptics-rmi4 - disable the relative position IRQ in the F12 driver Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 23/48] Input: synaptics-rmi4 - do not consume more data than we have (F11, F12) Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 24/48] Input: synaptics-rmi4 - clear IRQ enables for F54 Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 25/48] Input: synaptics-rmi4 - destroy F54 poller workqueue when removing Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 26/48] KVM: MMU: Do not treat ZONE_DEVICE pages as being reserved Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 27/48] IB/hfi1: Ensure r_tid_ack is valid before building TID RDMA ACK packet Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 28/48] IB/hfi1: Calculate flow weight based on QP MTU for TID RDMA Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 29/48] IB/hfi1: TID RDMA WRITE should not return IB_WC_RNR_RETRY_EXC_ERR Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 30/48] IB/hfi1: Ensure full Gen3 speed in a Gen4 system Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 31/48] IB/hfi1: Use a common pad buffer for 9B and 16B packets Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 32/48] i2c: acpi: Force bus speed to 400KHz if a Silead touchscreen is present Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 33/48] x86/quirks: Disable HPET on Intel Coffe Lake platforms Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 34/48] ecryptfs_lookup_interpose(): lower_dentry->d_inode is not stable Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 35/48] ecryptfs_lookup_interpose(): lower_dentry->d_parent is not stable either Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 36/48] io_uring: ensure registered buffer import returns the IO length Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 37/48] drm/i915: update rawclk also on resume Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 38/48] Revert "drm/i915/ehl: Update MOCS table for EHL" Greg Kroah-Hartman
2019-11-19  5:19 ` [PATCH 5.3 39/48] ntp/y2038: Remove incorrect time_t truncation Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 40/48] net: ethernet: dwmac-sun8i: Use the correct function in exit path Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 41/48] iommu/vt-d: Fix QI_DEV_IOTLB_PFSID and QI_DEV_EIOTLB_PFSID macros Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 42/48] mm: mempolicy: fix the wrong return value and potential pages leak of mbind Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 43/48] mm: memcg: switch to css_tryget() in get_mem_cgroup_from_mm() Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 44/48] mm: hugetlb: switch to css_tryget() in hugetlb_cgroup_charge_cgroup() Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 45/48] mm: slub: really fix slab walking for init_on_free Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 46/48] mm/memory_hotplug: fix try_offline_node() Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 47/48] mm/page_io.c: do not free shared swap slots Greg Kroah-Hartman
2019-11-19  5:20 ` [PATCH 5.3 48/48] mmc: sdhci-of-at91: fix quirk2 overwrite Greg Kroah-Hartman
2019-11-19 10:16 ` [PATCH 5.3 00/48] 5.3.12-stable review Jon Hunter
2019-11-19 10:16   ` Jon Hunter
2019-11-19 12:32   ` Greg Kroah-Hartman
2019-11-19 11:42 ` kernelci.org bot
2019-11-19 12:35 ` Holger Hoffstätte
2019-11-19 12:45   ` Greg Kroah-Hartman
2019-11-19 13:05     ` Holger Hoffstätte
2019-11-19 13:17       ` Naresh Kamboju
2019-11-19 18:45 ` Dan Rue
2019-11-20  6:00   ` Greg Kroah-Hartman
2019-11-19 20:34 ` Guenter Roeck

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=20191119050949.815080213@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=davem@davemloft.net \
    --cc=idosch@mellanox.com \
    --cc=jiri@mellanox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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.