Commit 11199720 authored by Kuen-Han Tsai's avatar Kuen-Han Tsai Committed by Greg Kroah-Hartman
Browse files

Revert "usb: gadget: f_ncm: Fix atomic context locking issue"



This reverts commit 0d6c8144.

This commit is being reverted as part of a series-wide revert.

By deferring the net_device allocation to the bind() phase, a single
function instance will spawn multiple network devices if it is symlinked
to multiple USB configurations.

This causes regressions for userspace tools (like the postmarketOS DHCP
daemon) that rely on reading the interface name (e.g., "usb0") from
configfs. Currently, configfs returns the template "usb%d", causing the
userspace network setup to fail.

Crucially, because this patch breaks the 1:1 mapping between the
function instance and the network device, this naming issue cannot
simply be patched. Configfs only exposes a single 'ifname' attribute per
instance, making it impossible to accurately report the actual interface
name when multiple underlying network devices can exist for that single
instance.

All configurations tied to the same function instance are meant to share
a single network device. Revert this change to restore the 1:1 mapping
by allocating the network device at the instance level (alloc_inst).

Reported-by: default avatarDavid Heidelberg <david@ixit.cz>
Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c951131249af@ixit.cz/


Fixes: 56a512a9 ("usb: gadget: f_ncm: align net_device lifecycle with bind/unbind")
Cc: stable <stable@kernel.org>
Signed-off-by: default avatarKuen-Han Tsai <khtsai@google.com>
Link: https://patch.msgid.link/20260309-f-ncm-revert-v2-1-ea2afbc7d9b2@google.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent e8557acf
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ struct f_ncm {
	u8				notify_state;
	atomic_t			notify_count;
	bool				is_open;
	bool				is_connected;

	const struct ndp_parser_opts	*parser_opts;
	bool				is_crc;
@@ -865,6 +864,7 @@ static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
	struct f_ncm		*ncm = func_to_ncm(f);
	struct f_ncm_opts	*opts = func_to_ncm_opts(f);
	struct usb_composite_dev *cdev = f->config->cdev;

	/* Control interface has only altsetting 0 */
@@ -887,9 +887,10 @@ static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
		if (alt > 1)
			goto fail;

		if (ncm->is_connected) {
		scoped_guard(mutex, &opts->lock)
			if (opts->net) {
				DBG(cdev, "reset ncm\n");
			ncm->is_connected = false;
				opts->net = NULL;
				gether_disconnect(&ncm->port);
				ncm_reset_values(ncm);
			}
@@ -925,7 +926,8 @@ static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
			net = gether_connect(&ncm->port);
			if (IS_ERR(net))
				return PTR_ERR(net);
			ncm->is_connected = true;
			scoped_guard(mutex, &opts->lock)
				opts->net = net;
		}

		spin_lock(&ncm->lock);
@@ -1372,12 +1374,14 @@ static int ncm_unwrap_ntb(struct gether *port,
static void ncm_disable(struct usb_function *f)
{
	struct f_ncm		*ncm = func_to_ncm(f);
	struct f_ncm_opts	*opts = func_to_ncm_opts(f);
	struct usb_composite_dev *cdev = f->config->cdev;

	DBG(cdev, "ncm deactivated\n");

	if (ncm->is_connected) {
		ncm->is_connected = false;
	scoped_guard(mutex, &opts->lock)
		if (opts->net) {
			opts->net = NULL;
			gether_disconnect(&ncm->port);
		}

@@ -1683,6 +1687,7 @@ static struct usb_function_instance *ncm_alloc_inst(void)
	if (!opts)
		return ERR_PTR(-ENOMEM);

	opts->net = NULL;
	opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id;
	gether_setup_opts_default(&opts->net_opts, "usb");

+10 −1
Original line number Diff line number Diff line
@@ -327,9 +327,18 @@ out: \
					      char *page)			\
	{									\
		struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item);		\
		const char *name;						\
										\
		guard(mutex)(&opts->lock);					\
		return sysfs_emit(page, "%s\n", opts->net_opts.name);		\
		rtnl_lock();							\
		if (opts->net_opts.ifname_set)					\
			name = opts->net_opts.name;				\
		else if (opts->net)						\
			name = netdev_name(opts->net);				\
		else								\
			name = "(inactive net_device)";				\
		rtnl_unlock();							\
		return sysfs_emit(page, "%s\n", name);				\
	}									\
										\
	static ssize_t _f_##_opts_ifname_store(struct config_item *item,	\
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

struct f_ncm_opts {
	struct usb_function_instance	func_inst;
	struct net_device		*net;

	struct gether_opts		net_opts;
	struct config_group		*ncm_interf_group;