Skip to contentSkip to conversation

Introduce userspace-driven ALSA timers

PATCH Superseded Ivan Orlov 3 participants 14 messages Jul 26, 2024 – Jul 28, 2024

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

cover Introduce userspace-driven ALSA timers

There are multiple possible timer sources which could be useful for the sound stream synchronization: hrtimers, hardware clocks (e.g. PTP), timer wheels (jiffies). Currently, using one of them to synchronize the audio stream of snd-aloop module would require writing a kernel-space driver which exports an ALSA timer through the snd_timer interface.

However, it is not really convenient for application developers, who may want to define their custom timer sources for audio synchronization.

For instance, we could have a network application which receives frames and sends them to snd-aloop pcm device, and another application listening on the other end of snd-aloop. It makes sense to transfer a new period of data only when certain amount of frames is received through the network, but definitely not when a certain amount of jiffies on a local system elapses. Since all of the devices are purely virtual it won't introduce any glitches and will help the application developers to avoid using sample-rate conversion.

This patch series introduces userspace-driven ALSA timers: virtual timers which are created and controlled from userspace. The timer can be created from the userspace using the new ioctl SNDRV_TIMER_IOCTL_CREATE. After creating a timer, it becomes available for use system-wide, so it can be passed to snd-aloop as a timer source (timer_source parameter would be "-1.SNDRV_TIMER_GLOBAL_UDRIVEN.{timer_id}"). When the userspace app decides to trigger a timer, it calls another ioctl SNDRV_TIMER_IOCTL_TRIGGER on the file descriptor of a timer. It initiates a transfer of a new period of data.

Userspace-driven timers are associated with file descriptors. If the application wishes to destroy the timer, it can simply release the file descriptor of a virtual timer.

I believe introducing new ioctl calls is quite inconvenient (as we have a limited amount of them), but other possible ways of app <-> kernel communication (like virtual FS) seem completely inappropriate for this task (but I'd love to discuss alternative solutions).

This patch series also updates the snd-aloop module so the global timers can be used as a timer_source for it (it allows using userspace-driven timers as timer source).

Ivan Orlov (4):
ALSA: aloop: Allow using global timers
Docs/sound: Add documentation for userspace-driven ALSA timers
ALSA: timer: Introduce virtual userspace-driven timers
selftests: ALSA: Cover userspace-driven timers with test

 Documentation/sound/index.rst               |   1 +
 Documentation/sound/utimers.rst             | 120 +++++++++++
 include/uapi/sound/asound.h                 |  17 ++
 sound/core/Kconfig                          |  11 +
 sound/core/timer.c                          | 226 ++++++++++++++++++++
 sound/drivers/aloop.c                       |   2 +
 tools/testing/selftests/alsa/Makefile       |   2 +-
 tools/testing/selftests/alsa/global-timer.c |  87 ++++++++
 tools/testing/selftests/alsa/utimer-test.c  | 133 ++++++++++++
 9 files changed, 598 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/sound/utimers.rst
 create mode 100644 tools/testing/selftests/alsa/global-timer.c
 create mode 100644 tools/testing/selftests/alsa/utimer-test.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/4 ALSA: aloop: Allow using global timers

Allow using global timers as a timer source when card id is equal to -1 in the timer_source parameter.

---
 sound/drivers/aloop.c | 2 ++
 1 file changed, 2 insertions(+)
sound/drivers/aloop.c +2 -0
index d6dd4b8c750a..a1e5e7fe9c72 100644
@@ -1130,6 +1130,8 @@ static int loopback_parse_timer_id(const char *str,
 			}
 		}
 	}
+	if (card_idx == -1)
+		tid->dev_class = SNDRV_TIMER_CLASS_GLOBAL;
 	if (!err && tid) {
 		tid->card = card_idx;
 		tid->device = dev;
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/4 Docs/sound: Add documentation for userspace-driven ALSA timers

In reply to Ivan Orlov

Add the documentation which describes the new userspace-driven timers API introduced in this patch series. The documentation contains:

- Description of userspace-driven ALSA timers, what they are for
- Description of the timers API
- Example of how the timers can be created and triggered
- How the timers can be used as a timer sources for snd-aloop module

---
 Documentation/sound/index.rst   |   1 +
 Documentation/sound/utimers.rst | 120 ++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+)
 create mode 100644 Documentation/sound/utimers.rst
Documentation/sound/index.rst +1 -0
index 7e67e12730d3..c437f2a4bc85 100644
@@ -13,6 +13,7 @@ Sound Subsystem Documentation
    alsa-configuration
    hd-audio/index
    cards/index
+   utimers
 
 .. only::  subproject and html
 
Documentation/sound/utimers.rst new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/4 ALSA: timer: Introduce virtual userspace-driven timers

In reply to Ivan Orlov

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---
 include/uapi/sound/asound.h |  17 +++
 sound/core/Kconfig          |  11 ++
 sound/core/timer.c          | 226 ++++++++++++++++++++++++++++++++++++
 3 files changed, 254 insertions(+)
include/uapi/sound/asound.h +17 -0
index 8bf7e8a0eb6f..ade952a54edd 100644
@@ -894,6 +894,7 @@ enum {
 #define SNDRV_TIMER_GLOBAL_RTC		1	/* unused */
 #define SNDRV_TIMER_GLOBAL_HPET		2
 #define SNDRV_TIMER_GLOBAL_HRTIMER	3
+#define SNDRV_TIMER_GLOBAL_UDRIVEN	4
 
 /* info flags */
 #define SNDRV_TIMER_FLG_SLAVE		(1<<0)	/* cannot be controlled */
@@ -974,6 +975,20 @@ struct snd_timer_status {
 };
 #endif
 
+/*
+ * This structure describes the userspace-driven timer. Such timers are purely virtual,
+ * and can only be triggered from software (for instance, by userspace application).
+ */
+struct snd_utimer_info {
+	/*
+	 * To pretend being a normal timer, we need to know the frame rate and
+	 * the period size in frames.
+	 */
+	snd_pcm_uframes_t frame_rate;
+	snd_pcm_uframes_t period_size;
+	unsigned int id;
+};
+
 #define SNDRV_TIMER_IOCTL_PVERSION	_IOR('T', 0x00, int)
 #define SNDRV_TIMER_IOCTL_NEXT_DEVICE	_IOWR('T', 0x01, struct snd_timer_id)
 #define SNDRV_TIMER_IOCTL_TREAD_OLD	_IOW('T', 0x02, int)
@@ -990,6 +1005,8 @@ struct snd_timer_status {
 #define SNDRV_TIMER_IOCTL_CONTINUE	_IO('T', 0xa2)
 #define SNDRV_TIMER_IOCTL_PAUSE		_IO('T', 0xa3)
 #define SNDRV_TIMER_IOCTL_TREAD64	_IOW('T', 0xa4, int)
+#define SNDRV_TIMER_IOCTL_CREATE	_IOWR('T', 0xa5, struct snd_utimer_info)
+#define SNDRV_TIMER_IOCTL_TRIGGER	_IO('T', 0xa6)
 
 #if __BITS_PER_LONG == 64
 #define SNDRV_TIMER_IOCTL_TREAD SNDRV_TIMER_IOCTL_TREAD_OLD
sound/core/Kconfig +11 -0
index b970a1734647..3cf82641fc67 100644
@@ -251,6 +251,17 @@ config SND_JACK_INJECTION_DEBUG
 	  Say Y if you are debugging via jack injection interface.
 	  If unsure select "N".
 
+config SND_UTIMER
+	bool "Enable support for userspace-controlled virtual timers"
+	depends on SND_TIMER
+	help
+	  Say Y to enable the support of userspace-controlled timers. These
+	  timers are purely virtual, and they are supposed to be triggered
+	  from userspace. They could be quite useful when synchronizing the
+	  sound timing with userspace applications (for instance, when sending
+	  data through snd-aloop).
+
+
 config SND_VMASTER
 	bool
 
sound/core/timer.c +226 -0

250 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#12 lore raw

PATCH 4/4 selftests: ALSA: Cover userspace-driven timers with test

In reply to Ivan Orlov

Add a test for the new functionality of userspace-driven timers and the tool which allows us to count timer ticks in a certain time period. The test:

1. Creates a userspace-driven timer with ioctl to /dev/snd/timer
2. Starts the global-timer application to count the ticks of the timer from step 1.
3. Asynchronously triggers the timer multiple times with some interval
4. Compares the amount of caught ticks with the amount of trigger calls.

Since we can't include <alsa/asoundlib.h> and <sound/asound.h> in one file due to overlapping declarations, I have to split the test into two
applications: one of them counts the amount of timer ticks in the defined time period, and another one is the actual test which creates the timer, triggers it periodically and starts the first app to count the amount of ticks in a separate thread.

Besides from testing the functionality itself, the test represents a sample application showing userspace-driven ALSA timers API.

---
 tools/testing/selftests/alsa/Makefile       |   2 +-
 tools/testing/selftests/alsa/global-timer.c |  87 +++++++++++++
 tools/testing/selftests/alsa/utimer-test.c  | 133 ++++++++++++++++++++
 3 files changed, 221 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/alsa/global-timer.c
 create mode 100644 tools/testing/selftests/alsa/utimer-test.c
tools/testing/selftests/alsa/Makefile +1 -1
index c1ce39874e2b..0d5bd8ea900b 100644
@@ -12,7 +12,7 @@ LDLIBS+=-lpthread
 
 OVERRIDE_TARGETS = 1
 
-TEST_GEN_PROGS := mixer-test pcm-test test-pcmtest-driver
+TEST_GEN_PROGS := mixer-test pcm-test utimer-test test-pcmtest-driver global-timer
 
 TEST_GEN_PROGS_EXTENDED := libatest.so
 
tools/testing/selftests/alsa/global-timer.c new file +87 -0

87 lines not on this page · raw message

tools/testing/selftests/alsa/utimer-test.c new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH 4/4] selftests: ALSA: Cover userspace-driven timers with test

kernel test robot noticed the following build warnings:

Hi Ivan,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on tiwai-sound/for-linus linus/master v6.10 next-20240726] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ivan-Orlov/ALSA-aloop-Allow-using-global-timers/20240726-155158
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
patch link:    https://lore.kernel.org/r/20240726074750.626671-5-ivan.orlov0322%40gmail.com
patch subject: [PATCH 4/4] selftests: ALSA: Cover userspace-driven timers with test

:::::: branch date: 2 days ago
:::::: commit date: 2 days ago
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240728/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

All warnings (new ones prefixed by >>):

>> utimer-test.c:73:1: warning: non-void function does not return a value [-Wreturn-type]
      73 | }
         | ^
>> utimer-test.c:117:11: warning: enumeration value 'TIMER_NO_EVENT' not handled in switch [-Wswitch]
     117 |                 switch (parse_timer_output(buf)) {
         |                         ^~~~~~~~~~~~~~~~~~~~~~~

2 warnings generated.

vim +73 tools/testing/selftests/alsa/utimer-test.c

5cf58530fa628b Ivan Orlov 2024-07-26  62  
5cf58530fa628b Ivan Orlov 2024-07-26  63  static void *ticking_func(void *data)
5cf58530fa628b Ivan Orlov 2024-07-26  64  {
5cf58530fa628b Ivan Orlov 2024-07-26  65  	int i;
5cf58530fa628b Ivan Orlov 2024-07-26  66  	int *fd = (int *)data;
5cf58530fa628b Ivan Orlov 2024-07-26  67  
5cf58530fa628b Ivan Orlov 2024-07-26  68  	for (i = 0; i < TICKS_COUNT; i++) {
5cf58530fa628b Ivan Orlov 2024-07-26  69  		/* Well, trigger the timer! */
5cf58530fa628b Ivan Orlov 2024-07-26  70  		ioctl(*fd, SNDRV_TIMER_IOCTL_TRIGGER, NULL);
5cf58530fa628b Ivan Orlov 2024-07-26  71  		sleep(TIMER_FREQ_SEC);
5cf58530fa628b Ivan Orlov 2024-07-26  72  	}

5cf58530fa628b Ivan Orlov 2024-07-26 @73 }
5cf58530fa628b Ivan Orlov 2024-07-26 74

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

In reply to Ivan Orlov

Le 26/07/2024 à 09:47, Ivan Orlov a écrit :

Quoted 18 lines

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

Quoted 8 lines

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---

Hi,

...

Quoted 8 lines
sound/core/Kconfig +0 -0
index b970a1734647..3cf82641fc67 100644
@@ -251,6 +251,17 @@ config SND_JACK_INJECTION_DEBUG
  	  Say Y if you are debugging via jack injection interface.
  	  If unsure select "N".
 

sound/core/Kconfig config SND_JACK_INJECTION_DEBUG · line 263

Quoted 10 lines
sound/core/Kconfig +10 -0
+config SND_UTIMER
+	bool "Enable support for userspace-controlled virtual timers"
+	depends on SND_TIMER
+	help
+	  Say Y to enable the support of userspace-controlled timers. These
+	  timers are purely virtual, and they are supposed to be triggered
+	  from userspace. They could be quite useful when synchronizing the
+	  sound timing with userspace applications (for instance, when sending
+	  data through snd-aloop).
+

Unneeded extra new line.

Quoted 4 lines
sound/core/Kconfig +1 -0
+
  config SND_VMASTER
  	bool
 

...

sound/core/Kconfig

Quoted 4 lines
sound/core/Kconfig +4 -0
+static void snd_utimer_free(struct snd_utimer *utimer)
+{
+	snd_timer_free(utimer->timer);
+	snd_utimer_put_id(utimer);

Missing kfree(utimer->name); ?

Quoted 2 lines
sound/core/Kconfig +2 -0
+	kfree(utimer);
+}

...

Quoted 5 lines
sound/core/Kconfig +5 -0
+static int snd_utimer_create(struct snd_utimer_info *utimer_info,
+			     struct snd_utimer **r_utimer)
+{
+	struct snd_utimer *utimer;
+	struct snd_timer *timer;

sound/core/Kconfig

Quoted 10 lines
sound/core/Kconfig +10 -0
+	struct snd_timer_id tid;
+	int utimer_id;
+	int err = 0;
+	char *timer_name;
+
+	utimer = kzalloc(sizeof(*utimer), GFP_KERNEL);
+	if (!utimer)
+		return -ENOMEM;
+
+	timer_name = kzalloc(SNDRV_UTIMER_NAME_LEN, GFP_KERNEL);

kasprintf(GFP_KERNEL, "snd-utimer%d", utimer_id); ? and SNDRV_UTIMER_NAME_LEN becomes useless too.

In snd_timer_new() it is copied in a char[64] anyway, and if utimer_id is small, we could even save a few bytes of memory.

CJ

Quoted 15 lines
sound/core/Kconfig +15 -0
+	if (!timer_name) {
+		kfree(utimer);
+		return -ENOMEM;
+	}
+
+	/* We hold the ioctl lock here so we won't get a race condition when allocating id */
+	utimer_id = snd_utimer_take_id();
+	if (utimer_id < 0) {
+		err = utimer_id;
+		goto err_take_id;
+	}
+
+	sprintf(timer_name, "snd-utimer%d", utimer_id);
+	utimer->name = timer_name;
+	utimer->id = utimer_id;

...

#6 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

In reply to Ivan Orlov

Le 26/07/2024 à 09:47, Ivan Orlov a écrit :

Quoted 26 lines

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---

...

Quoted 21 lines
sound/core/timer.c +21 -0
+#ifdef CONFIG_SND_UTIMER
+/*
+ * Since userspace-driven timers are passed to userspace, we need to have an identifier
+ * which will allow us to use them (basically, the subdevice number of udriven timer).
+ *
+ * We have a pool of SNDRV_UTIMERS_MAX_COUNT ids from 0 to (SNDRV_UTIMERS_MAX_COUNT - 1).
+ * When we take one of them, the corresponding entry in snd_utimer_ids becomes true.
+ */
+static bool snd_utimer_ids[SNDRV_UTIMERS_MAX_COUNT];
+
+static void snd_utimer_put_id(struct snd_utimer *utimer)
+{
+	int timer_id = utimer->id;
+
+	snd_BUG_ON(timer_id < 0 || timer_id >= SNDRV_UTIMERS_MAX_COUNT);
+	snd_utimer_ids[timer_id] = false;
+}
+
+static int snd_utimer_take_id(void)
+{
+	size_t i;

sound/core/timer.c enum { · line 2055

Quoted 10 lines
sound/core/timer.c +10 -0
+
+	for (i = 0; i < SNDRV_UTIMERS_MAX_COUNT; i++) {
+		if (!snd_utimer_ids[i]) {
+			snd_utimer_ids[i] = true;
+			return i;
+		}
+	}
+
+	return -EBUSY;
+}

Also the bitmap API could be useful here.

CJ

author
#5 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

In reply to Christophe JAILLET
Quoted 2 lines · Christophe JAILLET

Hi,

Hi Christophe,

Quoted 27 lines

...

Quoted 10 lines
sound/core/Kconfig +0 -0
index b970a1734647..3cf82641fc67 100644
@@ -251,6 +251,17 @@ config SND_JACK_INJECTION_DEBUG

        Say Y if you are debugging via jack injection interface. If unsure select "N".
+config SND_UTIMER
+    bool "Enable support for userspace-controlled virtual timers" +    depends on SND_TIMER

Quoted 8 lines

+    help
+      Say Y to enable the support of userspace-controlled timers. These +      timers are purely virtual, and they are supposed to be triggered +      from userspace. They could be quite useful when synchronizing the +      sound timing with userspace applications (for instance, when sending
+      data through snd-aloop).
+

Unneeded extra new line.

Quoted 3 lines

+
  config SND_VMASTER
      bool

Quoted 9 lines

...

sound/core/Kconfig

Quoted 4 lines
sound/core/Kconfig +4 -0
+static void snd_utimer_free(struct snd_utimer *utimer)
+{
+    snd_timer_free(utimer->timer);
+    snd_utimer_put_id(utimer);

Missing kfree(utimer->name); ?

Yeah, it definitely should be here... Thank you for finding this!

Quoted 24 lines
Quoted 2 lines

+    kfree(utimer);
+}

...

Quoted 5 lines
sound/core/Kconfig +5 -0
+static int snd_utimer_create(struct snd_utimer_info *utimer_info,
+                 struct snd_utimer **r_utimer)
+{
+    struct snd_utimer *utimer;
+    struct snd_timer *timer;

sound/core/Kconfig

Quoted 10 lines
sound/core/Kconfig +10 -0
+    struct snd_timer_id tid;
+    int utimer_id;
+    int err = 0;
+    char *timer_name;
+
+    utimer = kzalloc(sizeof(*utimer), GFP_KERNEL);
+    if (!utimer)
+        return -ENOMEM;
+
+    timer_name = kzalloc(SNDRV_UTIMER_NAME_LEN, GFP_KERNEL);

kasprintf(GFP_KERNEL, "snd-utimer%d", utimer_id); ? and SNDRV_UTIMER_NAME_LEN becomes useless too.

Quoted 2 lines

In snd_timer_new() it is copied in a char[64] anyway, and if utimer_id is small, we could even save a few bytes of memory.

Wow, cool, I haven't heard of kasprintf but now I'll use it here in V2. Thanks!

Signature · 2 lines
Kind regards,
Ivan Orlov
author
#7 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

In reply to Christophe JAILLET
Quoted 59 lines · Christophe JAILLET

Le 26/07/2024 à 09:47, Ivan Orlov a écrit :

Quoted 26 lines

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---

...

Quoted 29 lines
sound/core/timer.c +3 -0
+#ifdef CONFIG_SND_UTIMER
+/*
+ * Since userspace-driven timers are passed to userspace, we need to 

have an identifier
+ * which will allow us to use them (basically, the subdevice number of udriven timer).
+ *
+ * We have a pool of SNDRV_UTIMERS_MAX_COUNT ids from 0 to (SNDRV_UTIMERS_MAX_COUNT - 1).
+ * When we take one of them, the corresponding entry in snd_utimer_ids becomes true.
+ */

sound/core/timer.c +17 -0
+static bool snd_utimer_ids[SNDRV_UTIMERS_MAX_COUNT];
+
+static void snd_utimer_put_id(struct snd_utimer *utimer)
+{
+    int timer_id = utimer->id;
+
+    snd_BUG_ON(timer_id < 0 || timer_id >= SNDRV_UTIMERS_MAX_COUNT);
+    snd_utimer_ids[timer_id] = false;
+}
+
+static int snd_utimer_take_id(void)
+{
+    size_t i;
+
+    for (i = 0; i < SNDRV_UTIMERS_MAX_COUNT; i++) {
+        if (!snd_utimer_ids[i]) {
+            snd_utimer_ids[i] = true;
Quoted 9 lines

sound/core/timer.c enum { · line 2055

Quoted 6 lines

+            return i;
+        }
+    }

sound/core/timer.c +3 -0
+
+    return -EBUSY;
+}

Also the bitmap API could be useful here.

Awesome, will use it in V2.

Thank you!

Signature · 2 lines
Kind regards,
Ivan Orlov
#8 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

Le 28/07/2024 à 10:51, Ivan Orlov a écrit :

Quoted 70 lines · Christophe JAILLET
Quoted 59 lines

Le 26/07/2024 à 09:47, Ivan Orlov a écrit :

Quoted 26 lines

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---

...

Quoted 29 lines
sound/core/timer.c +3 -0
+#ifdef CONFIG_SND_UTIMER
+/*
+ * Since userspace-driven timers are passed to userspace, we need to 

have an identifier
+ * which will allow us to use them (basically, the subdevice number of udriven timer).
+ *
+ * We have a pool of SNDRV_UTIMERS_MAX_COUNT ids from 0 to (SNDRV_UTIMERS_MAX_COUNT - 1).
+ * When we take one of them, the corresponding entry in snd_utimer_ids becomes true.
+ */

sound/core/timer.c +17 -0
+static bool snd_utimer_ids[SNDRV_UTIMERS_MAX_COUNT];
+
+static void snd_utimer_put_id(struct snd_utimer *utimer)
+{
+    int timer_id = utimer->id;
+
+    snd_BUG_ON(timer_id < 0 || timer_id >= SNDRV_UTIMERS_MAX_COUNT);
+    snd_utimer_ids[timer_id] = false;
+}
+
+static int snd_utimer_take_id(void)
+{
+    size_t i;
+
+    for (i = 0; i < SNDRV_UTIMERS_MAX_COUNT; i++) {
+        if (!snd_utimer_ids[i]) {
+            snd_utimer_ids[i] = true;
Quoted 9 lines

sound/core/timer.c enum { · line 2055

Quoted 6 lines

+            return i;
+        }
+    }

sound/core/timer.c +3 -0
+
+    return -EBUSY;
+}

Also the bitmap API could be useful here.

Quoted 1 line

Awesome, will use it in V2.

Hmm, maybe DEFINE_IDA(), ida_alloc_max() and ida_free() would be even better.

CJ

Quoted 4 lines

Thank you!

author
#9 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

Quoted 71 lines · Christophe JAILLET

Le 28/07/2024 à 10:51, Ivan Orlov a écrit :

Quoted 70 lines · Christophe JAILLET
Quoted 68 lines

Le 26/07/2024 à 09:47, Ivan Orlov a écrit :

Quoted 26 lines

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---

...

Quoted 25 lines
sound/core/timer.c +3 -0
+#ifdef CONFIG_SND_UTIMER
+/*
+ * Since userspace-driven timers are passed to userspace, we need 

to have an identifier
+ * which will allow us to use them (basically, the subdevice number of udriven timer).
+ *
+ * We have a pool of SNDRV_UTIMERS_MAX_COUNT ids from 0 to (SNDRV_UTIMERS_MAX_COUNT - 1).
+ * When we take one of them, the corresponding entry in snd_utimer_ids becomes true.
+ */

sound/core/timer.c +13 -0
+static bool snd_utimer_ids[SNDRV_UTIMERS_MAX_COUNT];
+
+static void snd_utimer_put_id(struct snd_utimer *utimer)
+{
+    int timer_id = utimer->id;
+
+    snd_BUG_ON(timer_id < 0 || timer_id >= SNDRV_UTIMERS_MAX_COUNT);
+    snd_utimer_ids[timer_id] = false;
+}
+
+static int snd_utimer_take_id(void)
+{
+    size_t i;

sound/core/timer.c enum { · line 2055

Quoted 10 lines
sound/core/timer.c +10 -0
+
+    for (i = 0; i < SNDRV_UTIMERS_MAX_COUNT; i++) {
+        if (!snd_utimer_ids[i]) {
+            snd_utimer_ids[i] = true;
+            return i;
+        }
+    }
+
+    return -EBUSY;
+}

Also the bitmap API could be useful here.

Quoted 4 lines
Quoted 1 line

Awesome, will use it in V2.

Hmm, maybe DEFINE_IDA(), ida_alloc_max() and ida_free() would be even better.

It looks like IDA allocator uses XArrays under the hood to allocate ids between 0 and INT_MAX... Considering the fact, that we currently could have up to 128 userspace-driven timers in the system, using XArrays seems a bit redundant, and I believe bitmap approach would be more efficient. What do you think?

Signature · 2 lines
Kind regards,
Ivan Orlov
#10 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

Le 28/07/2024 à 11:42, Ivan Orlov a écrit :

Quoted 72 lines · Christophe JAILLET
Quoted 71 lines

Le 28/07/2024 à 10:51, Ivan Orlov a écrit :

Quoted 70 lines · Christophe JAILLET
Quoted 68 lines

Le 26/07/2024 à 09:47, Ivan Orlov a écrit :

Quoted 26 lines

Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.

The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the id field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances).

This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system.

Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks.

The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here.

---

...

Quoted 25 lines
sound/core/timer.c +3 -0
+#ifdef CONFIG_SND_UTIMER
+/*
+ * Since userspace-driven timers are passed to userspace, we need 

to have an identifier
+ * which will allow us to use them (basically, the subdevice number of udriven timer).
+ *
+ * We have a pool of SNDRV_UTIMERS_MAX_COUNT ids from 0 to (SNDRV_UTIMERS_MAX_COUNT - 1).
+ * When we take one of them, the corresponding entry in snd_utimer_ids becomes true.
+ */

sound/core/timer.c +13 -0
+static bool snd_utimer_ids[SNDRV_UTIMERS_MAX_COUNT];
+
+static void snd_utimer_put_id(struct snd_utimer *utimer)
+{
+    int timer_id = utimer->id;
+
+    snd_BUG_ON(timer_id < 0 || timer_id >= SNDRV_UTIMERS_MAX_COUNT);
+    snd_utimer_ids[timer_id] = false;
+}
+
+static int snd_utimer_take_id(void)
+{
+    size_t i;

sound/core/timer.c enum { · line 2055

Quoted 10 lines
sound/core/timer.c +10 -0
+
+    for (i = 0; i < SNDRV_UTIMERS_MAX_COUNT; i++) {
+        if (!snd_utimer_ids[i]) {
+            snd_utimer_ids[i] = true;
+            return i;
+        }
+    }
+
+    return -EBUSY;
+}

Also the bitmap API could be useful here.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Awesome, will use it in V2.

Hmm, maybe DEFINE_IDA(), ida_alloc_max() and ida_free() would be even better.

It looks like IDA allocator uses XArrays under the hood to allocate ids between 0 and INT_MAX... Considering the fact, that we currently could have up to 128 userspace-driven timers in the system, using XArrays seems a bit redundant, and I believe bitmap approach would be more efficient. What do you think?

I may be wrong but I think that ida allocates hunks for 1024 bits (128 bytes * 8) at a time. (see [1])

So with this extra sape and the sapce for the xarray, it would waste a few bytes of memory, yes.

With ida, there is also some locking that may be unnecessary (but harmless)

Hoping, I got it right, here are a few numbers:

On a x86_64, with allmodconfig:

Your initial patch:

   text	   data	    bss	    dec	    hex	filename
  55020	   1783	    268	  57071	   deef	sound/core/timer.o

With ida:
54763 1631 116 56510 dcbe sound/core/timer.o
+ 128 bytes of runtime memory allocation

With bitmap:
54805 1535 132 56472 dc98 sound/core/timer.o

I think that the code would be slightly more elegant with ida, but implementing it with a bitmap does not add that much complexity.

CJ

[1]: https://elixir.bootlin.com/linux/v6.10.2/source/include/linux/idr.h#L238

author
#11 lore raw

Re: [PATCH 3/4] ALSA: timer: Introduce virtual userspace-driven timers

Quoted 20 lines · Christophe JAILLET

I may be wrong but I think that ida allocates hunks for 1024 bits (128 bytes * 8) at a time. (see [1])

So with this extra sape and the sapce for the xarray, it would waste a few bytes of memory, yes.

With ida, there is also some locking that may be unnecessary (but harmless)

Hoping, I got it right, here are a few numbers:

On a x86_64, with allmodconfig:

Your initial patch:
   text       data        bss        dec        hex    filename 55020       1783        268      57071       deef    sound/core/timer.o

With ida:
  54763       1631        116      56510       dcbe    sound/core/timer.o
+ 128 bytes of runtime memory allocation

Quoted 6 lines

With bitmap:
  54805       1535        132      56472       dc98    sound/core/timer.o

I think that the code would be slightly more elegant with ida, but implementing it with a bitmap does not add that much complexity.

Ah, alright, I agree that the code would be cleaner when using IDA, and such a small memory overhead won't be significant/noticeable. I'm going to use IDA in the V2 instead of bitmap API, thank you so much for pointing me to it (I was wondering if the Kernel has a generic ID allocator and now I finally know it does :) ).

Thank you!

Signature · 2 lines
Kind regards,
Ivan Orlov