From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C421B3A6404 for ; Mon, 9 Mar 2026 12:05:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057922; cv=none; b=Plv6bu3FKIW/CzxDxccBCGBpj6YuicBS6Blyz2khsAVFvirgtyCvy026p0cORt7rHheoJItSD3A16g4RBV4tEn07y9GpnoyzkRKHb/RONudjiCAWkk5P6QvYPBS1KY174IJCkOUufU8vWUItYxlwkOBCoh4gcKn9T00olAOYPsc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057922; c=relaxed/simple; bh=5/YxhynNP5OtTJz20C93GRmdTUgl+LzpEZyjUYK3nk0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ndRfUk/DQvqir6DsG4oHudZmHLfNllUaSPrSoaNgXw3kZ2EMr+M+kwxR8S/ToS6TNZoNgUdBOvKHc12vdyilZLHheUvrgUtIL4tmtFYuMfRjxwyJHMMGmEDgAcH2VflVFXFrJp3F0ob0KEQpAK50Jiro1L5G9iKqLjpQqgvqOVY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EPep4LD9; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EPep4LD9" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae49080364so66391695ad.0 for ; Mon, 09 Mar 2026 05:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057918; x=1773662718; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Up+QEdUkj26uFbINRayYsiYrdHXv4LEpi2MdfDCCm0k=; b=EPep4LD9GhZSsD3Q8MOWfGAIpU3WV8tkKqdylJ2Cd57Mgto1hLrbfHtPav2X7m0o/N oT+KNtaYMSP5O/ki9FdPBSRm/3+CBDZKdmzy/wQHN7X/QLNVDlpeOVfak+lUB38+tsuh iVWv2CtJdqsp76ynMHKMcUUP9mIplM3Ws565TbPFQO1rzLiCmjuQ/TbHeMCs5cTCeeN2 54hI4DmylCfQ8SB/cBOGaIJAxXvbRNFyZd/fGG3lc16yH9e3hEahvo81TfGkFrxhU066 v4SUhXUcXtClGEDx4f7O7tNl94D42Dm6tXnrZHCOWNic8kmCrbwjNGMncF3uUMT1twBU z6iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057918; x=1773662718; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Up+QEdUkj26uFbINRayYsiYrdHXv4LEpi2MdfDCCm0k=; b=ddYsVGPXGlcTrMDMFm7wmWLTWVY0dQqhNDIxRdnUeDP+6FMPrn/nXmtBbSp1H6qeqn cIpWseuKlBmQqQ/+HtzVmlVSEK1x2NzVCbGfLeF2Wqu5Mm2wVvpqQLEzw5Pegb04hy6Z rGA56KiD8JLXdjb9XHi7keN2PRC46LzlLLGC8BjGTWwVH51GnWA9xX2vfo4NR5pMircd 5rWplONRUUpgYQhOp/mV1/ZKvui4vtHsDnXgg+dBzFNYJo1YDzTEtLqzdkK5j4qN9gr+ Ez3QKSF68BXVgks7JIFF8SNXbmizwqqqKJGz2XenumU/mt96UBpaxV+CsAdf9VqgtDlg WUxQ== X-Forwarded-Encrypted: i=1; AJvYcCVcqfZmn0FVb9aEuw4KMI8lM+W1TAOWT+LEMWLkQI7GrU9KMPTjbwBvLaHYuNfPAv3Al1EBEr3+R/5nLkw=@vger.kernel.org X-Gm-Message-State: AOJu0YzmaCnFwNOIUaxXMyjWxotS/hXaiphJ3qEyWg2HyiiGsiQIohol +yd+x/Fda6PB5vESVMy+xV6rZb5w3Jul0bWd8BCztdk+HwsLuE1yckDEs29ndWktgEjl6i2BCyS fS9l3tg== X-Received: from plbmi14.prod.google.com ([2002:a17:902:fcce:b0:2ae:8189:6d82]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:f87:b0:2ae:44f4:1678 with SMTP id d9443c01a7336-2ae824b83e7mr102046305ad.57.1773057917958; Mon, 09 Mar 2026 05:05:17 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:46 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=5610; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=5/YxhynNP5OtTJz20C93GRmdTUgl+LzpEZyjUYK3nk0=; b=x/I92mmzRMq+qRpeXxxEasVw5ga1d/zxTzpfK55lfaMSKWuCUqkBCVOhOqywT8qiyfBDrtl4E F1m+KinqSkaCYgpIQUtHQQnlG1GXqwuQt2ZK/eqT8VE9RK6tm2jMhez X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-1-ea2afbc7d9b2@google.com> Subject: [PATCH v2 1/7] Revert "usb: gadget: f_ncm: Fix atomic context locking issue" From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This reverts commit 0d6c8144ca4d93253de952a5ea0028c19ed7ab68. 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: David Heidelberg Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c95113124= 9af@ixit.cz/ Fixes: 56a512a9b410 ("usb: gadget: f_ncm: align net_device lifecycle with b= ind/unbind") Signed-off-by: Kuen-Han Tsai Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/function/f_ncm.c | 29 +++++++++++++++-------= ---- drivers/usb/gadget/function/u_ether_configfs.h | 11 +++++++++- drivers/usb/gadget/function/u_ncm.h | 1 + 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/funct= ion/f_ncm.c index 4da19864d70b..14fc7dce6f39 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -58,7 +58,6 @@ struct f_ncm { u8 notify_state; atomic_t notify_count; bool is_open; - bool is_connected; =20 const struct ndp_parser_opts *parser_opts; bool is_crc; @@ -865,6 +864,7 @@ static int ncm_setup(struct usb_function *f, const stru= ct usb_ctrlrequest *ctrl) static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) { struct f_ncm *ncm =3D func_to_ncm(f); + struct f_ncm_opts *opts =3D func_to_ncm_opts(f); struct usb_composite_dev *cdev =3D f->config->cdev; =20 /* Control interface has only altsetting 0 */ @@ -887,12 +887,13 @@ static int ncm_set_alt(struct usb_function *f, unsign= ed intf, unsigned alt) if (alt > 1) goto fail; =20 - if (ncm->is_connected) { - DBG(cdev, "reset ncm\n"); - ncm->is_connected =3D false; - gether_disconnect(&ncm->port); - ncm_reset_values(ncm); - } + scoped_guard(mutex, &opts->lock) + if (opts->net) { + DBG(cdev, "reset ncm\n"); + opts->net =3D NULL; + gether_disconnect(&ncm->port); + ncm_reset_values(ncm); + } =20 /* * CDC Network only sends data in non-default altsettings. @@ -925,7 +926,8 @@ static int ncm_set_alt(struct usb_function *f, unsigned= intf, unsigned alt) net =3D gether_connect(&ncm->port); if (IS_ERR(net)) return PTR_ERR(net); - ncm->is_connected =3D true; + scoped_guard(mutex, &opts->lock) + opts->net =3D net; } =20 spin_lock(&ncm->lock); @@ -1372,14 +1374,16 @@ static int ncm_unwrap_ntb(struct gether *port, static void ncm_disable(struct usb_function *f) { struct f_ncm *ncm =3D func_to_ncm(f); + struct f_ncm_opts *opts =3D func_to_ncm_opts(f); struct usb_composite_dev *cdev =3D f->config->cdev; =20 DBG(cdev, "ncm deactivated\n"); =20 - if (ncm->is_connected) { - ncm->is_connected =3D false; - gether_disconnect(&ncm->port); - } + scoped_guard(mutex, &opts->lock) + if (opts->net) { + opts->net =3D NULL; + gether_disconnect(&ncm->port); + } =20 if (ncm->notify->enabled) { usb_ep_disable(ncm->notify); @@ -1683,6 +1687,7 @@ static struct usb_function_instance *ncm_alloc_inst(v= oid) if (!opts) return ERR_PTR(-ENOMEM); =20 + opts->net =3D NULL; opts->ncm_os_desc.ext_compat_id =3D opts->ncm_ext_compat_id; gether_setup_opts_default(&opts->net_opts, "usb"); =20 diff --git a/drivers/usb/gadget/function/u_ether_configfs.h b/drivers/usb/g= adget/function/u_ether_configfs.h index 25d8fb05b598..217990a266b2 100644 --- a/drivers/usb/gadget/function/u_ether_configfs.h +++ b/drivers/usb/gadget/function/u_ether_configfs.h @@ -327,9 +327,18 @@ out: \ char *page) \ { \ struct f_##_f_##_opts *opts =3D 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 =3D opts->net_opts.name; \ + else if (opts->net) \ + name =3D netdev_name(opts->net); \ + else \ + name =3D "(inactive net_device)"; \ + rtnl_unlock(); \ + return sysfs_emit(page, "%s\n", name); \ } \ \ static ssize_t _f_##_opts_ifname_store(struct config_item *item, \ diff --git a/drivers/usb/gadget/function/u_ncm.h b/drivers/usb/gadget/funct= ion/u_ncm.h index 6d7538855744..d99330fe31e8 100644 --- a/drivers/usb/gadget/function/u_ncm.h +++ b/drivers/usb/gadget/function/u_ncm.h @@ -19,6 +19,7 @@ =20 struct f_ncm_opts { struct usb_function_instance func_inst; + struct net_device *net; =20 struct gether_opts net_opts; struct config_group *ncm_interf_group; --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D78A73A7F6B for ; Mon, 9 Mar 2026 12:05:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057924; cv=none; b=NUvwVEhC79dnFlEM//wEg41kuoOwccJux1GP1EDF9Wy4Bd5KkDhxBjIo4kAhDLHPEOo7hnoHUlV4wCpFOWggjZ3DW8dMFBj829phy3fTosyQrsU2VVqu+aknHj895YCP6RlaetMz5n3XjTL1ybm6a2UdeasTPXujjosAbTQsBCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057924; c=relaxed/simple; bh=hrijtfqfOvg8LYhTu0Jxjjl8F1NDkkXHNxjFaW5iGQQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nziWD7WDG/di98UZCcUZFw4ZNU35QP7qdUflCHEF7+Sab3VXEN7rALbVpN+DwpRx6dwZzvtFPZHxYx1UejiPO+BRhzBYSUAWmfH/aIYFztxE/1+qa5LDr9SAiyHVoa+Y1/ctmesgs/AvPMtj3StrNl2pn1W/SkXypBlphjFtNSA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PazsTmsy; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PazsTmsy" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3598c05c806so7788846a91.0 for ; Mon, 09 Mar 2026 05:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057920; x=1773662720; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TwK794DdqJYSdG6zV72tobi0ALWgKSn0ri9Q6Cr47G8=; b=PazsTmsy92oHJ8ebxQLOMhEVa4Gzfuc9UvmW+DkCBK0MgJeibvHOfCnSN4cp/w+jb/ e7pcn1sjiy9bnLd9vVsFGYj1O5p68C4bsjHYsq9NobU9CkkeSFvIyfcrW+AHvMNYAdrU uHSpKSnVn44XR+T/B1yeqWpQ7VMmBsb/crZm0PsGHf4/qBd+isavn3we2Oh4SR4I0uZF xKDX6yf+Ebvd00uZPuPNfLAoYd3L6Sq5b4Ozgvd/r5glSoSN/IUoPqGjst53is8vs5Kv i3ipmSvr0XhLQifiZISOVEW6rM+sUf9WTxU+6mcPQnsjflJ4xrDe2QNe6zQd/DCwnspY WOuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057920; x=1773662720; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TwK794DdqJYSdG6zV72tobi0ALWgKSn0ri9Q6Cr47G8=; b=pDt7gicocCCBUOwgFvUTt/i1hGdJgnSIb5sxn1PrD1N+P9MDWBajDgoBeoOEJ83Rxd zhx+SG4IjNnwh1Z3WDrYkwaowx20afGJTVPibf4D4SNlTdBGLeKyJnvUnsCvojixb7mH s4/xuD1sUQrnXdlXc1jC3/Y5ICqupCFxD/D3pUXOaMuP3SoZO/fKPqAo0KRCvIsxSU/Y vxRC5QlGssu8NfnZBQovch9CfGpFG6QWDxAifqE2gydBZY5Zz5j9tKtq+DM7bXqfoDiB 5FlUj5I86qpDdYSfa+6ti62VmP5E92Z1V++RBhgNzUYZuSXUXBNfKWt3kWos4/JATCY2 mmig== X-Forwarded-Encrypted: i=1; AJvYcCUnkJSO74Ek1BTYDFCFfproBjIInv2d9NZ7yckb9srp66YXL22tLUWmNxpTFGIjj4Wg+eeAJN5g7y/djm0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/EhXzCeQraPJmkzNDXex0KVdvGSPWqHOe0XpxfC7tQRm+I9dx ZJ8WiIZalG/qT24Df91BLEuKBDM8DCQ48PSFoOozUhIk7unRJ9AcQmPOfaY09lJjoH7TMh4Cc0A SB6j/bQ== X-Received: from pjbds20.prod.google.com ([2002:a17:90b:8d4:b0:354:c1db:b113]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a10:b0:356:22ef:57b9 with SMTP id 98e67ed59e1d1-359be2205a9mr9389334a91.3.1773057919973; Mon, 09 Mar 2026 05:05:19 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:47 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=3085; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=hrijtfqfOvg8LYhTu0Jxjjl8F1NDkkXHNxjFaW5iGQQ=; b=2Tv0AeQAtVUjZNyvZvH2DCe7nDFIVineVwXIFrOtbbpzhFvUgumR7ZB/4tpB3elWm9KbeSd9L mr7pc73YFmwC5swFG77KG2jDG2GB1DoJHzSv4f6LzuPrVvB0rzhLsBm X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-2-ea2afbc7d9b2@google.com> Subject: [PATCH v2 2/7] Revert "usb: legacy: ncm: Fix NPE in gncm_bind" From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This reverts commit fde0634ad9856b3943a2d1a8cc8de174a63ac840. 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: David Heidelberg Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c95113124= 9af@ixit.cz/ Fixes: 56a512a9b410 ("usb: gadget: f_ncm: align net_device lifecycle with b= ind/unbind") Signed-off-by: Kuen-Han Tsai Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/legacy/ncm.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/drivers/usb/gadget/legacy/ncm.c b/drivers/usb/gadget/legacy/nc= m.c index e8d565534053..0f1b45e3abd1 100644 --- a/drivers/usb/gadget/legacy/ncm.c +++ b/drivers/usb/gadget/legacy/ncm.c @@ -15,10 +15,8 @@ /* #define DEBUG */ /* #define VERBOSE_DEBUG */ =20 -#include #include #include -#include #include =20 #include "u_ether.h" @@ -131,7 +129,6 @@ static int gncm_bind(struct usb_composite_dev *cdev) struct usb_gadget *gadget =3D cdev->gadget; struct f_ncm_opts *ncm_opts; int status; - u8 mac[ETH_ALEN]; =20 f_ncm_inst =3D usb_get_function_instance("ncm"); if (IS_ERR(f_ncm_inst)) @@ -139,15 +136,11 @@ static int gncm_bind(struct usb_composite_dev *cdev) =20 ncm_opts =3D container_of(f_ncm_inst, struct f_ncm_opts, func_inst); =20 - ncm_opts->net_opts.qmult =3D qmult; - if (host_addr && mac_pton(host_addr, mac)) { - memcpy(&ncm_opts->net_opts.host_mac, mac, ETH_ALEN); + gether_set_qmult(ncm_opts->net, qmult); + if (!gether_set_host_addr(ncm_opts->net, host_addr)) pr_info("using host ethernet address: %s", host_addr); - } - if (dev_addr && mac_pton(dev_addr, mac)) { - memcpy(&ncm_opts->net_opts.dev_mac, mac, ETH_ALEN); + if (!gether_set_dev_addr(ncm_opts->net, dev_addr)) pr_info("using self ethernet address: %s", dev_addr); - } =20 /* Allocate string descriptor numbers ... note that string * contents can be overridden by the composite_dev glue. --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E64243A7F44 for ; Mon, 9 Mar 2026 12:05:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057925; cv=none; b=Pt15Fp+i3bb381pYZVfpk4Pmwl7QMpWghI2Gu9gGweZ7ZHug9hTrpvwqVVf3x34NLceVc+983UKy86MjXXd8lwg9Vsjc1+So2pWrfVJT9Hwcj9bcY3w+HBDSYR/tg8QcBPpg+Q+hE6frjzXYW/XReiHP/nilgjUkRWp5Z7uhROg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057925; c=relaxed/simple; bh=vxQAtytnOe9iQTybVOTNQYrvkk5AYCsq3Yn5ZOWsFdM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ETwxbYFnxpCXsKDhrzgQpqS6ghpnM54pjgXzVlwQ91tCpo7vXJZvOepuw0nDjrgXxphgIU/1YIteqYhsI2W/8tAR/ay5I+lo5xGezFR1WIa/ZhuzdRjwlA4xvR8ng7VVUExqVsniSqX3m5yh+QpsbAGVCDCpHNrXQcuS6/HfxJ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0my5u+6J; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0my5u+6J" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae3badc00dso96161015ad.3 for ; Mon, 09 Mar 2026 05:05:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057922; x=1773662722; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YtIMjWj8vbky6j7mb7WzKYLLu1mfMyKjlDVfZg0Jd9w=; b=0my5u+6J9N7svY308uQSfM4tOF+0dM0lh9MQxCFx1fBMumimD4GuI58x/Ck3GmXoQ2 B0FiZPNABHL5Pu12ZXAwyLyG5wcotoaHqWUne5oAiBbp+tjqEJiJJoIH2Ch/Mmp43ew4 v9m3bFJXUBnSFLVPOwqRfVnqEBEEnZSj0+MQmqIUuukuZP4yDPPcNauyHrQuxKuo/QoZ qIBICEwElshp9/+REmd+PVJG0f/f+4Pf3RZNHN5e7CW37GL8cIew3tk5upny1piWylLt d0QHj+sSsiDQVdpsXUE6Q0CHUCWnPVHGiKsfSbrdDNrSRzeAfVGIGUKQ2szEA49Fso4t j2dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057922; x=1773662722; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YtIMjWj8vbky6j7mb7WzKYLLu1mfMyKjlDVfZg0Jd9w=; b=xUQM2oSXuio+gXNuQQjPmmy34kfJVr+fxLf5FdQLk5Bgq2Gij7wuaMgenm3gPLI8NM krx5Uy9XMD3iINpDufU8EwiisgwbIqR4rr2VJK//I+eAlzc0gyb9ikdiHBkCBBfPQ541 8rZ8i0JhsqTzo1WS0a5fk7KrN5KpIfTT2bCe2aaxz7Pw3Mf8BFcgPhY/+q0km4qghUZ9 oypdiZdSwu+TXXfiXuy11ps30RHCoE1arxFDq4KIdRBHU5JExC4eI0udoSZofUB6w/Z7 075gq3iUIyaJpVPR0bgrwhBM3FRlLJwkKmLxRQDxPJFLKTlvs2Y6xeuqRK3VP1zEXbGj 6UwA== X-Forwarded-Encrypted: i=1; AJvYcCVfkEw9b49q5v/szXBrtZaxflSOnbfX6BZz+pRfLDG6/ahxIRdeto+TKmsx9eFFzdsjeHr2vcvghvwzejM=@vger.kernel.org X-Gm-Message-State: AOJu0YwDIBPovBL3EwrwDTbjmmpZiS5HbpOl7v1siHBy2s4+cOy5dO2k 0n52/nOK5Kc87vWAD0U+azEi8x99NhvuMknva/71XYvzLV/Uxc1ANzzO2+6HAIbXSth7NnmhFdc h/ba8DA== X-Received: from plbkk16.prod.google.com ([2002:a17:903:710:b0:2ae:635f:4b2b]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e790:b0:2ae:3afc:eb38 with SMTP id d9443c01a7336-2ae8246be8cmr107239915ad.41.1773057921929; Mon, 09 Mar 2026 05:05:21 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:48 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=11073; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=vxQAtytnOe9iQTybVOTNQYrvkk5AYCsq3Yn5ZOWsFdM=; b=prV7U7AyedqhnOSKcGSxBjutFmCb68hF81tZH4ecmhUK5uZmbXYcuueq6GxRybB1OEzdv0lLi xuicawNvqCOAcnPulkFbEHV0w6LbNnl0o0XDgmQtaXnLpnff+WFc2H6 X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-3-ea2afbc7d9b2@google.com> Subject: [PATCH v2 3/7] Revert "usb: gadget: f_ncm: align net_device lifecycle with bind/unbind" From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai , stable@kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This reverts commit 56a512a9b4107079f68701e7d55da8507eb963d9. 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: David Heidelberg Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c95113124= 9af@ixit.cz/ Fixes: 56a512a9b410 ("usb: gadget: f_ncm: align net_device lifecycle with b= ind/unbind") Cc: stable@kernel.org Signed-off-by: Kuen-Han Tsai Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/function/f_ncm.c | 128 ++++++++++++++++++--------------= ---- drivers/usb/gadget/function/u_ncm.h | 4 +- 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/funct= ion/f_ncm.c index 14fc7dce6f39..3d772c9beb91 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -83,11 +83,6 @@ static inline struct f_ncm *func_to_ncm(struct usb_funct= ion *f) return container_of(f, struct f_ncm, port.func); } =20 -static inline struct f_ncm_opts *func_to_ncm_opts(struct usb_function *f) -{ - return container_of(f->fi, struct f_ncm_opts, func_inst); -} - /*------------------------------------------------------------------------= -*/ =20 /* @@ -864,7 +859,6 @@ static int ncm_setup(struct usb_function *f, const stru= ct usb_ctrlrequest *ctrl) static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) { struct f_ncm *ncm =3D func_to_ncm(f); - struct f_ncm_opts *opts =3D func_to_ncm_opts(f); struct usb_composite_dev *cdev =3D f->config->cdev; =20 /* Control interface has only altsetting 0 */ @@ -887,13 +881,12 @@ static int ncm_set_alt(struct usb_function *f, unsign= ed intf, unsigned alt) if (alt > 1) goto fail; =20 - scoped_guard(mutex, &opts->lock) - if (opts->net) { - DBG(cdev, "reset ncm\n"); - opts->net =3D NULL; - gether_disconnect(&ncm->port); - ncm_reset_values(ncm); - } + if (ncm->netdev) { + DBG(cdev, "reset ncm\n"); + ncm->netdev =3D NULL; + gether_disconnect(&ncm->port); + ncm_reset_values(ncm); + } =20 /* * CDC Network only sends data in non-default altsettings. @@ -926,8 +919,7 @@ static int ncm_set_alt(struct usb_function *f, unsigned= intf, unsigned alt) net =3D gether_connect(&ncm->port); if (IS_ERR(net)) return PTR_ERR(net); - scoped_guard(mutex, &opts->lock) - opts->net =3D net; + ncm->netdev =3D net; } =20 spin_lock(&ncm->lock); @@ -1374,16 +1366,14 @@ static int ncm_unwrap_ntb(struct gether *port, static void ncm_disable(struct usb_function *f) { struct f_ncm *ncm =3D func_to_ncm(f); - struct f_ncm_opts *opts =3D func_to_ncm_opts(f); struct usb_composite_dev *cdev =3D f->config->cdev; =20 DBG(cdev, "ncm deactivated\n"); =20 - scoped_guard(mutex, &opts->lock) - if (opts->net) { - opts->net =3D NULL; - gether_disconnect(&ncm->port); - } + if (ncm->netdev) { + ncm->netdev =3D NULL; + gether_disconnect(&ncm->port); + } =20 if (ncm->notify->enabled) { usb_ep_disable(ncm->notify); @@ -1443,44 +1433,39 @@ static int ncm_bind(struct usb_configuration *c, st= ruct usb_function *f) { struct usb_composite_dev *cdev =3D c->cdev; struct f_ncm *ncm =3D func_to_ncm(f); - struct f_ncm_opts *ncm_opts =3D func_to_ncm_opts(f); struct usb_string *us; int status =3D 0; struct usb_ep *ep; + struct f_ncm_opts *ncm_opts; =20 struct usb_os_desc_table *os_desc_table __free(kfree) =3D NULL; - struct net_device *netdev __free(free_gether_netdev) =3D NULL; struct usb_request *request __free(free_usb_request) =3D NULL; =20 if (!can_support_ecm(cdev->gadget)) return -EINVAL; =20 + ncm_opts =3D container_of(f->fi, struct f_ncm_opts, func_inst); + if (cdev->use_os_string) { os_desc_table =3D kzalloc(sizeof(*os_desc_table), GFP_KERNEL); if (!os_desc_table) return -ENOMEM; } =20 - netdev =3D gether_setup_default(); - if (IS_ERR(netdev)) - return -ENOMEM; - - scoped_guard(mutex, &ncm_opts->lock) { - gether_apply_opts(netdev, &ncm_opts->net_opts); - netdev->mtu =3D ncm_opts->max_segment_size - ETH_HLEN; + mutex_lock(&ncm_opts->lock); + gether_set_gadget(ncm_opts->net, cdev->gadget); + if (!ncm_opts->bound) { + ncm_opts->net->mtu =3D (ncm_opts->max_segment_size - ETH_HLEN); + status =3D gether_register_netdev(ncm_opts->net); } + mutex_unlock(&ncm_opts->lock); =20 - gether_set_gadget(netdev, cdev->gadget); - status =3D gether_register_netdev(netdev); if (status) return status; =20 - /* export host's Ethernet address in CDC format */ - status =3D gether_get_host_addr_cdc(netdev, ncm->ethaddr, - sizeof(ncm->ethaddr)); - if (status < 12) - return -EINVAL; - ncm_string_defs[STRING_MAC_IDX].s =3D ncm->ethaddr; + ncm_opts->bound =3D true; + + ncm_string_defs[1].s =3D ncm->ethaddr; =20 us =3D usb_gstrings_attach(cdev, ncm_strings, ARRAY_SIZE(ncm_string_defs)); @@ -1578,8 +1563,6 @@ static int ncm_bind(struct usb_configuration *c, stru= ct usb_function *f) f->os_desc_n =3D 1; } ncm->notify_req =3D no_free_ptr(request); - ncm->netdev =3D no_free_ptr(netdev); - ncm->port.ioport =3D netdev_priv(ncm->netdev); =20 DBG(cdev, "CDC Network: IN/%s OUT/%s NOTIFY/%s\n", ncm->port.in_ep->name, ncm->port.out_ep->name, @@ -1594,19 +1577,19 @@ static inline struct f_ncm_opts *to_f_ncm_opts(stru= ct config_item *item) } =20 /* f_ncm_item_ops */ -USB_ETHER_OPTS_ITEM(ncm); +USB_ETHERNET_CONFIGFS_ITEM(ncm); =20 /* f_ncm_opts_dev_addr */ -USB_ETHER_OPTS_ATTR_DEV_ADDR(ncm); +USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm); =20 /* f_ncm_opts_host_addr */ -USB_ETHER_OPTS_ATTR_HOST_ADDR(ncm); +USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm); =20 /* f_ncm_opts_qmult */ -USB_ETHER_OPTS_ATTR_QMULT(ncm); +USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm); =20 /* f_ncm_opts_ifname */ -USB_ETHER_OPTS_ATTR_IFNAME(ncm); +USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm); =20 static ssize_t ncm_opts_max_segment_size_show(struct config_item *item, char *page) @@ -1672,27 +1655,34 @@ static void ncm_free_inst(struct usb_function_insta= nce *f) struct f_ncm_opts *opts; =20 opts =3D container_of(f, struct f_ncm_opts, func_inst); + if (opts->bound) + gether_cleanup(netdev_priv(opts->net)); + else + free_netdev(opts->net); kfree(opts->ncm_interf_group); kfree(opts); } =20 static struct usb_function_instance *ncm_alloc_inst(void) { - struct usb_function_instance *ret; + struct f_ncm_opts *opts; struct usb_os_desc *descs[1]; char *names[1]; struct config_group *ncm_interf_group; =20 - struct f_ncm_opts *opts __free(kfree) =3D kzalloc_obj(*opts); + opts =3D kzalloc_obj(*opts); if (!opts) return ERR_PTR(-ENOMEM); - - opts->net =3D NULL; opts->ncm_os_desc.ext_compat_id =3D opts->ncm_ext_compat_id; - gether_setup_opts_default(&opts->net_opts, "usb"); =20 mutex_init(&opts->lock); opts->func_inst.free_func_inst =3D ncm_free_inst; + opts->net =3D gether_setup_default(); + if (IS_ERR(opts->net)) { + struct net_device *net =3D opts->net; + kfree(opts); + return ERR_CAST(net); + } opts->max_segment_size =3D ETH_FRAME_LEN; INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop); =20 @@ -1703,22 +1693,26 @@ static struct usb_function_instance *ncm_alloc_inst= (void) ncm_interf_group =3D usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs, names, THIS_MODULE); - if (IS_ERR(ncm_interf_group)) + if (IS_ERR(ncm_interf_group)) { + ncm_free_inst(&opts->func_inst); return ERR_CAST(ncm_interf_group); + } opts->ncm_interf_group =3D ncm_interf_group; =20 - ret =3D &opts->func_inst; - retain_and_null_ptr(opts); - return ret; + return &opts->func_inst; } =20 static void ncm_free(struct usb_function *f) { - struct f_ncm_opts *opts =3D func_to_ncm_opts(f); + struct f_ncm *ncm; + struct f_ncm_opts *opts; =20 - scoped_guard(mutex, &opts->lock) - opts->refcnt--; - kfree(func_to_ncm(f)); + ncm =3D func_to_ncm(f); + opts =3D container_of(f->fi, struct f_ncm_opts, func_inst); + kfree(ncm); + mutex_lock(&opts->lock); + opts->refcnt--; + mutex_unlock(&opts->lock); } =20 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) @@ -1742,15 +1736,13 @@ static void ncm_unbind(struct usb_configuration *c,= struct usb_function *f) =20 kfree(ncm->notify_req->buf); usb_ep_free_request(ncm->notify, ncm->notify_req); - - ncm->port.ioport =3D NULL; - gether_cleanup(netdev_priv(ncm->netdev)); } =20 static struct usb_function *ncm_alloc(struct usb_function_instance *fi) { struct f_ncm *ncm; struct f_ncm_opts *opts; + int status; =20 /* allocate and initialize one new instance */ ncm =3D kzalloc(sizeof(*ncm), GFP_KERNEL); @@ -1758,12 +1750,22 @@ static struct usb_function *ncm_alloc(struct usb_fu= nction_instance *fi) return ERR_PTR(-ENOMEM); =20 opts =3D container_of(fi, struct f_ncm_opts, func_inst); + mutex_lock(&opts->lock); + opts->refcnt++; =20 - scoped_guard(mutex, &opts->lock) - opts->refcnt++; + /* export host's Ethernet address in CDC format */ + status =3D gether_get_host_addr_cdc(opts->net, ncm->ethaddr, + sizeof(ncm->ethaddr)); + if (status < 12) { /* strlen("01234567890a") */ + kfree(ncm); + mutex_unlock(&opts->lock); + return ERR_PTR(-EINVAL); + } =20 spin_lock_init(&ncm->lock); ncm_reset_values(ncm); + ncm->port.ioport =3D netdev_priv(opts->net); + mutex_unlock(&opts->lock); ncm->port.is_fixed =3D true; ncm->port.supports_multi_frame =3D true; =20 diff --git a/drivers/usb/gadget/function/u_ncm.h b/drivers/usb/gadget/funct= ion/u_ncm.h index d99330fe31e8..49ec095cdb4b 100644 --- a/drivers/usb/gadget/function/u_ncm.h +++ b/drivers/usb/gadget/function/u_ncm.h @@ -15,13 +15,11 @@ =20 #include =20 -#include "u_ether.h" - struct f_ncm_opts { struct usb_function_instance func_inst; struct net_device *net; + bool bound; =20 - struct gether_opts net_opts; struct config_group *ncm_interf_group; struct usb_os_desc ncm_os_desc; char ncm_ext_compat_id[16]; --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A19513A4F2C for ; Mon, 9 Mar 2026 12:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057925; cv=none; b=kC+tkgCP6XacVCxvK9oHbmj33M9ptcXfEea4dhdcUFn92OmWgGQkZzElx4rF4e8Z6+iXJ7wTXEp/IqhQeCG+pQD+h8BQqmvXePFVV7KpaPOXozn/3aMJaKTsCXctRfHD1qUx4FSeYppjPRlkUJj1cPDfNetoeaW1ZHGffKu/9cs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057925; c=relaxed/simple; bh=CGO0VfQC6nWNWzuPE6zNy8nqRc46IAy8DWObfpSsHuI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VRB4zWZk1TRnpECK0tFJh/3c/wvVEKsASARi9b+ENCYbDvSX0BO367UdFtYpYY6LgcCcN28tAN6iB31ZpBOKyo4A//XFrgW2sLWnFfb+siaikMgJTo31zb9xwDgMbIZT5SIZLRQOcnjGmCtFv8FFBMMSVBRcm2uscE8O/sEUB/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=u4isXG5Z; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="u4isXG5Z" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae47b3adacso77146805ad.3 for ; Mon, 09 Mar 2026 05:05:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057924; x=1773662724; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QMXsEKlWRclFBEv0xYibqErY/p3FsrhNFarJ+RqhwzI=; b=u4isXG5Ze6QNNq4KCvsy+FSloKVpNVB/n8TUHRQZAbLcdt+6alphdR0+7TOqOhB0BQ 0cn82h22dWUfcaWnzp3Ivn/CPGnHmUxffucpGq7KK5+gZzS0cplluFroTOD9q7z6E/f1 cz4Qo9fohpWmPsupU589OLE28fFIZylaPu0X+qVfOV/ePJNp6MWVpzwZ/0deAbJQpnrS GPA/bweU30WOJMGlAJU5f+x/aoyOMRHb1KTjFjMcpZi9DSmiz5IB86fT2zFNh8mD8B4R iGfaVng066TiPMrjJAujqeTOMsjzJeryKgkqYKPYdx4o55rVT8tw8c7SfPV6/a2OkBuk JVOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057924; x=1773662724; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QMXsEKlWRclFBEv0xYibqErY/p3FsrhNFarJ+RqhwzI=; b=CwXdeH/1JOx+vSt+jTfeAXLEwtTgRcH86vmBUUJNePePXW72gMngHEVHS88BhN8rH/ le+OR4YXlDM3kSS4/JXnB25Z5qHvgkMOVoxm64FFadCl+WZbFg696e4a6owe8hUevEYk 9e5luAyV2M/IKAdvRKxgRxaj3hJCdgGIzDRpxiB9PiHubPV0hLLfx+vKaTLrr65qIV3s SVcgRo4lYzhs525op4j81E0gQ/RbErq96btXfJnfH+eBx1d2OSI6wG07mEULi25po3kj OUVqphocKp85ePEvxeBKo7/nZjUnk1SwuTppk7Ksy3DvOMYgEVBWywNg0PwIjxhR8oZE viyw== X-Forwarded-Encrypted: i=1; AJvYcCU98c7YCwO53PJvN0Ug3RzNSAr4cPNfAucDyhE+lSDYV7rXesMXpNspjg9OEFU2ER9l/QFvOd4KnpJFRTc=@vger.kernel.org X-Gm-Message-State: AOJu0Ywj1QokCvA2DmqQlAJdKOOmelEBEbW/CKj25eIJSmmmd4UQeIQ7 M8O7L3plmFaiWq3qQQN0ZFlQ6vPl+DbPZ/TkbnNJaQ3p05L6WQEaXzy5QXwsGRy3KIR6P10cHTe cAzSFEA== X-Received: from ploh7.prod.google.com ([2002:a17:902:f707:b0:2ae:69d3:5b9d]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f70f:b0:2ae:4409:5993 with SMTP id d9443c01a7336-2ae82566029mr113712635ad.55.1773057923863; Mon, 09 Mar 2026 05:05:23 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:49 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=3155; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=CGO0VfQC6nWNWzuPE6zNy8nqRc46IAy8DWObfpSsHuI=; b=/M9ZIevcQVfuTs7LJ/pfq4hvrqTG4FI4H71uy/WOxzWy2i1cI8s+nYuaWQf7oRmEINKFx0aDn Cbcx3+38AEODyn3K85GlNH10cLMepyezAH4S45tCesESKniS2fgXN1N X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-4-ea2afbc7d9b2@google.com> Subject: [PATCH v2 4/7] Revert "usb: gadget: u_ether: Add auto-cleanup helper for freeing net_device" From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This reverts commit 0c0981126b99288ed354d3d414c8a5fd42ac9e25. 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: David Heidelberg Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c95113124= 9af@ixit.cz/ Fixes: 56a512a9b410 ("usb: gadget: f_ncm: align net_device lifecycle with b= ind/unbind") Signed-off-by: Kuen-Han Tsai Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/function/u_ether.c | 15 --------------- drivers/usb/gadget/function/u_ether.h | 2 -- 2 files changed, 17 deletions(-) diff --git a/drivers/usb/gadget/function/u_ether.c b/drivers/usb/gadget/fun= ction/u_ether.c index 338f6e2a85a9..15685b2f8887 100644 --- a/drivers/usb/gadget/function/u_ether.c +++ b/drivers/usb/gadget/function/u_ether.c @@ -1126,21 +1126,6 @@ void gether_cleanup(struct eth_dev *dev) } EXPORT_SYMBOL_GPL(gether_cleanup); =20 -void gether_unregister_free_netdev(struct net_device *net) -{ - if (!net) - return; - - struct eth_dev *dev =3D netdev_priv(net); - - if (net->reg_state =3D=3D NETREG_REGISTERED) { - unregister_netdev(net); - flush_work(&dev->work); - } - free_netdev(net); -} -EXPORT_SYMBOL_GPL(gether_unregister_free_netdev); - /** * gether_connect - notify network layer that USB link is active * @link: the USB link, set up with endpoints, descriptors matching diff --git a/drivers/usb/gadget/function/u_ether.h b/drivers/usb/gadget/fun= ction/u_ether.h index a212a8ec5eb1..63a0240df4d7 100644 --- a/drivers/usb/gadget/function/u_ether.h +++ b/drivers/usb/gadget/function/u_ether.h @@ -283,8 +283,6 @@ int gether_get_ifname(struct net_device *net, char *nam= e, int len); int gether_set_ifname(struct net_device *net, const char *name, int len); =20 void gether_cleanup(struct eth_dev *dev); -void gether_unregister_free_netdev(struct net_device *net); -DEFINE_FREE(free_gether_netdev, struct net_device *, gether_unregister_fre= e_netdev(_T)); =20 void gether_setup_opts_default(struct gether_opts *opts, const char *name); void gether_apply_opts(struct net_device *net, struct gether_opts *opts); --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EC063A9D88 for ; Mon, 9 Mar 2026 12:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057927; cv=none; b=sQS31jLYuboWRRFtBUifa3Zia2sZLUH9C7t7nrVGinyIK5enu33qjVxZv3c9f8gif0HJpf9UxfjfFmMJVzE4HM6fkLFDPBZ3Q6Iaj+FGN6ePIsHFZoLM9q3fjSBQUQnDGXcL+F3/H7JMixG5/rXma6ebnl3DWVjUHGoSB84msw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057927; c=relaxed/simple; bh=VjRaSPsUVE+x/Xi7XlDdoEy4TQXTYs9VwSowe6Il3dE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VLuL6lP+MoUPGvPFGmfiPgOdgfR9WOQrum08CxM8zHJH9LHcMQaLc3LWZu8AUhu9lqFthc9zzLiu4emDQUKcW7FFgloj4dIPQK+K8G42JrxIBLhNfui9UesYByr5eRrkMAyhWU/fYyoCCsw1Ur4hDsb5kR5BYTC1uxKnWbWWhv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mtlORqIy; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mtlORqIy" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae3e462daeso42902325ad.0 for ; Mon, 09 Mar 2026 05:05:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057926; x=1773662726; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ABqRA1fkx2Iasdc5RcVowDEiALyuzvn89TbSr3zsxLo=; b=mtlORqIydu3tyjgEfy7K++xJQi49Mg0jhI0yUW4yYgPtVCcS6hbx80qktKkOxVUtGz 5VUUafIDUkCZWBtFyJvWRc2iRzuwQPiy74s+nckd1cr7mB4rdOlUOHAKNGIvEdRH86PQ siu8Ho3TDo9JacVYFdz3IsEY941gAA+gjha5L0rQdjpViZQOJOHk7K8QS2f0mQIkTioF Sq+ctyXRxwJmBQX9aFl8NzC5D4CvVMJAhXTJcfp/QH9qWbi/5OYhcxARYbTTtS6vaaue yNL5LTBbu3sfPuKG1+FQ7olKGwdWL+oGHSP8KEBbpDXT4aa1a5ILt2+Gej6hCecmeoJ3 5oPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057926; x=1773662726; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ABqRA1fkx2Iasdc5RcVowDEiALyuzvn89TbSr3zsxLo=; b=L9cKiibrl9fvuEkj5tIeBtl5mtluMMjA3NmhS4JgJ0zX2LprK/ZdDXnhfYgXkNQlBn Ut4Z5bEIXkUFoXTgF9T4lXLAZBabdNO3HRnSIxVRj16qocVi58mAqmlcyw9I5Z0oWy0l oP4PYr4v5320J3HuiOsTDyyZZI111OuveuEIklBBr7xgCI+3j/o8O6KJupd94BL63w+R 4WPDVH/Gqy/f4bWb3AVcROhtXt6xPqqR08UlNN2E7WtE9/c+cGRZr51MNbyga3s1DIyY SrKmpZgYDdFVGF9dC9oLV6RtiZxJi4eN0oJXjrgALLwXCR45QvsLDBk7gh2HKPb1HCZE ymlw== X-Forwarded-Encrypted: i=1; AJvYcCVHY+a6SsYl5DbMHUbciq5zDOyB9GARr9n8UACnF9eEIvmDDtNzRnKBVL82giNOC9lSrPcoxc829lz35co=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7MtyPCcFq04yNTqGoLSez9koY9jJn7JPEuWBX13bAXjWuMPMh mAPYRdnaNj6Sl6XGU3SfrBIIG8f4dIMhb7rbpPW5UnJ4mDLorFS651bmKHfDFh9YDyk8RC/nAfw TFi8aug== X-Received: from plbkh13.prod.google.com ([2002:a17:903:64d:b0:2ae:6338:73bd]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:cf08:b0:2ae:4e20:9d05 with SMTP id d9443c01a7336-2ae824dd318mr105187195ad.35.1773057925753; Mon, 09 Mar 2026 05:05:25 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:50 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=2006; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=VjRaSPsUVE+x/Xi7XlDdoEy4TQXTYs9VwSowe6Il3dE=; b=eZH/YwaVqvhmkVfOaxIxfPQzVR1AH6vNtrWh1DD/LMuzoOAP4fCC+EE+cDPDdFgPpbK2hPNjR QCyHGW32ZULCZsHrLKMD7bwqa3JUv1CqztgOwj6EU41N3fGkEFxdl8V X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-5-ea2afbc7d9b2@google.com> Subject: [PATCH v2 5/7] Revert "usb: gadget: u_ether: use header file" From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This reverts commit 7a7930c0f934fb0c46de6e7ca08e14e11df35dd6. 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: David Heidelberg Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c95113124= 9af@ixit.cz/ Fixes: 56a512a9b410 ("usb: gadget: f_ncm: align net_device lifecycle with b= ind/unbind") Signed-off-by: Kuen-Han Tsai Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/function/u_ether_configfs.h | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/usb/gadget/function/u_ether_configfs.h b/drivers/usb/g= adget/function/u_ether_configfs.h index 217990a266b2..39d3a261496d 100644 --- a/drivers/usb/gadget/function/u_ether_configfs.h +++ b/drivers/usb/gadget/function/u_ether_configfs.h @@ -14,7 +14,6 @@ #define __U_ETHER_CONFIGFS_H =20 #include -#include #include #include #include --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7448B3A782A for ; Mon, 9 Mar 2026 12:05:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057930; cv=none; b=TSTbfd24HFhuomxg2t4Mu6ma5fbynaVkAmq9FX9rd5N2bvGl+6BJ2lrNDzKAL9ZgZ8HyIlSDX0akST9zgUuWdE0QmcH18TjCf5cILFBO05AA3VXZxTutF2gMtEzqNVGddACtWf5WlqSHbqo2C6g+nJsTO8wrpGQzAEmgHyKB9Z8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057930; c=relaxed/simple; bh=/m3b4zqGc1iZkhOBcgJHOpbEiI99gvlL58opOGTdGyE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jEYjxcyonj8EeDoh+Y7hbVFFEw9J/KmE31wnCeES+PlRnat/XdafK9lcjdgu0hXVMXT/K4RjzkBmhr/QDh7qex25IW9oc1iySb6Ezg0kxxTjUHh+9l3UPudQj3idMKJwGRX5KAEamOGIQed8fRGVtyi+ZMJq3nJf9bAd1GLICPA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZnlqewVy; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZnlqewVy" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae66ee7354so59107365ad.0 for ; Mon, 09 Mar 2026 05:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057928; x=1773662728; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=I/6AkYd5gYPDrKscM6W7dnA+in+IO1rRT3m4qc7cgao=; b=ZnlqewVyBJAqJ3o/liioT+rBtQGmDUX8cZHY7Grj0bO9shhQ2i965vAeE3Lwy7URVM 41ki/pGMBWHZuPMb6ctMBNsKcmaSA9r0RyDBYUrQdhmC349hg/Yro1UOimvQVE99Dkd2 9o0covG0y4Xv1UR9snTBeSaHzaLnzSP2jDZSdfQTdZIqLVeMsFrzPkftOrTjOOUJWxLx zm4GgBZ3U3YrigKfEwbOSNduEEDEwU3TA55aL1lTBPd14LEBscgX48W/VNOYLcNuHqbe iaQjbgnn5CYzbciLargajMsvxxN4U8q0wIhH5He01Qng0f9JaW9hV2caeCGyxj4c9pHo /XFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057928; x=1773662728; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=I/6AkYd5gYPDrKscM6W7dnA+in+IO1rRT3m4qc7cgao=; b=O69TdGnpYDjh7Gm8BqJgjO90DzqhVMgenv+Fpn2MGhaSTCRtCmMibvLL5w38NacDWq pzhDpSzO7gHP4zffbKTs9gAxb2uVVR7L/mzEZhauv0UAvA1wHnHadHTtLfUKfBjzYWY0 odwgYr7cuCgEHBz76rTs8Ux0p6rIANKoeIsSUPBn8yeom1uwXN9kht4eB8bRiMgpPNrW 7I4YBTBO2ZvBFLCFFSxjml5NTlfMBA0QaXlKGeBviq7AchZjxuCkbuA2I61Q4kYLSivm 3kSsturkK9lhTtjYZFsrBO8oYUJegpfwxeaqRMgNOGvtI++kDygYoB55p+Xta+VCOGMY kRbw== X-Forwarded-Encrypted: i=1; AJvYcCVdL0RmLpIgc9MtkHD6MothlAaRDJUTqrwl2Mc147G/41QUCV0tqy3x93IwiAXtaaO8gkmJ6JdnE4iPc1M=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+NCr+xF4eV5NV/zZ3DS6bDXSOGphP2Uje2Anb75Oc/AHcw97S f0elf4WRUJk8iIbDQe4JGEw3KImiNt83N3SctPMprJtb85FeGdAo0Nj2ieMGtyeuAfy+1l7tKYI 4uXtVJA== X-Received: from pluo12.prod.google.com ([2002:a17:903:4b0c:b0:2a9:6366:197c]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3bc7:b0:2ae:3bb2:a8cb with SMTP id d9443c01a7336-2ae825046e0mr105886065ad.40.1773057927790; Mon, 09 Mar 2026 05:05:27 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:51 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=11207; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=/m3b4zqGc1iZkhOBcgJHOpbEiI99gvlL58opOGTdGyE=; b=4kp8mfQy1hVr+bmdAXORHtGyriFERkOL/AeLV25sn3IsDmaEp9u9IqK99qP8andRF1HDrFEXk YPo8DYN8ljcBwWmHiDTnHl6Rg5M4JSfjFJl8OIwDEVF2i21jjwuKEkQ X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-6-ea2afbc7d9b2@google.com> Subject: [PATCH v2 6/7] Revert "usb: gadget: u_ether: add gether_opts for config caching" From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This reverts commit e065c6a7e46c2ee9c677fdbf50035323d2de1215. 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: David Heidelberg Closes: https://lore.kernel.org/linux-usb/70b558ea-a12e-4170-9b8e-c95113124= 9af@ixit.cz/ Fixes: 56a512a9b410 ("usb: gadget: f_ncm: align net_device lifecycle with b= ind/unbind") Signed-off-by: Kuen-Han Tsai Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/function/u_ether.c | 30 ----- drivers/usb/gadget/function/u_ether.h | 28 ---- drivers/usb/gadget/function/u_ether_configfs.h | 176 ---------------------= ---- 3 files changed, 234 deletions(-) diff --git a/drivers/usb/gadget/function/u_ether.c b/drivers/usb/gadget/fun= ction/u_ether.c index 15685b2f8887..c47965d850d4 100644 --- a/drivers/usb/gadget/function/u_ether.c +++ b/drivers/usb/gadget/function/u_ether.c @@ -1040,36 +1040,6 @@ int gether_set_ifname(struct net_device *net, const = char *name, int len) } EXPORT_SYMBOL_GPL(gether_set_ifname); =20 -void gether_setup_opts_default(struct gether_opts *opts, const char *name) -{ - opts->qmult =3D QMULT_DEFAULT; - snprintf(opts->name, sizeof(opts->name), "%s%%d", name); - eth_random_addr(opts->dev_mac); - opts->addr_assign_type =3D NET_ADDR_RANDOM; - eth_random_addr(opts->host_mac); -} -EXPORT_SYMBOL_GPL(gether_setup_opts_default); - -void gether_apply_opts(struct net_device *net, struct gether_opts *opts) -{ - struct eth_dev *dev =3D netdev_priv(net); - - dev->qmult =3D opts->qmult; - - if (opts->ifname_set) { - strscpy(net->name, opts->name, sizeof(net->name)); - dev->ifname_set =3D true; - } - - memcpy(dev->host_mac, opts->host_mac, sizeof(dev->host_mac)); - - if (opts->addr_assign_type =3D=3D NET_ADDR_SET) { - memcpy(dev->dev_mac, opts->dev_mac, sizeof(dev->dev_mac)); - net->addr_assign_type =3D opts->addr_assign_type; - } -} -EXPORT_SYMBOL_GPL(gether_apply_opts); - void gether_suspend(struct gether *link) { struct eth_dev *dev =3D link->ioport; diff --git a/drivers/usb/gadget/function/u_ether.h b/drivers/usb/gadget/fun= ction/u_ether.h index 63a0240df4d7..34be220cef77 100644 --- a/drivers/usb/gadget/function/u_ether.h +++ b/drivers/usb/gadget/function/u_ether.h @@ -38,31 +38,6 @@ =20 struct eth_dev; =20 -/** - * struct gether_opts - Options for Ethernet gadget function instances - * @name: Pattern for the network interface name (e.g., "usb%d"). - * Used to generate the net device name. - * @qmult: Queue length multiplier for high/super speed. - * @host_mac: The MAC address to be used by the host side. - * @dev_mac: The MAC address to be used by the device side. - * @ifname_set: True if the interface name pattern has been set by userspa= ce. - * @addr_assign_type: The method used for assigning the device MAC address - * (e.g., NET_ADDR_RANDOM, NET_ADDR_SET). - * - * This structure caches network-related settings provided through configfs - * before the net_device is fully instantiated. This allows for early - * configuration while deferring net_device allocation until the function - * is bound. - */ -struct gether_opts { - char name[IFNAMSIZ]; - unsigned int qmult; - u8 host_mac[ETH_ALEN]; - u8 dev_mac[ETH_ALEN]; - bool ifname_set; - unsigned char addr_assign_type; -}; - /* * This represents the USB side of an "ethernet" link, managed by a USB * function which provides control and (maybe) framing. Two functions @@ -284,9 +259,6 @@ int gether_set_ifname(struct net_device *net, const cha= r *name, int len); =20 void gether_cleanup(struct eth_dev *dev); =20 -void gether_setup_opts_default(struct gether_opts *opts, const char *name); -void gether_apply_opts(struct net_device *net, struct gether_opts *opts); - void gether_suspend(struct gether *link); void gether_resume(struct gether *link); =20 diff --git a/drivers/usb/gadget/function/u_ether_configfs.h b/drivers/usb/g= adget/function/u_ether_configfs.h index 39d3a261496d..51f0d79e5eca 100644 --- a/drivers/usb/gadget/function/u_ether_configfs.h +++ b/drivers/usb/gadget/function/u_ether_configfs.h @@ -13,12 +13,6 @@ #ifndef __U_ETHER_CONFIGFS_H #define __U_ETHER_CONFIGFS_H =20 -#include -#include -#include -#include -#include - #define USB_ETHERNET_CONFIGFS_ITEM(_f_) \ static void _f_##_attr_release(struct config_item *item) \ { \ @@ -203,174 +197,4 @@ out: \ \ CONFIGFS_ATTR(_f_##_opts_, _n_) =20 -#define USB_ETHER_OPTS_ITEM(_f_) \ - static void _f_##_attr_release(struct config_item *item) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - \ - usb_put_function_instance(&opts->func_inst); \ - } \ - \ - static struct configfs_item_operations _f_##_item_ops =3D { \ - .release =3D _f_##_attr_release, \ - } - -#define USB_ETHER_OPTS_ATTR_DEV_ADDR(_f_) \ - static ssize_t _f_##_opts_dev_addr_show(struct config_item *item, \ - char *page) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - \ - guard(mutex)(&opts->lock); \ - return sysfs_emit(page, "%pM\n", opts->net_opts.dev_mac); \ - } \ - \ - static ssize_t _f_##_opts_dev_addr_store(struct config_item *item, \ - const char *page, size_t len) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - u8 new_addr[ETH_ALEN]; \ - const char *p =3D page; \ - \ - guard(mutex)(&opts->lock); \ - if (opts->refcnt) \ - return -EBUSY; \ - \ - for (int i =3D 0; i < ETH_ALEN; i++) { \ - unsigned char num; \ - if ((*p =3D=3D '.') || (*p =3D=3D ':')) \ - p++; \ - num =3D hex_to_bin(*p++) << 4; \ - num |=3D hex_to_bin(*p++); \ - new_addr[i] =3D num; \ - } \ - if (!is_valid_ether_addr(new_addr)) \ - return -EINVAL; \ - memcpy(opts->net_opts.dev_mac, new_addr, ETH_ALEN); \ - opts->net_opts.addr_assign_type =3D NET_ADDR_SET; \ - return len; \ - } \ - \ - CONFIGFS_ATTR(_f_##_opts_, dev_addr) - -#define USB_ETHER_OPTS_ATTR_HOST_ADDR(_f_) \ - static ssize_t _f_##_opts_host_addr_show(struct config_item *item, \ - char *page) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - \ - guard(mutex)(&opts->lock); \ - return sysfs_emit(page, "%pM\n", opts->net_opts.host_mac); \ - } \ - \ - static ssize_t _f_##_opts_host_addr_store(struct config_item *item, \ - const char *page, size_t len) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - u8 new_addr[ETH_ALEN]; \ - const char *p =3D page; \ - \ - guard(mutex)(&opts->lock); \ - if (opts->refcnt) \ - return -EBUSY; \ - \ - for (int i =3D 0; i < ETH_ALEN; i++) { \ - unsigned char num; \ - if ((*p =3D=3D '.') || (*p =3D=3D ':')) \ - p++; \ - num =3D hex_to_bin(*p++) << 4; \ - num |=3D hex_to_bin(*p++); \ - new_addr[i] =3D num; \ - } \ - if (!is_valid_ether_addr(new_addr)) \ - return -EINVAL; \ - memcpy(opts->net_opts.host_mac, new_addr, ETH_ALEN); \ - return len; \ - } \ - \ - CONFIGFS_ATTR(_f_##_opts_, host_addr) - -#define USB_ETHER_OPTS_ATTR_QMULT(_f_) \ - static ssize_t _f_##_opts_qmult_show(struct config_item *item, \ - char *page) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - \ - guard(mutex)(&opts->lock); \ - return sysfs_emit(page, "%u\n", opts->net_opts.qmult); \ - } \ - \ - static ssize_t _f_##_opts_qmult_store(struct config_item *item, \ - const char *page, size_t len) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - u32 val; \ - int ret; \ - \ - guard(mutex)(&opts->lock); \ - if (opts->refcnt) \ - return -EBUSY; \ - \ - ret =3D kstrtou32(page, 0, &val); \ - if (ret) \ - return ret; \ - \ - opts->net_opts.qmult =3D val; \ - return len; \ - } \ - \ - CONFIGFS_ATTR(_f_##_opts_, qmult) - -#define USB_ETHER_OPTS_ATTR_IFNAME(_f_) \ - static ssize_t _f_##_opts_ifname_show(struct config_item *item, \ - char *page) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - const char *name; \ - \ - guard(mutex)(&opts->lock); \ - rtnl_lock(); \ - if (opts->net_opts.ifname_set) \ - name =3D opts->net_opts.name; \ - else if (opts->net) \ - name =3D netdev_name(opts->net); \ - else \ - name =3D "(inactive net_device)"; \ - rtnl_unlock(); \ - return sysfs_emit(page, "%s\n", name); \ - } \ - \ - static ssize_t _f_##_opts_ifname_store(struct config_item *item, \ - const char *page, size_t len) \ - { \ - struct f_##_f_##_opts *opts =3D to_f_##_f_##_opts(item); \ - char tmp[IFNAMSIZ]; \ - const char *p; \ - size_t c_len =3D len; \ - \ - if (c_len > 0 && page[c_len - 1] =3D=3D '\n') \ - c_len--; \ - \ - if (c_len >=3D sizeof(tmp)) \ - return -E2BIG; \ - \ - strscpy(tmp, page, c_len + 1); \ - if (!dev_valid_name(tmp)) \ - return -EINVAL; \ - \ - /* Require exactly one %d */ \ - p =3D strchr(tmp, '%'); \ - if (!p || p[1] !=3D 'd' || strchr(p + 2, '%')) \ - return -EINVAL; \ - \ - guard(mutex)(&opts->lock); \ - if (opts->refcnt) \ - return -EBUSY; \ - strscpy(opts->net_opts.name, tmp, sizeof(opts->net_opts.name)); \ - opts->net_opts.ifname_set =3D true; \ - return len; \ - } \ - \ - CONFIGFS_ATTR(_f_##_opts_, ifname) - #endif /* __U_ETHER_CONFIGFS_H */ --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 11:17:00 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E20833AE6E1 for ; Mon, 9 Mar 2026 12:05:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057933; cv=none; b=jmZYmijwAjS+wbvmO6OBBM0fF7c0/F/+3pgrSjDZ+ui5gr0eYZ8H6n1jTHwGmrFg6YnMoM/TkWYfzKnnCjMNOcssScKMIbZ6ChdC7PMsXSieakPkntdMcTdvIWraplHTuPuoLrZ4CsM4VU2nehNkYYk7UnioIRSRKZRZW0epYFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773057933; c=relaxed/simple; bh=GsIj0V/W8/cYPN0Hgl6XmEV91oEhDLD4KTWD5WDQI9o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pda7xzFL2p3yDMIS6VwaqNZ/DxZ4TG+YNUai3nhWngzceup4BJ0/gXxs+GPb+3GQ3DoB+JtFSChtpW1F1NAHikU/Ny9GuwqlGYuYTTdylfyjXO/NJWGPQWJ+jF3dhQsstFzIHEpAwyaPXXtvncQ+wGp/COIye/pHk0764OxWQFI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=U1Um76OF; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--khtsai.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="U1Um76OF" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2ae53ec06b0so395305475ad.0 for ; Mon, 09 Mar 2026 05:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773057930; x=1773662730; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=914jXMIZuPaEXfT7vA7eUV/wOyKsEtHPgtfCXws0Nlk=; b=U1Um76OFyZgppGOCY/VGjLTGumCX9X3RwS4FE1I+XMR0zf6tjRIaVGRiX8FUie6ocF HFR3pETRq8hc+xjKGMC0NPhoKS1RDitWa2zIQwOKHzHGEuluT2ne/j0jF4rkyt0Z7VOP 1GnhO6/wu+E94a4msEuhISH04v7uFCTzLz/tt5514Vl9rtqG0KimAFuyWgZCE4fRhpp3 b2flPYqCGfCP0yam9uZYba6T2nutzOclOYLCgf6aBAhZuDF4EPZGOi6Gx4YhujQSrKAz EokiVQtxpMIbgIOaqRTlUCSfAylYJltxTo3FzSS5OM6XCerv5881gmCNEpwy+J6pnnie ueig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773057930; x=1773662730; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=914jXMIZuPaEXfT7vA7eUV/wOyKsEtHPgtfCXws0Nlk=; b=XobER2FbeLP76hv8C3OU2ysgL1N7IWdqfoy9daOep783gqadIyVG5Z7ynD6nX4z0cO 8Jab7gisNderwUVhHc4fTj+fOe/R7A1IIwJIaYQ/eByfuTOnWTdfPON04MU8DmFfA1aj vjkcBGRyyluqiC44Xy7ZSgQWUJMiFNOJG4GI7te3KWKKRZAJ+2I+bun6kk9jPfY9RWsK 4dFxBXJyzYyX5mrgE62AJugODsYdydW+kOS7YvL/nq8FEFTujUOkoiRSsKvwDFegYISF wkSeK66vNo9rXUNe3drPWczS0bSj2Z2FFlYLjsXk31GSTRTULZcT4oUKb1wI8kF5r/aZ FRXA== X-Forwarded-Encrypted: i=1; AJvYcCXIZ9/lSYsfFOHg5vjXmVgiVYgNqJLYiN4/CmTEu/rnF3sj3AceqXmSLLYwgW1l8hMZre8Lf11DcAtzHGc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7RFTpXLA10BwvQLtnvVefT26ns3UgNJS6se7tqlXJNHUZof/F oW2nEGsEAknxDYzWPYgM/PrAvk7KKeouZ60sEDlW2p9zF40SQAnMsbNluADADB7trQvwx/9s9bW ccbCJsA== X-Received: from plhs17.prod.google.com ([2002:a17:903:3211:b0:2ae:5628:a179]) (user=khtsai job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3bac:b0:2ae:8081:c5a0 with SMTP id d9443c01a7336-2ae8245e0afmr118496885ad.39.1773057929786; Mon, 09 Mar 2026 05:05:29 -0700 (PDT) Date: Mon, 09 Mar 2026 20:04:52 +0800 In-Reply-To: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-f-ncm-revert-v2-0-ea2afbc7d9b2@google.com> X-Developer-Key: i=khtsai@google.com; a=ed25519; pk=abA4Pw6dY2ZufSbSXW9mtp7xiv1AVPtgRhCFWJSEqLE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773057914; l=7722; i=khtsai@google.com; s=20250916; h=from:subject:message-id; bh=GsIj0V/W8/cYPN0Hgl6XmEV91oEhDLD4KTWD5WDQI9o=; b=Tg/TDaqGuZtbKqIO9q5YE8/O1xrVFcNRF8sC7I/ibKuO/9MS/t9NKR01mAJBWXuLNchL+Foq8 Edq0/YLe7N/CpwXZHXcksOmcjYdFxlcUTynR6mIF4mxVFZwo7MCENq5 X-Mailer: b4 0.14.3 Message-ID: <20260309-f-ncm-revert-v2-7-ea2afbc7d9b2@google.com> Subject: [PATCH v2 7/7] usb: gadget: f_ncm: Fix net_device lifecycle with device_move From: Kuen-Han Tsai To: Greg Kroah-Hartman , Felipe Balbi , Kyungmin Park Cc: David Heidelberg , Ernest Van Hoecke , Jon Hunter , LI Qingwu , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Kuen-Han Tsai , stable@kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The network device outlived its parent gadget device during disconnection, resulting in dangling sysfs links and null pointer dereference problems. A prior attempt to solve this by removing SET_NETDEV_DEV entirely [1] was reverted due to power management ordering concerns and a NO-CARRIER regression. A subsequent attempt to defer net_device allocation to bind [2] broke 1:1 mapping between function instance and network device, making it impossible for configfs to report the resolved interface name. This results in a regression where the DHCP server fails on pmOS. Use device_move to reparent the net_device between the gadget device and /sys/devices/virtual/ across bind/unbind cycles. This preserves the network interface across USB reconnection, allowing the DHCP server to retain their binding. Introduce gether_attach_gadget()/gether_detach_gadget() helpers and use __free(detach_gadget) macro to undo attachment on bind failure. The bind_count ensures device_move executes only on the first bind. [1] https://lore.kernel.org/lkml/f2a4f9847617a0929d62025748384092e5f35cce.c= amel@crapouillou.net/ [2] https://lore.kernel.org/linux-usb/795ea759-7eaf-4f78-81f4-01ffbf2d7961@= ixit.cz/ Fixes: 40d133d7f542 ("usb: gadget: f_ncm: convert to new function interface= with backward compatibility") Cc: stable@kernel.org Signed-off-by: Kuen-Han Tsai Reported-by: David Heidelberg Tested-by: Luca Weiss # milos-fairphone-fp6 --- drivers/usb/gadget/function/f_ncm.c | 38 +++++++++++++++++++++++--------= ---- drivers/usb/gadget/function/u_ether.c | 22 ++++++++++++++++++++ drivers/usb/gadget/function/u_ether.h | 26 ++++++++++++++++++++++++ drivers/usb/gadget/function/u_ncm.h | 2 +- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/funct= ion/f_ncm.c index 3d772c9beb91..a6fa5ed3d6cb 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -1439,6 +1439,7 @@ static int ncm_bind(struct usb_configuration *c, stru= ct usb_function *f) struct f_ncm_opts *ncm_opts; =20 struct usb_os_desc_table *os_desc_table __free(kfree) =3D NULL; + struct net_device *net __free(detach_gadget) =3D NULL; struct usb_request *request __free(free_usb_request) =3D NULL; =20 if (!can_support_ecm(cdev->gadget)) @@ -1452,18 +1453,19 @@ static int ncm_bind(struct usb_configuration *c, st= ruct usb_function *f) return -ENOMEM; } =20 - mutex_lock(&ncm_opts->lock); - gether_set_gadget(ncm_opts->net, cdev->gadget); - if (!ncm_opts->bound) { - ncm_opts->net->mtu =3D (ncm_opts->max_segment_size - ETH_HLEN); - status =3D gether_register_netdev(ncm_opts->net); - } - mutex_unlock(&ncm_opts->lock); - - if (status) - return status; - - ncm_opts->bound =3D true; + scoped_guard(mutex, &ncm_opts->lock) + if (ncm_opts->bind_count =3D=3D 0) { + if (!device_is_registered(&ncm_opts->net->dev)) { + ncm_opts->net->mtu =3D (ncm_opts->max_segment_size - ETH_HLEN); + gether_set_gadget(ncm_opts->net, cdev->gadget); + status =3D gether_register_netdev(ncm_opts->net); + } else + status =3D gether_attach_gadget(ncm_opts->net, cdev->gadget); + + if (status) + return status; + net =3D ncm_opts->net; + } =20 ncm_string_defs[1].s =3D ncm->ethaddr; =20 @@ -1564,6 +1566,9 @@ static int ncm_bind(struct usb_configuration *c, stru= ct usb_function *f) } ncm->notify_req =3D no_free_ptr(request); =20 + ncm_opts->bind_count++; + retain_and_null_ptr(net); + DBG(cdev, "CDC Network: IN/%s OUT/%s NOTIFY/%s\n", ncm->port.in_ep->name, ncm->port.out_ep->name, ncm->notify->name); @@ -1655,7 +1660,7 @@ static void ncm_free_inst(struct usb_function_instanc= e *f) struct f_ncm_opts *opts; =20 opts =3D container_of(f, struct f_ncm_opts, func_inst); - if (opts->bound) + if (device_is_registered(&opts->net->dev)) gether_cleanup(netdev_priv(opts->net)); else free_netdev(opts->net); @@ -1718,9 +1723,12 @@ static void ncm_free(struct usb_function *f) static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) { struct f_ncm *ncm =3D func_to_ncm(f); + struct f_ncm_opts *ncm_opts; =20 DBG(c->cdev, "ncm unbind\n"); =20 + ncm_opts =3D container_of(f->fi, struct f_ncm_opts, func_inst); + hrtimer_cancel(&ncm->task_timer); =20 kfree(f->os_desc_table); @@ -1736,6 +1744,10 @@ static void ncm_unbind(struct usb_configuration *c, = struct usb_function *f) =20 kfree(ncm->notify_req->buf); usb_ep_free_request(ncm->notify, ncm->notify_req); + + ncm_opts->bind_count--; + if (ncm_opts->bind_count =3D=3D 0) + gether_detach_gadget(ncm_opts->net); } =20 static struct usb_function *ncm_alloc(struct usb_function_instance *fi) diff --git a/drivers/usb/gadget/function/u_ether.c b/drivers/usb/gadget/fun= ction/u_ether.c index c47965d850d4..1a9e7c495e2e 100644 --- a/drivers/usb/gadget/function/u_ether.c +++ b/drivers/usb/gadget/function/u_ether.c @@ -897,6 +897,28 @@ void gether_set_gadget(struct net_device *net, struct = usb_gadget *g) } EXPORT_SYMBOL_GPL(gether_set_gadget); =20 +int gether_attach_gadget(struct net_device *net, struct usb_gadget *g) +{ + int ret; + + ret =3D device_move(&net->dev, &g->dev, DPM_ORDER_DEV_AFTER_PARENT); + if (ret) + return ret; + + gether_set_gadget(net, g); + return 0; +} +EXPORT_SYMBOL_GPL(gether_attach_gadget); + +void gether_detach_gadget(struct net_device *net) +{ + struct eth_dev *dev =3D netdev_priv(net); + + device_move(&net->dev, NULL, DPM_ORDER_NONE); + dev->gadget =3D NULL; +} +EXPORT_SYMBOL_GPL(gether_detach_gadget); + int gether_set_dev_addr(struct net_device *net, const char *dev_addr) { struct eth_dev *dev; diff --git a/drivers/usb/gadget/function/u_ether.h b/drivers/usb/gadget/fun= ction/u_ether.h index 34be220cef77..c85a1cf3c115 100644 --- a/drivers/usb/gadget/function/u_ether.h +++ b/drivers/usb/gadget/function/u_ether.h @@ -150,6 +150,32 @@ static inline struct net_device *gether_setup_default(= void) */ void gether_set_gadget(struct net_device *net, struct usb_gadget *g); =20 +/** + * gether_attach_gadget - Reparent net_device to the gadget device. + * @net: The network device to reparent. + * @g: The target USB gadget device to parent to. + * + * This function moves the network device to be a child of the USB gadget + * device in the device hierarchy. This is typically done when the function + * is bound to a configuration. + * + * Returns 0 on success, or a negative error code on failure. + */ +int gether_attach_gadget(struct net_device *net, struct usb_gadget *g); + +/** + * gether_detach_gadget - Detach net_device from its gadget parent. + * @net: The network device to detach. + * + * This function moves the network device to be a child of the virtual + * devices parent, effectively detaching it from the USB gadget device + * hierarchy. This is typically done when the function is unbound + * from a configuration but the instance is not yet freed. + */ +void gether_detach_gadget(struct net_device *net); + +DEFINE_FREE(detach_gadget, struct net_device *, if (_T) gether_detach_gadg= et(_T)) + /** * gether_set_dev_addr - initialize an ethernet-over-usb link with eth add= ress * @net: device representing this link diff --git a/drivers/usb/gadget/function/u_ncm.h b/drivers/usb/gadget/funct= ion/u_ncm.h index 49ec095cdb4b..b1f3db8b68c1 100644 --- a/drivers/usb/gadget/function/u_ncm.h +++ b/drivers/usb/gadget/function/u_ncm.h @@ -18,7 +18,7 @@ struct f_ncm_opts { struct usb_function_instance func_inst; struct net_device *net; - bool bound; + int bind_count; =20 struct config_group *ncm_interf_group; struct usb_os_desc ncm_os_desc; --=20 2.53.0.473.g4a7958ca14-goog