Commit a02f7a17 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: seq: ports: Use guard() for locking

We can simplify the code gracefully with new guard() macro and co for
automatic cleanup of locks.

Only the code refactoring, and no functional changes.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240227085306.9764-15-tiwai@suse.de
parent 6768bd10
...@@ -48,17 +48,15 @@ struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client, ...@@ -48,17 +48,15 @@ struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client,
if (client == NULL) if (client == NULL)
return NULL; return NULL;
read_lock(&client->ports_lock); guard(read_lock)(&client->ports_lock);
list_for_each_entry(port, &client->ports_list_head, list) { list_for_each_entry(port, &client->ports_list_head, list) {
if (port->addr.port == num) { if (port->addr.port == num) {
if (port->closing) if (port->closing)
break; /* deleting now */ break; /* deleting now */
snd_use_lock_use(&port->use_lock); snd_use_lock_use(&port->use_lock);
read_unlock(&client->ports_lock);
return port; return port;
} }
} }
read_unlock(&client->ports_lock);
return NULL; /* not found */ return NULL; /* not found */
} }
...@@ -73,7 +71,7 @@ struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *cl ...@@ -73,7 +71,7 @@ struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *cl
num = pinfo->addr.port; num = pinfo->addr.port;
found = NULL; found = NULL;
read_lock(&client->ports_lock); guard(read_lock)(&client->ports_lock);
list_for_each_entry(port, &client->ports_list_head, list) { list_for_each_entry(port, &client->ports_list_head, list) {
if ((port->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) && if ((port->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) &&
!check_inactive) !check_inactive)
...@@ -93,7 +91,6 @@ struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *cl ...@@ -93,7 +91,6 @@ struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *cl
else else
snd_use_lock_use(&found->use_lock); snd_use_lock_use(&found->use_lock);
} }
read_unlock(&client->ports_lock);
return found; return found;
} }
...@@ -145,13 +142,12 @@ int snd_seq_create_port(struct snd_seq_client *client, int port, ...@@ -145,13 +142,12 @@ int snd_seq_create_port(struct snd_seq_client *client, int port,
snd_use_lock_use(&new_port->use_lock); snd_use_lock_use(&new_port->use_lock);
num = max(port, 0); num = max(port, 0);
mutex_lock(&client->ports_mutex); guard(mutex)(&client->ports_mutex);
write_lock_irq(&client->ports_lock); guard(write_lock_irq)(&client->ports_lock);
list_for_each_entry(p, &client->ports_list_head, list) { list_for_each_entry(p, &client->ports_list_head, list) {
if (p->addr.port == port) { if (p->addr.port == port) {
kfree(new_port); kfree(new_port);
num = -EBUSY; return -EBUSY;
goto unlock;
} }
if (p->addr.port > num) if (p->addr.port > num)
break; break;
...@@ -164,9 +160,6 @@ int snd_seq_create_port(struct snd_seq_client *client, int port, ...@@ -164,9 +160,6 @@ int snd_seq_create_port(struct snd_seq_client *client, int port,
new_port->addr.port = num; /* store the port number in the port */ new_port->addr.port = num; /* store the port number in the port */
sprintf(new_port->name, "port-%d", num); sprintf(new_port->name, "port-%d", num);
*port_ret = new_port; *port_ret = new_port;
unlock:
write_unlock_irq(&client->ports_lock);
mutex_unlock(&client->ports_mutex);
return num; return num;
} }
...@@ -281,8 +274,8 @@ int snd_seq_delete_port(struct snd_seq_client *client, int port) ...@@ -281,8 +274,8 @@ int snd_seq_delete_port(struct snd_seq_client *client, int port)
{ {
struct snd_seq_client_port *found = NULL, *p; struct snd_seq_client_port *found = NULL, *p;
mutex_lock(&client->ports_mutex); scoped_guard(mutex, &client->ports_mutex) {
write_lock_irq(&client->ports_lock); guard(write_lock_irq)(&client->ports_lock);
list_for_each_entry(p, &client->ports_list_head, list) { list_for_each_entry(p, &client->ports_list_head, list) {
if (p->addr.port == port) { if (p->addr.port == port) {
/* ok found. delete from the list at first */ /* ok found. delete from the list at first */
...@@ -292,8 +285,7 @@ int snd_seq_delete_port(struct snd_seq_client *client, int port) ...@@ -292,8 +285,7 @@ int snd_seq_delete_port(struct snd_seq_client *client, int port)
break; break;
} }
} }
write_unlock_irq(&client->ports_lock); }
mutex_unlock(&client->ports_mutex);
if (found) if (found)
return port_delete(client, found); return port_delete(client, found);
else else
...@@ -309,16 +301,16 @@ int snd_seq_delete_all_ports(struct snd_seq_client *client) ...@@ -309,16 +301,16 @@ int snd_seq_delete_all_ports(struct snd_seq_client *client)
/* move the port list to deleted_list, and /* move the port list to deleted_list, and
* clear the port list in the client data. * clear the port list in the client data.
*/ */
mutex_lock(&client->ports_mutex); guard(mutex)(&client->ports_mutex);
write_lock_irq(&client->ports_lock); scoped_guard(write_lock_irq, &client->ports_lock) {
if (! list_empty(&client->ports_list_head)) { if (!list_empty(&client->ports_list_head)) {
list_add(&deleted_list, &client->ports_list_head); list_add(&deleted_list, &client->ports_list_head);
list_del_init(&client->ports_list_head); list_del_init(&client->ports_list_head);
} else { } else {
INIT_LIST_HEAD(&deleted_list); INIT_LIST_HEAD(&deleted_list);
} }
client->num_ports = 0; client->num_ports = 0;
write_unlock_irq(&client->ports_lock); }
/* remove each port in deleted_list */ /* remove each port in deleted_list */
list_for_each_entry_safe(port, tmp, &deleted_list, list) { list_for_each_entry_safe(port, tmp, &deleted_list, list) {
...@@ -326,7 +318,6 @@ int snd_seq_delete_all_ports(struct snd_seq_client *client) ...@@ -326,7 +318,6 @@ int snd_seq_delete_all_ports(struct snd_seq_client *client)
snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port);
port_delete(client, port); port_delete(client, port);
} }
mutex_unlock(&client->ports_mutex);
return 0; return 0;
} }
...@@ -506,42 +497,37 @@ static int check_and_subscribe_port(struct snd_seq_client *client, ...@@ -506,42 +497,37 @@ static int check_and_subscribe_port(struct snd_seq_client *client,
int err; int err;
grp = is_src ? &port->c_src : &port->c_dest; grp = is_src ? &port->c_src : &port->c_dest;
err = -EBUSY; guard(rwsem_write)(&grp->list_mutex);
down_write(&grp->list_mutex);
if (exclusive) { if (exclusive) {
if (!list_empty(&grp->list_head)) if (!list_empty(&grp->list_head))
goto __error; return -EBUSY;
} else { } else {
if (grp->exclusive) if (grp->exclusive)
goto __error; return -EBUSY;
/* check whether already exists */ /* check whether already exists */
list_for_each(p, &grp->list_head) { list_for_each(p, &grp->list_head) {
s = get_subscriber(p, is_src); s = get_subscriber(p, is_src);
if (match_subs_info(&subs->info, &s->info)) if (match_subs_info(&subs->info, &s->info))
goto __error; return -EBUSY;
} }
} }
err = subscribe_port(client, port, grp, &subs->info, ack); err = subscribe_port(client, port, grp, &subs->info, ack);
if (err < 0) { if (err < 0) {
grp->exclusive = 0; grp->exclusive = 0;
goto __error; return err;
} }
/* add to list */ /* add to list */
write_lock_irq(&grp->list_lock); guard(write_lock_irq)(&grp->list_lock);
if (is_src) if (is_src)
list_add_tail(&subs->src_list, &grp->list_head); list_add_tail(&subs->src_list, &grp->list_head);
else else
list_add_tail(&subs->dest_list, &grp->list_head); list_add_tail(&subs->dest_list, &grp->list_head);
grp->exclusive = exclusive; grp->exclusive = exclusive;
atomic_inc(&subs->ref_count); atomic_inc(&subs->ref_count);
write_unlock_irq(&grp->list_lock);
err = 0;
__error: return 0;
up_write(&grp->list_mutex);
return err;
} }
/* called with grp->list_mutex held */ /* called with grp->list_mutex held */
...@@ -556,12 +542,12 @@ static void __delete_and_unsubscribe_port(struct snd_seq_client *client, ...@@ -556,12 +542,12 @@ static void __delete_and_unsubscribe_port(struct snd_seq_client *client,
grp = is_src ? &port->c_src : &port->c_dest; grp = is_src ? &port->c_src : &port->c_dest;
list = is_src ? &subs->src_list : &subs->dest_list; list = is_src ? &subs->src_list : &subs->dest_list;
write_lock_irq(&grp->list_lock); scoped_guard(write_lock_irq, &grp->list_lock) {
empty = list_empty(list); empty = list_empty(list);
if (!empty) if (!empty)
list_del_init(list); list_del_init(list);
grp->exclusive = 0; grp->exclusive = 0;
write_unlock_irq(&grp->list_lock); }
if (!empty) if (!empty)
unsubscribe_port(client, port, grp, &subs->info, ack); unsubscribe_port(client, port, grp, &subs->info, ack);
...@@ -575,9 +561,8 @@ static void delete_and_unsubscribe_port(struct snd_seq_client *client, ...@@ -575,9 +561,8 @@ static void delete_and_unsubscribe_port(struct snd_seq_client *client,
struct snd_seq_port_subs_info *grp; struct snd_seq_port_subs_info *grp;
grp = is_src ? &port->c_src : &port->c_dest; grp = is_src ? &port->c_src : &port->c_dest;
down_write(&grp->list_mutex); guard(rwsem_write)(&grp->list_mutex);
__delete_and_unsubscribe_port(client, port, subs, is_src, ack); __delete_and_unsubscribe_port(client, port, subs, is_src, ack);
up_write(&grp->list_mutex);
} }
/* connect two ports */ /* connect two ports */
...@@ -639,7 +624,7 @@ int snd_seq_port_disconnect(struct snd_seq_client *connector, ...@@ -639,7 +624,7 @@ int snd_seq_port_disconnect(struct snd_seq_client *connector,
/* always start from deleting the dest port for avoiding concurrent /* always start from deleting the dest port for avoiding concurrent
* deletions * deletions
*/ */
down_write(&dest->list_mutex); scoped_guard(rwsem_write, &dest->list_mutex) {
/* look for the connection */ /* look for the connection */
list_for_each_entry(subs, &dest->list_head, dest_list) { list_for_each_entry(subs, &dest->list_head, dest_list) {
if (match_subs_info(info, &subs->info)) { if (match_subs_info(info, &subs->info)) {
...@@ -650,7 +635,7 @@ int snd_seq_port_disconnect(struct snd_seq_client *connector, ...@@ -650,7 +635,7 @@ int snd_seq_port_disconnect(struct snd_seq_client *connector,
break; break;
} }
} }
up_write(&dest->list_mutex); }
if (err < 0) if (err < 0)
return err; return err;
...@@ -669,7 +654,7 @@ int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, ...@@ -669,7 +654,7 @@ int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
struct snd_seq_subscribers *s; struct snd_seq_subscribers *s;
int err = -ENOENT; int err = -ENOENT;
down_read(&src_grp->list_mutex); guard(rwsem_read)(&src_grp->list_mutex);
list_for_each_entry(s, &src_grp->list_head, src_list) { list_for_each_entry(s, &src_grp->list_head, src_list) {
if (addr_match(dest_addr, &s->info.dest)) { if (addr_match(dest_addr, &s->info.dest)) {
*subs = s->info; *subs = s->info;
...@@ -677,7 +662,6 @@ int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, ...@@ -677,7 +662,6 @@ int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp,
break; break;
} }
} }
up_read(&src_grp->list_mutex);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment