Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
xen/xenbus: Count pending messages for each watch
Browse files Browse the repository at this point in the history
commit 3dc86ca6b4c8cfcba9da7996189d1b5a358a94fc upstream.

This commit adds a counter of pending messages for each watch in the
struct.  It is used to skip unnecessary pending messages lookup in
'unregister_xenbus_watch()'.  It could also be used in 'will_handle'
callback.

This is part of XSA-349

Cc: stable@vger.kernel.org
Signed-off-by: SeongJae Park <sjpark@amazon.de>
Reported-by: Michael Kurth <mku@amazon.de>
Reported-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
Reviewed-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
sj-aws authored and gregkh committed Jan 9, 2021
1 parent 1b46811 commit c78b439
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 19 additions & 12 deletions drivers/xen/xenbus/xenbus_xs.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ int register_xenbus_watch(struct xenbus_watch *watch)

sprintf(token, "%lX", (long)watch);

watch->nr_pending = 0;

down_read(&xs_state.watch_mutex);

spin_lock(&watches_lock);
Expand Down Expand Up @@ -748,12 +750,15 @@ void unregister_xenbus_watch(struct xenbus_watch *watch)

/* Cancel pending watch events. */
spin_lock(&watch_events_lock);
list_for_each_entry_safe(msg, tmp, &watch_events, list) {
if (msg->u.watch.handle != watch)
continue;
list_del(&msg->list);
kfree(msg->u.watch.vec);
kfree(msg);
if (watch->nr_pending) {
list_for_each_entry_safe(msg, tmp, &watch_events, list) {
if (msg->u.watch.handle != watch)
continue;
list_del(&msg->list);
kfree(msg->u.watch.vec);
kfree(msg);
}
watch->nr_pending = 0;
}
spin_unlock(&watch_events_lock);

Expand Down Expand Up @@ -800,7 +805,6 @@ void xs_suspend_cancel(void)

static int xenwatch_thread(void *unused)
{
struct list_head *ent;
struct xs_stored_msg *msg;

for (;;) {
Expand All @@ -813,13 +817,15 @@ static int xenwatch_thread(void *unused)
mutex_lock(&xenwatch_mutex);

spin_lock(&watch_events_lock);
ent = watch_events.next;
if (ent != &watch_events)
list_del(ent);
msg = list_first_entry_or_null(&watch_events,
struct xs_stored_msg, list);
if (msg) {
list_del(&msg->list);
msg->u.watch.handle->nr_pending--;
}
spin_unlock(&watch_events_lock);

if (ent != &watch_events) {
msg = list_entry(ent, struct xs_stored_msg, list);
if (msg) {
msg->u.watch.handle->callback(
msg->u.watch.handle,
(const char **)msg->u.watch.vec,
Expand Down Expand Up @@ -909,6 +915,7 @@ static int process_msg(void)
msg->u.watch.vec_size))) {
spin_lock(&watch_events_lock);
list_add_tail(&msg->list, &watch_events);
msg->u.watch.handle->nr_pending++;
wake_up(&watch_events_waitq);
spin_unlock(&watch_events_lock);
} else {
Expand Down
2 changes: 2 additions & 0 deletions include/xen/xenbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct xenbus_watch
/* Path being watched. */
const char *node;

unsigned int nr_pending;

/*
* Called just before enqueing new event while a spinlock is held.
* The event will be discarded if this callback returns false.
Expand Down

0 comments on commit c78b439

Please sign in to comment.