Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] sound: Add a generic result header
@ 2026-12-04 11:41 Sreeraj S Kurup
  2026-12-04 11:48 ` sashiko-bot
  2026-12-04 16:45 ` Takashi Iwai
  0 siblings, 2 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-12-04 11:41 UTC (permalink / raw)
  To: perex, tiwai; +Cc: linux-kernel, linux-sound, Sreeraj S Kurup

Add include/sound/snd_result.h to provide a common result
structure for sound APIs.

Some sound APIs return unsigned int values but also use
negative error codes to report failures. Since the return
type is unsigned int, negative error codes are converted
to unsigned values. This makes it hard to tell whether
the returned value is an error or a valid result.

For example, returning -1 from an unsigned int function
produces 0xffffffff, which could also be a valid value
returned by the function. This makes it difficult to
handle errors when the full range of unsigned values
can be used.

The new result structure uses a type field to identify
what kind of value is stored. It supports error codes,
signed integers, unsigned integers and pointers. Helper
functions are provided to create each type of result.

This allows sound APIs to return errors and different
kinds of values without using special values to indicate
errors.

More result types and their corresponding helper
functions can be added later as needed by other
sound APIs.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
 include/sound/snd_result.h | 61 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 include/sound/snd_result.h

diff --git a/include/sound/snd_result.h b/include/sound/snd_result.h
new file mode 100644
index 000000000000..61503675e7fb
--- /dev/null
+++ b/include/sound/snd_result.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Generic result type and helpers for sound APIs
+ * that need to return an error code or a value of
+ * a different type.
+ *
+ */
+
+#ifndef __SOUND_RESULT_H
+#define __SOUND_RESULT_H
+
+enum snd_result_type {
+	SND_RESULT_ERR,
+	SND_RESULT_INT,
+	SND_RESULT_UINT,
+	SND_RESULT_PTR,
+};
+
+struct snd_result {
+	enum snd_result_type type;
+	union {
+		int err;
+		int i;
+		unsigned int u;
+		void *ptr;
+	};
+};
+
+static inline struct snd_result snd_result_err(int err)
+{
+	return (struct snd_result) {
+		.type = SND_RESULT_ERR,
+		.err = err,
+	};
+}
+
+static inline struct snd_result snd_result_int(int value)
+{
+	return (struct snd_result) {
+		.type = SND_RESULT_INT,
+		.i = value,
+	};
+}
+
+static inline struct snd_result snd_result_uint(unsigned int value)
+{
+	return (struct snd_result) {
+		.type = SND_RESULT_UINT,
+		.u = value,
+	};
+}
+
+static inline struct snd_result snd_result_ptr(void *value)
+{
+	return (struct snd_result) {
+		.type = SND_RESULT_PTR,
+		.ptr = value,
+	};
+}
+
+#endif
-- 
2.55.0


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

* Re: [PATCH] sound: Add a generic result header
  2026-12-04 11:41 [PATCH] sound: Add a generic result header Sreeraj S Kurup
@ 2026-12-04 11:48 ` sashiko-bot
  2026-12-04 16:45 ` Takashi Iwai
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-04 11:48 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] sound: unidiomatic type-erased struct snd_result duplicates
  kernel idioms
- [Low] sound: add unused public header include/sound/snd_result.h with
  no callers
--

commit 1e31fdcfe3387f0bfdf6718e049b56a19745e76e
Author: Sreeraj S Kurup <sreekuttan2156239@gmail.com>

sound: Add a generic result header

This commit adds include/sound/snd_result.h defining struct snd_result,
enum snd_result_type, and inline constructors for returning an error code,
integer, unsigned integer, or pointer from sound APIs.

> diff --git a/include/sound/snd_result.h b/include/sound/snd_result.h
> new file mode 100644
> index 0000000000000..61503675e7fba
> --- /dev/null
> +++ b/include/sound/snd_result.h
> @@ -0,0 +1,61 @@

[ ... ]

> +#ifndef __SOUND_RESULT_H
> +#define __SOUND_RESULT_H

[Severity: Low]
Should this header wait until there are in-tree callers using it?

Nothing in the tree includes sound/snd_result.h or uses struct snd_result
or any of the snd_result_*() helpers.  Without concrete call sites, the
header is not build-tested during normal builds and cannot be evaluated
against actual sound APIs.  Introducing it alongside the first conversions
would avoid adding unused infrastructure.

> +enum snd_result_type {
> +	SND_RESULT_ERR,
> +	SND_RESULT_INT,
> +	SND_RESULT_UINT,
> +	SND_RESULT_PTR,
> +};
> +
> +struct snd_result {
> +	enum snd_result_type type;
> +	union {
> +		int err;
> +		int i;
> +		unsigned int u;
> +		void *ptr;
> +	};
> +};
> +
> +static inline struct snd_result snd_result_err(int err)
> +{
> +	return (struct snd_result) {
> +		.type = SND_RESULT_ERR,
> +		.err = err,
> +	};
> +}

[ ... ]

> +static inline struct snd_result snd_result_ptr(void *value)
> +{
> +	return (struct snd_result) {
> +		.type = SND_RESULT_PTR,
> +		.ptr = value,
> +	};
> +}

[Severity: Medium]
Does ALSA need a custom type-erased struct snd_result when existing kernel
patterns already handle these return types?

Functions returning a full-range unsigned int with negative errno codes
conventionally return int with a typed output pointer, as
_snd_hdac_read_parm() in include/sound/hdaudio.h does.  Pointer returns use
ERR_PTR(), IS_ERR(), and PTR_ERR() from include/linux/err.h instead.

Using struct snd_result weakens compile-time type checking: all adopting
functions share one return type, snd_result_ptr() drops the pointee type
and const or address-space qualifiers, and callers read raw union members
without error predicates, accessors, __must_check, or ownership rules.

Assigning SND_RESULT_ERR to 0 makes any zero-initialized struct snd_result
appear as an error with err == 0, and snd_result_err() does not reject
non-negative values, so returning res.err on an error path can silently
return 0.

Returning a 16-byte struct by value on 64-bit targets (or via a hidden
stack pointer on 32-bit) also adds ABI overhead for simple integer results.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204114122.4465-1-sreekuttan2156239@gmail.com?part=1

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

* Re: [PATCH] sound: Add a generic result header
  2026-12-04 11:41 [PATCH] sound: Add a generic result header Sreeraj S Kurup
  2026-12-04 11:48 ` sashiko-bot
@ 2026-12-04 16:45 ` Takashi Iwai
  2026-12-04 17:33   ` Sreeraj S Kurup
  1 sibling, 1 reply; 4+ messages in thread
From: Takashi Iwai @ 2026-12-04 16:45 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: perex, tiwai, linux-kernel, linux-sound

On Fri, 04 Dec 2026 12:41:22 +0100,
Sreeraj S Kurup wrote:
> 
> Add include/sound/snd_result.h to provide a common result
> structure for sound APIs.
> 
> Some sound APIs return unsigned int values but also use
> negative error codes to report failures. Since the return
> type is unsigned int, negative error codes are converted
> to unsigned values. This makes it hard to tell whether
> the returned value is an error or a valid result.
> 
> For example, returning -1 from an unsigned int function
> produces 0xffffffff, which could also be a valid value
> returned by the function. This makes it difficult to
> handle errors when the full range of unsigned values
> can be used.

I guess this is specific to HD-audio verbs, no?
Do we have multiple such cases?

> The new result structure uses a type field to identify
> what kind of value is stored. It supports error codes,
> signed integers, unsigned integers and pointers. Helper
> functions are provided to create each type of result.
> 
> This allows sound APIs to return errors and different
> kinds of values without using special values to indicate
> errors.
> 
> More result types and their corresponding helper
> functions can be added later as needed by other
> sound APIs.

I'd like to avoid this kind of things.  First off, having a union of a
pointer and integers can be dangerous, as they can be different sizes.
And, for a pointer, we have already solid macros to deal with the
special pointer values indicating errors.

Then the rest is signed/unsigned integer, and we need no special
macro.


thanks,

Takashi

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

* Re: [PATCH] sound: Add a generic result header
  2026-12-04 16:45 ` Takashi Iwai
@ 2026-12-04 17:33   ` Sreeraj S Kurup
  0 siblings, 0 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-12-04 17:33 UTC (permalink / raw)
  To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239

Hi Takashi,

Yes, that's correct. The issue is specific to HD-audio.

Sorry for the noise, and thank you for taking the time to review the patch.

I understand all your points, and I agree with your reasoning. I'll drop this patch.

Thanks again for the feedback.

Best regards,
Sreeraj

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

end of thread, other threads:[~2026-12-04 17:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 11:41 [PATCH] sound: Add a generic result header Sreeraj S Kurup
2026-12-04 11:48 ` sashiko-bot
2026-12-04 16:45 ` Takashi Iwai
2026-12-04 17:33   ` Sreeraj S Kurup

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