All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Anderson <sean.anderson@linux.dev>
To: Simon Horman <horms@kernel.org>
Cc: netdev@vger.kernel.org, Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Russell King <linux@armlinux.org.uk>,
	upstream@airoha.com, Christian Marangi <ansuelsmth@gmail.com>,
	linux-kernel@vger.kernel.org,
	Kory Maincent <kory.maincent@bootlin.com>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org
Subject: Re: [net-next PATCH v3 03/11] net: pcs: Add subsystem
Date: Thu, 17 Apr 2025 10:56:08 -0400	[thread overview]
Message-ID: <98dbfe79-9ee7-40bc-85bd-3401310eacdd@linux.dev> (raw)
In-Reply-To: <20250417083559.GA2430521@horms.kernel.org>

On 4/17/25 04:35, Simon Horman wrote:
> On Tue, Apr 15, 2025 at 03:33:15PM -0400, Sean Anderson wrote:
>> This adds support for getting PCS devices from the device tree. PCS
>> drivers must first register with phylink_register_pcs. After that, MAC
>> drivers may look up their PCS using phylink_get_pcs.
>> 
>> We wrap registered PCSs in another PCS. This wrapper PCS is refcounted
>> and can outlive the wrapped PCS (such as if the wrapped PCS's driver is
>> unbound). The wrapper forwards all PCS callbacks to the wrapped PCS,
>> first checking to make sure the wrapped PCS still exists. This design
>> was inspired by Bartosz Golaszewski's talk at LPC [1].
>> 
>> pcs_get_by_fwnode_compat is a bit hairy, but it's necessary for
>> compatibility with existing drivers, which often attach to (devicetree)
>> nodes directly. We use the devicetree changeset system instead of
>> adding a (secondary) software node because mdio_bus_match calls
>> of_driver_match_device to match devices, and that function only works on
>> devicetree nodes.
>> 
>> [1] https://lpc.events/event/17/contributions/1627/
>> 
>> Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
> 
> Hi Sean,
> 
> Overall this looks quite clean to me.
> Please find minor some nits flagged by tooling below.
> 
>> +/**
>> + * struct pcs_wrapper - Wrapper for a registered PCS
>> + * @pcs: the wrapping PCS
>> + * @refcnt: refcount for the wrapper
>> + * @list: list head for pcs_wrappers
>> + * @dev: the device associated with this PCS
>> + * @fwnode: this PCS's firmware node; typically @dev.fwnode
>> + * @wrapped: the backing PCS
>> + */
>> +struct pcs_wrapper {
>> +	struct phylink_pcs pcs;
>> +	refcount_t refcnt;
>> +	struct list_head list;
>> +	struct device *dev;
>> +	struct fwnode_handle *fwnode;
>> +	struct phylink_pcs *wrapped;
>> +};
> 
> I think that wrapped needs an __rcu annotation.
> 
> Flagged by Sparse.
> 
> ...

Will add.

>> +static int pcs_post_config(struct phylink_pcs *pcs,
>> +			   phy_interface_t interface)
>> +{
>> +	struct pcs_wrapper *wrapper = pcs_to_wrapper(pcs);
> 
> The line above dereferences pcs.
> 
>> +	struct phylink_pcs *wrapped;
>> +	int ret, idx;
>> +
>> +	idx = srcu_read_lock(&pcs_srcu);
>> +
>> +	wrapped = srcu_dereference(wrapper->wrapped, &pcs_srcu);
>> +	if (pcs && wrapped->ops->pcs_post_config)
> 
> But here it is assumed that pcs may be NULL.
> This does not seem consistent.
> 
> Flagged by Smatch.

This should be if(wrapped && ...

>> +		ret = wrapped->ops->pcs_post_config(wrapped, interface);
>> +	else
>> +		ret = 0;
>> +
>> +	srcu_read_unlock(&pcs_srcu, idx);
>> +	return ret;
>> +}
> 
> ...
> 
>> +/**
>> + * pcs_unregister() - unregister a PCS
>> + * @pcs: a PCS previously registered with pcs_register()
>> + */
>> +void pcs_unregister(struct phylink_pcs *pcs)
>> +{
>> +	struct pcs_wrapper *wrapper;
>> +
>> +	mutex_lock(&pcs_mutex);
>> +	list_for_each_entry(wrapper, &pcs_wrappers, list) {
>> +		if (wrapper->wrapped == pcs)
> 
> Assuming that rcu_access_pointer() works with srcu,
> I think that this should be:
> 
> 		if (rcu_access_pointer(wrapper->wrapped) == pcs)
> 
> Also flagged by Sparse

OK

>> +			goto found;
>> +	}
>> +
>> +	mutex_unlock(&pcs_mutex);
>> +	WARN(1, "trying to unregister an already-unregistered PCS\n");
>> +	return;
>> +
>> +found:
>> +	list_del(&wrapper->list);
>> +	mutex_unlock(&pcs_mutex);
>> +
>> +	put_device(wrapper->dev);
>> +	fwnode_handle_put(wrapper->fwnode);
>> +	rcu_replace_pointer(wrapper->wrapped, NULL, true);
>> +	synchronize_srcu(&pcs_srcu);
>> +
>> +	if (!wrapper->pcs.poll)
>> +		phylink_pcs_change(&wrapper->pcs, false);
>> +	if (refcount_dec_and_test(&wrapper->refcnt))
>> +		kfree(wrapper);
>> +}
>> +EXPORT_SYMBOL_GPL(pcs_unregister);
>> +
>> +static void devm_pcs_unregister(void *pcs)
>> +{
>> +	pcs_unregister(pcs);
>> +}
>> +
>> +/**
>> + * devm_pcs_register - resource managed pcs_register()
> 
> nit: devm_pcs_register_full
> 
>      Flagged by W=1 builds, and ./scripts/kernel-doc -none

OK

>> + * @dev: device that is registering this PCS
>> + * @fwnode: The PCS's firmware node; typically @dev.fwnode
>> + * @pcs: the PCS to register
>> + *
>> + * Managed pcs_register(). For PCSs registered by this function,
>> + * pcs_unregister() is automatically called on driver detach. See
>> + * pcs_register() for more information.
>> + *
>> + * Return: 0 on success, or -errno on failure
>> + */
>> +int devm_pcs_register_full(struct device *dev, struct fwnode_handle *fwnode,
> 
> ...
> 
>> +/**
>> + * pcs_find_fwnode() - Find a PCS's fwnode
>> + * @mac_node: The fwnode referencing the PCS
>> + * @id: The name of the PCS to get. May be %NULL to get the first PCS.
>> + * @fallback: An optional fallback property to use if pcs-handle is absent
>> + * @optional: Whether the PCS is optional
>> + *
>> + * Find a PCS's fwnode, as referenced by @mac_node. This fwnode can later be
>> + * used with _pcs_get_tail() to get the actual PCS. ``pcs-handle-names`` is
>> + * used to match @id, then the fwnode is found using ``pcs-handle``.
>> + *
>> + * This function is internal to the PCS subsystem from a consumer
>> + * point-of-view. However, it may be used to implement fallbacks for legacy
>> + * behavior in PCS providers.
>> + *
>> + * Return: %NULL if @optional is set and the PCS cannot be found. Otherwise,
>> + * *       returns a PCS if found or an error pointer on failure.
>> + */
>> +struct fwnode_handle *pcs_find_fwnode(const struct fwnode_handle *mac_node,
>> +				      const char *id, const char *fallback,
>> +				      bool optional)
>> +{
>> +	int index;
>> +	struct fwnode_handle *pcs_fwnode;
> 
> Reverse xmas tree here please.

OK

> Edward Cree's xmastree tool can be helpful:
> https://github.com/ecree-solarflare/xmastree

I wonder if we could get this into checkpatch...

Thanks for the review.

--Sean

  reply	other threads:[~2025-04-17 14:56 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-15 19:33 [net-next PATCH v3 00/11] Add PCS core support Sean Anderson
2025-04-15 19:33 ` [net-next PATCH v3 01/11] dt-bindings: net: Add Xilinx PCS Sean Anderson
2025-04-15 19:33 ` [net-next PATCH v3 02/11] net: phylink: Support setting PCS link change callbacks Sean Anderson
2025-04-15 19:33 ` [net-next PATCH v3 03/11] net: pcs: Add subsystem Sean Anderson
2025-04-15 20:02   ` Sean Anderson
2025-04-17  8:35   ` Simon Horman
2025-04-17 14:56     ` Sean Anderson [this message]
2025-04-17  9:19   ` Simon Horman
2025-04-17 15:05     ` Sean Anderson
2025-04-18  9:47       ` Simon Horman
2025-04-15 19:33 ` [net-next PATCH v3 04/11] net: dsa: ocelot: suppress PHY device scanning on the internal MDIO bus Sean Anderson
2025-04-15 19:33 ` [net-next PATCH v3 05/11] net: pcs: lynx: Convert to an MDIO driver Sean Anderson
2025-05-06 21:58   ` Vladimir Oltean
2025-05-06 22:03     ` Sean Anderson
2025-05-06 22:18       ` Vladimir Oltean
2025-05-06 22:20         ` Sean Anderson
2025-05-06 22:29           ` Vladimir Oltean
2025-05-06 22:39             ` Sean Anderson
2025-05-07  0:19               ` Andrew Lunn
2025-05-08 15:57                 ` Sean Anderson
2025-05-07 15:04               ` Vladimir Oltean
2025-05-06 23:56         ` Daniel Golle
2025-05-07 14:02           ` Vladimir Oltean
2025-04-15 19:33 ` [net-next PATCH v3 06/11] net: phy: Export some functions Sean Anderson
2025-04-15 19:33 ` [net-next PATCH v3 07/11] net: pcs: Add Xilinx PCS driver Sean Anderson
2025-04-15 19:33 ` [net-next PATCH v3 08/11] net: axienet: Convert to use PCS subsystem Sean Anderson
2025-04-18  9:04   ` Gupta, Suraj
2025-04-15 19:33 ` [net-next PATCH v3 09/11] net: macb: Move most of mac_config to mac_prepare Sean Anderson
2025-04-15 19:35 ` [net-next PATCH v3 10/11] net: macb: Support external PCSs Sean Anderson
2025-04-15 19:35   ` [net-next PATCH v3 11/11] of: property: Add device link support for PCS Sean Anderson
2025-04-17 12:25 ` [net-next PATCH v3 00/11] Add PCS core support Russell King (Oracle)
2025-04-17 14:22   ` Sean Anderson
2025-04-17 15:24     ` Russell King (Oracle)
2025-04-17 15:29       ` Sean Anderson
2025-05-02 17:41         ` Sean Anderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98dbfe79-9ee7-40bc-85bd-3401310eacdd@linux.dev \
    --to=sean.anderson@linux.dev \
    --cc=andrew+netdev@lunn.ch \
    --cc=ansuelsmth@gmail.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=kory.maincent@bootlin.com \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=upstream@airoha.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.