Skip to content

Commit 1210243

Browse files
jacob-kellerdavem330
authored andcommitted
devlink: track snapshot id usage count using an xarray
Each snapshot created for a devlink region must have an id. These ids are supposed to be unique per "event" that caused the snapshot to be created. Drivers call devlink_region_snapshot_id_get to obtain a new id to use for a new event trigger. The id values are tracked per devlink, so that the same id number can be used if a triggering event creates multiple snapshots on different regions. There is no mechanism for snapshot ids to ever be reused. Introduce an xarray to store the count of how many snapshots are using a given id, replacing the snapshot_id field previously used for picking the next id. The devlink_region_snapshot_id_get() function will use xa_alloc to insert an initial value of 1 value at an available slot between 0 and U32_MAX. The new __devlink_snapshot_id_increment() and __devlink_snapshot_id_decrement() functions will be used to track how many snapshots currently use an id. Drivers must now call devlink_snapshot_id_put() in order to release their reference of the snapshot id after adding region snapshots. By tracking the total number of snapshots using a given id, it is possible for the decrement() function to erase the id from the xarray when it is not in use. With this method, a snapshot id can become reused again once all snapshots that referred to it have been deleted via DEVLINK_CMD_REGION_DEL, and the driver has finished adding snapshots. This work also paves the way to introduce a mechanism for userspace to request a snapshot. Signed-off-by: Jacob Keller <jacob.e.keller@intel.com> Reviewed-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7ef19d3 commit 1210243

4 files changed

Lines changed: 135 additions & 8 deletions

File tree

‎drivers/net/ethernet/mellanox/mlx4/crdump.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ int mlx4_crdump_collect(struct mlx4_dev *dev)
203203
mlx4_crdump_collect_crspace(dev, cr_space, id);
204204
mlx4_crdump_collect_fw_health(dev, cr_space, id);
205205

206+
/* Release reference on the snapshot id */
207+
devlink_region_snapshot_id_put(devlink, id);
208+
206209
crdump_disable_crspace_access(dev, cr_space);
207210

208211
iounmap(cr_space);

‎drivers/net/netdevsim/dev.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,27 @@ static ssize_t nsim_dev_take_snapshot_write(struct file *file,
4444
size_t count, loff_t *ppos)
4545
{
4646
struct nsim_dev *nsim_dev = file->private_data;
47+
struct devlink *devlink;
4748
void *dummy_data;
4849
int err;
4950
u32 id;
5051

52+
devlink = priv_to_devlink(nsim_dev);
53+
5154
dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL);
5255
if (!dummy_data)
5356
return -ENOMEM;
5457

5558
get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
5659

57-
err = devlink_region_snapshot_id_get(priv_to_devlink(nsim_dev), &id);
60+
err = devlink_region_snapshot_id_get(devlink, &id);
5861
if (err) {
5962
pr_err("Failed to get snapshot id\n");
6063
return err;
6164
}
6265
err = devlink_region_snapshot_create(nsim_dev->dummy_region,
6366
dummy_data, id);
67+
devlink_region_snapshot_id_put(devlink, id);
6468
if (err) {
6569
pr_err("Failed to create region snapshot\n");
6670
kfree(dummy_data);

‎include/net/devlink.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <net/net_namespace.h>
1919
#include <net/flow_offload.h>
2020
#include <uapi/linux/devlink.h>
21+
#include <linux/xarray.h>
2122

2223
struct devlink_ops;
2324

@@ -29,13 +30,13 @@ struct devlink {
2930
struct list_head resource_list;
3031
struct list_head param_list;
3132
struct list_head region_list;
32-
u32 snapshot_id;
3333
struct list_head reporter_list;
3434
struct mutex reporters_lock; /* protects reporter_list */
3535
struct devlink_dpipe_headers *dpipe_headers;
3636
struct list_head trap_list;
3737
struct list_head trap_group_list;
3838
const struct devlink_ops *ops;
39+
struct xarray snapshot_ids;
3940
struct device *dev;
4041
possible_net_t _net;
4142
struct mutex lock;
@@ -977,6 +978,7 @@ devlink_region_create(struct devlink *devlink,
977978
u32 region_max_snapshots, u64 region_size);
978979
void devlink_region_destroy(struct devlink_region *region);
979980
int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
981+
void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
980982
int devlink_region_snapshot_create(struct devlink_region *region,
981983
u8 *data, u32 snapshot_id);
982984
int devlink_info_serial_number_put(struct devlink_info_req *req,

‎net/core/devlink.c‎

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,6 +3768,83 @@ static void devlink_nl_region_notify(struct devlink_region *region,
37683768
nlmsg_free(msg);
37693769
}
37703770

3771+
/**
3772+
* __devlink_snapshot_id_increment - Increment number of snapshots using an id
3773+
* @devlink: devlink instance
3774+
* @id: the snapshot id
3775+
*
3776+
* Track when a new snapshot begins using an id. Load the count for the
3777+
* given id from the snapshot xarray, increment it, and store it back.
3778+
*
3779+
* Called when a new snapshot is created with the given id.
3780+
*
3781+
* The id *must* have been previously allocated by
3782+
* devlink_region_snapshot_id_get().
3783+
*
3784+
* Returns 0 on success, or an error on failure.
3785+
*/
3786+
static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
3787+
{
3788+
unsigned long count;
3789+
void *p;
3790+
3791+
lockdep_assert_held(&devlink->lock);
3792+
3793+
p = xa_load(&devlink->snapshot_ids, id);
3794+
if (WARN_ON(!p))
3795+
return -EINVAL;
3796+
3797+
if (WARN_ON(!xa_is_value(p)))
3798+
return -EINVAL;
3799+
3800+
count = xa_to_value(p);
3801+
count++;
3802+
3803+
return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
3804+
GFP_KERNEL));
3805+
}
3806+
3807+
/**
3808+
* __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
3809+
* @devlink: devlink instance
3810+
* @id: the snapshot id
3811+
*
3812+
* Track when a snapshot is deleted and stops using an id. Load the count
3813+
* for the given id from the snapshot xarray, decrement it, and store it
3814+
* back.
3815+
*
3816+
* If the count reaches zero, erase this id from the xarray, freeing it
3817+
* up for future re-use by devlink_region_snapshot_id_get().
3818+
*
3819+
* Called when a snapshot using the given id is deleted, and when the
3820+
* initial allocator of the id is finished using it.
3821+
*/
3822+
static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
3823+
{
3824+
unsigned long count;
3825+
void *p;
3826+
3827+
lockdep_assert_held(&devlink->lock);
3828+
3829+
p = xa_load(&devlink->snapshot_ids, id);
3830+
if (WARN_ON(!p))
3831+
return;
3832+
3833+
if (WARN_ON(!xa_is_value(p)))
3834+
return;
3835+
3836+
count = xa_to_value(p);
3837+
3838+
if (count > 1) {
3839+
count--;
3840+
xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
3841+
GFP_KERNEL);
3842+
} else {
3843+
/* If this was the last user, we can erase this id */
3844+
xa_erase(&devlink->snapshot_ids, id);
3845+
}
3846+
}
3847+
37713848
/**
37723849
* __devlink_region_snapshot_id_get - get snapshot ID
37733850
* @devlink: devlink instance
@@ -3776,17 +3853,20 @@ static void devlink_nl_region_notify(struct devlink_region *region,
37763853
* Allocates a new snapshot id. Returns zero on success, or a negative
37773854
* error on failure. Must be called while holding the devlink instance
37783855
* lock.
3856+
*
3857+
* Snapshot IDs are tracked using an xarray which stores the number of
3858+
* users of the snapshot id.
3859+
*
3860+
* Note that the caller of this function counts as a 'user', in order to
3861+
* avoid race conditions. The caller must release its hold on the
3862+
* snapshot by using devlink_region_snapshot_id_put.
37793863
*/
37803864
static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
37813865
{
37823866
lockdep_assert_held(&devlink->lock);
37833867

3784-
if (devlink->snapshot_id >= U32_MAX)
3785-
return -ENOSPC;
3786-
3787-
*id = ++devlink->snapshot_id;
3788-
3789-
return 0;
3868+
return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
3869+
xa_limit_32b, GFP_KERNEL);
37903870
}
37913871

37923872
/**
@@ -3809,6 +3889,7 @@ __devlink_region_snapshot_create(struct devlink_region *region,
38093889
{
38103890
struct devlink *devlink = region->devlink;
38113891
struct devlink_snapshot *snapshot;
3892+
int err;
38123893

38133894
lockdep_assert_held(&devlink->lock);
38143895

@@ -3823,6 +3904,10 @@ __devlink_region_snapshot_create(struct devlink_region *region,
38233904
if (!snapshot)
38243905
return -ENOMEM;
38253906

3907+
err = __devlink_snapshot_id_increment(devlink, snapshot_id);
3908+
if (err)
3909+
goto err_snapshot_id_increment;
3910+
38263911
snapshot->id = snapshot_id;
38273912
snapshot->region = region;
38283913
snapshot->data = data;
@@ -3833,15 +3918,24 @@ __devlink_region_snapshot_create(struct devlink_region *region,
38333918

38343919
devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
38353920
return 0;
3921+
3922+
err_snapshot_id_increment:
3923+
kfree(snapshot);
3924+
return err;
38363925
}
38373926

38383927
static void devlink_region_snapshot_del(struct devlink_region *region,
38393928
struct devlink_snapshot *snapshot)
38403929
{
3930+
struct devlink *devlink = region->devlink;
3931+
3932+
lockdep_assert_held(&devlink->lock);
3933+
38413934
devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
38423935
region->cur_snapshots--;
38433936
list_del(&snapshot->list);
38443937
region->ops->destructor(snapshot->data);
3938+
__devlink_snapshot_id_decrement(devlink, snapshot->id);
38453939
kfree(snapshot);
38463940
}
38473941

@@ -6495,6 +6589,7 @@ struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
64956589
if (!devlink)
64966590
return NULL;
64976591
devlink->ops = ops;
6592+
xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
64986593
__devlink_net_set(devlink, &init_net);
64996594
INIT_LIST_HEAD(&devlink->port_list);
65006595
INIT_LIST_HEAD(&devlink->sb_list);
@@ -6599,6 +6694,8 @@ void devlink_free(struct devlink *devlink)
65996694
WARN_ON(!list_empty(&devlink->sb_list));
66006695
WARN_ON(!list_empty(&devlink->port_list));
66016696

6697+
xa_destroy(&devlink->snapshot_ids);
6698+
66026699
kfree(devlink);
66036700
}
66046701
EXPORT_SYMBOL_GPL(devlink_free);
@@ -7790,6 +7887,9 @@ EXPORT_SYMBOL_GPL(devlink_region_destroy);
77907887
* Driver should use the same id for multiple snapshots taken
77917888
* on multiple regions at the same time/by the same trigger.
77927889
*
7890+
* The caller of this function must use devlink_region_snapshot_id_put
7891+
* when finished creating regions using this id.
7892+
*
77937893
* Returns zero on success, or a negative error code on failure.
77947894
*
77957895
* @devlink: devlink
@@ -7807,6 +7907,24 @@ int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
78077907
}
78087908
EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
78097909

7910+
/**
7911+
* devlink_region_snapshot_id_put - put snapshot ID reference
7912+
*
7913+
* This should be called by a driver after finishing creating snapshots
7914+
* with an id. Doing so ensures that the ID can later be released in the
7915+
* event that all snapshots using it have been destroyed.
7916+
*
7917+
* @devlink: devlink
7918+
* @id: id to release reference on
7919+
*/
7920+
void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
7921+
{
7922+
mutex_lock(&devlink->lock);
7923+
__devlink_snapshot_id_decrement(devlink, id);
7924+
mutex_unlock(&devlink->lock);
7925+
}
7926+
EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
7927+
78107928
/**
78117929
* devlink_region_snapshot_create - create a new snapshot
78127930
* This will add a new snapshot of a region. The snapshot

0 commit comments

Comments
 (0)