All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jacob Keller <jacob.e.keller@intel.com>
To: netdev@vger.kernel.org
Cc: Jakub Kicinski <kuba@kernel.org>, Jiri Pirko <jiri@resnulli.us>,
	Jacob Keller <jacob.e.keller@intel.com>,
	Jiri Pirko <jiri@mellanox.com>
Subject: [PATCH net-next v3 07/11] devlink: report error once U32_MAX snapshot ids have been used
Date: Thu, 26 Mar 2020 11:37:14 -0700	[thread overview]
Message-ID: <20200326183718.2384349-8-jacob.e.keller@intel.com> (raw)
In-Reply-To: <20200326183718.2384349-1-jacob.e.keller@intel.com>

The devlink_snapshot_id_get() function returns a snapshot id. The
snapshot id is a u32, so there is no way to indicate an error code.

A future change is going to possibly add additional cases where this
function could fail. Refactor the function to return the snapshot id in
an argument, so that it can return zero or an error value.

This ensures that snapshot ids cannot be confused with error values, and
aids in the future refactor of snapshot id allocation management.

Because there is no current way to release previously used snapshot ids,
add a simple check ensuring that an error is reported in case the
snapshot_id would over flow.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Reviewed-by: Jiri Pirko <jiri@mellanox.com>
---
Changes since RFC:
* Picked up Jiri's Reviewed-by

Changes since v1:
* Refactored this patch to return the id in an argument, instead of
  conflating id and error in the return value of the function.
* Allow up to U32_MAX instead of INT_MAX
* Removed Jiri's Reviewed-by, since this is a complete re-write

Changes since v2:
* Fix commit message to avoid incorrect statement about checking snapshot
  ids. This referred to a previous version of the code which has been
  removed.
* Fixed a build failure due to forgetting a '{' bracket which got lost in a
  rebase conflict.
* Picked up Jiri's Reviewed-by again

 drivers/net/ethernet/mellanox/mlx4/crdump.c | 11 ++++++---
 drivers/net/netdevsim/dev.c                 |  6 ++++-
 include/net/devlink.h                       |  2 +-
 net/core/devlink.c                          | 27 +++++++++++++++------
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx4/crdump.c b/drivers/net/ethernet/mellanox/mlx4/crdump.c
index c3f90c0f9554..792951f6df0d 100644
--- a/drivers/net/ethernet/mellanox/mlx4/crdump.c
+++ b/drivers/net/ethernet/mellanox/mlx4/crdump.c
@@ -169,6 +169,7 @@ int mlx4_crdump_collect(struct mlx4_dev *dev)
 	struct pci_dev *pdev = dev->persist->pdev;
 	unsigned long cr_res_size;
 	u8 __iomem *cr_space;
+	int err;
 	u32 id;
 
 	if (!dev->caps.health_buffer_addrs) {
@@ -189,10 +190,14 @@ int mlx4_crdump_collect(struct mlx4_dev *dev)
 		return -ENODEV;
 	}
 
-	crdump_enable_crspace_access(dev, cr_space);
-
 	/* Get the available snapshot ID for the dumps */
-	id = devlink_region_snapshot_id_get(devlink);
+	err = devlink_region_snapshot_id_get(devlink, &id);
+	if (err) {
+		mlx4_err(dev, "crdump: devlink get snapshot id err %d\n", err);
+		return err;
+	}
+
+	crdump_enable_crspace_access(dev, cr_space);
 
 	/* Try to capture dumps */
 	mlx4_crdump_collect_crspace(dev, cr_space, id);
diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c
index f7621ccb7b88..609005f2ac85 100644
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -54,7 +54,11 @@ static ssize_t nsim_dev_take_snapshot_write(struct file *file,
 
 	get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
 
-	id = devlink_region_snapshot_id_get(priv_to_devlink(nsim_dev));
+	err = devlink_region_snapshot_id_get(priv_to_devlink(nsim_dev), &id);
+	if (err) {
+		pr_err("Failed to get snapshot id\n");
+		return err;
+	}
 	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
 					     dummy_data, id);
 	if (err) {
diff --git a/include/net/devlink.h b/include/net/devlink.h
index 8869ad75b965..9a46bc7fed90 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -976,7 +976,7 @@ devlink_region_create(struct devlink *devlink,
 		      const struct devlink_region_ops *ops,
 		      u32 region_max_snapshots, u64 region_size);
 void devlink_region_destroy(struct devlink_region *region);
-u32 devlink_region_snapshot_id_get(struct devlink *devlink);
+int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
 int devlink_region_snapshot_create(struct devlink_region *region,
 				   u8 *data, u32 snapshot_id);
 int devlink_info_serial_number_put(struct devlink_info_req *req,
diff --git a/net/core/devlink.c b/net/core/devlink.c
index 457b8303ae59..77341c65868f 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -3771,14 +3771,22 @@ static void devlink_nl_region_notify(struct devlink_region *region,
 /**
  *	__devlink_region_snapshot_id_get - get snapshot ID
  *	@devlink: devlink instance
+ *	@id: storage to return snapshot id
  *
- *	Returns a new snapshot id. Must be called while holding the
- *	devlink instance lock.
+ *	Allocates a new snapshot id. Returns zero on success, or a negative
+ *	error on failure. Must be called while holding the devlink instance
+ *	lock.
  */
-static u32 __devlink_region_snapshot_id_get(struct devlink *devlink)
+static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
 {
 	lockdep_assert_held(&devlink->lock);
-	return ++devlink->snapshot_id;
+
+	if (devlink->snapshot_id >= U32_MAX)
+		return -ENOSPC;
+
+	*id = ++devlink->snapshot_id;
+
+	return 0;
 }
 
 /**
@@ -7781,17 +7789,20 @@ EXPORT_SYMBOL_GPL(devlink_region_destroy);
  *	Driver should use the same id for multiple snapshots taken
  *	on multiple regions at the same time/by the same trigger.
  *
+ *	Returns zero on success, or a negative error code on failure.
+ *
  *	@devlink: devlink
+ *	@id: storage to return id
  */
-u32 devlink_region_snapshot_id_get(struct devlink *devlink)
+int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
 {
-	u32 id;
+	int err;
 
 	mutex_lock(&devlink->lock);
-	id = __devlink_region_snapshot_id_get(devlink);
+	err = __devlink_region_snapshot_id_get(devlink, id);
 	mutex_unlock(&devlink->lock);
 
-	return id;
+	return err;
 }
 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
 
-- 
2.24.1


  parent reply	other threads:[~2020-03-26 18:37 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-26 18:37 [PATCH net-next v3 00/11] implement DEVLINK_CMD_REGION_NEW Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 01/11] devlink: prepare to support region operations Jacob Keller
2020-03-27  8:16   ` tanhuazhong
2020-03-27 19:59     ` Jacob Keller
2020-03-27 20:58     ` Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 02/11] devlink: convert snapshot destructor callback to region op Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 03/11] devlink: trivial: fix tab in function documentation Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 04/11] devlink: add function to take snapshot while locked Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 05/11] devlink: use -ENOSPC to indicate no more room for snapshots Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 06/11] devlink: extract snapshot id allocation to helper function Jacob Keller
2020-03-26 21:15   ` Jiri Pirko
2020-03-26 21:37     ` Jacob Keller
2020-03-26 18:37 ` Jacob Keller [this message]
2020-03-26 18:37 ` [PATCH net-next v3 08/11] devlink: track snapshot id usage count using an xarray Jacob Keller
2020-03-26 21:16   ` Jiri Pirko
2020-03-26 18:37 ` [PATCH net-next v3 09/11] devlink: implement DEVLINK_CMD_REGION_NEW Jacob Keller
2020-03-26 18:37 ` [PATCH net-next v3 10/11] netdevsim: support taking immediate snapshot via devlink Jacob Keller
2020-03-26 21:18   ` Jiri Pirko
2020-03-26 18:37 ` [PATCH net-next v3 11/11] ice: add a devlink region for dumping NVM contents Jacob Keller
2020-03-26 21:19   ` Jiri Pirko
2020-03-26 21:38     ` Jacob Keller
2020-03-26 22:35     ` Jacob Keller
2020-03-27  2:37       ` David Miller
2020-03-27 19:56         ` Jacob Keller
2020-03-27  2:39 ` [PATCH net-next v3 00/11] implement DEVLINK_CMD_REGION_NEW David Miller
2020-04-28  0:26 ` Jakub Kicinski
2020-04-28  2:03   ` Keller, Jacob E

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=20200326183718.2384349-8-jacob.e.keller@intel.com \
    --to=jacob.e.keller@intel.com \
    --cc=jiri@mellanox.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=netdev@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.