* [PATCH 0/2] SCMI protocols fixes and refactor @ 2026-11-24 18:51 Cristian Marussi 2026-11-24 18:51 ` [PATCH 1/2] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi 2026-11-24 18:51 ` [PATCH 2/2] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi 0 siblings, 2 replies; 5+ messages in thread From: Cristian Marussi @ 2026-11-24 18:51 UTC (permalink / raw) To: linux-kernel, linux-arm-kernel, arm-scmi Cc: sudeep.holla, philip.radford, Cristian Marussi Hi, A tiny series addressing a minor protocol initialization issue reported by Sashiko (patch 1/2) and a general tiny following refactor around that same protocol initialization/fnalization code. Note that the refactoring moves around the scmi_protocol_put() calls on the failure path of initialization AND on the protocol release code flow: this basicall causes the module_put() where needed to be called after the protocol devres group release, which is how it should have happened really all the time to avoid races at unload time. IOW the logic is a bit changed BUT there should NOT be any functional change...at least intended :P Moreover this standalone tiny series will be also reposted within the larger Telemetry series to avoid conflicts and ease merging. Thanks, Cristian Cristian Marussi (2): firmware: arm_scmi: Invoke proper protocol cleanup on failure firmware: arm_scmi: Refactor protocol allocation drivers/firmware/arm_scmi/driver.c | 58 ++++++++++++++---------------- 1 file changed, 26 insertions(+), 32 deletions(-) -- 2.54.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] firmware: arm_scmi: Invoke proper protocol cleanup on failure 2026-11-24 18:51 [PATCH 0/2] SCMI protocols fixes and refactor Cristian Marussi @ 2026-11-24 18:51 ` Cristian Marussi 2026-11-24 22:43 ` Jonathan Cameron 2026-11-24 18:51 ` [PATCH 2/2] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi 1 sibling, 1 reply; 5+ messages in thread From: Cristian Marussi @ 2026-11-24 18:51 UTC (permalink / raw) To: linux-kernel, linux-arm-kernel, arm-scmi Cc: sudeep.holla, philip.radford, Cristian Marussi, Sashiko After a failure in the protocol initialization phase, invoke if provided the protocol deinitialization routines. Fixes: 48dc16e2e52c ("firmware: arm_scmi: Extend protocol registration interfaces") Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/#/bug/linux-12cc0795-af8e-4d0c-8f6a-780a6728de7a Signed-off-by: Cristian Marussi <cristian.marussi@arm.com> --- drivers/firmware/arm_scmi/driver.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c index 05092301db29..2c4424aa0d37 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -2303,7 +2303,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1, GFP_KERNEL); if (ret != proto->id) - goto clean; + goto deinit; /* * Warn but ignore events registration errors since we do not want @@ -2324,6 +2324,9 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, return pi; +deinit: + if (pi->proto->instance_deinit) + pi->proto->instance_deinit(&pi->ph); clean: /* Take care to put the protocol module's owner before releasing all */ scmi_protocol_put(proto); -- 2.54.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] firmware: arm_scmi: Invoke proper protocol cleanup on failure 2026-11-24 18:51 ` [PATCH 1/2] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi @ 2026-11-24 22:43 ` Jonathan Cameron 0 siblings, 0 replies; 5+ messages in thread From: Jonathan Cameron @ 2026-11-24 22:43 UTC (permalink / raw) To: Cristian Marussi Cc: linux-kernel, linux-arm-kernel, arm-scmi, sudeep.holla, philip.radford, Sashiko On Tue, 24 Nov 2026 18:51:10 +0000 Cristian Marussi <cristian.marussi@arm.com> wrote: > After a failure in the protocol initialization phase, invoke if provided > the protocol deinitialization routines. > > Fixes: 48dc16e2e52c ("firmware: arm_scmi: Extend protocol registration interfaces") > Reported-by: Sashiko <sashiko-bot@kernel.org> > Closes: https://sashiko.dev/#/bug/linux-12cc0795-af8e-4d0c-8f6a-780a6728de7a > Signed-off-by: Cristian Marussi <cristian.marussi@arm.com> This function is 'fun' for it's mixing of devres, reference counting and manual cleanup. Plus why is it responsible for the scmi_protocol_put(proto) when the reference is grabbed in the caller? There is no reason I can see we can't hold that reference past the calls that follow which release a devres group but don't hold anything specific the protocol module. Anyhow, far as I can tell the fix is right but if anyone has the time I'd take a look at whether this can be simplified. So with that slightly doubtful set of comments in mind. Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com> > --- > drivers/firmware/arm_scmi/driver.c | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c > index 05092301db29..2c4424aa0d37 100644 > --- a/drivers/firmware/arm_scmi/driver.c > +++ b/drivers/firmware/arm_scmi/driver.c > @@ -2303,7 +2303,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, > ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1, > GFP_KERNEL); > if (ret != proto->id) > - goto clean; > + goto deinit; > > /* > * Warn but ignore events registration errors since we do not want > @@ -2324,6 +2324,9 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, > > return pi; > > +deinit: > + if (pi->proto->instance_deinit) > + pi->proto->instance_deinit(&pi->ph); > clean: > /* Take care to put the protocol module's owner before releasing all */ > scmi_protocol_put(proto); ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] firmware: arm_scmi: Refactor protocol allocation 2026-11-24 18:51 [PATCH 0/2] SCMI protocols fixes and refactor Cristian Marussi 2026-11-24 18:51 ` [PATCH 1/2] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi @ 2026-11-24 18:51 ` Cristian Marussi 2026-11-24 22:50 ` Jonathan Cameron 1 sibling, 1 reply; 5+ messages in thread From: Cristian Marussi @ 2026-11-24 18:51 UTC (permalink / raw) To: linux-kernel, linux-arm-kernel, arm-scmi Cc: sudeep.holla, philip.radford, Cristian Marussi Simplify protocol initialization and finalization using cleanup handlers. Relocate the protocol put operations out of the setup and cleanup code so as to be executed strictly after the protocol devres groups have been fully released. Signed-off-by: Cristian Marussi <cristian.marussi@arm.com> --- drivers/firmware/arm_scmi/driver.c | 53 +++++++++++++----------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c index 2c4424aa0d37..199f0a189432 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -2256,24 +2256,21 @@ static void scmi_protocol_version_initialize(struct device *dev, * * Context: Assumes to be called with @protocols_mtx already acquired. * Return: A reference to a freshly allocated and initialized protocol instance - * or ERR_PTR on failure. On failure the @proto reference is at first - * put using @scmi_protocol_put() before releasing all the devres group. + * or ERR_PTR on failure. */ static struct scmi_protocol_instance * scmi_alloc_init_protocol_instance(struct scmi_info *info, const struct scmi_protocol *proto) { + const struct scmi_handle *handle = &info->handle; + struct scmi_protocol_instance *pi; int ret = -ENOMEM; void *gid; - struct scmi_protocol_instance *pi; - const struct scmi_handle *handle = &info->handle; /* Protocol specific devres group */ gid = devres_open_group(handle->dev, NULL, GFP_KERNEL); - if (!gid) { - scmi_protocol_put(proto); - goto out; - } + if (!gid) + return ERR_PTR(ret); pi = devm_kzalloc_obj(handle->dev, *pi); if (!pi) @@ -2328,10 +2325,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, if (pi->proto->instance_deinit) pi->proto->instance_deinit(&pi->ph); clean: - /* Take care to put the protocol module's owner before releasing all */ - scmi_protocol_put(proto); devres_release_group(handle->dev, gid); -out: return ERR_PTR(ret); } @@ -2351,25 +2345,25 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, static struct scmi_protocol_instance * __must_check scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id) { - struct scmi_protocol_instance *pi; struct scmi_info *info = handle_to_scmi_info(handle); + struct scmi_protocol_instance *pi; + const struct scmi_protocol *proto; - mutex_lock(&info->protocols_mtx); + guard(mutex)(&info->protocols_mtx); pi = idr_find(&info->protocols, protocol_id); - if (pi) { refcount_inc(&pi->users); - } else { - const struct scmi_protocol *proto; - - /* Fails if protocol not registered on bus */ - proto = scmi_protocol_get(protocol_id, &info->version); - if (proto) - pi = scmi_alloc_init_protocol_instance(info, proto); - else - pi = ERR_PTR(-EPROBE_DEFER); + return pi; } - mutex_unlock(&info->protocols_mtx); + + /* Fails if protocol not registered on bus */ + proto = scmi_protocol_get(protocol_id, &info->version); + if (!proto) + return ERR_PTR(-EPROBE_DEFER); + + pi = scmi_alloc_init_protocol_instance(info, proto); + if (IS_ERR(pi)) + scmi_protocol_put(proto); return pi; } @@ -2402,12 +2396,13 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id) struct scmi_info *info = handle_to_scmi_info(handle); struct scmi_protocol_instance *pi; - mutex_lock(&info->protocols_mtx); + guard(mutex)(&info->protocols_mtx); pi = idr_find(&info->protocols, protocol_id); if (WARN_ON(!pi)) - goto out; + return; if (refcount_dec_and_test(&pi->users)) { + const struct scmi_protocol *proto = pi->proto; void *gid = pi->gid; if (pi->proto->events) @@ -2418,15 +2413,11 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id) idr_remove(&info->protocols, protocol_id); - scmi_protocol_put(pi->proto); - devres_release_group(handle->dev, gid); + scmi_protocol_put(proto); dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", protocol_id); } - -out: - mutex_unlock(&info->protocols_mtx); } void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph, -- 2.54.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] firmware: arm_scmi: Refactor protocol allocation 2026-11-24 18:51 ` [PATCH 2/2] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi @ 2026-11-24 22:50 ` Jonathan Cameron 0 siblings, 0 replies; 5+ messages in thread From: Jonathan Cameron @ 2026-11-24 22:50 UTC (permalink / raw) To: Cristian Marussi Cc: linux-kernel, linux-arm-kernel, arm-scmi, sudeep.holla, philip.radford On Tue, 24 Nov 2026 18:51:11 +0000 Cristian Marussi <cristian.marussi@arm.com> wrote: > Simplify protocol initialization and finalization using cleanup handlers. > Relocate the protocol put operations out of the setup and cleanup code so > as to be executed strictly after the protocol devres groups have been > fully released. > > Signed-off-by: Cristian Marussi <cristian.marussi@arm.com> Lol. I should have read on! Nice that you have found the time to look at this. > --- > drivers/firmware/arm_scmi/driver.c | 53 +++++++++++++----------------- > 1 file changed, 22 insertions(+), 31 deletions(-) > > diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c > index 2c4424aa0d37..199f0a189432 100644 > --- a/drivers/firmware/arm_scmi/driver.c > +++ b/drivers/firmware/arm_scmi/driver.c > @@ -2256,24 +2256,21 @@ static void scmi_protocol_version_initialize(struct device *dev, > * > * Context: Assumes to be called with @protocols_mtx already acquired. > * Return: A reference to a freshly allocated and initialized protocol instance > - * or ERR_PTR on failure. On failure the @proto reference is at first > - * put using @scmi_protocol_put() before releasing all the devres group. > + * or ERR_PTR on failure. > */ > static struct scmi_protocol_instance * > scmi_alloc_init_protocol_instance(struct scmi_info *info, > const struct scmi_protocol *proto) > { > + const struct scmi_handle *handle = &info->handle; > + struct scmi_protocol_instance *pi; > int ret = -ENOMEM; > void *gid; > - struct scmi_protocol_instance *pi; > - const struct scmi_handle *handle = &info->handle; > > /* Protocol specific devres group */ > gid = devres_open_group(handle->dev, NULL, GFP_KERNEL); > - if (!gid) { > - scmi_protocol_put(proto); > - goto out; > - } > + if (!gid) > + return ERR_PTR(ret); > > pi = devm_kzalloc_obj(handle->dev, *pi); > if (!pi) > @@ -2328,10 +2325,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info, > if (pi->proto->instance_deinit) > pi->proto->instance_deinit(&pi->ph); > clean: > - /* Take care to put the protocol module's owner before releasing all */ > - scmi_protocol_put(proto); > devres_release_group(handle->dev, gid); > -out: > return ERR_PTR(ret); > } Better though the devres group approach works best in my view when it's a wrapper around a helper. gid = dev_res_open_group(handle->dev, NULL, GFP_KERNEL); pi = __scmi_alloc_init_protocol_instance(); if (IS_ERR(pi)) { devres_release_group(handle->dev, gid); return pi; } devres_close_group(handlde->dev, gid); return pi; Though here you'd have more or less a wrapper surrounding a wrapper, so fine to just push this up into scmi_alloc_init_protocol_instance() Anyhow even if you leave like you have it this is a great improvement. Lifetimes in these calls are now a little less complex to reason about. Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com> ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 22:50 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-24 18:51 [PATCH 0/2] SCMI protocols fixes and refactor Cristian Marussi 2026-11-24 18:51 ` [PATCH 1/2] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi 2026-11-24 22:43 ` Jonathan Cameron 2026-11-24 18:51 ` [PATCH 2/2] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi 2026-11-24 22:50 ` Jonathan Cameron
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox