Netdev List
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
	andrew+netdev@lunn.ch, horms@kernel.org, daniel.zahka@gmail.com,
	willemdebruijn.kernel@gmail.com, donald.hunter@gmail.com,
	shuah@kernel.org, linux-kselftest@vger.kernel.org,
	Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 4/6] psp: notify about a disassociation once it has happened
Date: Sat, 12 Sep 2026 13:04:24 -0700	[thread overview]
Message-ID: <20260912200426.121025-5-kuba@kernel.org> (raw)
In-Reply-To: <20260912200426.121025-1-kuba@kernel.org>

The dev-change-ntf generated by dev-disassoc was built before the
association was unlinked, so the assoc-list it carried still contained
the device which was going away, and nothing corrected it afterwards.
We don't really expect associations to change during a lifetime of
a netns but this is still wrong. Netlink listeners need to be able
to tell the current "state of the world" based on notifications.

The notification had to be sent early because psp_nl_multicast_per_ns()
derives the set of namespaces to notify from the association list, so
a namespace losing its last associated device becomes unreachable once
the entry is gone. In that case - get the netns from the netdev itself,
and send that namespace a dev-del-ntf. If the netns had multiple
associated netdevs and only one disassoc'd we'll still send a change
notification, just with a correct list.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 net/psp/psp.h      |  1 +
 net/psp/psp_main.c | 16 +++++++++++-----
 net/psp/psp_nl.c   | 44 +++++++++++++++++++++++++++++++++++++-------
 3 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/net/psp/psp.h b/net/psp/psp.h
index bbb39e2f5b0a..b123c2427905 100644
--- a/net/psp/psp.h
+++ b/net/psp/psp.h
@@ -19,6 +19,7 @@ bool psp_has_assoc_dev_in_ns(struct psp_dev *psd, struct net *net);
 int psp_attach_netdev_notifier(void);
 
 void psp_nl_notify_dev(struct psp_dev *psd, u32 cmd);
+void psp_nl_notify_disassoc(struct psp_dev *psd, struct net *net);
 
 struct psp_assoc *psp_assoc_create(struct psp_dev *psd);
 struct psp_dev *psp_dev_get_for_sock(struct sock *sk);
diff --git a/net/psp/psp_main.c b/net/psp/psp_main.c
index 91473f96ad21..273b010d2355 100644
--- a/net/psp/psp_main.c
+++ b/net/psp/psp_main.c
@@ -408,7 +408,7 @@ int psp_dev_rcv(struct sk_buff *skb, u16 dev_id, u8 generation, bool strip_icv)
 }
 EXPORT_SYMBOL(psp_dev_rcv);
 
-static void psp_dev_disassoc_one(struct psp_dev *psd, struct net_device *dev)
+static bool psp_dev_disassoc_one(struct psp_dev *psd, struct net_device *dev)
 {
 	struct psp_assoc_dev *entry;
 
@@ -419,9 +419,11 @@ static void psp_dev_disassoc_one(struct psp_dev *psd, struct net_device *dev)
 			rcu_assign_pointer(entry->assoc_dev->psp_dev, NULL);
 			netdev_put(entry->assoc_dev, &entry->dev_tracker);
 			kfree(entry);
-			return;
+			return true;
 		}
 	}
+
+	return false;
 }
 
 static int psp_netdev_event(struct notifier_block *nb, unsigned long event,
@@ -438,9 +440,13 @@ static int psp_netdev_event(struct notifier_block *nb, unsigned long event,
 	if (psd && psp_dev_tryget(psd)) {
 		rcu_read_unlock();
 		mutex_lock(&psd->lock);
-		if (psp_dev_is_registered(psd))
-			psp_nl_notify_dev(psd, PSP_CMD_DEV_CHANGE_NTF);
-		psp_dev_disassoc_one(psd, dev);
+		/* Nothing to report if the device was never on the list,
+		 * dev-assoc may have failed after publishing dev->psp_dev,
+		 * and this is also the main netdevice's path.
+		 */
+		if (psp_dev_disassoc_one(psd, dev) &&
+		    psp_dev_is_registered(psd))
+			psp_nl_notify_disassoc(psd, dev_net(dev));
 		mutex_unlock(&psd->lock);
 		psp_dev_put(psd);
 	} else {
diff --git a/net/psp/psp_nl.c b/net/psp/psp_nl.c
index b57366b5e032..cdfc2d72fb39 100644
--- a/net/psp/psp_nl.c
+++ b/net/psp/psp_nl.c
@@ -356,6 +356,40 @@ void psp_nl_notify_dev(struct psp_dev *psd, u32 cmd)
 				psp_nl_build_dev_ntf, &cmd);
 }
 
+/**
+ * psp_nl_notify_disassoc() - notify about a device losing an association
+ * @psd: PSP device (must be locked)
+ * @net: netns of the netdevice which got disassociated
+ *
+ * Must be called once @psd no longer has the association, so that the
+ * notifications carry the state after the change.
+ */
+void psp_nl_notify_disassoc(struct psp_dev *psd, struct net *net)
+{
+	struct sk_buff *ntf;
+	bool still_visible;
+	u32 cmd;
+
+	lockdep_assert_held(&psd->lock);
+
+	psp_nl_notify_dev(psd, PSP_CMD_DEV_CHANGE_NTF);
+
+	/* psp_nl_notify_dev() reaches the main netdevice's netns and every
+	 * netns which still has an associated device. If @net is neither,
+	 * the device is gone from @net and we should send a delete ntf.
+	 */
+	still_visible = !psp_dev_check_access(psd, net, false);
+	if (still_visible || !maybe_get_net(net))
+		return;
+
+	cmd = PSP_CMD_DEV_DEL_NTF;
+	ntf = psp_nl_build_dev_ntf(psd, net, &cmd);
+	if (ntf)
+		genlmsg_multicast_netns(&psp_nl_family, net, ntf, 0,
+					PSP_NLGRP_MGMT, GFP_KERNEL);
+	put_net(net);
+}
+
 int psp_nl_dev_get_doit(struct sk_buff *req, struct genl_info *info)
 {
 	struct psp_dev *psd = info->user_ptr[0];
@@ -620,13 +654,6 @@ int psp_nl_dev_disassoc_doit(struct sk_buff *skb, struct genl_info *info)
 		return -ENOMEM;
 	}
 
-	put_net(net);
-
-	/* Notify before removal so listeners in the disassociated namespace
-	 * still receive the notification.
-	 */
-	psp_nl_notify_dev(psd, PSP_CMD_DEV_CHANGE_NTF);
-
 	/* Remove from the association list */
 	list_del(&found->dev_list);
 	psd->assoc_dev_cnt--;
@@ -634,6 +661,9 @@ int psp_nl_dev_disassoc_doit(struct sk_buff *skb, struct genl_info *info)
 	netdev_put(found->assoc_dev, &found->dev_tracker);
 	kfree(found);
 
+	psp_nl_notify_disassoc(psd, net);
+	put_net(net);
+
 	return psp_nl_reply_send(rsp, info);
 }
 
-- 
2.55.0


  parent reply	other threads:[~2026-09-12 20:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-12 20:04 [PATCH net-next 0/6] psp: correct notifications and device info around device assoc Jakub Kicinski
2026-09-12 20:04 ` [PATCH net-next 1/6] selftests: drv-net: psp: fix linter issues Jakub Kicinski
2026-09-12 20:04 ` [PATCH net-next 2/6] psp: don't report the main netdevice's ifindex to associated namespaces Jakub Kicinski
2026-09-12 20:04 ` [PATCH net-next 3/6] selftests: drv-net: psp: check the ifindex an associated netns sees Jakub Kicinski
2026-09-12 20:04 ` Jakub Kicinski [this message]
2026-09-12 20:04 ` [PATCH net-next 5/6] selftests: drv-net: psp: factor out creating a netkit in the test netns Jakub Kicinski
2026-09-12 20:04 ` [PATCH net-next 6/6] selftests: drv-net: psp: check the PSP disassociation notifications Jakub Kicinski

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=20260912200426.121025-5-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=daniel.zahka@gmail.com \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=willemdebruijn.kernel@gmail.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