[RFC PATCH v2 20/22] devlink: refactor region_read_snapshot_fill to use a callback function

Jacob Keller
2020-02-14T23:22:53Z

The devlink_nl_region_read_snapshot_fill is used to copy the contents of
a snapshot into a message for reporting to userspace via the
DEVLINK_CMG_REGION_READ netlink message.

A future change is going to add support for directly reading from
a region. Almost all of the logic for this new capability is identical.

To help reduce code duplication and make this logic more generic,
refactor the function to take a cb and cb_priv pointer for doing the
actual copy.

Add a devlink_region_snapshot_fill implementation that will simply copy
the relevant chunk of the region. This does require allocating some
storage for the chunk as opposed to simply passing the correct address
forward to the devlink_nl_cmg_region_read_chunk_fill function.

A future change to implement support for directly reading from a region
without a snapshot will provide a separate implementation that calls the
newly added devlink region operation.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
---
net/core/devlink.c | 43 ++++++++++++++++++++++++++++++++++---------
1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/net/core/devlink.c b/net/core/devlink.c
index a722272f42b4..c200701e1839 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -4140,24 +4140,34 @@ static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,

#define DEVLINK_REGION_READ_CHUNK_SIZE 256

+typedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size,
+ u64 curr_offset,
+ struct netlink_ext_ack *extack);
+
static int
-devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
- struct devlink_snapshot *snapshot,
- u64 start_offset,
- u64 end_offset,
- u64 *new_offset)
+devlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb,
+ void *cb_priv, u64 start_offset, u64 end_offset,
+ u64 *new_offset, struct netlink_ext_ack *extack)
{
u64 curr_offset = start_offset;
int err = 0;
+ u8 *data;
+
+ /* Allocate and re-use a single buffer */
+ data = kzalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;

*new_offset = start_offset;

while (curr_offset < end_offset) {
u32 data_size = min_t(u32, end_offset - curr_offset,
DEVLINK_REGION_READ_CHUNK_SIZE);
- u8 *data;

- data = &snapshot->data[curr_offset];
+ err = cb(cb_priv, data, data_size, curr_offset, extack);
+ if (err)
+ break;
+
err = devlink_nl_cmd_region_read_chunk_fill(skb, data,
data_size,
curr_offset);
@@ -4168,9 +4178,23 @@ devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
}
*new_offset = curr_offset;

+ kfree(data);
+
return err;
}

+static int
+devlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
+ u64 curr_offset,
+ struct __always_unused netlink_ext_ack *extack)
+{
+ struct devlink_snapshot *snapshot = cb_priv;
+
+ memcpy(chunk, &snapshot->data[curr_offset], chunk_size);
+
+ return 0;
+}
+
static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
struct netlink_callback *cb)
{
@@ -4270,8 +4294,9 @@ static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
end_offset = region->size;
}

- err = devlink_nl_region_read_snapshot_fill(skb, snapshot, start_offset,
- end_offset, &ret_offset);
+ err = devlink_nl_region_read_fill(skb, &devlink_region_snapshot_fill,
+ snapshot, start_offset, end_offset,
+ &ret_offset, cb->extack);

if (err && err != -EMSGSIZE)
goto nla_put_failure;
--
2.25.0.368.g28a2d05eebfb