mbox series

[v2,0/6] iommu/exynos: Convert to a module

Message ID 20221103195154.21495-1-semen.protsenko@linaro.org
Headers show
Series iommu/exynos: Convert to a module | expand

Message

Sam Protsenko Nov. 3, 2022, 7:51 p.m. UTC
As exynos-iommu driver is not a critical platform driver, it can be
converted to a loadable module to avoid loading it on non-Exynos
platforms in order to improve the RAM footprint. This patch series
converts it to a module and does some related cleanups. IOMMU/DMA
specifics were taken into the account, so remove/exit methods weren't
added.

There are two drivers using CONFIG_EXYNOS_IOMMU in their code:
DRM_EXYNOS and S5P_MFC. Both were checked, and only a slight change was
needed for S5P_MFC driver (patch #6).

Changes in v2:
  - Extracted the "shutdown" method addition into a separate patch
  - Added MODULE_DEVICE_TABLE(of, ...) to support hot-plug loading
  - Added MODULE_ALIAS("platform:exynos-sysmmu")
  - Added fix for S5P_MFC driver to work correctly with EXYNOS_IOMMU=m
  - Fixed checkpatch coding style suggestion with "--strict" flag
  - Rebased on top of most recent joro/iommu.git:next

Sam Protsenko (6):
  iommu: Export iommu_group_default_domain() API
  iommu/exynos: Fix retval on getting clocks in probe
  iommu/exynos: Modularize the driver
  iommu/exynos: Implement shutdown driver method
  iommu/exynos: Rearrange the platform driver code
  media: platform: Use IS_ENABLED() to check EXYNOS_IOMMU in s5p_mfc

 drivers/iommu/Kconfig                         |   2 +-
 drivers/iommu/exynos-iommu.c                  | 355 +++++++++---------
 drivers/iommu/iommu.c                         |   1 +
 .../platform/samsung/s5p-mfc/s5p_mfc_iommu.h  |   4 +-
 4 files changed, 191 insertions(+), 171 deletions(-)

Comments

Marek Szyprowski Nov. 4, 2022, 12:10 p.m. UTC | #1
On 03.11.2022 20:51, Sam Protsenko wrote:
> As exynos-iommu driver is not a critical platform driver, it can be
> converted to a loadable module to avoid loading it on non-Exynos
> platforms in order to improve the RAM footprint. This patch series
> converts it to a module and does some related cleanups. IOMMU/DMA
> specifics were taken into the account, so remove/exit methods weren't
> added.
>
> There are two drivers using CONFIG_EXYNOS_IOMMU in their code:
> DRM_EXYNOS and S5P_MFC. Both were checked, and only a slight change was
> needed for S5P_MFC driver (patch #6).

Funny, compiling this driver as a module revealed an issue in the driver 
initialization sequence, here is a fix that need to be applied before 
this patchset:

https://lore.kernel.org/all/20221104115511.28256-1-m.szyprowski@samsung.com/

Besides that, the driver nukes with NULL pointer dereference in 
exynos_iommu_of_xlate() when compiled as a module on ARM 64bit 
Exynos5433 based TM2e board. ARM 32bit based board works fine. I'm 
checking this issue now.

> Changes in v2:
>    - Extracted the "shutdown" method addition into a separate patch
>    - Added MODULE_DEVICE_TABLE(of, ...) to support hot-plug loading
>    - Added MODULE_ALIAS("platform:exynos-sysmmu")
>    - Added fix for S5P_MFC driver to work correctly with EXYNOS_IOMMU=m
>    - Fixed checkpatch coding style suggestion with "--strict" flag
>    - Rebased on top of most recent joro/iommu.git:next
>
> Sam Protsenko (6):
>    iommu: Export iommu_group_default_domain() API
>    iommu/exynos: Fix retval on getting clocks in probe
>    iommu/exynos: Modularize the driver
>    iommu/exynos: Implement shutdown driver method
>    iommu/exynos: Rearrange the platform driver code
>    media: platform: Use IS_ENABLED() to check EXYNOS_IOMMU in s5p_mfc
>
>   drivers/iommu/Kconfig                         |   2 +-
>   drivers/iommu/exynos-iommu.c                  | 355 +++++++++---------
>   drivers/iommu/iommu.c                         |   1 +
>   .../platform/samsung/s5p-mfc/s5p_mfc_iommu.h  |   4 +-
>   4 files changed, 191 insertions(+), 171 deletions(-)
>
Best regards
Sam Protsenko Nov. 4, 2022, 1:22 p.m. UTC | #2
On Fri, 4 Nov 2022 at 13:10, Marek Szyprowski <m.szyprowski@samsung.com> wrote:
>
> On 03.11.2022 20:51, Sam Protsenko wrote:
> > As exynos-iommu driver is not a critical platform driver, it can be
> > converted to a loadable module to avoid loading it on non-Exynos
> > platforms in order to improve the RAM footprint. This patch series
> > converts it to a module and does some related cleanups. IOMMU/DMA
> > specifics were taken into the account, so remove/exit methods weren't
> > added.
> >
> > There are two drivers using CONFIG_EXYNOS_IOMMU in their code:
> > DRM_EXYNOS and S5P_MFC. Both were checked, and only a slight change was
> > needed for S5P_MFC driver (patch #6).
>
> Funny, compiling this driver as a module revealed an issue in the driver
> initialization sequence, here is a fix that need to be applied before
> this patchset:
>
> https://lore.kernel.org/all/20221104115511.28256-1-m.szyprowski@samsung.com/
>

I wonder why I didn't stumble upon that issue. Anyway, the fix looks
valid. I'll wait until you finish looking into that NULL pointer
dereference, and then we can figure out how to organize our patches.
Maybe I could send v3, including your patches in the beginning of the
series, etc.

> Besides that, the driver nukes with NULL pointer dereference in
> exynos_iommu_of_xlate() when compiled as a module on ARM 64bit
> Exynos5433 based TM2e board. ARM 32bit based board works fine. I'm
> checking this issue now.
>

Thanks for looking into that, Marek! I've tested the modularization
patch series using my emulated translation driver [1,2]. It works
fine, but exynos_iommu_of_xlate() is only executed once in my case,
when the test driver module is loaded. It's finished successfully
though. Not sure what is the cause of NULL pointer dereferences you're
seeing, alas I still don't have real users of SysMMU driver on my
platform, and my test driver is the only way for me to test it. Please
let me know if I can help in any way (e.g. I can trace some calls on
my board for you).

[1] https://github.com/joe-skb7/linux/blob/e850-96-mainline-iommu/drivers/iommu/samsung-iommu-test.c#L263
[2] https://github.com/joe-skb7/linux/blob/e850-96-mainline-iommu/arch/arm64/boot/dts/exynos/exynos850.dtsi#L477



> > Changes in v2:
> >    - Extracted the "shutdown" method addition into a separate patch
> >    - Added MODULE_DEVICE_TABLE(of, ...) to support hot-plug loading
> >    - Added MODULE_ALIAS("platform:exynos-sysmmu")
> >    - Added fix for S5P_MFC driver to work correctly with EXYNOS_IOMMU=m
> >    - Fixed checkpatch coding style suggestion with "--strict" flag
> >    - Rebased on top of most recent joro/iommu.git:next
> >
> > Sam Protsenko (6):
> >    iommu: Export iommu_group_default_domain() API
> >    iommu/exynos: Fix retval on getting clocks in probe
> >    iommu/exynos: Modularize the driver
> >    iommu/exynos: Implement shutdown driver method
> >    iommu/exynos: Rearrange the platform driver code
> >    media: platform: Use IS_ENABLED() to check EXYNOS_IOMMU in s5p_mfc
> >
> >   drivers/iommu/Kconfig                         |   2 +-
> >   drivers/iommu/exynos-iommu.c                  | 355 +++++++++---------
> >   drivers/iommu/iommu.c                         |   1 +
> >   .../platform/samsung/s5p-mfc/s5p_mfc_iommu.h  |   4 +-
> >   4 files changed, 191 insertions(+), 171 deletions(-)
> >
> Best regards
> --
> Marek Szyprowski, PhD
> Samsung R&D Institute Poland
>
Krzysztof Kozlowski Nov. 4, 2022, 1:46 p.m. UTC | #3
On 03/11/2022 15:51, Sam Protsenko wrote:
> Rework the driver so it can be built as a loadable module. That can be
> useful as not all ARM64 platforms need it. And that's ok for it to be a
> module because it's not a critical driver (platform can work when it's
> disabled).
> 
> Remove method and module exit function are not implemented, as the
> removal of IOMMUs cannot be done reliably. As Robin Murphy mentioned in
> [1]:
> 
>     ...it's better not to even pretend that removing an IOMMU's driver
>     while other drivers are using it (usually via DMA ops without even
>     realising) is going to have anything other than catastrophic
>     results.
> 
> [1] https://lore.kernel.org/lkml/20220702213724.3949-2-semen.protsenko@linaro.org/T/#md7e1e3f5b2c9e7fa5bc28fe33e818b6aa4a7237c
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>


Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof
Krzysztof Kozlowski Nov. 4, 2022, 1:48 p.m. UTC | #4
On 03/11/2022 15:51, Sam Protsenko wrote:
> Now that CONFIG_EXYNOS_IOMMU can be built as a module, it's not correct
> anymore to check whether it's enabled or not just with #ifdef. Use
> proper IS_ENABLED() macro to handle both built-in and module cases.
> 
> Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
> Changes in v2:


Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof
Marek Szyprowski Nov. 10, 2022, 2:36 p.m. UTC | #5
On 04.11.2022 13:10, Marek Szyprowski wrote:
> On 03.11.2022 20:51, Sam Protsenko wrote:
>> As exynos-iommu driver is not a critical platform driver, it can be
>> converted to a loadable module to avoid loading it on non-Exynos
>> platforms in order to improve the RAM footprint. This patch series
>> converts it to a module and does some related cleanups. IOMMU/DMA
>> specifics were taken into the account, so remove/exit methods weren't
>> added.
>>
>> There are two drivers using CONFIG_EXYNOS_IOMMU in their code:
>> DRM_EXYNOS and S5P_MFC. Both were checked, and only a slight change was
>> needed for S5P_MFC driver (patch #6).
>
> Funny, compiling this driver as a module revealed an issue in the 
> driver initialization sequence, here is a fix that need to be applied 
> before this patchset:
>
> https://lore.kernel.org/all/20221104115511.28256-1-m.szyprowski@samsung.com/ 
>
>
> Besides that, the driver nukes with NULL pointer dereference in 
> exynos_iommu_of_xlate() when compiled as a module on ARM 64bit 
> Exynos5433 based TM2e board. ARM 32bit based board works fine. I'm 
> checking this issue now.
>
I've finally made Exynos IOMMU working as a module on Exynos5433 based 
TM2e board. It looks that this will be a bit longer journey that I've 
initially thought. I've posted a simple update of the fix for the driver 
initialization sequence, but the real problem is in the platform driver 
framework and OF helpers.

Basically to get it working as a module I had to apply the following 
changes:

diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 3dda62503102..f6921f5fcab6 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -257,7 +257,7 @@ static int deferred_devs_show(struct seq_file *s, 
void *data)
  DEFINE_SHOW_ATTRIBUTE(deferred_devs);

  #ifdef CONFIG_MODULES
-int driver_deferred_probe_timeout = 10;
+int driver_deferred_probe_timeout = 30;
  #else
  int driver_deferred_probe_timeout;
  #endif
diff --git a/drivers/of/property.c b/drivers/of/property.c
index 967f79b59016..e5df6672fee6 100644
--- a/drivers/of/property.c
+++ b/drivers/of/property.c
@@ -1384,7 +1384,7 @@ static struct device_node *parse_interrupts(struct 
device_node *np,
  static const struct supplier_bindings of_supplier_bindings[] = {
         { .parse_prop = parse_clocks, },
         { .parse_prop = parse_interconnects, },
-       { .parse_prop = parse_iommus, .optional = true, },
+       { .parse_prop = parse_iommus, },
         { .parse_prop = parse_iommu_maps, .optional = true, },
         { .parse_prop = parse_mboxes, },
         { .parse_prop = parse_io_channels, },

Without that a really nasty things happened.

Initialization of the built-in drivers and loading modules takes time, 
so the default 10s deferred probe timeout is not enough to ensure that 
the built-in driver won't be probed before the Exynos IOMMU driver is 
loaded.

The second change fixes the problem that driver core probes Exynos IOMMU 
controllers in parallel to probing the master devices, what results in 
calling exynos_iommu_of_xlate() and exynos_iommu_probe_device() even on 
the partially initialized IOMMU controllers or initializing the dma_ops 
under the already probed and working master device. This was easy to 
observe especially on the master devices with multiple IOMMU 
controllers. I wasn't able to solve this concurrency/race issues inside 
the Exynos IOMMU driver.

Frankly speaking I don't know what is the rationale for making the 
'iommus' property optional, but this simply doesn't work well with IOMMU 
driver being a module. CCed Saravana and Rob for this.

Without fixing the above issues, I would add a warning that compiling 
the driver as a module leads to serious issues.


>> Changes in v2:
>>    - Extracted the "shutdown" method addition into a separate patch
>>    - Added MODULE_DEVICE_TABLE(of, ...) to support hot-plug loading
>>    - Added MODULE_ALIAS("platform:exynos-sysmmu")
>>    - Added fix for S5P_MFC driver to work correctly with EXYNOS_IOMMU=m
>>    - Fixed checkpatch coding style suggestion with "--strict" flag
>>    - Rebased on top of most recent joro/iommu.git:next
>>
>> Sam Protsenko (6):
>>    iommu: Export iommu_group_default_domain() API
>>    iommu/exynos: Fix retval on getting clocks in probe
>>    iommu/exynos: Modularize the driver
>>    iommu/exynos: Implement shutdown driver method
>>    iommu/exynos: Rearrange the platform driver code
>>    media: platform: Use IS_ENABLED() to check EXYNOS_IOMMU in s5p_mfc
>>
>>   drivers/iommu/Kconfig                         |   2 +-
>>   drivers/iommu/exynos-iommu.c                  | 355 +++++++++---------
>>   drivers/iommu/iommu.c                         |   1 +
>>   .../platform/samsung/s5p-mfc/s5p_mfc_iommu.h  |   4 +-
>>   4 files changed, 191 insertions(+), 171 deletions(-)
>>
> Best regards

Best regards
Sam Protsenko Nov. 11, 2022, 1:29 p.m. UTC | #6
On Thu, 10 Nov 2022 at 15:36, Marek Szyprowski <m.szyprowski@samsung.com> wrote:

[snip]

> I've finally made Exynos IOMMU working as a module on Exynos5433 based
> TM2e board. It looks that this will be a bit longer journey that I've
> initially thought. I've posted a simple update of the fix for the driver
> initialization sequence, but the real problem is in the platform driver
> framework and OF helpers.
>
> Basically to get it working as a module I had to apply the following
> changes:
>
> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> index 3dda62503102..f6921f5fcab6 100644
> --- a/drivers/base/dd.c
> +++ b/drivers/base/dd.c
> @@ -257,7 +257,7 @@ static int deferred_devs_show(struct seq_file *s,
> void *data)
>   DEFINE_SHOW_ATTRIBUTE(deferred_devs);
>
>   #ifdef CONFIG_MODULES
> -int driver_deferred_probe_timeout = 10;
> +int driver_deferred_probe_timeout = 30;
>   #else
>   int driver_deferred_probe_timeout;
>   #endif
> diff --git a/drivers/of/property.c b/drivers/of/property.c
> index 967f79b59016..e5df6672fee6 100644
> --- a/drivers/of/property.c
> +++ b/drivers/of/property.c
> @@ -1384,7 +1384,7 @@ static struct device_node *parse_interrupts(struct
> device_node *np,
>   static const struct supplier_bindings of_supplier_bindings[] = {
>          { .parse_prop = parse_clocks, },
>          { .parse_prop = parse_interconnects, },
> -       { .parse_prop = parse_iommus, .optional = true, },
> +       { .parse_prop = parse_iommus, },
>          { .parse_prop = parse_iommu_maps, .optional = true, },
>          { .parse_prop = parse_mboxes, },
>          { .parse_prop = parse_io_channels, },
>
> Without that a really nasty things happened.
>
> Initialization of the built-in drivers and loading modules takes time,
> so the default 10s deferred probe timeout is not enough to ensure that
> the built-in driver won't be probed before the Exynos IOMMU driver is
> loaded.
>

Yeah, the whole time-based sync looks nasty... I remember coming
across the slides by Andrzej Hajda called "Deferred Problem" [1], but
I guess the proposed solution was never applied. Just hope that
increasing the timeout is upstreamable solution.

[1] https://events19.linuxfoundation.org/wp-content/uploads/2017/12/Deferred-Problem-Issues-With-Complex-Dependencies-Between-Devices-in-Linux-Kernel-Andrzej-Hajda-Samsung.pdf

> The second change fixes the problem that driver core probes Exynos IOMMU
> controllers in parallel to probing the master devices, what results in
> calling exynos_iommu_of_xlate() and exynos_iommu_probe_device() even on
> the partially initialized IOMMU controllers or initializing the dma_ops
> under the already probed and working master device. This was easy to
> observe especially on the master devices with multiple IOMMU
> controllers. I wasn't able to solve this concurrency/race issues inside
> the Exynos IOMMU driver.
>
> Frankly speaking I don't know what is the rationale for making the
> 'iommus' property optional, but this simply doesn't work well with IOMMU
> driver being a module. CCed Saravana and Rob for this.
>

The patch which makes 'iommus' optional doesn't provide much of
insight on reasons in commit message either.

> Without fixing the above issues, I would add a warning that compiling
> the driver as a module leads to serious issues.
>

Nice catch! It doesn't reproduce on my platform, alas. Can I expect
you to submit those patches? If so, I'll probably just wait for those
to be applied, and then re-send my modularization series on top of it.
Does that sounds reasonable?

[snip]

>
> Best regards
> --
> Marek Szyprowski, PhD
> Samsung R&D Institute Poland
>
Saravana Kannan Feb. 7, 2023, 3:32 a.m. UTC | #7
On Fri, Nov 11, 2022 at 5:30 AM Sam Protsenko
<semen.protsenko@linaro.org> wrote:
>
> On Thu, 10 Nov 2022 at 15:36, Marek Szyprowski <m.szyprowski@samsung.com> wrote:
>
> [snip]

Hi Marek and Sam,

I'm replying to both of your comments in this email.

> > I've finally made Exynos IOMMU working as a module on Exynos5433 based
> > TM2e board. It looks that this will be a bit longer journey that I've
> > initially thought. I've posted a simple update of the fix for the driver
> > initialization sequence, but the real problem is in the platform driver
> > framework and OF helpers.
> >
> > Basically to get it working as a module I had to apply the following
> > changes:
> >
> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> > index 3dda62503102..f6921f5fcab6 100644
> > --- a/drivers/base/dd.c
> > +++ b/drivers/base/dd.c
> > @@ -257,7 +257,7 @@ static int deferred_devs_show(struct seq_file *s,
> > void *data)
> >   DEFINE_SHOW_ATTRIBUTE(deferred_devs);
> >
> >   #ifdef CONFIG_MODULES
> > -int driver_deferred_probe_timeout = 10;
> > +int driver_deferred_probe_timeout = 30;
> >   #else
> >   int driver_deferred_probe_timeout;
> >   #endif
> > diff --git a/drivers/of/property.c b/drivers/of/property.c
> > index 967f79b59016..e5df6672fee6 100644
> > --- a/drivers/of/property.c
> > +++ b/drivers/of/property.c
> > @@ -1384,7 +1384,7 @@ static struct device_node *parse_interrupts(struct
> > device_node *np,
> >   static const struct supplier_bindings of_supplier_bindings[] = {
> >          { .parse_prop = parse_clocks, },
> >          { .parse_prop = parse_interconnects, },
> > -       { .parse_prop = parse_iommus, .optional = true, },
> > +       { .parse_prop = parse_iommus, },
> >          { .parse_prop = parse_iommu_maps, .optional = true, },
> >          { .parse_prop = parse_mboxes, },
> >          { .parse_prop = parse_io_channels, },
> >
> > Without that a really nasty things happened.

I have a command line option to do this without code changes. Use
fw_devlink.strict=1. That makes all optional properties into mandatory
ones.

I sent out a series[1] that tried to make fw_devlink.strict=1 the
default and then use the timeout behavior (more details) to handle
cases where iommu and dmas (or any other supplier) are optional on a
specific board. The cover letter of [1] should give some more context.

> > Initialization of the built-in drivers and loading modules takes time,
> > so the default 10s deferred probe timeout is not enough to ensure that
> > the built-in driver won't be probed before the Exynos IOMMU driver is
> > loaded.

The 10 second is the minimum delay from the time we hit late_initcall.
If a driver is registered before the 10s expires, then the timer will
be extended by another 10s. This behavior landed sometime around the
end of May 2022. So it should have been in your tree when you tested
this. I'm surprised this isn't sufficient for your case. Is there
really a 10s gap in your boot sequence where no module is being loaded
and then IOMMU modules get loaded later on? I'm kinda surprised by
this. Is it this long because some serial UART is enabled and it's
slowing down boot? Or something else?

I'm not saying your case isn't valid or we shouldn't extend the
timeout. I'm just trying to understand why the current timer behavior
wasn't able to cover your case.

> Yeah, the whole time-based sync looks nasty... I remember coming
> across the slides by Andrzej Hajda called "Deferred Problem" [1], but
> I guess the proposed solution was never applied. Just hope that
> increasing the timeout is upstreamable solution.
>
> [1] https://events19.linuxfoundation.org/wp-content/uploads/2017/12/Deferred-Problem-Issues-With-Complex-Dependencies-Between-Devices-in-Linux-Kernel-Andrzej-Hajda-Samsung.pdf

Sam, I kinda skimmed the slides right now. Looks like it talks about
device links and why they aren't sufficient and makes an alternate
proposal. fw_devlink is a solution that uses device links and I think
addresses a lot of the issues that were raised about device links.
There's still a bunch of TODOs left, but I think the end goal is the
same. I'm hoping to keep chipping away at it. For now, I've tried to
make the timer a bit more smart about detecting when modules are
getting loaded and extending the timer. fw_devlink also enables
something called sync_state() that's invaluable on a fully modular
system (search lore for references to that to get some idea).

The slides talk about a solution that will allow devices to probe with
limited functionality with whatever suppliers are available and then
reprobe as more suppliers are available. I'm not sure how well that'll
work across the board. It's going to be a bit weird if your phone
display goes off and then comes on again because an IOMMU driver got
loaded (and it can now do DRM playback). For now, I'm not going to
focus on that option because there are enough existing issues/TODOs to
work on for fw_devlink.

> > The second change fixes the problem that driver core probes Exynos IOMMU
> > controllers in parallel to probing the master devices, what results in
> > calling exynos_iommu_of_xlate() and exynos_iommu_probe_device() even on
> > the partially initialized IOMMU controllers or initializing the dma_ops
> > under the already probed and working master device. This was easy to
> > observe especially on the master devices with multiple IOMMU
> > controllers. I wasn't able to solve this concurrency/race issues inside
> > the Exynos IOMMU driver.
> >
> > Frankly speaking I don't know what is the rationale for making the
> > 'iommus' property optional, but this simply doesn't work well with IOMMU
> > driver being a module. CCed Saravana and Rob for this.
> >
>
> The patch which makes 'iommus' optional doesn't provide much of
> insight on reasons in commit message either.

This was the commit text:

    Not all DT bindings are mandatory bindings. Add support for optional DT
    bindings and mark iommus, iommu-map, dmas as optional DT bindings.

I thought it was obvious enough, but I guess I could have done better.
Geert convinced me that iommu's aren't always necessary and devices
could work perfectly well without them or dmas. And he has a bunch of
boards like that. So I went with adding optional and then introducing
fw_devlink.strict.

However, at this point in time, I believe none of them should be
marked as optional because technically any property can be optional
depending on what the firmware has set up and what the driver does. We
should figure this out at runtime on a board level -- which is what
[1] is trying to do. Yeah, not very pretty, but there hasn't been a
better solution that's not "have userspace tell us it's done loading
modules" (that's a "kernel depends on userspace to work correctly"
thing that no one likes). I've fixed some of the issues raised in [1]
in a fw_devlink improvement series[2] and I plan on continuing to work
on this until hopefully [1] can land.

> > Without fixing the above issues, I would add a warning that compiling
> > the driver as a module leads to serious issues.
> >
>
> Nice catch! It doesn't reproduce on my platform, alas. Can I expect
> you to submit those patches? If so, I'll probably just wait for those
> to be applied, and then re-send my modularization series on top of it.
> Does that sounds reasonable?

For now, maybe we could add a config to enable fw_devlink.strict=1 by
default and then select it if you make specific iommu drivers into
modules? And then Geert won't set it for his driver, but you can set
it for your driver?

Thanks,
Saravana

[1] - https://lore.kernel.org/lkml/20220601070707.3946847-1-saravanak@google.com/
[2] - https://lore.kernel.org/lkml/20230207014207.1678715-1-saravanak@google.com/
Sam Protsenko Feb. 23, 2023, 4:33 a.m. UTC | #8
On Mon, 6 Feb 2023 at 21:33, Saravana Kannan <saravanak@google.com> wrote:
>
> On Fri, Nov 11, 2022 at 5:30 AM Sam Protsenko
> <semen.protsenko@linaro.org> wrote:
> >
> > On Thu, 10 Nov 2022 at 15:36, Marek Szyprowski <m.szyprowski@samsung.com> wrote:
> >
> > [snip]
>
> Hi Marek and Sam,
>
> I'm replying to both of your comments in this email.
>
> > > I've finally made Exynos IOMMU working as a module on Exynos5433 based
> > > TM2e board. It looks that this will be a bit longer journey that I've
> > > initially thought. I've posted a simple update of the fix for the driver
> > > initialization sequence, but the real problem is in the platform driver
> > > framework and OF helpers.
> > >
> > > Basically to get it working as a module I had to apply the following
> > > changes:
> > >
> > > diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> > > index 3dda62503102..f6921f5fcab6 100644
> > > --- a/drivers/base/dd.c
> > > +++ b/drivers/base/dd.c
> > > @@ -257,7 +257,7 @@ static int deferred_devs_show(struct seq_file *s,
> > > void *data)
> > >   DEFINE_SHOW_ATTRIBUTE(deferred_devs);
> > >
> > >   #ifdef CONFIG_MODULES
> > > -int driver_deferred_probe_timeout = 10;
> > > +int driver_deferred_probe_timeout = 30;
> > >   #else
> > >   int driver_deferred_probe_timeout;
> > >   #endif
> > > diff --git a/drivers/of/property.c b/drivers/of/property.c
> > > index 967f79b59016..e5df6672fee6 100644
> > > --- a/drivers/of/property.c
> > > +++ b/drivers/of/property.c
> > > @@ -1384,7 +1384,7 @@ static struct device_node *parse_interrupts(struct
> > > device_node *np,
> > >   static const struct supplier_bindings of_supplier_bindings[] = {
> > >          { .parse_prop = parse_clocks, },
> > >          { .parse_prop = parse_interconnects, },
> > > -       { .parse_prop = parse_iommus, .optional = true, },
> > > +       { .parse_prop = parse_iommus, },
> > >          { .parse_prop = parse_iommu_maps, .optional = true, },
> > >          { .parse_prop = parse_mboxes, },
> > >          { .parse_prop = parse_io_channels, },
> > >
> > > Without that a really nasty things happened.
>
> I have a command line option to do this without code changes. Use
> fw_devlink.strict=1. That makes all optional properties into mandatory
> ones.
>
> I sent out a series[1] that tried to make fw_devlink.strict=1 the
> default and then use the timeout behavior (more details) to handle
> cases where iommu and dmas (or any other supplier) are optional on a
> specific board. The cover letter of [1] should give some more context.
>
> > > Initialization of the built-in drivers and loading modules takes time,
> > > so the default 10s deferred probe timeout is not enough to ensure that
> > > the built-in driver won't be probed before the Exynos IOMMU driver is
> > > loaded.
>
> The 10 second is the minimum delay from the time we hit late_initcall.
> If a driver is registered before the 10s expires, then the timer will
> be extended by another 10s. This behavior landed sometime around the
> end of May 2022. So it should have been in your tree when you tested
> this. I'm surprised this isn't sufficient for your case. Is there
> really a 10s gap in your boot sequence where no module is being loaded
> and then IOMMU modules get loaded later on? I'm kinda surprised by
> this. Is it this long because some serial UART is enabled and it's
> slowing down boot? Or something else?
>
> I'm not saying your case isn't valid or we shouldn't extend the
> timeout. I'm just trying to understand why the current timer behavior
> wasn't able to cover your case.
>
> > Yeah, the whole time-based sync looks nasty... I remember coming
> > across the slides by Andrzej Hajda called "Deferred Problem" [1], but
> > I guess the proposed solution was never applied. Just hope that
> > increasing the timeout is upstreamable solution.
> >
> > [1] https://events19.linuxfoundation.org/wp-content/uploads/2017/12/Deferred-Problem-Issues-With-Complex-Dependencies-Between-Devices-in-Linux-Kernel-Andrzej-Hajda-Samsung.pdf
>
> Sam, I kinda skimmed the slides right now. Looks like it talks about
> device links and why they aren't sufficient and makes an alternate
> proposal. fw_devlink is a solution that uses device links and I think
> addresses a lot of the issues that were raised about device links.
> There's still a bunch of TODOs left, but I think the end goal is the
> same. I'm hoping to keep chipping away at it. For now, I've tried to
> make the timer a bit more smart about detecting when modules are
> getting loaded and extending the timer. fw_devlink also enables
> something called sync_state() that's invaluable on a fully modular
> system (search lore for references to that to get some idea).
>
> The slides talk about a solution that will allow devices to probe with
> limited functionality with whatever suppliers are available and then
> reprobe as more suppliers are available. I'm not sure how well that'll
> work across the board. It's going to be a bit weird if your phone
> display goes off and then comes on again because an IOMMU driver got
> loaded (and it can now do DRM playback). For now, I'm not going to
> focus on that option because there are enough existing issues/TODOs to
> work on for fw_devlink.
>
> > > The second change fixes the problem that driver core probes Exynos IOMMU
> > > controllers in parallel to probing the master devices, what results in
> > > calling exynos_iommu_of_xlate() and exynos_iommu_probe_device() even on
> > > the partially initialized IOMMU controllers or initializing the dma_ops
> > > under the already probed and working master device. This was easy to
> > > observe especially on the master devices with multiple IOMMU
> > > controllers. I wasn't able to solve this concurrency/race issues inside
> > > the Exynos IOMMU driver.
> > >
> > > Frankly speaking I don't know what is the rationale for making the
> > > 'iommus' property optional, but this simply doesn't work well with IOMMU
> > > driver being a module. CCed Saravana and Rob for this.
> > >
> >
> > The patch which makes 'iommus' optional doesn't provide much of
> > insight on reasons in commit message either.
>
> This was the commit text:
>
>     Not all DT bindings are mandatory bindings. Add support for optional DT
>     bindings and mark iommus, iommu-map, dmas as optional DT bindings.
>
> I thought it was obvious enough, but I guess I could have done better.
> Geert convinced me that iommu's aren't always necessary and devices
> could work perfectly well without them or dmas. And he has a bunch of
> boards like that. So I went with adding optional and then introducing
> fw_devlink.strict.
>
> However, at this point in time, I believe none of them should be
> marked as optional because technically any property can be optional
> depending on what the firmware has set up and what the driver does. We
> should figure this out at runtime on a board level -- which is what
> [1] is trying to do. Yeah, not very pretty, but there hasn't been a
> better solution that's not "have userspace tell us it's done loading
> modules" (that's a "kernel depends on userspace to work correctly"
> thing that no one likes). I've fixed some of the issues raised in [1]
> in a fw_devlink improvement series[2] and I plan on continuing to work
> on this until hopefully [1] can land.
>
> > > Without fixing the above issues, I would add a warning that compiling
> > > the driver as a module leads to serious issues.
> > >
> >
> > Nice catch! It doesn't reproduce on my platform, alas. Can I expect
> > you to submit those patches? If so, I'll probably just wait for those
> > to be applied, and then re-send my modularization series on top of it.
> > Does that sounds reasonable?
>
> For now, maybe we could add a config to enable fw_devlink.strict=1 by
> default and then select it if you make specific iommu drivers into
> modules? And then Geert won't set it for his driver, but you can set
> it for your driver?
>

Marek, what is your take on this? I probably can't provide any useful
comments, as the issue is not even reproducible on my platform. Do you
think some easy solution exists to make modularized IOMMU driver work?
That series has been sitting for a while on ML now, so I wonder if we
can handle it somehow, w.r.t. Saravana's input.

> Thanks,
> Saravana
>
> [1] - https://lore.kernel.org/lkml/20220601070707.3946847-1-saravanak@google.com/
> [2] - https://lore.kernel.org/lkml/20230207014207.1678715-1-saravanak@google.com/
Marek Szyprowski March 13, 2023, 3:51 p.m. UTC | #9
Hi Saravana,

I'm sorry for the delay again, but finally I managed to get back to this 
topic.

On 07.02.2023 04:32, Saravana Kannan wrote:
> On Fri, Nov 11, 2022 at 5:30 AM Sam Protsenko
> <semen.protsenko@linaro.org> wrote:
>> On Thu, 10 Nov 2022 at 15:36, Marek Szyprowski <m.szyprowski@samsung.com> wrote:
>>
>> [snip]
> Hi Marek and Sam,
>
> I'm replying to both of your comments in this email.
>
>>> I've finally made Exynos IOMMU working as a module on Exynos5433 based
>>> TM2e board. It looks that this will be a bit longer journey that I've
>>> initially thought. I've posted a simple update of the fix for the driver
>>> initialization sequence, but the real problem is in the platform driver
>>> framework and OF helpers.
>>>
>>> Basically to get it working as a module I had to apply the following
>>> changes:
>>>
>>> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
>>> index 3dda62503102..f6921f5fcab6 100644
>>> --- a/drivers/base/dd.c
>>> +++ b/drivers/base/dd.c
>>> @@ -257,7 +257,7 @@ static int deferred_devs_show(struct seq_file *s,
>>> void *data)
>>>    DEFINE_SHOW_ATTRIBUTE(deferred_devs);
>>>
>>>    #ifdef CONFIG_MODULES
>>> -int driver_deferred_probe_timeout = 10;
>>> +int driver_deferred_probe_timeout = 30;
>>>    #else
>>>    int driver_deferred_probe_timeout;
>>>    #endif
>>> diff --git a/drivers/of/property.c b/drivers/of/property.c
>>> index 967f79b59016..e5df6672fee6 100644
>>> --- a/drivers/of/property.c
>>> +++ b/drivers/of/property.c
>>> @@ -1384,7 +1384,7 @@ static struct device_node *parse_interrupts(struct
>>> device_node *np,
>>>    static const struct supplier_bindings of_supplier_bindings[] = {
>>>           { .parse_prop = parse_clocks, },
>>>           { .parse_prop = parse_interconnects, },
>>> -       { .parse_prop = parse_iommus, .optional = true, },
>>> +       { .parse_prop = parse_iommus, },
>>>           { .parse_prop = parse_iommu_maps, .optional = true, },
>>>           { .parse_prop = parse_mboxes, },
>>>           { .parse_prop = parse_io_channels, },
>>>
>>> Without that a really nasty things happened.
> I have a command line option to do this without code changes. Use
> fw_devlink.strict=1. That makes all optional properties into mandatory
> ones.
>
> I sent out a series[1] that tried to make fw_devlink.strict=1 the
> default and then use the timeout behavior (more details) to handle
> cases where iommu and dmas (or any other supplier) are optional on a
> specific board. The cover letter of [1] should give some more context.

I'm for removing the optional properties or making them dependent on the 
whole subsystem availability (see my next comments).


>>> Initialization of the built-in drivers and loading modules takes time,
>>> so the default 10s deferred probe timeout is not enough to ensure that
>>> the built-in driver won't be probed before the Exynos IOMMU driver is
>>> loaded.
> The 10 second is the minimum delay from the time we hit late_initcall.
> If a driver is registered before the 10s expires, then the timer will
> be extended by another 10s. This behavior landed sometime around the
> end of May 2022. So it should have been in your tree when you tested
> this. I'm surprised this isn't sufficient for your case. Is there
> really a 10s gap in your boot sequence where no module is being loaded
> and then IOMMU modules get loaded later on? I'm kinda surprised by
> this. Is it this long because some serial UART is enabled and it's
> slowing down boot? Or something else?
>
> I'm not saying your case isn't valid or we shouldn't extend the
> timeout. I'm just trying to understand why the current timer behavior
> wasn't able to cover your case.


Well, I have almost all possible CONFIG_DEBUG_* options enabled in 
exynos_defconfig, as well as kernel messages routed to UART console, so 
all my test system are really slow, but I do this intentionally. I 
suspect that over half of the issues I've reported were found because of 
such 'unusual' config. Loading modules also takes time in such case, so 
maybe the timeout should be extended also by each loaded module?


>> Yeah, the whole time-based sync looks nasty... I remember coming
>> across the slides by Andrzej Hajda called "Deferred Problem" [1], but
>> I guess the proposed solution was never applied. Just hope that
>> increasing the timeout is upstreamable solution.
>>
>> [1] https://events19.linuxfoundation.org/wp-content/uploads/2017/12/Deferred-Problem-Issues-With-Complex-Dependencies-Between-Devices-in-Linux-Kernel-Andrzej-Hajda-Samsung.pdf
> Sam, I kinda skimmed the slides right now. Looks like it talks about
> device links and why they aren't sufficient and makes an alternate
> proposal. fw_devlink is a solution that uses device links and I think
> addresses a lot of the issues that were raised about device links.
> There's still a bunch of TODOs left, but I think the end goal is the
> same. I'm hoping to keep chipping away at it. For now, I've tried to
> make the timer a bit more smart about detecting when modules are
> getting loaded and extending the timer. fw_devlink also enables
> something called sync_state() that's invaluable on a fully modular
> system (search lore for references to that to get some idea).
>
> The slides talk about a solution that will allow devices to probe with
> limited functionality with whatever suppliers are available and then
> reprobe as more suppliers are available. I'm not sure how well that'll
> work across the board. It's going to be a bit weird if your phone
> display goes off and then comes on again because an IOMMU driver got
> loaded (and it can now do DRM playback). For now, I'm not going to
> focus on that option because there are enough existing issues/TODOs to
> work on for fw_devlink.
>
>>> The second change fixes the problem that driver core probes Exynos IOMMU
>>> controllers in parallel to probing the master devices, what results in
>>> calling exynos_iommu_of_xlate() and exynos_iommu_probe_device() even on
>>> the partially initialized IOMMU controllers or initializing the dma_ops
>>> under the already probed and working master device. This was easy to
>>> observe especially on the master devices with multiple IOMMU
>>> controllers. I wasn't able to solve this concurrency/race issues inside
>>> the Exynos IOMMU driver.
>>>
>>> Frankly speaking I don't know what is the rationale for making the
>>> 'iommus' property optional, but this simply doesn't work well with IOMMU
>>> driver being a module. CCed Saravana and Rob for this.
>>>
>> The patch which makes 'iommus' optional doesn't provide much of
>> insight on reasons in commit message either.
> This was the commit text:
>
>      Not all DT bindings are mandatory bindings. Add support for optional DT
>      bindings and mark iommus, iommu-map, dmas as optional DT bindings.
>
> I thought it was obvious enough, but I guess I could have done better.
> Geert convinced me that iommu's aren't always necessary and devices
> could work perfectly well without them or dmas. And he has a bunch of
> boards like that. So I went with adding optional and then introducing
> fw_devlink.strict.

Well, that depends heavily on the kernel config and the intentions 
behind it. Indeed one might have IOMMU driver not selected in the config 
(for various reasons, like forcing drivers to use contiguous memory 
buffers for max performance) and system should boot fine. That time one 
didn't consider the IOMMUs being compiled as modules. The main problem 
is how to distinguish between the first case (intentionally no IOMMU 
driver or even no IOMMU support) and the modularized case (driver not 
yet loaded). I assume that one can skip waiting for IOMMUs only if the 
whole IOMMU subsystem is not compiled in, otherwise the IOMMU driver can 
still come in and it cannot properly start operating if client device 
drivers already started their operation.

DMAs, although similar a bit, are imho a bit different. Drivers usually 
have some kind of fallback if DMA is not yet available, but usually 
drivers can switch their operation on-fly between PIO and DMA (and some 
even do that intentionally and use PIO for very small transfers).

> However, at this point in time, I believe none of them should be
> marked as optional because technically any property can be optional
> depending on what the firmware has set up and what the driver does. We
> should figure this out at runtime on a board level -- which is what
> [1] is trying to do. Yeah, not very pretty, but there hasn't been a
> better solution that's not "have userspace tell us it's done loading
> modules" (that's a "kernel depends on userspace to work correctly"
> thing that no one likes). I've fixed some of the issues raised in [1]
> in a fw_devlink improvement series[2] and I plan on continuing to work
> on this until hopefully [1] can land.
>
>>> Without fixing the above issues, I would add a warning that compiling
>>> the driver as a module leads to serious issues.
>>>
>> Nice catch! It doesn't reproduce on my platform, alas. Can I expect
>> you to submit those patches? If so, I'll probably just wait for those
>> to be applied, and then re-send my modularization series on top of it.
>> Does that sounds reasonable?
> For now, maybe we could add a config to enable fw_devlink.strict=1 by
> default and then select it if you make specific iommu drivers into
> modules? And then Geert won't set it for his driver, but you can set
> it for your driver?

I think the best we can do now is to either add a kconfig option to 
force strict fw_devlinks (so Exynos IOMMU can be selected as a module 
only if that one is selected) or do a runtime check during the Exynos 
IOMMU initalization - and fail with appropriate error message if 
fw_devlink.strict!=1. What do you think?


Best regards