diff mbox series

[v2,2/3] pwm: tegra: Check for match_data being NULL

Message ID ed943d9be3b785514e0f65a5b8c13a78aba7a090.1789741839.git.u.kleine-koenig@baylibre.com
State New
Headers show
Series pwm: tegra: Cleanups and .get_state() | expand

Commit Message

Uwe Kleine-König Sept. 18, 2026, 2:33 p.m. UTC
It's unlikely but not impossible that of_device_get_match_data() returns
NULL. Handle this case instead of triggering a NULL pointer exception.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
 drivers/pwm/pwm-tegra.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Thierry Reding Sept. 21, 2026, 9:47 a.m. UTC | #1
On Fri, Sep 18, 2026 at 04:33:46PM +0200, Uwe Kleine-König wrote:
> It's unlikely but not impossible that of_device_get_match_data() returns
> NULL. Handle this case instead of triggering a NULL pointer exception.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> ---
>  drivers/pwm/pwm-tegra.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
> index efb7ab60f602..b461d3877f43 100644
> --- a/drivers/pwm/pwm-tegra.c
> +++ b/drivers/pwm/pwm-tegra.c
> @@ -323,6 +323,14 @@ static int tegra_pwm_probe(struct platform_device *pdev)
>  	int ret;
>  
>  	soc = of_device_get_match_data(dev);
> +	if (!soc) {
> +		/*
> +		 * This can only happen if pdev was matched via pdev->name
> +		 * (which should not happen today) or in combination with a
> +		 * driver override.
> +		 */
> +		return dev_err_probe(dev, -ENODEV, "Unsupported device\n");
> +	}

We don't usually do this. Matching via anything other than OF device ID
tables (or ACPI, I suppose) is a programming error and you deserve the
crash which forces you to fix things rather than continue with an error
that is easy to miss.

Driver overrides aren't going to work with these devices anyway, so I'm
beginning to think it might be worth looking into opting out of the
override behaviour for select drivers.

Thierry
Uwe Kleine-König Sept. 21, 2026, 2:34 p.m. UTC | #2
Hello Thierry,

On Mon, Sep 21, 2026 at 11:47:17AM +0200, Thierry Reding wrote:
> On Fri, Sep 18, 2026 at 04:33:46PM +0200, Uwe Kleine-König wrote:
> > It's unlikely but not impossible that of_device_get_match_data() returns
> > NULL. Handle this case instead of triggering a NULL pointer exception.
> > 
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> > ---
> >  drivers/pwm/pwm-tegra.c | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> > 
> > diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
> > index efb7ab60f602..b461d3877f43 100644
> > --- a/drivers/pwm/pwm-tegra.c
> > +++ b/drivers/pwm/pwm-tegra.c
> > @@ -323,6 +323,14 @@ static int tegra_pwm_probe(struct platform_device *pdev)
> >  	int ret;
> >  
> >  	soc = of_device_get_match_data(dev);
> > +	if (!soc) {
> > +		/*
> > +		 * This can only happen if pdev was matched via pdev->name
> > +		 * (which should not happen today) or in combination with a
> > +		 * driver override.
> > +		 */
> > +		return dev_err_probe(dev, -ENODEV, "Unsupported device\n");
> > +	}
> 
> We don't usually do this. Matching via anything other than OF device ID
> tables (or ACPI, I suppose) is a programming error and you deserve the
> crash which forces you to fix things rather than continue with an error
> that is easy to miss.

I don't agree to "you deserve the crash". IMHO even root should be
unable to make the kernel crash. I don't understand what you think
should be fixed if I hit that crash. My userspace interactions in /sys?
Which error is easy to miss?

> Driver overrides aren't going to work with these devices anyway, so I'm
> beginning to think it might be worth looking into opting out of the
> override behaviour for select drivers.

I think there are much more drivers that don't expect to be forced on a
device, so opting in for override would be a more sensible result. Of
course the path to there is more painful ...

Best regards
Uwe
Thierry Reding Sept. 21, 2026, 4:24 p.m. UTC | #3
On Mon, Sep 21, 2026 at 04:34:09PM +0200, Uwe Kleine-König wrote:
> Hello Thierry,
> 
> On Mon, Sep 21, 2026 at 11:47:17AM +0200, Thierry Reding wrote:
> > On Fri, Sep 18, 2026 at 04:33:46PM +0200, Uwe Kleine-König wrote:
> > > It's unlikely but not impossible that of_device_get_match_data() returns
> > > NULL. Handle this case instead of triggering a NULL pointer exception.
> > > 
> > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> > > ---
> > >  drivers/pwm/pwm-tegra.c | 8 ++++++++
> > >  1 file changed, 8 insertions(+)
> > > 
> > > diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
> > > index efb7ab60f602..b461d3877f43 100644
> > > --- a/drivers/pwm/pwm-tegra.c
> > > +++ b/drivers/pwm/pwm-tegra.c
> > > @@ -323,6 +323,14 @@ static int tegra_pwm_probe(struct platform_device *pdev)
> > >  	int ret;
> > >  
> > >  	soc = of_device_get_match_data(dev);
> > > +	if (!soc) {
> > > +		/*
> > > +		 * This can only happen if pdev was matched via pdev->name
> > > +		 * (which should not happen today) or in combination with a
> > > +		 * driver override.
> > > +		 */
> > > +		return dev_err_probe(dev, -ENODEV, "Unsupported device\n");
> > > +	}
> > 
> > We don't usually do this. Matching via anything other than OF device ID
> > tables (or ACPI, I suppose) is a programming error and you deserve the
> > crash which forces you to fix things rather than continue with an error
> > that is easy to miss.
> 
> I don't agree to "you deserve the crash". IMHO even root should be
> unable to make the kernel crash. I don't understand what you think
> should be fixed if I hit that crash. My userspace interactions in /sys?
> Which error is easy to miss?

Oh, root can easily make the kernel crash in any number of ways. That's
really kind of baked into the concept.

To me this is in the same category as force-unloading a module. You can
do it, but you should know that it's potentially dangerous and most of
the time doesn't make sense either. It's called forcing because there
are guardrails in place to prevent you from trying to do it.

If a device cannot operate without device data, it doesn't make sense to
bind to it with a driver override because then you just don't get that
data.

I'll grant you that purposefully crashing the system is maybe a bit of
an exaggeration if there's a knob specifically designed to let you do
this, hence why I volunteered to look into opting out of driver_override
where it doesn't make sense.

> > Driver overrides aren't going to work with these devices anyway, so I'm
> > beginning to think it might be worth looking into opting out of the
> > override behaviour for select drivers.
> 
> I think there are much more drivers that don't expect to be forced on a
> device, so opting in for override would be a more sensible result. Of
> course the path to there is more painful ...
> 
> Best regards
> Uwe

Yeah, driver_override has been baked into the driver core for quite a
long time (platform devices seem to have had this functionality for more
than a decade).

You're probably not wrong about opt-in being the more natural choice,
but looking at commit 3d713e0e382e ("driver core: platform: add device
binding path 'driver_override'"), the intended use-cases are very
generic, so it would probably lead to a continuous stream of patches
needing to be added whenever a new device wants to be supported with
vfio or something.

Thierry
Uwe Kleine-König Sept. 21, 2026, 8:10 p.m. UTC | #4
On Mon, Sep 21, 2026 at 06:24:39PM +0200, Thierry Reding wrote:
> On Mon, Sep 21, 2026 at 04:34:09PM +0200, Uwe Kleine-König wrote:
> > Hello Thierry,
> > 
> > On Mon, Sep 21, 2026 at 11:47:17AM +0200, Thierry Reding wrote:
> > > On Fri, Sep 18, 2026 at 04:33:46PM +0200, Uwe Kleine-König wrote:
> > > > diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
> > > > index efb7ab60f602..b461d3877f43 100644
> > > > --- a/drivers/pwm/pwm-tegra.c
> > > > +++ b/drivers/pwm/pwm-tegra.c
> > > > @@ -323,6 +323,14 @@ static int tegra_pwm_probe(struct platform_device *pdev)
> > > >  	int ret;
> > > >  
> > > >  	soc = of_device_get_match_data(dev);
> > > > +	if (!soc) {
> > > > +		/*
> > > > +		 * This can only happen if pdev was matched via pdev->name
> > > > +		 * (which should not happen today) or in combination with a
> > > > +		 * driver override.
> > > > +		 */
> > > > +		return dev_err_probe(dev, -ENODEV, "Unsupported device\n");
> > > > +	}
> > > 
> > > We don't usually do this. Matching via anything other than OF device ID
> > > tables (or ACPI, I suppose) is a programming error and you deserve the
> > > crash which forces you to fix things rather than continue with an error
> > > that is easy to miss.
> > 
> > I don't agree to "you deserve the crash". IMHO even root should be
> > unable to make the kernel crash. I don't understand what you think
> > should be fixed if I hit that crash. My userspace interactions in /sys?
> > Which error is easy to miss?
> 
> Oh, root can easily make the kernel crash in any number of ways. That's
> really kind of baked into the concept.

Yeah, right. root can poke in /dev/mem (unless STRICT_DEVMEM=y). And
root can allocate memory until the machine crashes (unless a resource
limit is in place). And root can unbind devices, or bring down the
network, but that shouldn't result in a kernel crash. If it does, that's
a bug worth fixing.
 
> To me this is in the same category as force-unloading a module. You can
> do it, but you should know that it's potentially dangerous and most of
> the time doesn't make sense either.

Yes, force-unloading is another such thing, but this can only be done
if MODULE_FORCE_LOAD (default n) is enabled.

The only thing I'm aware that root can do to crash the kernel where I'm
not aware of a guard rail is `kill 1`.

> It's called forcing because there
> are guardrails in place to prevent you from trying to do it.
> 
> If a device cannot operate without device data, it doesn't make sense to
> bind to it with a driver override because then you just don't get that
> data.

Ack, it doesn't make sense, and so IMHO it's worth to spend a check to
prevent that from happening.

> I'll grant you that purposefully crashing the system is maybe a bit of
> an exaggeration if there's a knob specifically designed to let you do
> this, hence why I volunteered to look into opting out of driver_override
> where it doesn't make sense.

Cc: me if you find something. Until that happens I consider introducing
that check the right thing to do. My patch is essentially a codifycation
of such an opt-out. :-D But I agree a more semantical version would be
nicer (but not sooo nice that *I*'d start a new quest for it).

Best regards
Uwe
Uwe Kleine-König Sept. 22, 2026, 8:33 a.m. UTC | #5
Hello,

On Mon, Sep 21, 2026 at 06:24:39PM +0200, Thierry Reding wrote:
> You're probably not wrong about opt-in being the more natural choice,
> but looking at commit 3d713e0e382e ("driver core: platform: add device
> binding path 'driver_override'"), the intended use-cases are very
> generic, so it would probably lead to a continuous stream of patches
> needing to be added whenever a new device wants to be supported with
> vfio or something.

thinking a bit more about that: The use-case presented in that commit is
about

	echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override

. If we had an opt-in mechanism on the driver side, it would only be
vfio* that would need it, wouldn't it? That sounds handleable.

Best regards
Uwe
Thierry Reding Sept. 22, 2026, 10:06 a.m. UTC | #6
On Tue, Sep 22, 2026 at 10:33:16AM +0200, Uwe Kleine-König wrote:
> Hello,
> 
> On Mon, Sep 21, 2026 at 06:24:39PM +0200, Thierry Reding wrote:
> > You're probably not wrong about opt-in being the more natural choice,
> > but looking at commit 3d713e0e382e ("driver core: platform: add device
> > binding path 'driver_override'"), the intended use-cases are very
> > generic, so it would probably lead to a continuous stream of patches
> > needing to be added whenever a new device wants to be supported with
> > vfio or something.
> 
> thinking a bit more about that: The use-case presented in that commit is
> about
> 
> 	echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override
> 
> . If we had an opt-in mechanism on the driver side, it would only be
> vfio* that would need it, wouldn't it? That sounds handleable.

I have a prototype patch that I'm going to send out shortly (after
testing that it actually works). The problem ended up being that the
driver_override is a device attribute, so there's no good way to drop it
based on a driver flag.

What I ended up doing was add a flag to the driver that causes the
override matching to abort if the driver doesn't allow it.

And yes, you could probably do this the other way around and require
drivers to opt-in, but given how long this has been there and how
generic the interface is (and it is ABI after all), I don't know if
vfio-platform is the only one where this is being used. For all we know
there could be a myriad of odd use-cases where people are using this in
one way or another.

I was briefly pondering a more automatic way where we'd check for the
presence of any device ID match tables and checking the device data
pointers, but that's a bad heuristic since there's nothing stopping
anyone from providing "sensible" defaults if there is not matched data.

So ultimately I think individual drivers opting out of this behaviour if
they explicitly don't want to support it is probably the only safe way
to do it.

Thierry
Uwe Kleine-König Sept. 30, 2026, 11:55 a.m. UTC | #7
On Tue, Sep 22, 2026 at 12:06:14PM +0200, Thierry Reding wrote:
> On Tue, Sep 22, 2026 at 10:33:16AM +0200, Uwe Kleine-König wrote:
> > Hello,
> > 
> > On Mon, Sep 21, 2026 at 06:24:39PM +0200, Thierry Reding wrote:
> > > You're probably not wrong about opt-in being the more natural choice,
> > > but looking at commit 3d713e0e382e ("driver core: platform: add device
> > > binding path 'driver_override'"), the intended use-cases are very
> > > generic, so it would probably lead to a continuous stream of patches
> > > needing to be added whenever a new device wants to be supported with
> > > vfio or something.
> > 
> > thinking a bit more about that: The use-case presented in that commit is
> > about
> > 
> > 	echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override
> > 
> > . If we had an opt-in mechanism on the driver side, it would only be
> > vfio* that would need it, wouldn't it? That sounds handleable.
> 
> I have a prototype patch that I'm going to send out shortly (after
> testing that it actually works). The problem ended up being that the
> driver_override is a device attribute, so there's no good way to drop it
> based on a driver flag.

Did you see that I sent such a patch already.
https://lore.kernel.org/lkml/0f7446324f6a0c8f0153d6532d92a6eeecd6a308.1790612298.git.u.kleine-koenig@baylibre.com/ .
You were on Cc:, I thought this to be enough to make you aware and
didn't mention it in this thread here.

I hope to not have provoked much duplicate work.

Best regards
Uwe
Thierry Reding Sept. 30, 2026, 1:14 p.m. UTC | #8
On Wed, Sep 30, 2026 at 01:55:59PM +0200, Uwe Kleine-König wrote:
> On Tue, Sep 22, 2026 at 12:06:14PM +0200, Thierry Reding wrote:
> > On Tue, Sep 22, 2026 at 10:33:16AM +0200, Uwe Kleine-König wrote:
> > > Hello,
> > > 
> > > On Mon, Sep 21, 2026 at 06:24:39PM +0200, Thierry Reding wrote:
> > > > You're probably not wrong about opt-in being the more natural choice,
> > > > but looking at commit 3d713e0e382e ("driver core: platform: add device
> > > > binding path 'driver_override'"), the intended use-cases are very
> > > > generic, so it would probably lead to a continuous stream of patches
> > > > needing to be added whenever a new device wants to be supported with
> > > > vfio or something.
> > > 
> > > thinking a bit more about that: The use-case presented in that commit is
> > > about
> > > 
> > > 	echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override
> > > 
> > > . If we had an opt-in mechanism on the driver side, it would only be
> > > vfio* that would need it, wouldn't it? That sounds handleable.
> > 
> > I have a prototype patch that I'm going to send out shortly (after
> > testing that it actually works). The problem ended up being that the
> > driver_override is a device attribute, so there's no good way to drop it
> > based on a driver flag.
> 
> Did you see that I sent such a patch already.
> https://lore.kernel.org/lkml/0f7446324f6a0c8f0153d6532d92a6eeecd6a308.1790612298.git.u.kleine-koenig@baylibre.com/ .
> You were on Cc:, I thought this to be enough to make you aware and
> didn't mention it in this thread here.
> 
> I hope to not have provoked much duplicate work.

Well, from the looks of it you ended up hijacking my earlier attempt
while it was being discussed. It's customary to ask before you do so,
specifically in order to avoid duplication of work. Or at least answer
on the original thread that you plan to take this in a different
direction so that people know what's going on.

Anyway, I was still waiting for any conclusions on the original thread
and didn't spend any more time revisiting my original series, so no harm
done.

Thierry
Uwe Kleine-König Sept. 30, 2026, 5:05 p.m. UTC | #9
Hello Thierry,

On Wed, Sep 30, 2026 at 03:14:35PM +0200, Thierry Reding wrote:
> On Wed, Sep 30, 2026 at 01:55:59PM +0200, Uwe Kleine-König wrote:
> > On Tue, Sep 22, 2026 at 12:06:14PM +0200, Thierry Reding wrote:
> > > I have a prototype patch that I'm going to send out shortly (after
> > > testing that it actually works). The problem ended up being that the
> > > driver_override is a device attribute, so there's no good way to drop it
> > > based on a driver flag.
> > 
> > Did you see that I sent such a patch already.
> > https://lore.kernel.org/lkml/0f7446324f6a0c8f0153d6532d92a6eeecd6a308.1790612298.git.u.kleine-koenig@baylibre.com/ .
> > You were on Cc:, I thought this to be enough to make you aware and
> > didn't mention it in this thread here.
> > 
> > I hope to not have provoked much duplicate work.
> 
> Well, from the looks of it you ended up hijacking my earlier attempt
> while it was being discussed. It's customary to ask before you do so,
> specifically in order to avoid duplication of work. Or at least answer
> on the original thread that you plan to take this in a different
> direction so that people know what's going on.

Yeah, sorry about that. On that other thread where we were discussing
taints in context of driver_overrides the topics overlapped, so my idea
was to get all the suggestions into a single place. It seems I failed to
make you notice that in time. Mea culpa.
 
> Anyway, I was still waiting for any conclusions on the original thread
> and didn't spend any more time revisiting my original series, so no harm
> done.

😅

Best regards
Uwe
diff mbox series

Patch

diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
index efb7ab60f602..b461d3877f43 100644
--- a/drivers/pwm/pwm-tegra.c
+++ b/drivers/pwm/pwm-tegra.c
@@ -323,6 +323,14 @@  static int tegra_pwm_probe(struct platform_device *pdev)
 	int ret;
 
 	soc = of_device_get_match_data(dev);
+	if (!soc) {
+		/*
+		 * This can only happen if pdev was matched via pdev->name
+		 * (which should not happen today) or in combination with a
+		 * driver override.
+		 */
+		return dev_err_probe(dev, -ENODEV, "Unsupported device\n");
+	}
 
 	chip = devm_pwmchip_alloc(dev, soc->num_channels, sizeof(*pc));
 	if (IS_ERR(chip))