* [PATCH] spi: Add spi_for_each_controller() helper
@ 2026-05-14 8:45 ChaoHuang
2026-05-16 6:48 ` Mark Brown
0 siblings, 1 reply; 5+ messages in thread
From: ChaoHuang @ 2026-05-14 8:45 UTC (permalink / raw)
To: broonie; +Cc: linux-spi, linux-kernel, Chao Huang
From: Chao Huang <huangchao@kylinos.cn>
Add a new helper function to iterate over all SPI controllers.
This provides a convenient way for SPI core code and drivers
to perform operations on all registered controllers.
The implementation follows existing SPI subsystem patterns:
- Uses spi_controller_list with board_lock protection
- Directly operates on spi_controller objects
- Breaks iteration on callback error
- Follows kernel naming conventions (spi_for_each_*)
This helper is useful for scenarios like statistics gathering,
configuration updates, or debugging operations that need to
process all SPI controllers in the system.
Signed-off-by: Chao Huang <huangchao@kylinos.cn>
---
drivers/spi/spi.c | 17 +++++++++++++++++
include/linux/spi/spi.h | 2 ++
2 files changed, 19 insertions(+)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 104279858f56..7cd07b390a4b 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -5116,6 +5116,23 @@ static struct notifier_block spi_acpi_notifier = {
extern struct notifier_block spi_acpi_notifier;
#endif
+int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data)
+{
+ struct spi_controller *ctlr;
+ int ret = 0;
+
+ mutex_lock(&board_lock);
+ list_for_each_entry(ctlr, &spi_controller_list, list) {
+ ret = fn(ctlr, data);
+ if (ret)
+ break;
+ }
+ mutex_unlock(&board_lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(spi_for_each_controller);
+
static int __init spi_init(void)
{
int status;
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 79513f5941cc..8fcf3faf9739 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -934,6 +934,8 @@ static inline int acpi_spi_count_resources(struct acpi_device *adev)
}
#endif
+int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data);
+
/*
* SPI resource management while processing a SPI message
*/
--
2.25.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] spi: Add spi_for_each_controller() helper
2026-05-14 8:45 [PATCH] spi: Add spi_for_each_controller() helper ChaoHuang
@ 2026-05-16 6:48 ` Mark Brown
[not found] ` <tencent_74D72238CFEAA838A967727CA08D92C23005@qq.com>
0 siblings, 1 reply; 5+ messages in thread
From: Mark Brown @ 2026-05-16 6:48 UTC (permalink / raw)
To: ChaoHuang; +Cc: linux-spi, linux-kernel, Chao Huang
[-- Attachment #1: Type: text/plain, Size: 1948 bytes --]
On Thu, May 14, 2026 at 04:45:44PM +0800, ChaoHuang wrote:
> From: Chao Huang <huangchao@kylinos.cn>
>
> Add a new helper function to iterate over all SPI controllers.
> This provides a convenient way for SPI core code and drivers
> to perform operations on all registered controllers.
> This helper is useful for scenarios like statistics gathering,
> configuration updates, or debugging operations that need to
> process all SPI controllers in the system.
Do you have an actual user for this or is this just purely theoretical
at this point? It'd make a lot more sense to add this along with a
user.
> +int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data)
> +{
> + struct spi_controller *ctlr;
> + int ret = 0;
> +
> + mutex_lock(&board_lock);
> + list_for_each_entry(ctlr, &spi_controller_list, list) {
> + ret = fn(ctlr, data);
When unregistering controllers we do start the teardown process before
we pull the device off the controller list, and drop the lock while
doing so. That's probably fine for a lot of uses.
There's also some obvious potential for things to go wrong here with
something in the callback deadlocking, though that's a bit of a "don't
do that kind of thing".
> + if (ret)
> + break;
> + }
> + mutex_unlock(&board_lock);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(spi_for_each_controller);
> +
> static int __init spi_init(void)
> {
> int status;
> diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
> index 79513f5941cc..8fcf3faf9739 100644
> --- a/include/linux/spi/spi.h
> +++ b/include/linux/spi/spi.h
> @@ -934,6 +934,8 @@ static inline int acpi_spi_count_resources(struct acpi_device *adev)
> }
> #endif
>
> +int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data);
> +
> /*
> * SPI resource management while processing a SPI message
> */
> --
> 2.25.1
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] spi: Add spi_for_each_controller() helper
[not found] ` <tencent_74D72238CFEAA838A967727CA08D92C23005@qq.com>
@ 2026-05-18 8:22 ` Mark Brown
0 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2026-05-18 8:22 UTC (permalink / raw)
To: Chao Huang; +Cc: linux-spi, linux-kernel, Chao Huang
[-- Attachment #1: Type: text/plain, Size: 1547 bytes --]
On Mon, May 18, 2026 at 03:10:26PM +0800, Chao Huang wrote:
> > Do you have an actual user for this or is this just purely theoretical
> > at this point? It'd make a lot more sense to add this along with a
> > user.
> Yes, I do have an actual user for this helper in my project.
> The goal is to provide a new SPI controller API that allows users to
> access SPI controllers by bus number and chip select (CS). The
> `spi_for_each_controller()` helper is used to iterate over all registered
> controllers, find the target controller by bus number, and then operate
> on it.
OK, I would hold off on this patch and sumbit it along with the user -
it's not really something that's clearly useful without that user.
> > When unregistering controllers we do start the teardown process before
> > we pull the device off the controller list, and drop the lock while
> > doing so. That's probably fine for a lot of uses.
> >
> Regarding the controller unregistration case you mentioned — do you mean
> the `spi_unregister_controller()` function?
Yes.
> In my `fn` callback, I call `spi_controller_get()` to safely grab a
> reference to the controller, and later call `spi_controller_put()` at an
> appropriate time (e.g., after the operation is done). This should
> prevent the controller from being freed while it is still in use, even
> if unregistration happens concurrently.
The controller itself will be there but we do things like stopping the
queue before we destroy the controller.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] spi: Add spi_for_each_controller() helper
2026-05-14 6:13 958028483
@ 2026-05-14 7:08 ` Mark Brown
0 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2026-05-14 7:08 UTC (permalink / raw)
To: 958028483; +Cc: linux-spi, linux-kernel, huangchao
[-- Attachment #1: Type: text/plain, Size: 372 bytes --]
On Thu, May 14, 2026 at 02:13:08PM +0800, 958028483@qq.com wrote:
> From: huangchao <huangchao@kylinos.cn>
> Signed-off-by: huangchao <huangchao@kylinos.cn>
Your email address and (non-)name don't match your signoff at all so I
can't really verify that you're the same person, could you please bring
the two in line somehow so I can verify that there's a valid signoff?
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH] spi: Add spi_for_each_controller() helper
@ 2026-05-14 6:13 958028483
2026-05-14 7:08 ` Mark Brown
0 siblings, 1 reply; 5+ messages in thread
From: 958028483 @ 2026-05-14 6:13 UTC (permalink / raw)
To: broonie; +Cc: linux-spi, linux-kernel, huangchao
From: huangchao <huangchao@kylinos.cn>
Add a new helper function to iterate over all SPI controllers.
This provides a convenient way for SPI core code and drivers
to perform operations on all registered controllers.
The implementation follows existing SPI subsystem patterns:
- Uses spi_controller_list with board_lock protection
- Directly operates on spi_controller objects
- Breaks iteration on callback error
- Follows kernel naming conventions (spi_for_each_*)
This helper is useful for scenarios like statistics gathering,
configuration updates, or debugging operations that need to
process all SPI controllers in the system.
Signed-off-by: huangchao <huangchao@kylinos.cn>
---
drivers/spi/spi.c | 17 +++++++++++++++++
include/linux/spi/spi.h | 2 ++
2 files changed, 19 insertions(+)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 104279858f56..7cd07b390a4b 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -5116,6 +5116,23 @@ static struct notifier_block spi_acpi_notifier = {
extern struct notifier_block spi_acpi_notifier;
#endif
+int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data)
+{
+ struct spi_controller *ctlr;
+ int ret = 0;
+
+ mutex_lock(&board_lock);
+ list_for_each_entry(ctlr, &spi_controller_list, list) {
+ ret = fn(ctlr, data);
+ if (ret)
+ break;
+ }
+ mutex_unlock(&board_lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(spi_for_each_controller);
+
static int __init spi_init(void)
{
int status;
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 79513f5941cc..8fcf3faf9739 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -934,6 +934,8 @@ static inline int acpi_spi_count_resources(struct acpi_device *adev)
}
#endif
+int spi_for_each_controller(int (*fn)(struct spi_controller *, void *), void *data);
+
/*
* SPI resource management while processing a SPI message
*/
--
2.25.1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-05-18 8:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-14 8:45 [PATCH] spi: Add spi_for_each_controller() helper ChaoHuang
2026-05-16 6:48 ` Mark Brown
[not found] ` <tencent_74D72238CFEAA838A967727CA08D92C23005@qq.com>
2026-05-18 8:22 ` Mark Brown
-- strict thread matches above, loose matches on Subject: below --
2026-05-14 6:13 958028483
2026-05-14 7:08 ` Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®