🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #23795

Re: [PATCH net-next v4 1/2] rust: net: phy: take &self in methods that do not need phy_device->lock

#23795
From: SeungJong Ha
Date: Sat, 28 Nov 2026 04:34
105 lines
15204 bytes
2026년 11월 28일 (토) 오전 12:01, <netdev-bot+sashiko@kernel.org>님이 작성:

> rust: net: phy: take &self in methods that do not need phy_device->lock
>
> This commit splits Device::from_raw() into a shared from_raw() and a new
> from_raw_mut(). It redefines &mut Device to mean exclusive access to the
> phy_device state that phy_device->lock protects. The getters for that state
> now take &mut self, and phy_id(), read(), write() and genphy_suspend() now
> take &self. The callback SAFETY comments are updated to say when PHYLIB
> calls the callbacks without the lock.
>
> > diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> > index c4e7b1d6c6f4a..bb2cc6b3c94c8 100644
> > --- a/rust/kernel/net/phy.rs
> > +++ b/rust/kernel/net/phy.rs
> > @@ -57,50 +57,71 @@ pub enum DuplexMode {
>
> [ ... ]
>
> > +/// PHYLIB calls the callbacks that get a `&mut Device` with `phy_device->lock` held, except:
> > +///
> > +/// - [`Driver::probe`] and [`Driver::get_features`], which `phy_probe()` calls without the lock
> > +///   before the device is attached.
> > +/// - [`Driver::soft_reset`], which `phy_init_hw()` calls without the lock while the state machine
> > +///   is not running, or from a MAC driver that then ensures exclusive access. `phy_mii_ioctl()`,
> > +///   which PHYLIB documents as "use at own risk", is outside this guarantee.
> > +/// - [`Driver::read_status`], when a MAC driver calls `phy_read_status()` directly. The MAC
> > +///   driver is then responsible for exclusive access.
> > +///
> >  /// # Invariants
> >  ///
> > -/// - Referencing a `phy_device` using this struct asserts that you are in
> > -///   a context where all methods defined on this struct are safe to call.
> > -/// - This struct always has a valid `self.0.mdio.dev`.
> > +/// - `self.0` is a valid `phy_device`, and `self.0.mdio.dev` is a valid `struct device`.
> > +/// - For the lifetime of a `&mut Device`, no other thread accesses the state of the `phy_device`
> > +///   that `phy_device->lock` protects.
>
> [ ... ]
>
> > +    /// Creates a new mutable [`Device`] reference from a raw pointer.
> > +    ///
> > +    /// # Safety
> > +    ///
> > +    /// For the duration of `'a`,
> > +    /// - the pointer must point at a valid `phy_device`.
> > +    /// - `(*ptr).mdio.dev` must be valid.
> > +    /// - the caller must have the exclusive access that [`Device`] describes.
> > +    unsafe fn from_raw_mut<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
>
> [ ... ]
>
> > @@ -309,10 +331,10 @@ impl<T: Driver> Adapter<T> {
> >      /// `phydev` must be passed by the corresponding callback in `phy_driver`.
> >      unsafe extern "C" fn soft_reset_callback(phydev: *mut bindings::phy_device) -> c_int {
> >          from_result(|| {
> > -            // SAFETY: This callback is called only in contexts
> > -            // where we hold `phy_device->lock`, so the accessors on
> > -            // `Device` are okay to call.
> > -            let dev = unsafe { Device::from_raw(phydev) };
> > +            // SAFETY: `phy_init_hw()` calls this with `phy_device->lock` held, or without it while
> > +            // the state machine is not running or a MAC driver ensures exclusive access. The
> > +            // `phy_mii_ioctl()` path is outside this guarantee, see `Device`.
> > +            let dev = unsafe { Device::from_raw_mut(phydev) };
>
> [Severity: Medium]
> Is it sound to call Device::from_raw_mut() in soft_reset_callback() when
> the SAFETY comment says the phy_mii_ioctl() path does not give the
> exclusive access that from_raw_mut() now requires?
>
> The new invariant on Device says that for the lifetime of a &mut Device,
> no other thread accesses the phy_device state that phy_device->lock
> protects. However, phy_mii_ioctl() reaches this callback without
> phydev->lock, and the PHY state machine may be running at that point:

As discussed on v3 [1], this is documented as an exception, see the
Device docs above. Passing &Device to soft_reset would drop
genphy_soft_reset(), which ax88796b_rust needs, and FUJITA agreed on
keeping &mut Device there [2].

[1] https://lore.kernel.org/all/bfd7a44f-2bba-4aee-9758-1dd88f94566f@lunn.ch/
[2] https://lore.kernel.org/all/20261126.081155.1605990248496749195.tomo@flapping.org/
Technical Details
Message-ID: <CAGAB667-6_gX7_q13GvgmAZQGHNTqxxJ5_nWFLrpvA42ZfAzBg@mail.gmail.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373028-linux-kernel-netdev=dewy.ru
References: <20261126-exp-phy-rs-maybelocked-v4-1-16f9a10bb579@gmail.com> <179579171493.3135.13012003515691317246@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373028-linux-kernel-netdev=dewy.ru
From: engineer.jjhama@gmail.com (SeungJong Ha)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next v4 1/2] rust: net: phy: take &self in methods
 that do not need phy_device->lock
Date: Sat, 28 Nov 2026 13:34:39 +0900
Organization: Usenet Network
Message-ID: <CAGAB667-6_gX7_q13GvgmAZQGHNTqxxJ5_nWFLrpvA42ZfAzBg@mail.gmail.com>
References: <20261126-exp-phy-rs-maybelocked-v4-1-16f9a10bb579@gmail.com> <179579171493.3135.13012003515691317246@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
	logging-data="878733"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:gKDmpJS9vvFFtoM4bv7K4KcZtJs= sha256:Rpw/xz9pz5eP3533aNiOBBe4mPTVMntSddNQgrc4S7k=
	sha1:sQJU3femTjAgaud3/Zwu3WTGqVo= sha256:F3jgHAd5Nu4aJnCykwOVxLfhgydTVYz5xeMP10wrOt8=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5B6E1125F6D; Sat, 28 Nov 2026 07:35:21 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.148
X-Spam-Status: No, score=-3.148 tagged_above=-9999 required=6.31
	tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
	FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001,
	HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id lvEs-ptl7IbN;
	Sat, 28 Nov 2026 07:35:12 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-373028-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 47AF8125F69; Sat, 28 Nov 2026 07:35:01 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id B16DC2B9D1; Sat, 28 Nov 2026 04:34:59 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id AD20D606A32A;
	Sat, 28 Nov 2026 04:34:57 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yBlDy5ox
X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=209.85.160.54
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail;
	t=1795840491;
	b=qS7lnB4R6KWatXBhVpvrv9W4pU6K6qiaurZtMR9lGoZjSB2no9chElcG8rXvOKhef6z6
	 8x16OBCJKcmXv1VEmCLqVypi099v4wEMQj4kdeYDkq2wT4HKkouM3m2ETre9yMJogobnE
	 9LFgug7IHUdIBGpmrNBXO+FybPUUA7x02SI8fq6uPurdrP7ZsaoZYHjHDVesX2W1K3hdU
	 3UebVgYSTW4dTzNfLzdZDuV/slqXL6l85BX/Z6jU4I/SabCuH3qHzedQN8rX0SsFrTY1C
	 QQdfi+ZIBP3JyLVoGW9QUjOxiGUJWBYPjljnOCeJgbjiDcPlh5FGiqvq6dqwj0ZalOw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795840491;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 MIME-Version:References:In-Reply-To:From:Date:X-Gm-Features:
	 Message-ID:Subject:To:Cc:Content-Type:Content-Transfer-Encoding;
	bh=Ft3cUGMZwuJbnCtx/nSJpqRqOJFMOjO+enjfoT4AQ1w=;
	b=FAFNId1O44J19qB2rNQPTS9Z8uWjG3ZBrGD5Yy1u/hx0Z4PW3aviBXvq/z2rmPlD0+sq
	 MNTP8vClaxa6fG5Jxq9TxCJWr8vf8y1o7lEcPLkKz6c0cWwwtdULfCUaaO2e+uybGUsUK
	 QOJF5iWvfid54Xw+va0ZeZyDndCkSrzJTk0tUUJzZww41urSxp3f3XK3sQQ78YbI1W5HN
	 ecTaGpRZleIm0+7he9C9zrgvdoVaHcVUwdsFREuPVcKE9B+sCC45OkY5GavtDT5Gffint
	 AxYLEsEwggim8lcQIC8ptDfrw3w8hhrtcuRNvZ65KzVHwL98EnC2+Z1Bgra6sBqS/XA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yBlDy5ox;
	arc=fail smtp.remote-ip=209.85.160.54
X-Received: from mail-oa1-f54.google.com (mail-oa1-f54.google.com [209.85.160.54])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FCCA606A31E
	for <netdev@vger.kernel.org>; Sat, 28 Nov 2026 04:34:51 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7FCCA606A31E
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-4d3dc583029so4995fac.0
        for <netdev@vger.kernel.org>; Fri, 27 Nov 2026 20:34:51 -0800 (PST)
X-ARC-Seal: i=1; a=rsa-sha256; t=1795840490; cv=none;
        d=google.com; s=arc-20260327;
        b=kgBnxazOUJLnD2kxkqFsqNl1AiqZPrX2BFm1ZY4snEEMfQHo4QanBhYJQpvQ0Xk7nB
         Hzxm3+/LAbfj29yqN5ZBTrCUTu3Y2th+vWgZTBneyghVpxnjfKwsSOMp5j3ZBaQn1Uu0
         BEPY6pGFyd+5EQ8LoZaPDyP+DyOLZoJwKNr/CVuNko5PCy8oTpXHKIS6G91fX3JW4ZHd
         fRp1qBpZ9xdNcyMEFYG+tAWxB5ZWe2xcLDKYbtX3tnbeckVwmuyv+/vhNwjrPiGFPFDd
         wfoVQ+1Cec76cRRXaAGwaVnt9SwG+oSLxcvqkYFAsQcYJHLwyRWIyUJQlBE667kMjD9l
         2Itw==
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327;
        h=content-transfer-encoding:cc:to:subject:message-id:date:from
         :in-reply-to:references:mime-version:dkim-signature;
        bh=Ft3cUGMZwuJbnCtx/nSJpqRqOJFMOjO+enjfoT4AQ1w=;
        fh=qWawIHK6IWPtlm7n+3y63s4RCRg7Hy5OH2dg7YK9ECE=;
        b=V/CZca637P+WCzXyukj/2X5jB6NLD7IfFd03vbsG3ZukXlPZNrgEQxffYL4/S+hQMU
         tFA/D/MBGZdpuw6a3sx5rQkwXtmkTxmwAqEgholdn6yLkv6c1Mm2F9qxA9PIIo98nmq9
         x8PGXi94c5dSBHWOyDgaso3M+y4QLjZfqyIDktj0xXwXJJT31fTmfFSQ0QnA4Mt7I/uf
         iERdVgu+LCM8JqYvFsMhYoyISItUnpT2wYmcQKPCG0euzNrHXUog+5Yd0CQE3urKx7ox
         MFI45UYAAjRYFixiqIXZlGlLHsA3OFj8emSwl8c8x0D/Hf/WCsCLJin+GASjLAym7lLA
         1ipQ==;
        darn=vger.kernel.org
X-ARC-Authentication-Results: i=1; mx.google.com; arc=none
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1795840490; x=1796445290; darn=vger.kernel.org;
        h=content-transfer-encoding:content-type:cc:to:subject:message-id
         :date:from:in-reply-to:references:mime-version:from:to:cc:subject
         :date:message-id:reply-to:content-type;
        bh=Ft3cUGMZwuJbnCtx/nSJpqRqOJFMOjO+enjfoT4AQ1w=;
        b=yBlDy5oxOhAUHry053sMlyAdv00U90rbHk36Bz+1T0NNwtlGh+TkZxTo3VwvUNhOsz
         7JFQSZWLd/Vol7U3Llt8kBlhkze7fYCb8FLYdAGTtfhjQZ6Pc3+AWNMTZj+8bD4POi5E
         yaGB7YL6AHO1vyG9dMLloSOnSX+BW0p+mDmbXeG8sGOvNMJoZrtvla9G3crknJyLh9PI
         VWqXpc/7+wf0Y4DCQmvI24qNLzswJ6BpntvRw/EignLknmZnjaZ16M0dnFEDyvPAx3GR
         1g2f50VdrU0UlcR+eDU4fp0Hz34xpqlrHVQn/mwnLnXNolGzNsJj1F+ht4FyH3+03+wP
         k6mQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1795840490; x=1796445290;
        h=content-transfer-encoding:content-type:cc:to:subject:message-id
         :date:from:in-reply-to:references:mime-version:x-gm-gg
         :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
         :content-type;
        bh=Ft3cUGMZwuJbnCtx/nSJpqRqOJFMOjO+enjfoT4AQ1w=;
        b=r3lLjskpkAlczk4OEHg6V/ZWp8poKfUGt1fYGG9KW/3/b4ukpZAW6ExcTOziheKXWp
         lzuuCauTiVSFpX9sVon/9pOOi7ZKc/FXajqZQtxJeCNdbjcJ1NOlYFFZQXrfXhWL6Rmq
         ABX9m6URDgkNGRyGKlYR2pr40Q8mafCfCN9FMs7KDmD0usKxnSfXv0RjTJS3NtEkgXpC
         YhUJsuL4m+KiM6Yyfs7f07OOE4Usdfyel2iESfZB/68k5bw0lpKZuXPkylkgf+U0v7ge
         8Lk3pps0U56JHscWeA/Kn7gBPaJs0ZPqfvv7/ogwBQofdsph2B3LjL39D0vFk5XGMNe3
         IHJw==
X-Forwarded-Encrypted: i=1; AL+hErFtUHX5RNXuBABL8zjImo5Pj0mtdMfhMaEaoH8WMjXFPS6kESxFtkeDkcIueuGwSTre9MGqFBM=@vger.kernel.org
X-Gm-Message-State: APuU4Z/9zNO16vkGepOOuBDVPqB74KXIn/dLmL1svPYKYKo/04TKJ/k1
	uyKOLIVxdDinRZhUE8bOhyf8H/96mNIPFrokG8+w+2JDRdkahhVkdkBHPvNKiOQ4VhCch4mA+w+
	IrgGjhhGGCE6e+DGowbSGuY6MksdiMyU=
X-Gm-Gg: AZFdTZemC+kAM+8nYLh32E0hphtkREYDgHMMxg6i0UedQOVO7kYzbawqj49kL3nxN34
	mv4upqNpYckB7vMJsKEAvaoKUxnO49bUNwPKYfy/mvsURjdoIWJhhXhct83RhN6/YIzZwZ2M3Pl
	e0u86eRLaZuJosTYlS9/olFfNBgIDT5EA5MRqdUZpTKNy6WqXjyHPRrGac2hJabgfqYNxewiv58
	u9hsIFuMz86Q4CbjW42gCq7CDAFaH3WQMWjvjvafMjHmtPIeSK95ToKN1vL1C+mz34q4Kgesgtp
	RoQ+brEZmKBz1FVIVnW2YMURETq9K/rSdatY4fT4SeMZ/kYHuR9QisrucwjwwGsJr2x9oiphxLx
	cmdvuCraOr0T5boXFcgeMYamMCxPqCcWqgVeuJWWyVwN3CGolntJ9dV1JgEeEVb7IDcnH4Q4VBu
	tyZRPN5xu2aACrXw7iplJDO6NmskVD1f9psa5xsuE0P0VcB10wxXr/hJMw
X-Received: by 2002:a05:6870:b154:b0:4a6:f676:ecef with SMTP id
 586e51a60fabf-4bc85902a3cmr23268703fac.55.1795840490274; Fri, 27 Nov 2026
 20:34:50 -0800 (PST)
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <179579171493.3135.13012003515691317246@kernel.org>
X-Gm-Features: Aa3M54oVMmGH-ZjaDn3eIIdiuG-rT6kTcFR9f6TCVy1lL0hu446KZxkRFm9B_ts
X-Cc: fujita.tomonori@gmail.com, tmgross@umich.edu, davem@davemloft.net, 
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, 
	ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, 
	bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, 
	aliceryhl@google.com, dakr@kernel.org, daniel.almeida@collabora.com, 
	tamird@kernel.org, acourbot@nvidia.com, work@onurozkan.dev, andrew@lunn.ch, 
	hkallweit1@gmail.com, linux@armlinux.org.uk, netdev@vger.kernel.org, 
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org