From: Alexander Grund <theflamefire89@gmail.com>
To: cip-dev@lists.cip-project.org
Cc: uli+cip@fpond.eu
Subject: [PATCH 4.4 3/3] ALSA: control: use counting semaphore as write lock for ELEM_WRITE operation
Date: Fri, 24 Mar 2023 18:18:12 +0100 [thread overview]
Message-ID: <20230324171812.221086-4-theflamefire89@gmail.com> (raw)
In-Reply-To: <20230324171812.221086-1-theflamefire89@gmail.com>
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
In ALSA control interface, applications can execute two types of request
for value of members on each element; ELEM_READ and ELEM_WRITE. In ALSA
control core, these two requests are handled within read lock of a
counting semaphore, therefore several processes can run to execute these
two requests at the same time. This has an issue because ELEM_WRITE
requests have an effect to change state of the target element. Concurrent
access should be controlled for each of ELEM_READ/ELEM_WRITE case.
This commit uses the counting semaphore as write lock for ELEM_WRITE
requests, while use it as read lock for ELEM_READ requests. The state of
a target element is maintained exclusively between ELEM_WRITE/ELEM_READ
operations.
There's a concern. If the counting semaphore is acquired for read lock
in implementations of 'struct snd_kcontrol.put()' in each driver, this
commit shall cause dead lock. As of v4.13-rc5, 'snd-mixer-oss.ko',
'snd-emu10k1.ko' and 'snd-soc-sst-atom-hifi2-platform.ko' includes codes
for read locks, but these are not in a call graph from
'struct snd_kcontrol.put(). Therefore, this commit is safe.
In current implementation, the same solution is applied for the other
operations to element; e.g. ELEM_LOCK and ELEM_UNLOCK. There's another
discussion about an overhead to maintain concurrent access to an element
during operating the other elements on the same card instance, because the
lock primitive is originally implemented to maintain a list of elements on
the card instance. There's a substantial difference between
per-element-list lock and per-element lock.
Here, let me investigate another idea to add per-element lock to maintain
the concurrent accesses with inquiry/change requests to an element. It's
not so frequent for applications to operate members on elements, while
adding a new lock primitive to structure increases memory footprint for
all of element sets somehow. Experimentally, inquiry operation is more
frequent than change operation and usage of counting semaphore for the
inquiry operation brings no blocking to the other inquiry operations. Thus
the overhead is not so critical for usual applications. For the above
reasons, in this commit, the per-element lock is not introduced.
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Alexander Grund <theflamefire89@gmail.com>
---
sound/core/control.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/sound/core/control.c b/sound/core/control.c
index 3ca81e85a1492..04b939df3768b 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -963,9 +963,9 @@ static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
snd_power_lock(card);
result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
if (result >= 0) {
- down_read(&card->controls_rwsem);
+ down_write(&card->controls_rwsem);
result = snd_ctl_elem_write(card, file, control);
- up_read(&card->controls_rwsem);
+ up_write(&card->controls_rwsem);
}
snd_power_unlock(card);
if (result >= 0)
--
2.40.0
next prev parent reply other threads:[~2023-03-24 17:18 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-24 17:18 [PATCH 4.4 0/3] ALSA: control: Fix locking around snd_ctl_elem_read/write Alexander Grund
2023-03-24 17:18 ` [PATCH 4.4 1/3] ALSA: control: code refactoring for ELEM_READ/ELEM_WRITE operations Alexander Grund
2023-03-29 22:29 ` [cip-dev] " nobuhiro1.iwamatsu
2023-03-31 15:45 ` Alexander Grund
2023-03-24 17:18 ` [PATCH 4.4 2/3] ALSA: control: Fix memory corruption risk in snd_ctl_elem_read Alexander Grund
2023-03-29 22:33 ` [cip-dev] " nobuhiro1.iwamatsu
2023-03-31 15:52 ` Alexander Grund
2023-03-24 17:18 ` Alexander Grund [this message]
2023-03-29 22:39 ` [cip-dev] [PATCH 4.4 3/3] ALSA: control: use counting semaphore as write lock for ELEM_WRITE operation nobuhiro1.iwamatsu
2023-03-31 15:51 ` Alexander Grund
2023-04-03 8:56 ` [cip-dev] " Ulrich Hecht
2023-04-03 17:13 ` Alexander Grund
2023-04-02 16:30 ` [cip-dev] [PATCH 4.4 0/3] ALSA: control: Fix locking around snd_ctl_elem_read/write Pavel Machek
2023-04-02 18:34 ` Alexander Grund
2023-04-03 8:56 ` [cip-dev] " Ulrich Hecht
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=20230324171812.221086-4-theflamefire89@gmail.com \
--to=theflamefire89@gmail.com \
--cc=cip-dev@lists.cip-project.org \
--cc=uli+cip@fpond.eu \
/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