From: Lee Jones <lee@kernel.org>
To: Tobias Junghans <tobias.junghans@inhub.de>,
Andrew Lunn <andrew+netdev@lunn.ch>
Cc: linux-leds@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH v2] leds: trigger: netdev: Match net in netdev_trig_notify()
Date: Thu, 10 Apr 2025 11:17:59 +0100 [thread overview]
Message-ID: <20250410101759.GT372032@google.com> (raw)
In-Reply-To: <20250407090455.677846-1-tobias.junghans@inhub.de>
Andrew et al., please could you verify for sanity?
On Mon, 07 Apr 2025, Tobias Junghans wrote:
> Different network devices may have the same device name if they are in
> different network namespaces. This confuses ledtrig-netdev and leads to
> undesired effects in certain situations.
>
> When setting device_name to eth0, the trigger is attached to the
> corresponding (physical) network device. Later a Docker container is
> started. Docker now creates a virtual Ethernet interface (vethXXXX),
> moves it to the container's net namespace and renames it to "eth0".
> Subsequently ledtrig-netdev receives a NETDEV_CHANGENAME notification,
> recognizes "eth0" as device and and switches its activity over to this
> device. As a result the LED no longer blinks for the original (physical)
> network device.
>
> The described erroneous behavior can be fixed by tracking and comparing
> the network namespaces of network devices.
>
> Signed-off-by: Tobias Junghans <tobias.junghans@inhub.de>
> ---
> drivers/leds/trigger/ledtrig-netdev.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c
> index 4b0863db901a..72bcb86cdcdb 100644
> --- a/drivers/leds/trigger/ledtrig-netdev.c
> +++ b/drivers/leds/trigger/ledtrig-netdev.c
> @@ -62,6 +62,7 @@ struct led_netdev_data {
>
> struct led_classdev *led_cdev;
> struct net_device *net_dev;
> + struct net *net;
>
> char device_name[IFNAMSIZ];
> atomic_t interval;
> @@ -274,6 +275,7 @@ static int set_device_name(struct led_netdev_data *trigger_data,
> if (trigger_data->net_dev) {
> dev_put(trigger_data->net_dev);
> trigger_data->net_dev = NULL;
> + trigger_data->net = NULL;
> }
>
> memcpy(trigger_data->device_name, name, size);
> @@ -284,6 +286,8 @@ static int set_device_name(struct led_netdev_data *trigger_data,
> if (trigger_data->device_name[0] != 0)
> trigger_data->net_dev =
> dev_get_by_name(&init_net, trigger_data->device_name);
> + if (trigger_data->net_dev)
> + trigger_data->net = dev_net(trigger_data->net_dev);
>
> trigger_data->carrier_link_up = false;
> trigger_data->link_speed = SPEED_UNKNOWN;
> @@ -573,15 +577,16 @@ static int netdev_trig_notify(struct notifier_block *nb,
> struct led_netdev_data *trigger_data =
> container_of(nb, struct led_netdev_data, notifier);
> struct led_classdev *led_cdev = trigger_data->led_cdev;
> + bool same_net = !trigger_data->net || net_eq(dev_net(dev), trigger_data->net);
>
> if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
> && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER
> && evt != NETDEV_CHANGENAME)
> return NOTIFY_DONE;
>
> - if (!(dev == trigger_data->net_dev ||
> - (evt == NETDEV_CHANGENAME && !strcmp(dev->name, trigger_data->device_name)) ||
> - (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
> + if (!((dev == trigger_data->net_dev && same_net) ||
> + (evt == NETDEV_CHANGENAME && !strcmp(dev->name, trigger_data->device_name) && same_net) ||
> + (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name) && same_net)))
> return NOTIFY_DONE;
>
> cancel_delayed_work_sync(&trigger_data->work);
> @@ -597,12 +602,14 @@ static int netdev_trig_notify(struct notifier_block *nb,
> dev_put(trigger_data->net_dev);
> dev_hold(dev);
> trigger_data->net_dev = dev;
> + trigger_data->net = dev_net(dev);
> if (evt == NETDEV_CHANGENAME)
> get_device_state(trigger_data);
> break;
> case NETDEV_UNREGISTER:
> dev_put(trigger_data->net_dev);
> trigger_data->net_dev = NULL;
> + trigger_data->net = NULL;
> break;
> case NETDEV_UP:
> case NETDEV_CHANGE:
> @@ -702,6 +709,7 @@ static int netdev_trig_activate(struct led_classdev *led_cdev)
>
> trigger_data->led_cdev = led_cdev;
> trigger_data->net_dev = NULL;
> + trigger_data->net = NULL;
> trigger_data->device_name[0] = 0;
>
> trigger_data->mode = 0;
> --
> 2.43.0
>
>
--
Lee Jones [李琼斯]
next parent reply other threads:[~2025-04-10 10:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20250404151042.GC372032@google.com>
[not found] ` <20250407090455.677846-1-tobias.junghans@inhub.de>
2025-04-10 10:17 ` Lee Jones [this message]
2025-04-10 20:39 ` [PATCH v2] leds: trigger: netdev: Match net in netdev_trig_notify() Andrew Lunn
2025-04-11 7:44 ` Lee Jones
2025-04-23 13:13 ` [PATCH v2] leds: trigger: netdev: refactor dev matching " Tobias Junghans
2025-04-24 8:09 ` Lee Jones
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=20250410101759.GT372032@google.com \
--to=lee@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=linux-leds@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=tobias.junghans@inhub.de \
/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;
as well as URLs for NNTP newsgroup(s).