Netdev List
 help / color / mirror / Atom feed
From: Przemek Kitszel <przemyslaw.kitszel@intel.com>
To: netdev@vger.kernel.org, pabeni@redhat.com, willemb@google.com,
	donald.hunter@gmail.com, jiri@resnulli.us
Cc: intel-wired-lan@lists.osuosl.org, anthony.l.nguyen@intel.com,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	horms@kernel.org, andrew+netdev@lunn.ch,
	Petr Oros <poros@redhat.com>,
	Przemek Kitszel <przemyslaw.kitszel@intel.com>
Subject: [PATCH net 3/3] devlink: make room for many nested instances in a dev message
Date: Wed,  2 Dec 2026 13:48:03 +0100	[thread overview]
Message-ID: <20261202124956.18277-4-przemyslaw.kitszel@intel.com> (raw)
In-Reply-To: <20261202124956.18277-1-przemyslaw.kitszel@intel.com>

A DEVLINK_CMD_GET reply, and a DEVLINK_CMD_NEW/DEL notification, carry
the handle of every devlink instance nested in the described one. Each
handle takes about 40 bytes, 8 more with a netns id, so
NLMSG_DEFAULT_SIZE, below a page, fits only ~80 of them. Then the reply
fails with -EMSGSIZE, and the notification is dropped. A dump skb of
NLMSG_GOODSIZE has the same limit, and there it is silent: an instance
that does not fit in an empty skb ends the dump, so it and all the
instances after it are missing from the output.

ice nests the devlink of each subfunction in the devlink of its PF, and
their number is limited only by the HW resources. In a separate series,
I plan to nest there also the devlinks of up to 256 VFs.

Add the size of the nested handles to NLMSG_DEFAULT_SIZE for the reply
and the notification. For the dump, start with min_dump_alloc set to the
biggest needed size among the instances, and raise it when an instance
does not fit, so the next dump skb has room for it. Count the dump size
from 512 bytes, not from NLMSG_DEFAULT_SIZE, as the rest of the message
takes ~300 bytes: a dump skb over a page is truncated by readers with a
page-sized buffer, like iproute2 (ENOSPC). When an instance still does
not fit in an empty skb, e.g. as it got more nested instances during the
dump, fail the dump with -EMSGSIZE instead of ending it. Do that for all
devlink dumps.

Fixes: f43e3be662e6 ("ice: allocate devlink for subfunction")
Reported-by: Petr Oros <poros@redhat.com>
Closes: https://lore.kernel.org/netdev/20261126094321.40071-1-poros@redhat.com
Signed-off-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
---
 Documentation/netlink/specs/devlink.yaml |  1 +
 net/devlink/devl_internal.h              |  2 +
 net/devlink/netlink_gen.h                |  1 +
 net/devlink/core.c                       | 13 +++++++
 net/devlink/dev.c                        | 48 +++++++++++++++++++++---
 net/devlink/netlink.c                    | 14 ++++++-
 net/devlink/netlink_gen.c                |  1 +
 7 files changed, 73 insertions(+), 7 deletions(-)

diff --git a/Documentation/netlink/specs/devlink.yaml b/Documentation/netlink/specs/devlink.yaml
index 4706de628392..5ff809eb6d44 100644
--- a/Documentation/netlink/specs/devlink.yaml
+++ b/Documentation/netlink/specs/devlink.yaml
@@ -1486,6 +1486,7 @@ operations:
             - dev-stats
             - nested-devlink
       dump:
+        pre: devlink-nl-get-pre-dumpit
         reply: *get-reply
 
     -
diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h
index 798e707002b9..ba3798fee416 100644
--- a/net/devlink/devl_internal.h
+++ b/net/devlink/devl_internal.h
@@ -149,6 +149,7 @@ void devlink_rel_nested_in_notify(struct devlink *devlink);
 int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink,
 				   u32 rel_index, int attrtype,
 				   bool *msg_updated);
+size_t devlink_rel_devlink_handle_size(u32 rel_index);
 
 /* Netlink */
 struct devlink_nl_ctx {
@@ -231,6 +232,7 @@ static inline int devlink_nl_put_u64(struct sk_buff *msg, int attrtype, u64 val)
 
 int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net,
 				 struct devlink *devlink, int attrtype);
+size_t devlink_nl_nested_handle_size(struct devlink *devlink);
 int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info);
 
 static inline bool devlink_nl_notify_need(struct devlink *devlink)
diff --git a/net/devlink/netlink_gen.h b/net/devlink/netlink_gen.h
index 99ccacc693b7..9f0b1d653961 100644
--- a/net/devlink/netlink_gen.h
+++ b/net/devlink/netlink_gen.h
@@ -43,6 +43,7 @@ void
 devlink_nl_post_doit_parent_dev_optional(const struct genl_split_ops *ops,
 					 struct sk_buff *skb,
 					 struct genl_info *info);
+int devlink_nl_get_pre_dumpit(struct netlink_callback *cb);
 
 int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
diff --git a/net/devlink/core.c b/net/devlink/core.c
index 9b844e0c1bd2..b565db8ec793 100644
--- a/net/devlink/core.c
+++ b/net/devlink/core.c
@@ -252,6 +252,19 @@ int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink,
 	return err;
 }
 
+size_t devlink_rel_devlink_handle_size(u32 rel_index)
+{
+	struct devlink *rel_devlink;
+	size_t size;
+
+	rel_devlink = devlink_rel_devlink_get(rel_index);
+	if (!rel_devlink)
+		return 0;
+	size = devlink_nl_nested_handle_size(rel_devlink);
+	devlink_put(rel_devlink);
+	return size;
+}
+
 void *devlink_priv(struct devlink *devlink)
 {
 	return &devlink->priv;
diff --git a/net/devlink/dev.c b/net/devlink/dev.c
index b7e5aebf13be..07fad82d9e1c 100644
--- a/net/devlink/dev.c
+++ b/net/devlink/dev.c
@@ -156,6 +156,21 @@ static int devlink_nl_nested_fill(struct sk_buff *msg, struct devlink *devlink)
 	return 0;
 }
 
+static size_t devlink_nl_dev_msg_size(struct devlink *devlink, size_t size)
+{
+	unsigned long rel_index;
+	void *unused;
+
+	xa_for_each(&devlink->nested_rels, rel_index, unused)
+		size += devlink_rel_devlink_handle_size(rel_index);
+	return size;
+}
+
+static size_t devlink_nl_dev_dump_size(struct devlink *devlink)
+{
+	return devlink_nl_dev_msg_size(devlink, 512);
+}
+
 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
 			   enum devlink_command cmd, u32 portid,
 			   u32 seq, int flags)
@@ -207,7 +222,8 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
 	if (!devlink_nl_notify_need(devlink))
 		return;
 
-	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+	msg = nlmsg_new(devlink_nl_dev_msg_size(devlink, NLMSG_DEFAULT_SIZE),
+			GFP_KERNEL);
 	if (!msg)
 		return;
 
@@ -226,7 +242,8 @@ int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
 	struct sk_buff *msg;
 	int err;
 
-	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+	msg = nlmsg_new(devlink_nl_dev_msg_size(devlink, NLMSG_DEFAULT_SIZE),
+			GFP_KERNEL);
 	if (!msg)
 		return -ENOMEM;
 
@@ -240,13 +257,34 @@ int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
 	return genlmsg_reply(msg, info);
 }
 
+int devlink_nl_get_pre_dumpit(struct netlink_callback *cb)
+{
+	struct net *net = sock_net(cb->skb->sk);
+	struct devlink *devlink;
+	unsigned long index;
+
+	/* no instance lock, the size is only a hint for the first dump skb */
+	devlinks_xa_for_each_registered_get(net, index, devlink) {
+		cb->min_dump_alloc = max(cb->min_dump_alloc,
+					 devlink_nl_dev_dump_size(devlink));
+		devlink_put(devlink);
+	}
+	return 0;
+}
+
 static int
 devlink_nl_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
 			struct netlink_callback *cb, int flags)
 {
-	return devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
-			       NETLINK_CB(cb->skb).portid,
-			       cb->nlh->nlmsg_seq, flags);
+	int err;
+
+	err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
+			      NETLINK_CB(cb->skb).portid,
+			      cb->nlh->nlmsg_seq, flags);
+	if (err == -EMSGSIZE)
+		cb->min_dump_alloc = max(cb->min_dump_alloc,
+					 devlink_nl_dev_dump_size(devlink));
+	return err;
 }
 
 int devlink_nl_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
diff --git a/net/devlink/netlink.c b/net/devlink/netlink.c
index a8eda727bff1..57d2ee4d73d0 100644
--- a/net/devlink/netlink.c
+++ b/net/devlink/netlink.c
@@ -172,6 +172,16 @@ int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net,
 	return -EMSGSIZE;
 }
 
+/* upper bound of what devlink_nl_put_nested_handle() puts */
+size_t devlink_nl_nested_handle_size(struct devlink *devlink)
+{
+	return nla_total_size(0) +
+	       nla_total_size(strlen(devlink_bus_name(devlink)) + 1) +
+	       nla_total_size(strlen(devlink_dev_name(devlink)) + 1) +
+	       nla_total_size(sizeof(u64)) +	/* DEVLINK_ATTR_INDEX */
+	       nla_total_size(sizeof(s32));	/* DEVLINK_ATTR_NETNS_ID */
+}
+
 int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info)
 {
 	int err;
@@ -402,7 +412,7 @@ static int devlink_nl_inst_single_dumpit(struct sk_buff *msg,
 	devl_unlock(devlink);
 	devlink_put(devlink);
 
-	if (err != -EMSGSIZE)
+	if (err != -EMSGSIZE || !msg->len)
 		return err;
 	return msg->len;
 }
@@ -438,7 +448,7 @@ static int devlink_nl_inst_iter_dumpit(struct sk_buff *msg,
 		state->port_ctx.index_valid = false;
 	}
 
-	if (err != -EMSGSIZE)
+	if (err != -EMSGSIZE || !msg->len)
 		return err;
 	return msg->len;
 }
diff --git a/net/devlink/netlink_gen.c b/net/devlink/netlink_gen.c
index 9e1b4c081a7b..296d6d6e4111 100644
--- a/net/devlink/netlink_gen.c
+++ b/net/devlink/netlink_gen.c
@@ -714,6 +714,7 @@ const struct genl_split_ops devlink_nl_ops[75] = {
 	{
 		.cmd		= DEVLINK_CMD_GET,
 		.validate	= GENL_DONT_VALIDATE_DUMP,
+		.start		= devlink_nl_get_pre_dumpit,
 		.dumpit		= devlink_nl_get_dumpit,
 		.flags		= GENL_CMD_CAP_DUMP,
 	},
-- 
2.51.1


  parent reply	other threads:[~2026-12-02 12:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:48 [PATCH net 0/3] ice, devlink: fixes needed for XLVF Przemek Kitszel
2026-12-02 12:48 ` [PATCH net 1/3] ice: move only the valid VF queues on LAG reclaim and reset rebuild Przemek Kitszel
2026-12-02 12:48 ` [PATCH net 2/3] ice: lock VF config while changing its MSI-X vector count Przemek Kitszel
2026-12-02 12:48 ` Przemek Kitszel [this message]
2026-12-02 12:54 ` [PATCH net 0/3] ice, devlink: fixes needed for XLVF netdev-bot+sinfo

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=20261202124956.18277-4-przemyslaw.kitszel@intel.com \
    --to=przemyslaw.kitszel@intel.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=anthony.l.nguyen@intel.com \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=intel-wired-lan@lists.osuosl.org \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=poros@redhat.com \
    --cc=willemb@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox