From nobody Fri Oct 18 08:44:53 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XVJDQ09FMz5Ywgx; Fri, 18 Oct 2024 08:44:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XVJDP5lyCz4lpS; Fri, 18 Oct 2024 08:44:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729241093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tlIYIBqYMDr2TUTisVowHNjd5ThO5KrjshGevrpXD10=; b=gK/JW7k5mJWopAY6fb+/9GchiXZu5JK2YsBtFzDNxUdq1p2A9pevCGwnEB9eFU0qulnqJB VizLHHl+mw8xb2/Lg9ln1IkhHfDyCsB6GQUT6RzmpWmQhoC4cvGxmnSGc9YezKx5U7veQD 8maDks/v2Arm642WwDiSpZ/yMxNl6Ixc5+fd7JuGVQMRhPpgN1U8Jxa7FA+4wn6OV29ber eLbnfRUowPgccOjXhKZ0H+4yC7x73kQ+zD1b47vQrzCoyeZN94vh9rajrnDiA14mUrPotk uxPbcHEesTRwVoIh2QULIAR4Yl4PXCk1Gvv2voQMI0iE3uhF7z064dK91S3O6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729241093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tlIYIBqYMDr2TUTisVowHNjd5ThO5KrjshGevrpXD10=; b=iZZfxsP7VjBfZbsO1BRr7jNWwemMk1m/Gf1uKLb9eE9A3A+iKcboebZejAX7Hkcci2ISb3 gBm7yPKkvShpV4eviKYQAtt+OxUsKDRHeomRTXyNttyxcyNMt1ClvXjVKuvfUOFPFSQsjD b6+B4nEm701CF+Fm+6JCcz/aYy4u467XrG5vEy4D/IlpsDwu4Z2tIew8/hhI5h2oMaKdjD vhzvDdK/jYo4faozGR8rj6U0KA7Zf/CkbtctsRZ9/WBnpvMCLwULp5fnaoZXdFq943JZ50 ZdSY2Vi2Irj1sQqkTTQaDWCl3Z47GIW8CeqsLSQsuF9AnGBCbt1G2AESiqBUWA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729241093; a=rsa-sha256; cv=none; b=N2pLRyQvN/HTjjcDlKpawJxzihEX8EvHs3OFcXCSyymwNwCFXm/WsY5VG3M7Z7RUezDJZ3 eCXlR61OPpyrCjT9PdT/0vixr+d+D/3znsKv9WG0RxfMlCOi7040mGVACJ4a9AicP24sEj 38xkEAX2cMpbdXKxdIqhazDNL1ieMG0J1xvl2rwfmFhHaHP1Pd34nz+9dMHZf4ZOU12NRm ybyTaLugxbvhCtUZkbHQN3oZCJvpzl/Vt1N3X6kE/Pw+SNPFreDyRKQByzFHfX12OrjYQf P3iu3KlrGIM6FyqG31KNOruSWvPJNd1vaPhs9lRoUCTn45IZUThclPafXh6DtQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XVJDP5Kr4zRgw; Fri, 18 Oct 2024 08:44:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49I8irFQ087853; Fri, 18 Oct 2024 08:44:53 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49I8irlR087850; Fri, 18 Oct 2024 08:44:53 GMT (envelope-from git) Date: Fri, 18 Oct 2024 08:44:53 GMT Message-Id: <202410180844.49I8irlR087850@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: ad4c8671bdda - main - sound: Use unr(9) to produce unique channel unit numbers List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ad4c8671bddaa30bf0413089c74dde6a618d9021 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=ad4c8671bddaa30bf0413089c74dde6a618d9021 commit ad4c8671bddaa30bf0413089c74dde6a618d9021 Author: Christos Margiolis AuthorDate: 2024-10-18 08:39:08 +0000 Commit: Christos Margiolis CommitDate: 2024-10-18 08:39:08 +0000 sound: Use unr(9) to produce unique channel unit numbers Currently it is possible to assign a unit number that already exists. Suppose the following channel list: [vp2] If we create 3 channels, we'll end up with the following list: [vp0, vp1, vp2, vp2] This happens because chn_init() does not check if the unit number we are assigning already exists. While fixing this is trivial when the channel list is sorted in ascending order, it is way more involved when sorted in descending order. Even though sorting the list in descending order would require deliberately modifying pcm_chn_add(), and is most likely not going to happen, make the mechanism more robust by using a unr(9) allocator for each channel type. Sponsored by: The FreeBSD Foundation MFC after: 2 days Reviewed by: dev_submerge.ch Differential Revision: https://reviews.freebsd.org/D46680 --- sys/dev/sound/pcm/channel.c | 31 ++++++++++++++++++++++--------- sys/dev/sound/pcm/sound.c | 12 ++++++++++++ sys/dev/sound/pcm/sound.h | 4 ++++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/sys/dev/sound/pcm/channel.c b/sys/dev/sound/pcm/channel.c index bf9c74e2ed1d..d1c9bc616dcf 100644 --- a/sys/dev/sound/pcm/channel.c +++ b/sys/dev/sound/pcm/channel.c @@ -1157,6 +1157,24 @@ chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd) return r; } +static struct unrhdr * +chn_getunr(struct snddev_info *d, int type) +{ + switch (type) { + case SND_DEV_DSPHW_PLAY: + return (d->p_unr); + case SND_DEV_DSPHW_VPLAY: + return (d->vp_unr); + case SND_DEV_DSPHW_REC: + return (d->r_unr); + case SND_DEV_DSPHW_VREC: + return (d->vr_unr); + default: + __assert_unreachable(); + } + +} + struct pcm_channel * chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, void *devinfo) @@ -1165,7 +1183,7 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, struct feeder_class *fc; struct snd_dbuf *b, *bs; char *dirs, buf[CHN_NAMELEN]; - int i, direction, type, unit; + int i, direction, type; PCM_BUSYASSERT(d); PCM_LOCKASSERT(d); @@ -1198,13 +1216,6 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, return (NULL); } - unit = 0; - CHN_FOREACH(c, d, channels.pcm) { - if (c->type != type) - continue; - unit++; - } - PCM_UNLOCK(d); b = NULL; bs = NULL; @@ -1216,7 +1227,7 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, CHN_INIT(c, children.busy); c->direction = direction; c->type = type; - c->unit = unit; + c->unit = alloc_unr(chn_getunr(d, c->type)); c->format = SND_FORMAT(AFMT_U8, 1, 0); c->speed = DSP_DEFAULT_SPEED; c->pid = -1; @@ -1304,6 +1315,7 @@ chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, return (c); fail: + free_unr(chn_getunr(d, c->type), c->unit); feeder_remove(c); if (c->devinfo && CHANNEL_FREE(c->methods, c->devinfo)) sndbuf_free(b); @@ -1335,6 +1347,7 @@ chn_kill(struct pcm_channel *c) chn_trigger(c, PCMTRIG_ABORT); CHN_UNLOCK(c); } + free_unr(chn_getunr(c->parentsnddev, c->type), c->unit); feeder_remove(c); if (CHANNEL_FREE(c->methods, c->devinfo)) sndbuf_free(b); diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c index 1aaf614078a4..f9c87facfeec 100644 --- a/sys/dev/sound/pcm/sound.c +++ b/sys/dev/sound/pcm/sound.c @@ -568,6 +568,10 @@ pcm_register(device_t dev, void *devinfo, int numplay, int numrec) d->pvchanformat = 0; d->rvchanrate = 0; d->rvchanformat = 0; + d->p_unr = new_unrhdr(0, INT_MAX, NULL); + d->vp_unr = new_unrhdr(0, INT_MAX, NULL); + d->r_unr = new_unrhdr(0, INT_MAX, NULL); + d->vr_unr = new_unrhdr(0, INT_MAX, NULL); CHN_INIT(d, channels.pcm); CHN_INIT(d, channels.pcm.busy); @@ -653,6 +657,14 @@ pcm_unregister(device_t dev) cv_destroy(&d->cv); PCM_UNLOCK(d); snd_mtxfree(d->lock); + if (d->p_unr != NULL) + delete_unrhdr(d->p_unr); + if (d->vp_unr != NULL) + delete_unrhdr(d->vp_unr); + if (d->r_unr != NULL) + delete_unrhdr(d->r_unr); + if (d->vr_unr != NULL) + delete_unrhdr(d->vr_unr); if (snd_unit == device_get_unit(dev)) { snd_unit = pcm_best_unit(-1); diff --git a/sys/dev/sound/pcm/sound.h b/sys/dev/sound/pcm/sound.h index 5f32dd767fe8..2de6ab29bd66 100644 --- a/sys/dev/sound/pcm/sound.h +++ b/sys/dev/sound/pcm/sound.h @@ -325,6 +325,10 @@ struct snddev_info { struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx; struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree; struct cv cv; + struct unrhdr *p_unr; + struct unrhdr *vp_unr; + struct unrhdr *r_unr; + struct unrhdr *vr_unr; }; void sound_oss_sysinfo(oss_sysinfo *);