2024-02-08 07:06:02

by Raag Jadav

[permalink] [raw]
Subject: [PATCH v2 0/5] DesignWare PWM improvements

This series implements 16 channel PWM support for Intel Elkhart Lake
along with minor cleanups for DesignWare PWM driver.

Changes since v1:
- Drop redundant error check
- Provide dwc_pwm_init_one() to initialize one PWM instance
- Use dev_get_drvdata() instead of pci_get_drvdata()
- Use pm_sleep_ptr() instead of use pm_ptr()

Raag Jadav (5):
pwm: dwc: drop redundant error check
pwm: dwc: Add 16 channel support for Intel Elkhart Lake
pwm: dwc: simplify error handling
pwm: dwc: access driver_data using dev_get_drvdata()
pwm: dwc: use pm_sleep_ptr() macro

drivers/pwm/pwm-dwc.c | 59 ++++++++++++++++++++++++-------------------
drivers/pwm/pwm-dwc.h | 5 ++++
2 files changed, 38 insertions(+), 26 deletions(-)


base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d
--
2.35.3



2024-02-08 07:06:11

by Raag Jadav

[permalink] [raw]
Subject: [PATCH v2 3/5] pwm: dwc: simplify error handling

Simplify error handling in ->probe() function using dev_err_probe() helper.

Signed-off-by: Raag Jadav <[email protected]>
---
drivers/pwm/pwm-dwc.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/pwm/pwm-dwc.c b/drivers/pwm/pwm-dwc.c
index 47d76f5367fe..e1e3c62ecc56 100644
--- a/drivers/pwm/pwm-dwc.c
+++ b/drivers/pwm/pwm-dwc.c
@@ -51,18 +51,14 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
int i, ret;

ret = pcim_enable_device(pci);
- if (ret) {
- dev_err(dev, "Failed to enable device (%pe)\n", ERR_PTR(ret));
- return ret;
- }
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to enable device (%pe)\n", ERR_PTR(ret));

pci_set_master(pci);

ret = pcim_iomap_regions(pci, BIT(0), pci_name(pci));
- if (ret) {
- dev_err(dev, "Failed to iomap PCI BAR (%pe)\n", ERR_PTR(ret));
- return ret;
- }
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to iomap PCI BAR (%pe)\n", ERR_PTR(ret));

info = (const struct dwc_pwm_info *)id->driver_data;

--
2.35.3


2024-02-08 07:06:30

by Raag Jadav

[permalink] [raw]
Subject: [PATCH v2 2/5] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
function with 8 channels each. Add support for the remaining channels.

Signed-off-by: Raag Jadav <[email protected]>
Tested-by: Lakshmi Sowjanya D <[email protected]>
---
drivers/pwm/pwm-dwc.c | 33 +++++++++++++++++++++++++--------
drivers/pwm/pwm-dwc.h | 5 +++++
2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/drivers/pwm/pwm-dwc.c b/drivers/pwm/pwm-dwc.c
index 596a0bb35c40..47d76f5367fe 100644
--- a/drivers/pwm/pwm-dwc.c
+++ b/drivers/pwm/pwm-dwc.c
@@ -25,16 +25,31 @@

#include "pwm-dwc.h"

-static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
+/* Elkhart Lake */
+static const struct dwc_pwm_info ehl_pwm_info = {
+ .nr = 2,
+ .size = 0x1000,
+};
+
+static int dwc_pwm_init_one(struct device *dev, void __iomem *base, unsigned int offset)
{
- struct device *dev = &pci->dev;
struct dwc_pwm *dwc;
- int ret;

dwc = dwc_pwm_alloc(dev);
if (!dwc)
return -ENOMEM;

+ dwc->base = base + offset;
+
+ return devm_pwmchip_add(dev, &dwc->chip);
+}
+
+static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
+{
+ const struct dwc_pwm_info *info;
+ struct device *dev = &pci->dev;
+ int i, ret;
+
ret = pcim_enable_device(pci);
if (ret) {
dev_err(dev, "Failed to enable device (%pe)\n", ERR_PTR(ret));
@@ -49,11 +64,13 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
return ret;
}

- dwc->base = pcim_iomap_table(pci)[0];
+ info = (const struct dwc_pwm_info *)id->driver_data;

- ret = devm_pwmchip_add(dev, &dwc->chip);
- if (ret)
- return ret;
+ for (i = 0; i < info->nr; i++) {
+ ret = dwc_pwm_init_one(dev, pcim_iomap_table(pci)[0], i * info->size);
+ if (ret)
+ return ret;
+ }

pm_runtime_put(dev);
pm_runtime_allow(dev);
@@ -105,7 +122,7 @@ static int dwc_pwm_resume(struct device *dev)
static DEFINE_SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume);

static const struct pci_device_id dwc_pwm_id_table[] = {
- { PCI_VDEVICE(INTEL, 0x4bb7) }, /* Elkhart Lake */
+ { PCI_VDEVICE(INTEL, 0x4bb7), (kernel_ulong_t)&ehl_pwm_info },
{ } /* Terminating Entry */
};
MODULE_DEVICE_TABLE(pci, dwc_pwm_id_table);
diff --git a/drivers/pwm/pwm-dwc.h b/drivers/pwm/pwm-dwc.h
index 64795247c54c..c9bbfc77b568 100644
--- a/drivers/pwm/pwm-dwc.h
+++ b/drivers/pwm/pwm-dwc.h
@@ -33,6 +33,11 @@ MODULE_IMPORT_NS(dwc_pwm);
#define DWC_TIM_CTRL_INT_MASK BIT(2)
#define DWC_TIM_CTRL_PWM BIT(3)

+struct dwc_pwm_info {
+ unsigned int nr;
+ unsigned int size;
+};
+
struct dwc_pwm_ctx {
u32 cnt;
u32 cnt2;
--
2.35.3


2024-02-08 07:06:40

by Raag Jadav

[permalink] [raw]
Subject: [PATCH v2 1/5] pwm: dwc: drop redundant error check

pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
check for failure if the latter is already successful.

Suggested-by: Andy Shevchenko <[email protected]>
Signed-off-by: Raag Jadav <[email protected]>
---
drivers/pwm/pwm-dwc.c | 4 ----
1 file changed, 4 deletions(-)

diff --git a/drivers/pwm/pwm-dwc.c b/drivers/pwm/pwm-dwc.c
index 4929354f8cd9..596a0bb35c40 100644
--- a/drivers/pwm/pwm-dwc.c
+++ b/drivers/pwm/pwm-dwc.c
@@ -50,10 +50,6 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
}

dwc->base = pcim_iomap_table(pci)[0];
- if (!dwc->base) {
- dev_err(dev, "Base address missing\n");
- return -ENOMEM;
- }

ret = devm_pwmchip_add(dev, &dwc->chip);
if (ret)
--
2.35.3


2024-02-08 07:06:46

by Raag Jadav

[permalink] [raw]
Subject: [PATCH v2 5/5] pwm: dwc: use pm_sleep_ptr() macro

Since we don't have runtime PM handles here, we should be using
pm_sleep_ptr() macro, so that the compiler can discard it in case
CONFIG_PM_SLEEP=n.

Fixes: 30b5b066fa83 ("pwm: dwc: Use DEFINE_SIMPLE_DEV_PM_OPS for PM functions")
Signed-off-by: Raag Jadav <[email protected]>
---
drivers/pwm/pwm-dwc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/pwm/pwm-dwc.c b/drivers/pwm/pwm-dwc.c
index cc5bba977f47..bb39cc34f895 100644
--- a/drivers/pwm/pwm-dwc.c
+++ b/drivers/pwm/pwm-dwc.c
@@ -127,7 +127,7 @@ static struct pci_driver dwc_pwm_driver = {
.remove = dwc_pwm_remove,
.id_table = dwc_pwm_id_table,
.driver = {
- .pm = pm_ptr(&dwc_pwm_pm_ops),
+ .pm = pm_sleep_ptr(&dwc_pwm_pm_ops),
},
};

--
2.35.3


2024-02-08 07:47:13

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:
> pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
> check for failure if the latter is already successful.

Is this really true? pcim_iomap_table() calls devres_alloc_node() which
might fail if the allocation fails. (Yes, I know
https://lwn.net/Articles/627419/, but the rule is still to check for
errors, right?)

What am I missing?

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (608.00 B)
signature.asc (499.00 B)
Download all attachments

2024-02-08 17:11:27

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Thu, Feb 08, 2024 at 07:04:34PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 08, 2024 at 08:46:44AM +0100, Uwe Kleine-K?nig wrote:
> > On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:

..

> > (Yes, I know https://lwn.net/Articles/627419/,

Btw, it has nothing to do with this case.

--
With Best Regards,
Andy Shevchenko



2024-02-08 17:22:36

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 3/5] pwm: dwc: simplify error handling

On Thu, Feb 08, 2024 at 12:35:27PM +0530, Raag Jadav wrote:
> Simplify error handling in ->probe() function using dev_err_probe() helper.

..

> + return dev_err_probe(dev, ret, "Failed to enable device (%pe)\n", ERR_PTR(ret));

Have you checked the output?
Note, it will duplicate error codes which we don't want.

..

> + return dev_err_probe(dev, ret, "Failed to iomap PCI BAR (%pe)\n", ERR_PTR(ret));

Ditto.

--
With Best Regards,
Andy Shevchenko



2024-02-08 17:23:07

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 5/5] pwm: dwc: use pm_sleep_ptr() macro

On Thu, Feb 08, 2024 at 12:35:29PM +0530, Raag Jadav wrote:
> Since we don't have runtime PM handles here, we should be using
> pm_sleep_ptr() macro, so that the compiler can discard it in case
> CONFIG_PM_SLEEP=n.

> Fixes: 30b5b066fa83 ("pwm: dwc: Use DEFINE_SIMPLE_DEV_PM_OPS for PM functions")

Fixes always should go first in the series.

--
With Best Regards,
Andy Shevchenko



2024-02-08 17:23:48

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 0/5] DesignWare PWM improvements

On Thu, Feb 08, 2024 at 12:35:24PM +0530, Raag Jadav wrote:
> This series implements 16 channel PWM support for Intel Elkhart Lake
> along with minor cleanups for DesignWare PWM driver.

> Raag Jadav (5):
> pwm: dwc: drop redundant error check
> pwm: dwc: Add 16 channel support for Intel Elkhart Lake
> pwm: dwc: simplify error handling
> pwm: dwc: access driver_data using dev_get_drvdata()
> pwm: dwc: use pm_sleep_ptr() macro

For patches except #3
Reviewed-by: Andy Shevchenko <[email protected]>

--
With Best Regards,
Andy Shevchenko



2024-02-08 20:16:08

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/5] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

On Thu, Feb 08, 2024 at 12:35:26PM +0530, Raag Jadav wrote:
> Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> function with 8 channels each. Add support for the remaining channels.

Side Q: Have you used --histogram diff algo when prepared the series?
If no, it's better to start using it.

--
With Best Regards,
Andy Shevchenko



2024-02-08 22:04:03

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Thu, Feb 08, 2024 at 08:46:44AM +0100, Uwe Kleine-K?nig wrote:
> On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:
> > pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
> > check for failure if the latter is already successful.
>
> Is this really true? pcim_iomap_table() calls devres_alloc_node() which
> might fail if the allocation fails. (Yes, I know
> https://lwn.net/Articles/627419/, but the rule is still to check for
> errors, right?)

We do not add a dead code to the kernel, right?

> What am I missing?

Mysterious ways of the twisted PCI devres code.
Read the above commit message again :-)

For your convenience I can elaborate. pcim_iomap_table() calls _first_
devres_find() which _will_ succeed if the pcim_iomap_regions() previously
succeeded. Does it help to understand how it designed?

--
With Best Regards,
Andy Shevchenko



2024-02-09 19:20:06

by Raag Jadav

[permalink] [raw]
Subject: Re: [PATCH v2 2/5] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

On Thu, Feb 08, 2024 at 07:20:59PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 08, 2024 at 12:35:26PM +0530, Raag Jadav wrote:
> > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > function with 8 channels each. Add support for the remaining channels.
>
> Side Q: Have you used --histogram diff algo when prepared the series?
> If no, it's better to start using it.

I used it for a few weeks, but I think I've grown a bit too comfortable
with patience.

I'll use histogram for pinctrl stuff if you insist :)

Raag

2024-02-09 20:33:15

by Raag Jadav

[permalink] [raw]
Subject: Re: [PATCH v2 3/5] pwm: dwc: simplify error handling

On Thu, Feb 08, 2024 at 07:22:13PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 08, 2024 at 12:35:27PM +0530, Raag Jadav wrote:
> > Simplify error handling in ->probe() function using dev_err_probe() helper.
>
> ...
>
> > + return dev_err_probe(dev, ret, "Failed to enable device (%pe)\n", ERR_PTR(ret));
>
> Have you checked the output?
> Note, it will duplicate error codes which we don't want.

True. Does it make sense to remove it?

Raag

2024-02-10 00:52:14

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/5] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

On Fri, Feb 09, 2024 at 09:18:41PM +0200, Raag Jadav wrote:
> On Thu, Feb 08, 2024 at 07:20:59PM +0200, Andy Shevchenko wrote:
> > On Thu, Feb 08, 2024 at 12:35:26PM +0530, Raag Jadav wrote:
> > > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > > function with 8 channels each. Add support for the remaining channels.
> >
> > Side Q: Have you used --histogram diff algo when prepared the series?
> > If no, it's better to start using it.
>
> I used it for a few weeks, but I think I've grown a bit too comfortable
> with patience.
>
> I'll use histogram for pinctrl stuff if you insist :)

It's recommended by Torvalds:
https://lore.kernel.org/linux-gpio/CAHk-=wiVNOFP1dzKdCqXvoery5p8QoBB5THiJUMbZ1TxJb7FhQ@mail.gmail.com/

--
With Best Regards,
Andy Shevchenko



2024-02-10 17:20:16

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 2/5] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

Hello,

On Fri, Feb 09, 2024 at 09:41:50PM +0200, Andy Shevchenko wrote:
> On Fri, Feb 09, 2024 at 09:18:41PM +0200, Raag Jadav wrote:
> > On Thu, Feb 08, 2024 at 07:20:59PM +0200, Andy Shevchenko wrote:
> > > On Thu, Feb 08, 2024 at 12:35:26PM +0530, Raag Jadav wrote:
> > > > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > > > function with 8 channels each. Add support for the remaining channels.
> > >
> > > Side Q: Have you used --histogram diff algo when prepared the series?
> > > If no, it's better to start using it.
> >
> > I used it for a few weeks, but I think I've grown a bit too comfortable
> > with patience.
> >
> > I'll use histogram for pinctrl stuff if you insist :)
>
> It's recommended by Torvalds:
> https://lore.kernel.org/linux-gpio/CAHk-=wiVNOFP1dzKdCqXvoery5p8QoBB5THiJUMbZ1TxJb7FhQ@mail.gmail.com/

BTW, the magic to use it by default is:

git config --global diff.algorithm histogram

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.12 kB)
signature.asc (499.00 B)
Download all attachments

2024-02-12 11:52:34

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 3/5] pwm: dwc: simplify error handling

On Fri, Feb 09, 2024 at 10:33:01PM +0200, Raag Jadav wrote:
> On Thu, Feb 08, 2024 at 07:22:13PM +0200, Andy Shevchenko wrote:
> > On Thu, Feb 08, 2024 at 12:35:27PM +0530, Raag Jadav wrote:

..

> > > + return dev_err_probe(dev, ret, "Failed to enable device (%pe)\n", ERR_PTR(ret));
> >
> > Have you checked the output?
> > Note, it will duplicate error codes which we don't want.
>
> True. Does it make sense to remove it?

"...which we don't want." had been stated above :-)

--
With Best Regards,
Andy Shevchenko



2024-02-12 11:53:47

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/5] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

On Sat, Feb 10, 2024 at 06:19:53PM +0100, Uwe Kleine-K?nig wrote:
> On Fri, Feb 09, 2024 at 09:41:50PM +0200, Andy Shevchenko wrote:
> > On Fri, Feb 09, 2024 at 09:18:41PM +0200, Raag Jadav wrote:
> > > On Thu, Feb 08, 2024 at 07:20:59PM +0200, Andy Shevchenko wrote:
> > > > On Thu, Feb 08, 2024 at 12:35:26PM +0530, Raag Jadav wrote:

..

> > > > Side Q: Have you used --histogram diff algo when prepared the series?
> > > > If no, it's better to start using it.
> > >
> > > I used it for a few weeks, but I think I've grown a bit too comfortable
> > > with patience.
> > >
> > > I'll use histogram for pinctrl stuff if you insist :)
> >
> > It's recommended by Torvalds:
> > https://lore.kernel.org/linux-gpio/CAHk-=wiVNOFP1dzKdCqXvoery5p8QoBB5THiJUMbZ1TxJb7FhQ@mail.gmail.com/
>
> BTW, the magic to use it by default is:
>
> git config --global diff.algorithm histogram

Yep, that's what I have on my machines for development.

--
With Best Regards,
Andy Shevchenko



2024-02-14 17:46:12

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

Hello Andy,

On Thu, Feb 08, 2024 at 07:04:33PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 08, 2024 at 08:46:44AM +0100, Uwe Kleine-K?nig wrote:
> > On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:
> > > pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
> > > check for failure if the latter is already successful.
> >
> > Is this really true? pcim_iomap_table() calls devres_alloc_node() which
> > might fail if the allocation fails. (Yes, I know
> > https://lwn.net/Articles/627419/, but the rule is still to check for
> > errors, right?)
>
> We do not add a dead code to the kernel, right?
>
> > What am I missing?
>
> Mysterious ways of the twisted PCI devres code.
> Read the above commit message again :-)
>
> For your convenience I can elaborate. pcim_iomap_table() calls _first_
> devres_find() which _will_ succeed if the pcim_iomap_regions() previously
> succeeded. Does it help to understand how it designed?

I assume you're saying that after pcim_iomap_regions() succeeded it's
already known that pcim_iomap_table() succeeds (because the former
already called the latter).

I'm still concerned here. I agree that error checking might be skipped
if it's clear that no error can happen (the device cannot disappear
between these two calls, can it?), but for me as an uninitiated pci code
reader, I wonder about

dwc->base = pcim_iomap_table(pci)[0];

without error checking. (OTOH, if pcim_iomap_table() returned NULL, the
"[0]" part is already problematic.)

I'd like to have a code comment here saying that pcim_iomap_table()
won't return NULL.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.78 kB)
signature.asc (499.00 B)
Download all attachments

2024-02-14 17:57:36

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Wed, Feb 14, 2024 at 06:45:48PM +0100, Uwe Kleine-K?nig wrote:
> On Thu, Feb 08, 2024 at 07:04:33PM +0200, Andy Shevchenko wrote:
> > On Thu, Feb 08, 2024 at 08:46:44AM +0100, Uwe Kleine-K?nig wrote:
> > > On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:
> > > > pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
> > > > check for failure if the latter is already successful.
> > >
> > > Is this really true? pcim_iomap_table() calls devres_alloc_node() which
> > > might fail if the allocation fails. (Yes, I know
> > > https://lwn.net/Articles/627419/, but the rule is still to check for
> > > errors, right?)
> >
> > We do not add a dead code to the kernel, right?
> >
> > > What am I missing?
> >
> > Mysterious ways of the twisted PCI devres code.
> > Read the above commit message again :-)
> >
> > For your convenience I can elaborate. pcim_iomap_table() calls _first_
> > devres_find() which _will_ succeed if the pcim_iomap_regions() previously
> > succeeded. Does it help to understand how it designed?
>
> I assume you're saying that after pcim_iomap_regions() succeeded it's
> already known that pcim_iomap_table() succeeds (because the former
> already called the latter).
>
> I'm still concerned here. I agree that error checking might be skipped
> if it's clear that no error can happen (the device cannot disappear
> between these two calls, can it?),

It depends. If you call it in some asynchronous callbacks which may be run
after PCI device disappears, then indeed, it's problematic. But you probably
will have much bigger issue at that point already.

In ->probe() it's guaranteed to work as I suggested (assuming properly working
hardware).

> but for me as an uninitiated pci code
> reader, I wonder about
>
> dwc->base = pcim_iomap_table(pci)[0];
>
> without error checking. (OTOH, if pcim_iomap_table() returned NULL, the
> "[0]" part is already problematic.)

Seems it's your problem, many drivers use the way I suggested.

> I'd like to have a code comment here saying that pcim_iomap_table()
> won't return NULL.

Why? It's redundant. If you use it, you should know this API.
So, the bottom line, does this API needs better documentation?

--
With Best Regards,
Andy Shevchenko



2024-02-15 09:25:05

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Wed, Feb 14, 2024 at 07:54:58PM +0200, Andy Shevchenko wrote:
> On Wed, Feb 14, 2024 at 06:45:48PM +0100, Uwe Kleine-K?nig wrote:
> > On Thu, Feb 08, 2024 at 07:04:33PM +0200, Andy Shevchenko wrote:
> > > On Thu, Feb 08, 2024 at 08:46:44AM +0100, Uwe Kleine-K?nig wrote:
> > > > On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:
> > > > > pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
> > > > > check for failure if the latter is already successful.
> > > >
> > > > Is this really true? pcim_iomap_table() calls devres_alloc_node() which
> > > > might fail if the allocation fails. (Yes, I know
> > > > https://lwn.net/Articles/627419/, but the rule is still to check for
> > > > errors, right?)
> > >
> > > We do not add a dead code to the kernel, right?
> > >
> > > > What am I missing?
> > >
> > > Mysterious ways of the twisted PCI devres code.
> > > Read the above commit message again :-)
> > >
> > > For your convenience I can elaborate. pcim_iomap_table() calls _first_
> > > devres_find() which _will_ succeed if the pcim_iomap_regions() previously
> > > succeeded. Does it help to understand how it designed?
> >
> > I assume you're saying that after pcim_iomap_regions() succeeded it's
> > already known that pcim_iomap_table() succeeds (because the former
> > already called the latter).
> >
> > I'm still concerned here. I agree that error checking might be skipped
> > if it's clear that no error can happen (the device cannot disappear
> > between these two calls, can it?),
>
> It depends. If you call it in some asynchronous callbacks which may be run
> after PCI device disappears, then indeed, it's problematic. But you probably
> will have much bigger issue at that point already.
>
> In ->probe() it's guaranteed to work as I suggested (assuming properly working
> hardware).

Assuming properly working hardware allows to drop many error checks :-)

> > but for me as an uninitiated pci code
> > reader, I wonder about
> >
> > dwc->base = pcim_iomap_table(pci)[0];
> >
> > without error checking. (OTOH, if pcim_iomap_table() returned NULL, the
> > "[0]" part is already problematic.)
>
> Seems it's your problem, many drivers use the way I suggested.
>
> > I'd like to have a code comment here saying that pcim_iomap_table()
> > won't return NULL.
>
> Why? It's redundant. If you use it, you should know this API.
> So, the bottom line, does this API needs better documentation?

If a driver author knows it while writing the code, it's obvious. But if
the driver author looks again in 2 years or someone else (e.g. me with
the PWM maintainer hat on and with little pci experience) that knowledge
might be faded.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (2.86 kB)
signature.asc (499.00 B)
Download all attachments

2024-02-15 11:22:55

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 5/5] pwm: dwc: use pm_sleep_ptr() macro

On Thu, Feb 08, 2024 at 07:22:52PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 08, 2024 at 12:35:29PM +0530, Raag Jadav wrote:
> > Since we don't have runtime PM handles here, we should be using
> > pm_sleep_ptr() macro, so that the compiler can discard it in case
> > CONFIG_PM_SLEEP=n.
>
> > Fixes: 30b5b066fa83 ("pwm: dwc: Use DEFINE_SIMPLE_DEV_PM_OPS for PM functions")
>
> Fixes always should go first in the series.

No need to resend for that, I'll pick patches 3-5 in the order I
consider sensible. Git seems to handle that just fine.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (737.00 B)
signature.asc (499.00 B)
Download all attachments

2024-02-15 13:36:40

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Thu, Feb 15, 2024 at 10:22:57AM +0100, Uwe Kleine-K?nig wrote:
> On Wed, Feb 14, 2024 at 07:54:58PM +0200, Andy Shevchenko wrote:
> > On Wed, Feb 14, 2024 at 06:45:48PM +0100, Uwe Kleine-K?nig wrote:
> > > On Thu, Feb 08, 2024 at 07:04:33PM +0200, Andy Shevchenko wrote:
> > > > On Thu, Feb 08, 2024 at 08:46:44AM +0100, Uwe Kleine-K?nig wrote:
> > > > > On Thu, Feb 08, 2024 at 12:35:25PM +0530, Raag Jadav wrote:
> > > > > > pcim_iomap_table() fails only if pcim_iomap_regions() fails. No need to
> > > > > > check for failure if the latter is already successful.
> > > > >
> > > > > Is this really true? pcim_iomap_table() calls devres_alloc_node() which
> > > > > might fail if the allocation fails. (Yes, I know
> > > > > https://lwn.net/Articles/627419/, but the rule is still to check for
> > > > > errors, right?)
> > > >
> > > > We do not add a dead code to the kernel, right?
> > > >
> > > > > What am I missing?
> > > >
> > > > Mysterious ways of the twisted PCI devres code.
> > > > Read the above commit message again :-)
> > > >
> > > > For your convenience I can elaborate. pcim_iomap_table() calls _first_
> > > > devres_find() which _will_ succeed if the pcim_iomap_regions() previously
> > > > succeeded. Does it help to understand how it designed?
> > >
> > > I assume you're saying that after pcim_iomap_regions() succeeded it's
> > > already known that pcim_iomap_table() succeeds (because the former
> > > already called the latter).
> > >
> > > I'm still concerned here. I agree that error checking might be skipped
> > > if it's clear that no error can happen (the device cannot disappear
> > > between these two calls, can it?),
> >
> > It depends. If you call it in some asynchronous callbacks which may be run
> > after PCI device disappears, then indeed, it's problematic. But you probably
> > will have much bigger issue at that point already.
> >
> > In ->probe() it's guaranteed to work as I suggested (assuming properly working
> > hardware).
>
> Assuming properly working hardware allows to drop many error checks :-)

Yes, and we have some checks are being not implemented ("dropped"), but here is
the thing: this is a PCI device and surprise removal (while it's not possible
for the on-die devices) should be handled differently, not related to this code
anyway. Malicious hardware is out of scope either.

> > > but for me as an uninitiated pci code
> > > reader, I wonder about
> > >
> > > dwc->base = pcim_iomap_table(pci)[0];
> > >
> > > without error checking. (OTOH, if pcim_iomap_table() returned NULL, the
> > > "[0]" part is already problematic.)
> >
> > Seems it's your problem, many drivers use the way I suggested.
> >
> > > I'd like to have a code comment here saying that pcim_iomap_table()
> > > won't return NULL.
> >
> > Why? It's redundant. If you use it, you should know this API.
> > So, the bottom line, does this API needs better documentation?
>
> If a driver author knows it while writing the code, it's obvious. But if
> the driver author looks again in 2 years or someone else (e.g. me with
> the PWM maintainer hat on and with little pci experience) that knowledge
> might be faded.

This is widely used pattern. Anybody who works with Git should know how
to use `git grep` tool. If in doubts, always can ask in the mailing lists.
I still consider it redundant.

P.S. That's what you call "bikeshedding" (done by yourself here)?



--
With Best Regards,
Andy Shevchenko



2024-02-15 17:20:51

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

Hello Andy,

On Thu, Feb 15, 2024 at 03:36:12PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 15, 2024 at 10:22:57AM +0100, Uwe Kleine-K?nig wrote:
> > If a driver author knows it while writing the code, it's obvious. But if
> > the driver author looks again in 2 years or someone else (e.g. me with
> > the PWM maintainer hat on and with little pci experience) that knowledge
> > might be faded.
>
> This is widely used pattern. Anybody who works with Git should know how
> to use `git grep` tool. If in doubts, always can ask in the mailing lists.

IMHO you're assuming to much. If someone sees this pattern and quickly
looks at the implementation of pcim_iomap_table() they might (as I did)
conclude that this call should be error checked. If they send a patch in
say 2 years I think I won't remember this discussion/patch and happily
accept this patch. And I probably won't get enough doubts to start
grepping around.

> I still consider it redundant.
>
> P.S. That's what you call "bikeshedding" (done by yourself here)?

I can understand that you consider that bikeshedding given that for you
it's obvious that the second function cannot fail. For me it's not and I
take this as a hint that it's not obvious for everyone.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.40 kB)
signature.asc (499.00 B)
Download all attachments

2024-02-15 20:37:21

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/5] pwm: dwc: drop redundant error check

On Thu, Feb 15, 2024 at 06:20:15PM +0100, Uwe Kleine-K?nig wrote:
> On Thu, Feb 15, 2024 at 03:36:12PM +0200, Andy Shevchenko wrote:
> > On Thu, Feb 15, 2024 at 10:22:57AM +0100, Uwe Kleine-K?nig wrote:
> > > If a driver author knows it while writing the code, it's obvious. But if
> > > the driver author looks again in 2 years or someone else (e.g. me with
> > > the PWM maintainer hat on and with little pci experience) that knowledge
> > > might be faded.
> >
> > This is widely used pattern. Anybody who works with Git should know how
> > to use `git grep` tool. If in doubts, always can ask in the mailing lists.
>
> IMHO you're assuming to much. If someone sees this pattern and quickly
> looks at the implementation of pcim_iomap_table() they might (as I did)
> conclude that this call should be error checked. If they send a patch in
> say 2 years I think I won't remember this discussion/patch and happily
> accept this patch. And I probably won't get enough doubts to start
> grepping around.
>
> > I still consider it redundant.
> >
> > P.S. That's what you call "bikeshedding" (done by yourself here)?
>
> I can understand that you consider that bikeshedding given that for you
> it's obvious that the second function cannot fail. For me it's not and I
> take this as a hint that it's not obvious for everyone.

The bottom line that PCI devres code should be refactored. And IIRC somebody
is doing that job, not sure at which stage it is now.

--
With Best Regards,
Andy Shevchenko