From nobody Sun Oct 20 11:21:32 2024 X-Original-To: dev-commits-src-all@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 4XWbcF3w1vz5Yvmx; Sun, 20 Oct 2024 11:21:33 +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 4XWbcF0vYsz4Wdn; Sun, 20 Oct 2024 11:21:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729423293; 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=B530ySfvqRwTpmVYhVGrfyCuFH81G8wmmM6kxiA65dA=; b=C04QuBbts7huYcyBBAy5/wiVbJ7TC4pwtPuXjG6kLyJUcDHQU75MULOro7OEqMWsFNoYsv Kd9o3YyWNjDGa/nwGDLdKv0zYxLZ6bz2uEfm4DdDFDI2DbGqtxDV9WfHWvwu/tOiu2S0tG MVpI5jsujeQIWfd/gEpKVam28h4Yx5Xd2LM6tIgccTUn3oaqwAFk1dcaTZgLRXaz56wD+s +R79Ac0htejwkKH8XmMuu4x9tyG+JOW5jvKdQclpCfhHdl8snDqyxgclOWHORhvQTI+3Uw Ql2GfeBoFwTHHDVkiYzak8/lyCdcq1BdIFg1H2RGbv8e4wf3M0RwSvPfzhROsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729423293; 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=B530ySfvqRwTpmVYhVGrfyCuFH81G8wmmM6kxiA65dA=; b=xTE38U6kfwV56XQjf83fGSualtDWBToYXhp0Zez4CV3KfZgKc5D7+PIn6E/qPQQgM2dnmJ fAe6erQk/IQ496f9WCiG8LGtV3hkZWYRWJnkmuPkGpyR6TnPkGkhT1wIYCwQn3utpfYhBL V30GI3bXF6n8wkXPaMYEU0fZChCHHJEHoxFI1yM2NwGFAUPuck6nf6NUnzH6BZ0MsnjOfw q4uJmDefyVPZjzLAaAqPmaacAuNzBMFz2A17RawTh2hyBIfhUKLPuI1BhphyDFt6F5Jksr Zuc6DKS+JMDL4pseOxP4wOwlQ7DTWnSwb/zzTG5qdabMnPVqWie5kd2PExWA2g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729423293; a=rsa-sha256; cv=none; b=U27T0cw7LBcI2R+8dF4YZY3FdNjnB6SXrCshh/hU89H5AE/Z/2yw5BonRq2ARetmqACh+o v/fgYD9rQvCpM5B3oxzsFBILxn0K9uLfL+E43kWl1LI+HnHD9ec+kO3m8JG1oNnkXnb/Oo vPkxB+Q1YYCI6Bdg2ODN9yNdZgKThzI/SisUw3SPL6dbzGQUaRy5D/TCnq8PCAJMtN3KDw y161ZjtfwKAw0PntMrJ4xO7zMiCvDfk2BC35fqJJvgfP/qYOYJ7l/g6PV+vD0wA9KTR2so OhkWJhDUeyNw8yzN52cmk1lAj+aYugqVif16qcdZF2GoyTnx1v1OKzcPi6Ck9w== 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 4XWbcF08T8z10Vl; Sun, 20 Oct 2024 11:21:33 +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 49KBLWIe062621; Sun, 20 Oct 2024 11:21:32 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49KBLWUO062617; Sun, 20 Oct 2024 11:21:32 GMT (envelope-from git) Date: Sun, 20 Oct 2024 11:21:32 GMT Message-Id: <202410201121.49KBLWUO062617@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Christos Margiolis Subject: git: 463eafcff98f - stable/14 - sound: Use unr(9) to produce unique channel unit numbers List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 463eafcff98f8036cef0e2f7e00ecd0a8e5437d4 Auto-Submitted: auto-generated The branch stable/14 has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=463eafcff98f8036cef0e2f7e00ecd0a8e5437d4 commit 463eafcff98f8036cef0e2f7e00ecd0a8e5437d4 Author: Christos Margiolis AuthorDate: 2024-10-18 08:39:08 +0000 Commit: Christos Margiolis CommitDate: 2024-10-20 11:21:04 +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 (cherry picked from commit ad4c8671bddaa30bf0413089c74dde6a618d9021) --- 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 *);