public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Ivan Orlov <ivan.orlov0322@gmail.com>
Cc: perex@perex.cz, tiwai@suse.com, corbet@lwn.net,
	broonie@kernel.org, shuah@kernel.org,
	linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	christophe.jaillet@wanadoo.fr, Axel Holzinger <aholzinger@gmx.de>
Subject: Re: [PATCH v2 3/4] ALSA: timer: Introduce virtual userspace-driven timers
Date: Mon, 29 Jul 2024 16:02:22 +0200	[thread overview]
Message-ID: <87h6c89tzl.wl-tiwai@suse.de> (raw)
In-Reply-To: <20240729085905.6602-4-ivan.orlov0322@gmail.com>

On Mon, 29 Jul 2024 10:59:04 +0200,
Ivan Orlov wrote:
> --- a/include/uapi/sound/asound.h
> +++ b/include/uapi/sound/asound.h
(snip)
> +/*
> + * 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;

The units in timer API should be independent from PCM.
So use the explicit type such as __u64 here (so that you don't need
the compat ioctl conversion, too).

> +	unsigned int id;
> +};

We often put some reserved fields for future extension.
But I'm not sure whether it's needed at this time for this kind of
simple interface, though.

>  #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)

Once after adding the new API, don't forget to bump the protocol
version defined in SNDRV_TIMER_VERSION.

> --- a/sound/core/timer.c
> +++ b/sound/core/timer.c
(snip)
> +#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).
> + */
> +DEFINE_IDA(snd_utimer_ids);

Missing static.

> +static int snd_utimer_create(struct snd_utimer_info *utimer_info,
> +			     struct snd_utimer **r_utimer)
> +{
(snip)
> +	err = snd_timer_new(NULL, utimer->name, &tid, &timer);
> +	if (err < 0) {
> +		pr_err("Can't create userspace-driven timer\n");
> +		goto err_timer_new;
> +	}
> +
> +	timer->module = THIS_MODULE;
> +	timer->hw = timer_hw;
> +	timer->hw.resolution = NANO / utimer_info->frame_rate * utimer_info->period_size;

A sanity check is definitely needed for parameters like this.
e.g. you'd hit a zero-division Oops with this code.
Also, the resolution should be neither too small nor too high.

> +static int snd_utimer_ioctl_create(struct file *file,
> +				   struct snd_utimer_info __user *_utimer_info)
> +{
> +	struct snd_utimer *utimer;
> +	struct snd_utimer_info *utimer_info;
> +	int err;
> +
> +	utimer_info = memdup_user(_utimer_info, sizeof(*utimer_info));
> +	if (IS_ERR(utimer_info))
> +		return PTR_ERR(no_free_ptr(utimer_info));

no_free_ptr() is used only for the automatic cleanup stuff.

> +static int snd_utimer_ioctl_create(struct file *file,
> +				   struct snd_utimer_info __user *_utimer_info)
> +{
> +	return -EINVAL;

Better to keep -ENOTTY?


thanks,

Takashi

  reply	other threads:[~2024-07-29 14:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-29  8:59 [PATCH v2 0/4] Introduce userspace-driven ALSA timers Ivan Orlov
2024-07-29  8:59 ` [PATCH v2 1/4] ALSA: aloop: Allow using global timers Ivan Orlov
2024-07-29  8:59 ` [PATCH v2 2/4] Docs/sound: Add documentation for userspace-driven ALSA timers Ivan Orlov
2024-07-29  8:59 ` [PATCH v2 3/4] ALSA: timer: Introduce virtual userspace-driven timers Ivan Orlov
2024-07-29 14:02   ` Takashi Iwai [this message]
2024-07-29 22:37     ` Ivan Orlov
2024-07-30  2:57   ` kernel test robot
2024-07-29  8:59 ` [PATCH v2 4/4] selftests: ALSA: Cover userspace-driven timers with test Ivan Orlov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h6c89tzl.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=aholzinger@gmx.de \
    --cc=broonie@kernel.org \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=corbet@lwn.net \
    --cc=ivan.orlov0322@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=shuah@kernel.org \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox