public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures
@ 2025-09-04 14:13 Leo Yan
  2025-09-04 14:46 ` James Clark
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Leo Yan @ 2025-09-04 14:13 UTC (permalink / raw)
  To: Suzuki K Poulose, Mike Leach, James Clark, Anshuman Khandual,
	Alexander Shishkin, Greg Kroah-Hartman
  Cc: coresight, linux-arm-kernel, linux-kernel, Tamas Zsoldos, Leo Yan

When the TRBE driver fails to allocate a buffer, it currently returns
the error code "-ENOMEM". However, the caller etm_setup_aux() only
checks for a NULL pointer, so it misses the error. As a result, the
driver continues and eventually causes a kernel panic.

Fix this by returning a NULL pointer from arm_trbe_alloc_buffer() on
allocation failures. This allows that the callers can properly handle
the failure.

Fixes: 3fbf7f011f24 ("coresight: sink: Add TRBE driver")
Reported-by: Tamas Zsoldos <tamas.zsoldos@arm.com>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
Changes in v2:
- Fix TRBE driver instead of changing coresight-etm-perf.c.
- Link to v1: https://lore.kernel.org/r/20250904-cs_etm_auxsetup_fix_error_handling-v1-1-ecc5edf282a5@arm.com
---
 drivers/hwtracing/coresight/coresight-trbe.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-trbe.c b/drivers/hwtracing/coresight/coresight-trbe.c
index 10f3fb401edf6a00b24b38cdaa7c2865e7a191ac..8f9bbef71f236b327d35a288689df9b0dd8ff3f4 100644
--- a/drivers/hwtracing/coresight/coresight-trbe.c
+++ b/drivers/hwtracing/coresight/coresight-trbe.c
@@ -748,12 +748,12 @@ static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
 
 	buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
 	if (!buf)
-		return ERR_PTR(-ENOMEM);
+		return NULL;
 
 	pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
 	if (!pglist) {
 		kfree(buf);
-		return ERR_PTR(-ENOMEM);
+		return NULL;
 	}
 
 	for (i = 0; i < nr_pages; i++)
@@ -763,7 +763,7 @@ static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
 	if (!buf->trbe_base) {
 		kfree(pglist);
 		kfree(buf);
-		return ERR_PTR(-ENOMEM);
+		return NULL;
 	}
 	buf->trbe_limit = buf->trbe_base + nr_pages * PAGE_SIZE;
 	buf->trbe_write = buf->trbe_base;

---
base-commit: fa71e9cb4cfa59abb196229667ec84929bdc18fe
change-id: 20250904-cs_etm_auxsetup_fix_error_handling-cb7e07ed9adf

Best regards,
-- 
Leo Yan <leo.yan@arm.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures
  2025-09-04 14:13 [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures Leo Yan
@ 2025-09-04 14:46 ` James Clark
  2025-09-04 16:10 ` Markus Elfring
  2025-09-05 12:26 ` Suzuki K Poulose
  2 siblings, 0 replies; 4+ messages in thread
From: James Clark @ 2025-09-04 14:46 UTC (permalink / raw)
  To: Leo Yan, Suzuki K Poulose
  Cc: coresight, linux-arm-kernel, linux-kernel, Tamas Zsoldos,
	Mike Leach, Anshuman Khandual, Alexander Shishkin,
	Greg Kroah-Hartman



On 04/09/2025 3:13 pm, Leo Yan wrote:
> When the TRBE driver fails to allocate a buffer, it currently returns
> the error code "-ENOMEM". However, the caller etm_setup_aux() only
> checks for a NULL pointer, so it misses the error. As a result, the
> driver continues and eventually causes a kernel panic.
> 
> Fix this by returning a NULL pointer from arm_trbe_alloc_buffer() on
> allocation failures. This allows that the callers can properly handle
> the failure.
> 
> Fixes: 3fbf7f011f24 ("coresight: sink: Add TRBE driver")
> Reported-by: Tamas Zsoldos <tamas.zsoldos@arm.com>
> Signed-off-by: Leo Yan <leo.yan@arm.com>
> ---
> Changes in v2:
> - Fix TRBE driver instead of changing coresight-etm-perf.c.
> - Link to v1: https://lore.kernel.org/r/20250904-cs_etm_auxsetup_fix_error_handling-v1-1-ecc5edf282a5@arm.com
> ---
>   drivers/hwtracing/coresight/coresight-trbe.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/hwtracing/coresight/coresight-trbe.c b/drivers/hwtracing/coresight/coresight-trbe.c
> index 10f3fb401edf6a00b24b38cdaa7c2865e7a191ac..8f9bbef71f236b327d35a288689df9b0dd8ff3f4 100644
> --- a/drivers/hwtracing/coresight/coresight-trbe.c
> +++ b/drivers/hwtracing/coresight/coresight-trbe.c
> @@ -748,12 +748,12 @@ static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
>   
>   	buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, trbe_alloc_node(event));
>   	if (!buf)
> -		return ERR_PTR(-ENOMEM);
> +		return NULL;
>   
>   	pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
>   	if (!pglist) {
>   		kfree(buf);
> -		return ERR_PTR(-ENOMEM);
> +		return NULL;
>   	}
>   
>   	for (i = 0; i < nr_pages; i++)
> @@ -763,7 +763,7 @@ static void *arm_trbe_alloc_buffer(struct coresight_device *csdev,
>   	if (!buf->trbe_base) {
>   		kfree(pglist);
>   		kfree(buf);
> -		return ERR_PTR(-ENOMEM);
> +		return NULL;
>   	}
>   	buf->trbe_limit = buf->trbe_base + nr_pages * PAGE_SIZE;
>   	buf->trbe_write = buf->trbe_base;
> 
> ---
> base-commit: fa71e9cb4cfa59abb196229667ec84929bdc18fe
> change-id: 20250904-cs_etm_auxsetup_fix_error_handling-cb7e07ed9adf
> 
> Best regards,

Reviewed-by: James Clark <james.clark@linaro.org>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures
  2025-09-04 14:13 [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures Leo Yan
  2025-09-04 14:46 ` James Clark
@ 2025-09-04 16:10 ` Markus Elfring
  2025-09-05 12:26 ` Suzuki K Poulose
  2 siblings, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2025-09-04 16:10 UTC (permalink / raw)
  To: Leo Yan, coresight, linux-arm-kernel
  Cc: LKML, Alexander Shishkin, Anshuman Khandual, Greg Kroah-Hartman,
	James Clark, Mike Leach, Suzuki Poulouse, Tamas Zsoldos

…
> Fix this by returning a NULL pointer from arm_trbe_alloc_buffer() on
> allocation failures. This allows that the callers can properly handle
> the failure.

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc4#n94


By the way:
* Would you like to improve the exception handling by using a goto chain?

* How do you think about to increase the application of scope-based resource management?
  https://elixir.bootlin.com/linux/v6.17-rc4/source/include/linux/slab.h#L476


Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures
  2025-09-04 14:13 [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures Leo Yan
  2025-09-04 14:46 ` James Clark
  2025-09-04 16:10 ` Markus Elfring
@ 2025-09-05 12:26 ` Suzuki K Poulose
  2 siblings, 0 replies; 4+ messages in thread
From: Suzuki K Poulose @ 2025-09-05 12:26 UTC (permalink / raw)
  To: Mike Leach, James Clark, Anshuman Khandual, Alexander Shishkin,
	Greg Kroah-Hartman, Leo Yan
  Cc: Suzuki K Poulose, coresight, linux-arm-kernel, linux-kernel,
	Tamas Zsoldos


On Thu, 04 Sep 2025 15:13:52 +0100, Leo Yan wrote:
> When the TRBE driver fails to allocate a buffer, it currently returns
> the error code "-ENOMEM". However, the caller etm_setup_aux() only
> checks for a NULL pointer, so it misses the error. As a result, the
> driver continues and eventually causes a kernel panic.
> 
> Fix this by returning a NULL pointer from arm_trbe_alloc_buffer() on
> allocation failures. This allows that the callers can properly handle
> the failure.
> 
> [...]

Applied, thanks!

[1/1] coresight: trbe: Return NULL pointer for allocation failures
      https://git.kernel.org/coresight/c/811e07b1812a

Best regards,
-- 
Suzuki K Poulose <suzuki.poulose@arm.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-09-05 12:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-04 14:13 [PATCH v2] coresight: trbe: Return NULL pointer for allocation failures Leo Yan
2025-09-04 14:46 ` James Clark
2025-09-04 16:10 ` Markus Elfring
2025-09-05 12:26 ` Suzuki K Poulose

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox