From: Andre Carvalho <asantostc@gmail.com>
To: Breno Leitao <leitao@debian.org>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Shuah Khan <shuah@kernel.org>,
Simon Horman <horms@kernel.org>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
Andre Carvalho <asantostc@gmail.com>
Subject: [PATCH net-next v4 4/5] netconsole: resume previously deactivated target
Date: Sun, 16 Nov 2025 17:14:04 +0000 [thread overview]
Message-ID: <20251116-netcons-retrigger-v4-4-5290b5f140c2@gmail.com> (raw)
In-Reply-To: <20251116-netcons-retrigger-v4-0-5290b5f140c2@gmail.com>
Attempt to resume a previously deactivated target when the associated
interface comes back (NETDEV_UP event is received) by calling
__netpoll_setup on the device.
Depending on how the target was setup (by mac or interface name), the
corresponding field is compared with the device being brought up.
Targets that are candidates for resuming are removed from the target list
and added to a temp list, as __netpoll_setup is IRQ unsafe.
__netpoll_setup assumes RTNL is held (which is guaranteed to be the
case when handling the event). In case of success, hold a reference to
the device which will be removed upon target (or netconsole) removal by
netpoll_cleanup.
Target transitions to STATE_DISABLED in case of failures resuming it to
avoid retrying the same target indefinitely.
Signed-off-by: Andre Carvalho <asantostc@gmail.com>
---
drivers/net/netconsole.c | 81 ++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 75 insertions(+), 6 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 5a374e6d178d..2a5c470317b5 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -135,10 +135,14 @@ enum target_state {
* @stats: Packet send stats for the target. Used for debugging.
* @state: State of the target.
* Visible from userspace (read-write).
- * We maintain a strict 1:1 correspondence between this and
- * whether the corresponding netpoll is active or inactive.
+ * From a userspace perspective, the target is either enabled or
+ * disabled. Internally, although both STATE_DISABLED and
+ * STATE_DEACTIVATED correspond to inactive targets, the latter is
+ * due to automatic interface state changes and will try
+ * recover automatically, if the interface comes back
+ * online.
* Also, other parameters of a target may be modified at
- * runtime only when it is disabled (state == STATE_DISABLED).
+ * runtime only when it is disabled (state != STATE_ENABLED).
* @extended: Denotes whether console is extended or not.
* @release: Denotes whether kernel release version should be prepended
* to the message. Depends on extended console.
@@ -1445,17 +1449,75 @@ static int prepare_extradata(struct netconsole_target *nt)
}
#endif /* CONFIG_NETCONSOLE_DYNAMIC */
+/* Attempts to resume logging to a deactivated target. */
+static void maybe_resume_target(struct netconsole_target *nt,
+ struct net_device *ndev)
+{
+ int ret;
+
+ ret = __netpoll_setup(&nt->np, ndev);
+ if (ret) {
+ /* netpoll fails setup once, do not try again. */
+ nt->state = STATE_DISABLED;
+ return;
+ }
+
+ netdev_hold(ndev, &nt->np.dev_tracker, GFP_KERNEL);
+ nt->state = STATE_ENABLED;
+ pr_info("network logging resumed on interface %s\n", nt->np.dev_name);
+}
+
+/* Check if the target was bound by mac address. */
+static bool bound_by_mac(struct netconsole_target *nt)
+{
+ return is_valid_ether_addr(nt->np.dev_mac);
+}
+
+/* Checks if a deactivated target matches a device. */
+static bool deactivated_target_match(struct netconsole_target *nt,
+ struct net_device *ndev)
+{
+ if (nt->state != STATE_DEACTIVATED)
+ return false;
+
+ if (bound_by_mac(nt))
+ return !memcmp(nt->np.dev_mac, ndev->dev_addr, ETH_ALEN);
+ return !strncmp(nt->np.dev_name, ndev->name, IFNAMSIZ);
+}
+
+/* Process targets in resume_list and returns then to target_list */
+static void process_resumable_targets(struct list_head *resume_list,
+ struct net_device *ndev)
+{
+ struct netconsole_target *nt, *tmp;
+ unsigned long flags;
+
+ list_for_each_entry_safe(nt, tmp, resume_list, list) {
+ maybe_resume_target(nt, ndev);
+
+ /* At this point the target is either enabled or disabled and
+ * was cleaned up before getting deactivated. Either way, add it
+ * back to target list.
+ */
+ spin_lock_irqsave(&target_list_lock, flags);
+ list_move(&nt->list, &target_list);
+ spin_unlock_irqrestore(&target_list_lock, flags);
+ }
+}
+
/* Handle network interface device notifications */
static int netconsole_netdev_event(struct notifier_block *this,
unsigned long event, void *ptr)
{
- unsigned long flags;
- struct netconsole_target *nt, *tmp;
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+ struct netconsole_target *nt, *tmp;
+ LIST_HEAD(resume_list);
bool stopped = false;
+ unsigned long flags;
if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
- event == NETDEV_RELEASE || event == NETDEV_JOIN))
+ event == NETDEV_RELEASE || event == NETDEV_JOIN ||
+ event == NETDEV_UP))
goto done;
mutex_lock(&target_cleanup_list_lock);
@@ -1475,6 +1537,11 @@ static int netconsole_netdev_event(struct notifier_block *this,
stopped = true;
}
}
+ if (event == NETDEV_UP && deactivated_target_match(nt, dev))
+ /* maybe_resume_target is IRQ unsafe, remove target from
+ * target_list in order to resume it with IRQ enabled.
+ */
+ list_move(&nt->list, &resume_list);
netconsole_target_put(nt);
}
spin_unlock_irqrestore(&target_list_lock, flags);
@@ -1498,6 +1565,8 @@ static int netconsole_netdev_event(struct notifier_block *this,
dev->name, msg);
}
+ process_resumable_targets(&resume_list, dev);
+
/* Process target_cleanup_list entries. By the end, target_cleanup_list
* should be empty
*/
--
2.51.2
next prev parent reply other threads:[~2025-11-16 17:14 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-16 17:14 [PATCH net-next v4 0/5] netconsole: support automatic target recovery Andre Carvalho
2025-11-16 17:14 ` [PATCH net-next v4 1/5] netconsole: add target_state enum Andre Carvalho
2025-11-16 17:14 ` [PATCH net-next v4 2/5] netconsole: convert 'enabled' flag to enum for clearer state management Andre Carvalho
2025-11-16 17:14 ` [PATCH net-next v4 3/5] netconsole: add STATE_DEACTIVATED to track targets disabled by low level Andre Carvalho
2025-11-16 17:14 ` Andre Carvalho [this message]
2025-11-16 21:09 ` [PATCH net-next v4 4/5] netconsole: resume previously deactivated target Andre Carvalho
2025-11-17 10:29 ` Breno Leitao
2025-11-16 17:14 ` [PATCH net-next v4 5/5] selftests: netconsole: validate target resume Andre Carvalho
2025-11-16 18:59 ` Andre Carvalho
2025-11-17 10:35 ` Breno Leitao
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=20251116-netcons-retrigger-v4-4-5290b5f140c2@gmail.com \
--to=asantostc@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
/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