From patchwork Fri Mar 1 01:28:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13577819 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3B61381B9 for ; Fri, 1 Mar 2024 01:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256693; cv=none; b=lkY/QYB49UtOA4lw5BTEF42DAl12Gwtlwi7FEftW6R+R+Skp6rqHGY4kAlkKcMqHi2dR9H/GhL4ne2+kuUz1jHfizk3FUkAzKhHnWZhwmPtJWFmZprqWgWPEmvrNFtc8VY4Yj1l+8J5D/AyAd7/HX4NqbDJYGqapWPJwrDSeB8c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256693; c=relaxed/simple; bh=bW/QVUMBf2pmWVa3nHFpfrvYovxTopYIJ3df/9Vv1Xo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ubjdhmSVOniOusdgfQSl7/OqzJlo9GcDJsNqHa88X7Fxb8vUCHLXVTxPp1k1NAVzgRsp78HxZ/p558cguvFqKUXk7YXs4ifA7NOvmbMXgILQ72hsQXudoZbFF8yqaRMj8aobfzJ4kjMJVVkmjzqp+LCAL6My5ykw67SkupI3egM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SMXSe3Fe; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SMXSe3Fe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6ACDAC433A6; Fri, 1 Mar 2024 01:31:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709256691; bh=bW/QVUMBf2pmWVa3nHFpfrvYovxTopYIJ3df/9Vv1Xo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SMXSe3Fe/d3PRjOxPSG8OpJrB+pS98hXONtDIpBzjbQ1ZulmtpkpzvIP0nfE13Ur7 56uQOjUpp2hBQakbt9n63yE6OELiU0+uIpOBq5BasCC9Bx63khgy+e+LiBH0EdnoQk X50SAZ326Tc6D4QplY2NKLJ71ovBT5d0hiu/DlzgdAlDFMyMU18nE4BrDfjB3Wqma8 npLYEi8giCwfVMvybLY9fFPg18eVSqp2sHXRRLfuO9EsFVO74DSCjrepG11qirkUIz cTCO9GNwjUk5JXyAsEvjorwB19ICzy9jTVc6dw8Q+6oI5pEoAwdZg7MPfpoJ/ila6d 9ObO+oUgoiYzA== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, johannes@sipsolutions.net, fw@strlen.de, pablo@netfilter.org, idosch@nvidia.com, jiri@resnulli.us, Jakub Kicinski , kuniyu@amazon.com Subject: [PATCH net-next 1/3] netlink: handle EMSGSIZE errors in the core Date: Thu, 29 Feb 2024 17:28:43 -0800 Message-ID: <20240301012845.2951053-2-kuba@kernel.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240301012845.2951053-1-kuba@kernel.org> References: <20240301012845.2951053-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Eric points out that our current suggested way of handling EMSGSIZE errors ((err == -EMSGSIZE) ? skb->len : err) will break if we didn't fit even a single object into the buffer provided by the user. This should not happen for well behaved applications, but we can fix that, and free netlink families from dealing with that completely by moving error handling into the core. Let's assume from now on that all EMSGSIZE errors in dumps are because we run out of skb space. Families can now propagate the error nla_put_*() etc generated and not worry about any return value magic. If some family really wants to send EMSGSIZE to user space, assuming it generates the same error on the next dump iteration the skb->len should be 0, and user space should still see the EMSGSIZE. This should simplify families and prevent mistakes in return values which lead to DONE being forced into a separate recv() call as discovered by Ido some time ago. Signed-off-by: Jakub Kicinski Reviewed-by: Eric Dumazet --- CC: kuniyu@amazon.com --- net/netlink/af_netlink.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index ad7b645e3ae7..da846212fb9b 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -2267,6 +2267,15 @@ static int netlink_dump(struct sock *sk, bool lock_taken) if (extra_mutex) mutex_unlock(extra_mutex); + /* EMSGSIZE plus something already in the skb means + * that there's more to dump but current skb has filled up. + * If the callback really wants to return EMSGSIZE to user space + * it needs to do so again, on the next cb->dump() call, + * without putting data in the skb. + */ + if (nlk->dump_done_errno == -EMSGSIZE && skb->len) + nlk->dump_done_errno = skb->len; + cb->extack = NULL; } From patchwork Fri Mar 1 01:28:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13577820 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BCAD38385 for ; Fri, 1 Mar 2024 01:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256693; cv=none; b=hi5PSM6Df3HnvMcAx809aIZLr39TQ7GbeprUoiN9yBZC8ZbwdfpB3t1tJqTjQHpfYwOWcozYlhhojjnJLK+g5hMQFAabxx0Nq7bY8v/KuOaqp4uAixXYa6WMQkBz9Z1FR93vqAoSmNctRylq6vacwwPFOL4pEa6CiD/C65rNy4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256693; c=relaxed/simple; bh=ZE8kCGROixQFjmDHzXFrlZmJHoUUpZVn2UngoaJrBPU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=goVojvExNEOrntlOP9tVTQJ84QuDJqlXPgl+eVT0gPwlGFcF8QSGtkNLPaXURSzqasVmJ1tQDglEvki5RW1Bn3I79rJLSxVh8c89LBfJA1BnisRxZGckFpTZ7rbP7CFesxIahxfORK7YttXfk7kEd1XjZc9JUZYSU7Mvi5m438c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jG6U2EVu; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jG6U2EVu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 247F0C433F1; Fri, 1 Mar 2024 01:31:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709256692; bh=ZE8kCGROixQFjmDHzXFrlZmJHoUUpZVn2UngoaJrBPU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jG6U2EVutgrtvB4HHcYy8bB8Uq/TjVjCAE5Zk7hJeDMKT5tUWF7L6bfhnCF2QgsBT naae5FeG7Br1YHXRgLbAkWqEzOry4yfdHdau/Qyy5RnlSdhQm2gEeK7y01MnFv3QLo o6vPkxxpGhq0TVWigsrfazJpk0hp6blvVCkCwMrjiSUpyxcVDCfc0ijrpj18KV3pmi Wg0zomNodEgVn1eTS0RMTg/dj+/HnaxQs8JDIk84cVLGUElX9cB+psPC2YXpL3KLnT hXAHou+bGpSzWyVHGLx1g8rRAyQxyHQpGfna3ywRearvw0O9OoheyOF/O1MWWV30q0 0IHp3VNmLFebQ== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, johannes@sipsolutions.net, fw@strlen.de, pablo@netfilter.org, idosch@nvidia.com, jiri@resnulli.us, Jakub Kicinski , amritha.nambiar@intel.com, sridhar.samudrala@intel.com, hawk@kernel.org Subject: [PATCH net-next 2/3] netdev: let netlink core handle -EMSGSIZE errors Date: Thu, 29 Feb 2024 17:28:44 -0800 Message-ID: <20240301012845.2951053-3-kuba@kernel.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240301012845.2951053-1-kuba@kernel.org> References: <20240301012845.2951053-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Previous change added -EMSGSIZE handling to af_netlink, we don't have to hide these errors any longer. Theoretically the error handling changes from: if (err == -EMSGSIZE) to if (err == -EMSGSIZE && skb->len) everywhere, but in practice it doesn't matter. All messages fit into NLMSG_GOODSIZE, so overflow of an empty skb cannot happen. Signed-off-by: Jakub Kicinski Reviewed-by: Eric Dumazet --- CC: amritha.nambiar@intel.com CC: sridhar.samudrala@intel.com CC: hawk@kernel.org --- net/core/netdev-genl.c | 15 +++------------ net/core/page_pool_user.c | 2 -- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c index fd98936da3ae..918b109e0cf4 100644 --- a/net/core/netdev-genl.c +++ b/net/core/netdev-genl.c @@ -152,10 +152,7 @@ int netdev_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) } rtnl_unlock(); - if (err != -EMSGSIZE) - return err; - - return skb->len; + return err; } static int @@ -287,10 +284,7 @@ int netdev_nl_napi_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) } rtnl_unlock(); - if (err != -EMSGSIZE) - return err; - - return skb->len; + return err; } static int @@ -463,10 +457,7 @@ int netdev_nl_queue_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) } rtnl_unlock(); - if (err != -EMSGSIZE) - return err; - - return skb->len; + return err; } static int netdev_genl_netdevice_event(struct notifier_block *nb, diff --git a/net/core/page_pool_user.c b/net/core/page_pool_user.c index 278294aca66a..3a3277ba167b 100644 --- a/net/core/page_pool_user.c +++ b/net/core/page_pool_user.c @@ -103,8 +103,6 @@ netdev_nl_page_pool_get_dump(struct sk_buff *skb, struct netlink_callback *cb, mutex_unlock(&page_pools_lock); rtnl_unlock(); - if (skb->len && err == -EMSGSIZE) - return skb->len; return err; } From patchwork Fri Mar 1 01:28:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 13577821 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBE2638DD2 for ; Fri, 1 Mar 2024 01:31:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256693; cv=none; b=YpIOyC+yUZJe7a0xgmVX7ncYCkV/bIcW3G7ig2uLJ6PYT1trIlNmZ08s+DdXCtA6gqxwzDeKTGLfChtkk2t/n50kupOOQMjkmHQCYcSNE+EPtm6hRkZU7MvaO3OuDYMjgJHbQd3FbVW+n9kXGCrwRF025MSNzGo3lGrfCR8srb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256693; c=relaxed/simple; bh=oKn4bPhEphu1JF5rSJz7CO+oWbhu/2FjrGzo1B8k604=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uiWwpvLf3q5tgbRVIWrrKtBiuydMS9vqjN6dhytgVNEjed0vVDr86JBW7ThQEfA7s3XN0Ws7d4O8dEpbvLf49fZY9dnnfEXgodTGdQ9rCYaMZs02uI1BJqZ5HG0zvE/Qd6fUTLKo01n6VDS349pq3XM5c3Gcdsp+9aisVRZigRo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XFXYmxRv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XFXYmxRv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7E85C43399; Fri, 1 Mar 2024 01:31:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709256693; bh=oKn4bPhEphu1JF5rSJz7CO+oWbhu/2FjrGzo1B8k604=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XFXYmxRvWhsaKXB8lAKIUIVgJkwohgm/9u6v3l8XKLa5+kkDOV2WMSAMZvfZgSxsj Z/6xnuuueUOCv3VbB78sS23tsvlRtQNxzHO5O7kgg4XItreMuYCd210PtmxcHp6fi+ bFlZxGUtxtDm2ZQP+bQQXNfDtpqR2fPV+8OYstZE5oSE4+ZOUuzA4YqDJw44JkKUQb yCD+QlibQ3oGSLF14cOaiFbJi36nCyEObUVGgDAVePUeKWE+1fG72wlB7FvA5DtDD9 1Vgf0xTo5qN3Ratt4vh0mH2zCNhCFUhcILe8tAbq4EteCameUMOkiYU3DbFwqf2tyo 4T5kcC7bZtZzA== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, johannes@sipsolutions.net, fw@strlen.de, pablo@netfilter.org, idosch@nvidia.com, jiri@resnulli.us, Jakub Kicinski Subject: [PATCH net-next 3/3] genetlink: fit NLMSG_DONE into same read() as families Date: Thu, 29 Feb 2024 17:28:45 -0800 Message-ID: <20240301012845.2951053-4-kuba@kernel.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240301012845.2951053-1-kuba@kernel.org> References: <20240301012845.2951053-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Make sure ctrl_fill_info() returns sensible error codes and propagate them out to netlink core. Let netlink core decide when to return skb->len and when to treat the exit as an error. Netlink core does better job at it, if we always return skb->len the core doesn't know when we're done dumping and NLMSG_DONE ends up in a separate read(). Signed-off-by: Jakub Kicinski Reviewed-by: Eric Dumazet --- CC: jiri@resnulli.us --- net/netlink/genetlink.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c index 50ec599a5cff..70379ecfb6ed 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -1232,7 +1232,7 @@ static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq, hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); if (hdr == NULL) - return -1; + return -EMSGSIZE; if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) || @@ -1355,6 +1355,7 @@ static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) struct net *net = sock_net(skb->sk); int fams_to_skip = cb->args[0]; unsigned int id; + int err; idr_for_each_entry(&genl_fam_idr, rt, id) { if (!rt->netnsok && !net_eq(net, &init_net)) @@ -1363,16 +1364,17 @@ static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) if (n++ < fams_to_skip) continue; - if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid, - cb->nlh->nlmsg_seq, NLM_F_MULTI, - skb, CTRL_CMD_NEWFAMILY) < 0) { + err = ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid, + cb->nlh->nlmsg_seq, NLM_F_MULTI, + skb, CTRL_CMD_NEWFAMILY); + if (err) { n--; break; } } cb->args[0] = n; - return skb->len; + return err; } static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,