Netdev List
 help / color / mirror / Atom feed
From: Vladimir Oltean <vladimir.oltean@nxp.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: netdev@vger.kernel.org, Andrew Lunn <andrew@lunn.ch>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	Russell King <linux@armlinux.org.uk>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	linux-kernel@vger.kernel.org,
	Herve Codina <herve.codina@bootlin.com>,
	Mark Brown <broonie@kernel.org>,
	Serge Semin <fancer.lancer@gmail.com>,
	Maxime Chevallier <maxime.chevallier@bootlin.com>,
	Lee Jones <lee@kernel.org>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	devicetree@vger.kernel.org, Jiawen Wu <jiawenwu@trustnetic.com>
Subject: Re: [PATCH v2 net-next 01/15] net: mdio-regmap: permit working with non-MMIO regmaps
Date: Fri, 23 Jan 2026 15:55:01 +0200	[thread overview]
Message-ID: <20260123135501.7m5wqkcfluxqeowb@skbuf> (raw)
In-Reply-To: <20260123121529.inik6xrfdianljq6@skbuf>

On Fri, Jan 23, 2026 at 02:15:29PM +0200, Vladimir Oltean wrote:
> > > > > > > > +	unsigned int base;
> > > > > > > 
> > > > > > > Hmm... resource_size_t ?
> > > > 
> > > > > > Well, regmap_read() takes "unsigned int reg".
> > > > > > https://elixir.bootlin.com/linux/v6.18.6/source/include/linux/regmap.h#L1297
> > > > > > So in practice, a truncation will be done somewhere if the register base
> > > > > > exceeds unsigned int storage capacity. But I didn't feel that it's worth
> > > > > > handling that.
> > > > > 
> > > > > Would this address your feedback?
> > > > 
> > > > Yes and no. See my remarks below.
> > 
> > ...
> > 
> > > > > -	if (config->resource)
> > > > > +	if (config->resource) {
> > > > 
> > > > Btw, this might be not enough, one should check size and flags as well
> > > > before use. There was a discussion about this recently. Maybe we should
> > > > just move to a simple unsigned int in the config for now? Because handling
> > > > resources maybe considered as over engineering in this case.
> > > 
> > > The resource flags are never taken into consideration, but I can for
> > > sure replace the resource in struct mdio_regmap_config with just an
> > > unsigned int start and an end, but that doesn't get rid of the resource
> > > usage. The dev_get_resource(dev->parent, NULL) call is how we learn of
> > > where our register window is located in the "one big regmap" provided by
> > > the parent (SJA1105). So we still need this check somewhere else if we
> > > wanted to not fail silently in case of address bits truncation.
> > 
> > Hmm... Bu why we can't embed the full struct resource in such a case?
> 
> We can also embed the full struct resource, I never said we can't...
> 
> > Because resource should have a flag check, otherwise it's a wrong check.
> > 
> > Discussion I mentioned is this:
> > https://lore.kernel.org/lkml/20251207215359.28895-1-ansuelsmth@gmail.com/
> > 
> > Fixes due to that finding:
> > https://lore.kernel.org/lkml/20251208200437.14199-1-ansuelsmth@gmail.com/
> > https://lore.kernel.org/lkml/20251208145654.5294-1-ilpo.jarvinen@linux.intel.com/
> 
> The linked issues seem unrelated; they are caused by the assumption that
> resource_size() can be zero. But I'm not using the resource_size()
> helper, and even if I were, I'm not testing it against zero.
> 
> As opposed to the PCI BAR case, we don't keep around in an altered form
> the resources exceeding 4G. Just need to reject them once and be done
> with them.
> 
> Also, what else to even check about the resource flags? We get the
> resource using "platform_get_resource(pdev, IORESOURCE_REG, 0)", so we
> know they're of that type. I don't think IORESOURCE_REG resources have
> any other valid bits in flags except for IORESOURCE_TYPE_BITS.
> 
> > > > > +		if (config->resource->start > U32_MAX ||
> > > > > +		    config->resource->end > U32_MAX) {
> > > > 
> > > > Ideally it should be resource_overlaps() check. But see above.
> > > 
> > > resource_overlaps_with_what? The only problem is that the resource can
> > > exceed the 32 bit representation that regmap works with.
> > 
> > Obviously with the 4G address space :-)
> > 
> > 	struct resource r4g = DEFINE_RESOURCE...(..., 0, SZ_4G...);
> > 
> > 	if (resource_overlaps(&r4g, config->resource))
> > 		aiaiai! // using %pR to print the content
> 
> This is a buggy replacement of my intention. I need to sanity check that
> my IORESOURCE_REG resource is entirely within the 0-4G region.
> 
> The correct way to express this using helpers:
> 
> 	if (!resource_contains(&r4g, config->resource))
> 		nazad!
> 
> but... you see my point? In trying to make use of "standard" helpers, we
> overcomplicate simple things and introduce bugs.
> 
> My initially proposed test can be written even simpler:
> 
> 	if (config->resource->end > U32_MAX) {
> 		...
> 
> because end >= start, so also testing resource->start is redundant.
> 
> > > > > +			dev_err(config->parent,
> > > > > +				"Resource exceeds regmap API addressing possibilities\n");
> > > > > +			return ERR_PTR(-EINVAL);
> > > > > +		}
> > > > >  		mr->base = config->resource->start;
> > > > > +	}

A data structure which I find a bit under-utilized in the kernel is

/**
 * struct regmap_range - A register range, used for access related checks
 *                       (readable/writeable/volatile/precious checks)
 *
 * @range_min: address of first register
 * @range_max: address of last register
 */
struct regmap_range {
	unsigned int range_min;
	unsigned int range_max;
};

I could imagine a helper like:

/* Type adaptation between phy_addr_t and unsigned int */
static inline int __must_check regmap_range_from_resource(const struct resource *res,
							  struct regmap_range *range)
{
	struct resource r4g = DEFINE_RES(0, SZ_4G, res->flags);

	if (res->flags != IORESOURCE_REG) {
		pr_err("%s should be used only with IORESOURCE_REG resources\n");
		return -EINVAL;
	}

	if (!resource_contains(&r4g, res)) {
		pr_err("Resource exceeds regmap API addressing possibilities\n");
		return -EINVAL;
	}

	range->range_min = res->start;
	range->range_max = res->end;

	return 0;
}

and then proceed to use the simpler and validated regmap_range structure in the driver.
Too bad such use is not an established coding pattern...

  reply	other threads:[~2026-01-23 13:55 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 10:56 [PATCH v2 net-next 00/15] Probe SJA1105 DSA children as platform sub-devices Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 01/15] net: mdio-regmap: permit working with non-MMIO regmaps Vladimir Oltean
2026-01-22 12:06   ` Andy Shevchenko
2026-01-22 12:13     ` Vladimir Oltean
2026-01-22 12:16       ` Russell King (Oracle)
2026-01-22 12:21         ` Vladimir Oltean
2026-01-22 13:47       ` Vladimir Oltean
2026-01-22 14:38         ` Andy Shevchenko
2026-01-22 22:18           ` Vladimir Oltean
2026-01-23  7:20             ` Andy Shevchenko
2026-01-23 12:15               ` Vladimir Oltean
2026-01-23 13:55                 ` Vladimir Oltean [this message]
2026-01-23 14:31                   ` Andy Shevchenko
2026-01-23 15:10                     ` Vladimir Oltean
2026-01-23 15:39                       ` Andy Shevchenko
2026-01-23 15:54                         ` Andy Shevchenko
2026-01-23 14:23                 ` Andy Shevchenko
2026-01-22 14:28       ` Andy Shevchenko
2026-01-22 10:56 ` [PATCH v2 net-next 02/15] net: mdio: add driver for NXP SJA1110 100BASE-T1 embedded PHYs Vladimir Oltean
2026-01-22 12:12   ` Andy Shevchenko
2026-01-22 12:47     ` Vladimir Oltean
2026-01-22 14:44       ` Andy Shevchenko
2026-01-22 22:10         ` Vladimir Oltean
2026-01-22 23:11           ` Andrew Lunn
2026-01-23  7:25           ` Andy Shevchenko
2026-01-22 10:56 ` [PATCH v2 net-next 03/15] net: mdio: add generic driver for NXP SJA1110 100BASE-TX " Vladimir Oltean
2026-01-22 12:20   ` Andy Shevchenko
2026-01-22 13:31     ` Vladimir Oltean
2026-01-22 14:48       ` Andy Shevchenko
2026-01-22 10:56 ` [PATCH v2 net-next 04/15] net: dsa: sja1105: prepare regmap for passing to child devices Vladimir Oltean
2026-01-22 12:23   ` Andy Shevchenko
2026-01-22 13:42     ` Vladimir Oltean
2026-01-22 14:54       ` Andy Shevchenko
2026-01-22 16:17         ` Russell King (Oracle)
2026-01-22 16:34           ` Andy Shevchenko
2026-01-22 10:56 ` [PATCH v2 net-next 05/15] net: dsa: sja1105: include spi.h from sja1105.h Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 06/15] net: dsa: sja1105: transition OF-based MDIO controllers to standalone sub-devices Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 07/15] net: pcs: xpcs: introduce xpcs_create_pcs_fwnode() Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 08/15] net: pcs: xpcs-plat: convert to regmap Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 09/15] dt-bindings: net: dsa: sja1105: document the PCS nodes Vladimir Oltean
2026-01-29 18:10   ` Rob Herring (Arm)
2026-01-22 10:56 ` [PATCH v2 net-next 10/15] net: pcs: xpcs-plat: add NXP SJA1105/SJA1110 support Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 11/15] net: dsa: sja1105: fill device tree with ethernet-pcs sub-devices under "regs" node Vladimir Oltean
2026-01-23 19:45   ` kernel test robot
2026-01-22 10:56 ` [PATCH v2 net-next 12/15] net: dsa: sja1105: replace mdiobus-pcs with xpcs-plat driver Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 13/15] net: dsa: sja1105: permit finding the XPCS via pcs-handle Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 14/15] dt-bindings: net: xpcs: allow properties from phy-common-props.yaml Vladimir Oltean
2026-01-22 10:56 ` [PATCH v2 net-next 15/15] net: pcs: xpcs: allow generic polarity inversion Vladimir Oltean

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=20260123135501.7m5wqkcfluxqeowb@skbuf \
    --to=vladimir.oltean@nxp.com \
    --cc=andrew@lunn.ch \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@google.com \
    --cc=fancer.lancer@gmail.com \
    --cc=herve.codina@bootlin.com \
    --cc=hkallweit1@gmail.com \
    --cc=jiawenwu@trustnetic.com \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=robh@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox