Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp14491579pxu; Tue, 5 Jan 2021 03:05:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzh5a7fgsWbFxapl1HiL/nlrMGFd08Tn+A0s86EfAwHm8g1kGSmWzJebBVrOw3A5baKjpjp X-Received: by 2002:a05:6402:312b:: with SMTP id dd11mr76325179edb.308.1609844739755; Tue, 05 Jan 2021 03:05:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609844739; cv=none; d=google.com; s=arc-20160816; b=X5ES6KGn7HJBzrMIwCmd6Sioj44TOpCF17XSga9xqFJXz2sWTkUxOQCzQlQ4iiSXix u5gJikl/W/pLUUv64WV7eBUGMiOUIJhZ7uXjOzLtG7iMnIGdKb+Pq2DU374IeTm2iwk8 TXVx3sNDczPomq+H/yOKOIwNqm4BWHRcQxGUzutzuKYk9zNjgEsE5EYNSFwven7I46qt zwSyC0I20qNHgA9SCWyerIgKG5b1iBUFbx+7Ce66BzeRiQvP7nq/4yeR0G6VSey1SJWQ 3IwyYxkfyiwiSgpOKMxgoJ5I0aLwiycT/GjFWsII177BeAc3heGEwu3UC6U97zQ+7h9O pYvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=xjHY5J/WYul7+xwIEcWyaKX9q1gd+fu132oSR5IfpOM=; b=StYeOQoL9Iy6HkoksdrqDPqm6oZ9s317qVYWcC3ECpI/4btUknN8nF2KJ6QQGWhesp /jJwzQG7jP4PiPs4SzBUc6wFaP+rOpm743OaukGeamyYpQyMNbnohmtOcUXMFSbHbC81 KBBMp+i16eLMXxRjjK6MPE+o8mEZD16fM9MW9FC2GE3bBSLiD7Y0Oml8l1oKSM2ou1Pd gEu52B2xNNnO/6JFgMfGLeuw2qEAIjtn/+BrlG8q1EXiS+6UnU52pJqI1WX7QDLaF3eQ dLaioUvg+8kfgY9u7gWKICt7gSIbZEG4NAVobnLu12hytTtxz2OQ5xqFigNmRy3fJVnj nchA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=dY8aNAIi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lu18si29920156ejb.272.2021.01.05.03.05.16; Tue, 05 Jan 2021 03:05:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=dY8aNAIi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729484AbhAELDa (ORCPT + 99 others); Tue, 5 Jan 2021 06:03:30 -0500 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:14777 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729463AbhAELDR (ORCPT ); Tue, 5 Jan 2021 06:03:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1609844597; x=1641380597; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=xjHY5J/WYul7+xwIEcWyaKX9q1gd+fu132oSR5IfpOM=; b=dY8aNAIiKQ/GPkOUIqLgkW+VvjU00/9Ck68zUvVxc/49mdPVCqhsKv6M 38JB0qiawTBnlerveAhQri+LW5Z8suhZ1zk6DOevweflZh71BVsDRtK/x 7J9wQx8KgT8Mgw8i36XbqcSzVGqdYeNkFBVquzENVVoYuvRtn10mtsudf w=; X-IronPort-AV: E=Sophos;i="5.78,476,1599523200"; d="scan'208";a="101273214" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2c-397e131e.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 05 Jan 2021 11:02:30 +0000 Received: from EX13D31EUA001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2c-397e131e.us-west-2.amazon.com (Postfix) with ESMTPS id 510AEA2111; Tue, 5 Jan 2021 11:02:28 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.162.94) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 5 Jan 2021 11:02:22 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , Subject: [PATCH 4/5] xen/xenbus: Count pending messages for each watch Date: Tue, 5 Jan 2021 12:01:41 +0100 Message-ID: <20210105110142.1810-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210105110142.1810-1-sjpark@amazon.com> References: <20210105110142.1810-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.94] X-ClientProxiedBy: EX13D45UWA001.ant.amazon.com (10.43.160.91) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: SeongJae Park 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 This is upstream commit 3dc86ca6b4c8cfcba9da7996189d1b5a358a94fc Cc: stable@vger.kernel.org Signed-off-by: SeongJae Park Reported-by: Michael Kurth Reported-by: Pawel Wieczorkiewicz Reviewed-by: Juergen Gross Signed-off-by: Juergen Gross --- drivers/xen/xenbus/xenbus_xs.c | 31 +++++++++++++++++++------------ include/xen/xenbus.h | 2 ++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/drivers/xen/xenbus/xenbus_xs.c b/drivers/xen/xenbus/xenbus_xs.c index d200aa707988..88b443637106 100644 --- a/drivers/xen/xenbus/xenbus_xs.c +++ b/drivers/xen/xenbus/xenbus_xs.c @@ -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); @@ -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); @@ -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 (;;) { @@ -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, @@ -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 { diff --git a/include/xen/xenbus.h b/include/xen/xenbus.h index 1772507dc2c9..ed9e7e3307b7 100644 --- a/include/xen/xenbus.h +++ b/include/xen/xenbus.h @@ -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. -- 2.17.1