From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 85A9A265623 for ; Sun, 14 Dec 2025 18:29:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765736951; cv=none; b=tCVCuiyGjScjEwwIh0UiiPP92Q699MgAotgIFtpo1n27FnYQAdyspkUdYKm2gmPA3TcPOgLdRksAWr2j7QblDZ3QOojQMd/lc0oAy4IJOe1deSUrQorzmZKbvbEJmEARocHs9WaPNppujywhvsUcvXaWwgVCpy3GgZCmKnFS5Eo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765736951; c=relaxed/simple; bh=/8isi1cKHZhAhrD8kVnVCU5d2bq2C2Qe2cEca+QwxXU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mFs1KWnMfZgAvs8HpXblBIUl5nguknGZed4vvpIByrNUuyDS3USJYuNWkQbdrB/2MlpaMAMonGGiY7TrCQvF8+Kjb5QHroL9/rCpY0KOATZrlgy4MTG0PM989RlmtMTcRiBKV5PcBwshVTn4SJyLLIEH4WWb7WLQmHEWFVZ+zeY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YQxn+Jix; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YQxn+Jix" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-477985aea2bso2634495e9.3 for ; Sun, 14 Dec 2025 10:29:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765736947; x=1766341747; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=UXKngzqm+2esFum9rKImGD1y+Fv/z9dlaOgfVk605ww=; b=YQxn+JixEYeLJxm04jU9ahwbMbYXd1b2KFYSDAX8UZegtnvg4BzpnEHmXAYbcY9uHw iKehzxDbQXptzQPJfzkfqS6qYe1RxXQToLMW5YOLRHwg6Waj9XzVpjuE6IiUAUJfrffV n6hYnRU8MfLFFXS+fcFROJZaVJ3yQflida//+Qx0tCbhV4W54WqQqTkaJyoPvgauXs/e zmB9kmKc+FhoVji8B6S7Qcz9J+3fIV+miDxpGWi7DgIzMJFm9XOcZIofQVRoGT834tCW +0+GHSmU7zZ0na216Y1Ha9/AXEdHyxNx1t/EF71wwl0DAHBCPUPEnied934Ht2Pt1FI3 97BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765736947; x=1766341747; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UXKngzqm+2esFum9rKImGD1y+Fv/z9dlaOgfVk605ww=; b=QPYQJ82hGvk7fZneZ27nMPnWBzGorUr6ZBgcTxeb6Ug5aB8khMwjo7Tcvy4Zptx03I 94h88ewrc0J/ye10AAd3D0p86d5IBcZDR/cmOgz/GWAAv+6MVPqrn8LMkp8yRM+R6vGw ORALIn9OAZOM3NB91OcdZiUZOFGMTmUGCEaydX8iVl3Ybf2rvzX7lJZ6ulV4+ZNAklVv ltDF4nP3n3UH/HLWag86FGhHIqkOKJr91nkAgUmdbTdctXyulT66Yl66IOFxWp9rIghT wy5S9fr4fr/WyxPd+TOnG+dINzoV0k5Ngpp1RbPsvKOjTVP5NWnXhLjycRLdrTnDtlNJ fChA== X-Forwarded-Encrypted: i=1; AJvYcCWHHJkPz3R7xXhp6Bxyi46WSTPyIIdm1Q3XWcCobyHCXerrx5Fe3N+MjVnieQCnNSB/puF3JCE=@vger.kernel.org X-Gm-Message-State: AOJu0YyFYldrE4GLDNhUctegP7N84fYshV2wX97ojbVe5W8qoGT1eX6f EfQ0i8NAhaR3UBDmvQaugCeRg1UiPYgJls0LBSBPAY4s7E1JP68v8omB X-Gm-Gg: AY/fxX6d/+lBlelGwr2rFZaaQfCJFNZbmEYAPbqSjhG1eNnmKOEexWSsurdJ7FOYCy8 YupP9wxCRYizUAs056jnKpDVC6u9VgGA70vwCRmGiqZIw9Xjp2vUzLZdg9UAmh4bqp5XzZK5kJJ MInJB20zUawEDV/sMsKPa4TdVnT1gwLq2aFgaWkmZpFNzNemDglG53hOVXC6e/qRRsxU6+oVw75 a1cHhXW+hH6b35dq2fxV/ACQWA+X93eS9jksxWN/vmYeeqpaXLLllYuJP7tW00qz0H2RbXLp+I5 rSS2XOGUM44eKcUyMbVW+Bxc4qv3tNgvmXbZgh0yVwDnA5CJ6zY00FeDuVr1bYomdql/tZrEsUb LykCVkkAxBISuUVNqYsaWIHsvlNOUUOpmDZBp5+gmhdpDRvGgC2dV85uwtmcl0gSaX5PQxqnTLY h+Z2IG0tD3wWZn X-Google-Smtp-Source: AGHT+IEUyGCBMjMVwm3ua7eaTqXkvOVXl13Mvkk9VgTU35LOdR1nVLR/ra92ekgflA4AeDVRF/vjFg== X-Received: by 2002:a05:600c:8115:b0:468:7a5a:14cc with SMTP id 5b1f17b1804b1-47a8f904148mr51973905e9.3.1765736946494; Sun, 14 Dec 2025 10:29:06 -0800 (PST) Received: from skbuf ([2a02:2f04:d106:d600:10b6:92bb:682a:9a0]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47a8f39ed88sm145050515e9.4.2025.12.14.10.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Dec 2025 10:29:05 -0800 (PST) Date: Sun, 14 Dec 2025 20:29:03 +0200 From: Vladimir Oltean To: Jonas Gorski Cc: Ma Ke , andrew@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, tobias@waldekranz.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, stable@vger.kernel.org Subject: Re: [PATCH v2] net: dsa: Fix error handling in dsa_port_parse_of Message-ID: <20251214182903.jliq46n6ulbrwoha@skbuf> References: <20251214131204.4684-1-make24@iscas.ac.cn> <39ba16a9-9b7d-4c26-91b5-cf775a7f8169@gmail.com> <20251214161023.5qcyyifscu73b47u@skbuf> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Sun, Dec 14, 2025 at 05:14:04PM +0100, Jonas Gorski wrote: > On Sun, Dec 14, 2025 at 5:10 PM Vladimir Oltean wrote: > > > > Hi Jonas, Ma Ke, > > > > On Sun, Dec 14, 2025 at 05:02:33PM +0100, Jonas Gorski wrote: > > > Hi, > > > > > > On 12/14/25 14:12, Ma Ke wrote: > > > > When of_find_net_device_by_node() successfully acquires a reference to > > > > > > Your subject is missing the () of dsa_port_parse_of() > > > > > > > a network device but the subsequent call to dsa_port_parse_cpu() > > > > fails, dsa_port_parse_of() returns without releasing the reference > > > > count on the network device. > > > > > > > > of_find_net_device_by_node() increments the reference count of the > > > > returned structure, which should be balanced with a corresponding > > > > put_device() when the reference is no longer needed. > > > > > > > > Found by code review. > > > > > > I agree with the reference not being properly released on failure, > > > but I don't think this fix is complete. > > > > > > I was trying to figure out where the put_device() would happen in > > > the success case (or on removal), and I failed to find it. > > > > > > Also if the (indirect) top caller of dsa_port_parse_of(), > > > dsa_switch_probe(), fails at a later place the reference won't be > > > released either. > > > > > > The only explicit put_device() that happens is in > > > dsa_dev_to_net_device(), which seems to convert a device > > > reference to a netdev reference via dev_hold(). > > > > > > But the only caller of that, dsa_port_parse() immediately > > > calls dev_put() on it, essentially dropping all references, and > > > then continuing using it. > > > > > > dsa_switch_shutdown() talks about dropping references taken via > > > netdev_upper_dev_link(), but AFAICT this happens only after > > > dsa_port_parse{,_of}() setup the conduit, so it looks like there > > > could be a window without any reference held onto the conduit. > > > > > > So AFAICT the current state is: > > > > > > dsa_port_parse_of() keeps the device reference. > > > dsa_port_parse() drops the device reference, and shortly has a > > > dev_hold(), but it does not extend beyond the function. > > > > > > Therefore if my analysis is correct (which it may very well not > > > be), the correct fix(es) here could be: > > > > > > dsa_port_parse{,_of}() should keep a reference via e.g. dev_hold() > > > on success to the conduit. > > > > > > Or maybe they should unconditionally drop if *after* calling > > > dsa_port_parse_cpu(), and dsa_port_parse_cpu() should take one > > > when assigning dsa_port::conduit. > > > > > > Regardless, the end result should be that there is a reference on > > > the conduit stored in dsa_port::conduit. > > > > > > dsa_switch_release_ports() should drop the references, as this > > > seems to be called in all error paths of dsa_port_parse{,of} as > > > well by dsa_switch_remove(). > > > > > > And maybe dsa_switch_shutdown() then also needs to drop the > > > reference? Though it may need to then retake the reference on > > > resume, and I don't know where that exactly should happen. Maybe > > > it should also lookup the conduit(s) again to be correct. > > > > > > But here I'm more doing educated guesses then actually knowing > > > what's correct. > > > > > > The alternative/quick "fix" would be to just drop the > > > reference unconditionally, which would align the behaviour > > > to that of dsa_port_parse(). Not sure if it should mirror the > > > dev_hold() / dev_put() spiel as well. > > > > > > Not that I think this would be the correct behaviour though. > > > > > > Sorry for the lengthy review/train of thought. > > > > > > Best regards, > > > Jonas > > > > Thank you for your thoughts on this topic. Indeed there is a problem, > > for which I managed to find a few hours today to investigate. I was > > going to just submit a patch directly and refer Ma Ke to it directly, > > but since you started looking into the situation as well, I just thought > > I'd reply "please standby". It's currently undergoing testing. > > A patch already, that's even better! I'll gladly stand by :) > > Best regards, > Jonas Patch location (for tracking purposes): https://lore.kernel.org/netdev/20251214182449.3900190-1-vladimir.oltean@nxp.com/ Ma Ke's patch unfortunately does not compile even in v2, so: pw-bot: cr (although "changes requested" is a figure of speech here, in the sense that I don't expect a follow-up patch from Ma Ke)