From patchwork Sat Dec 11 00:02:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 523377 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2DD7C433F5 for ; Sat, 11 Dec 2021 00:02:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243597AbhLKAGG (ORCPT ); Fri, 10 Dec 2021 19:06:06 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:44858 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230472AbhLKAGG (ORCPT ); Fri, 10 Dec 2021 19:06:06 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 933EAB82A48; Sat, 11 Dec 2021 00:02:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29209C341C6; Sat, 11 Dec 2021 00:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639180948; bh=NG/2rpNIoOuXW0kEaBE8DZNMg4kHewm20HOlMHfvuCs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oXUoubL5+ktTr5WJspzo2i70HQmdYm9ejp2eqeX+KJ2MDmPMXAmDzfZoL6Ub66GjZ Sn0ZmcDsT2UwQgJ8+9ap/qUP8cWziT7Crd81zM+6KPyvY4tb1oWINO6WPxmB9qMzS7 d/POfQOpRD5+SEa3aWL6zNmFOdc6sLi+GMHVe98BgCkesNcxf+WjK74SrLS+DRvEJP x3usIa8cklFpQX0hl51mndVBublrYLPffx5a8kDw+6VL7A9Zon9FeSsOXIK7K8gcUx Cx8+iplHT2TrhPczujxrvEZlkmaqpN3rRTp4Auofv2Tx99RbTCoAqBbdA9p0pfw0I0 Ks8m7S42aGYCQ== From: Eric Biggers To: stable@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds Subject: [PATCH 4.14 1/3] wait: add wake_up_pollfree() Date: Fri, 10 Dec 2021 16:02:21 -0800 Message-Id: <20211211000223.50630-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20211211000223.50630-1-ebiggers@kernel.org> References: <20211211000223.50630-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Eric Biggers commit 42288cb44c4b5fff7653bc392b583a2b8bd6a8c0 upstream. Several ->poll() implementations are special in that they use a waitqueue whose lifetime is the current task, rather than the struct file as is normally the case. This is okay for blocking polls, since a blocking poll occurs within one task; however, non-blocking polls require another solution. This solution is for the queue to be cleared before it is freed, using 'wake_up_poll(wq, EPOLLHUP | POLLFREE);'. However, that has a bug: wake_up_poll() calls __wake_up() with nr_exclusive=1. Therefore, if there are multiple "exclusive" waiters, and the wakeup function for the first one returns a positive value, only that one will be called. That's *not* what's needed for POLLFREE; POLLFREE is special in that it really needs to wake up everyone. Considering the three non-blocking poll systems: - io_uring poll doesn't handle POLLFREE at all, so it is broken anyway. - aio poll is unaffected, since it doesn't support exclusive waits. However, that's fragile, as someone could add this feature later. - epoll doesn't appear to be broken by this, since its wakeup function returns 0 when it sees POLLFREE. But this is fragile. Although there is a workaround (see epoll), it's better to define a function which always sends POLLFREE to all waiters. Add such a function. Also make it verify that the queue really becomes empty after all waiters have been woken up. Reported-by: Linus Torvalds Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20211209010455.42744-2-ebiggers@kernel.org Signed-off-by: Eric Biggers --- include/linux/wait.h | 26 ++++++++++++++++++++++++++ kernel/sched/wait.c | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/wait.h b/include/linux/wait.h index 158715445ffb6..42e648083610e 100644 --- a/include/linux/wait.h +++ b/include/linux/wait.h @@ -191,6 +191,7 @@ void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head, void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key); void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr); void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr); +void __wake_up_pollfree(struct wait_queue_head *wq_head); #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) @@ -215,6 +216,31 @@ void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr); #define wake_up_interruptible_sync_poll(x, m) \ __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m)) +/** + * wake_up_pollfree - signal that a polled waitqueue is going away + * @wq_head: the wait queue head + * + * In the very rare cases where a ->poll() implementation uses a waitqueue whose + * lifetime is tied to a task rather than to the 'struct file' being polled, + * this function must be called before the waitqueue is freed so that + * non-blocking polls (e.g. epoll) are notified that the queue is going away. + * + * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via + * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU. + */ +static inline void wake_up_pollfree(struct wait_queue_head *wq_head) +{ + /* + * For performance reasons, we don't always take the queue lock here. + * Therefore, we might race with someone removing the last entry from + * the queue, and proceed while they still hold the queue lock. + * However, rcu_read_lock() is required to be held in such cases, so we + * can safely proceed with an RCU-delayed free. + */ + if (waitqueue_active(wq_head)) + __wake_up_pollfree(wq_head); +} + #define ___wait_cond_timeout(condition) \ ({ \ bool __cond = (condition); \ diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index e296084643821..82f4c8f216f8a 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -213,6 +213,13 @@ void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr_e } EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ +void __wake_up_pollfree(struct wait_queue_head *wq_head) +{ + __wake_up(wq_head, TASK_NORMAL, 0, poll_to_key(POLLHUP | POLLFREE)); + /* POLLFREE must have cleared the queue. */ + WARN_ON_ONCE(waitqueue_active(wq_head)); +} + /* * Note: we use "set_current_state()" _after_ the wait-queue add, * because we need a memory barrier there on SMP, so that any From patchwork Sat Dec 11 00:02:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 523051 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C496CC433F5 for ; Sat, 11 Dec 2021 00:02:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244162AbhLKAGI (ORCPT ); Fri, 10 Dec 2021 19:06:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244140AbhLKAGH (ORCPT ); Fri, 10 Dec 2021 19:06:07 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 125E3C061746; Fri, 10 Dec 2021 16:02:32 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 5528DCE2DE5; Sat, 11 Dec 2021 00:02:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6912DC341CA; Sat, 11 Dec 2021 00:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639180948; bh=WXeVC6e7Tyth97oaL6C9JBLCiD3VQxmIBxJsYErOJNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tyBYlRaF23L0PcqZFdgQxIIQkm0oNe5Wr05H53fNwe/Fb2VN2f/I1FiCnMD76f4S2 V9WEQ4+7E2KKq9jUJ6abfKRAkTXPTckUtbY3ONkkyE6AIj16abv5xMLRE1EK7Tlp+1 lWAWCEjN4zf1SOmQ7pIplcULO1tBsmhy/UQeI727bdowmu5dpiGQlu1fd25edp3iFp PtyKQ52/w7a7+yOztFHgDuSGOX5jhzVnUPZrxFqvGaNWn3RW8uvr4+gVe4CP6DQUqM rLgILTwMa1vEWVp8P6NdR9ePIwxK/+t3nusOJpO420TRK6EypriWqZheCBw0KdRmt7 x+IelgdqMAeow== From: Eric Biggers To: stable@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds Subject: [PATCH 4.14 2/3] binder: use wake_up_pollfree() Date: Fri, 10 Dec 2021 16:02:22 -0800 Message-Id: <20211211000223.50630-3-ebiggers@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20211211000223.50630-1-ebiggers@kernel.org> References: <20211211000223.50630-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Eric Biggers commit a880b28a71e39013e357fd3adccd1d8a31bc69a8 upstream. wake_up_poll() uses nr_exclusive=1, so it's not guaranteed to wake up all exclusive waiters. Yet, POLLFREE *must* wake up all waiters. epoll and aio poll are fortunately not affected by this, but it's very fragile. Thus, the new function wake_up_pollfree() has been introduced. Convert binder to use wake_up_pollfree(). Reported-by: Linus Torvalds Fixes: f5cb779ba163 ("ANDROID: binder: remove waitqueue when thread exits.") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20211209010455.42744-3-ebiggers@kernel.org Signed-off-by: Eric Biggers --- drivers/android/binder.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 63bf8a7d477ba..2412e219b7c3a 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -4336,23 +4336,20 @@ static int binder_thread_release(struct binder_proc *proc, } /* - * If this thread used poll, make sure we remove the waitqueue - * from any epoll data structures holding it with POLLFREE. - * waitqueue_active() is safe to use here because we're holding - * the inner lock. + * If this thread used poll, make sure we remove the waitqueue from any + * poll data structures holding it. */ - if ((thread->looper & BINDER_LOOPER_STATE_POLL) && - waitqueue_active(&thread->wait)) { - wake_up_poll(&thread->wait, POLLHUP | POLLFREE); - } + if (thread->looper & BINDER_LOOPER_STATE_POLL) + wake_up_pollfree(&thread->wait); binder_inner_proc_unlock(thread->proc); /* - * This is needed to avoid races between wake_up_poll() above and - * and ep_remove_waitqueue() called for other reasons (eg the epoll file - * descriptor being closed); ep_remove_waitqueue() holds an RCU read - * lock, so we can be sure it's done after calling synchronize_rcu(). + * This is needed to avoid races between wake_up_pollfree() above and + * someone else removing the last entry from the queue for other reasons + * (e.g. ep_remove_wait_queue() being called due to an epoll file + * descriptor being closed). Such other users hold an RCU read lock, so + * we can be sure they're done after we call synchronize_rcu(). */ if (thread->looper & BINDER_LOOPER_STATE_POLL) synchronize_rcu(); From patchwork Sat Dec 11 00:02:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 523050 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44CA5C4332F for ; Sat, 11 Dec 2021 00:02:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244140AbhLKAGK (ORCPT ); Fri, 10 Dec 2021 19:06:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244143AbhLKAGI (ORCPT ); Fri, 10 Dec 2021 19:06:08 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5232AC0617A1; Fri, 10 Dec 2021 16:02:32 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 971BECE2DE4; Sat, 11 Dec 2021 00:02:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9413C341C8; Sat, 11 Dec 2021 00:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639180948; bh=g2fpJY8Ik0eUgfvQC3WA/DMV+Xj4NaJeRayahqJ4rYQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c3cb7o5GcFRBiCPeKWbyHQmT4gsFpBNYza6+azVVyUG0j9s/y53/L4dHLsnNftiII uqoUUNYOel0wclD1QoQj2ewPd5pEiTfmnnXg2V+1C2hJ0sKEqvznQ9Yy3lfWZxtqwR FWF6O8NiPy+XLWPtPMn/ymDsKbnLSeOVoc/XgJW2yIxaEHTvhwdIWF7NK6J25j0sN4 xLuIIP0l1qoGgoxIq4mIBbYa7sW48rPPgjZ0Xcyuo/Zp3+Ojy7NRTmBg2Y8cwQnzlq z1cLWhjmeMCP+ZQVMHI2KMGlSf5E6ZEjIoDUnYy0wNntExEkby8ljGd1LNzxB75B/1 5xR4fd1pX8R3Q== From: Eric Biggers To: stable@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds Subject: [PATCH 4.14 3/3] signalfd: use wake_up_pollfree() Date: Fri, 10 Dec 2021 16:02:23 -0800 Message-Id: <20211211000223.50630-4-ebiggers@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20211211000223.50630-1-ebiggers@kernel.org> References: <20211211000223.50630-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Eric Biggers commit 9537bae0da1f8d1e2361ab6d0479e8af7824e160 upstream. wake_up_poll() uses nr_exclusive=1, so it's not guaranteed to wake up all exclusive waiters. Yet, POLLFREE *must* wake up all waiters. epoll and aio poll are fortunately not affected by this, but it's very fragile. Thus, the new function wake_up_pollfree() has been introduced. Convert signalfd to use wake_up_pollfree(). Reported-by: Linus Torvalds Fixes: d80e731ecab4 ("epoll: introduce POLLFREE to flush ->signalfd_wqh before kfree()") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20211209010455.42744-4-ebiggers@kernel.org Signed-off-by: Eric Biggers --- fs/signalfd.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/fs/signalfd.c b/fs/signalfd.c index 1c667af86da52..0b7c6c2c95b89 100644 --- a/fs/signalfd.c +++ b/fs/signalfd.c @@ -35,17 +35,7 @@ void signalfd_cleanup(struct sighand_struct *sighand) { - wait_queue_head_t *wqh = &sighand->signalfd_wqh; - /* - * The lockless check can race with remove_wait_queue() in progress, - * but in this case its caller should run under rcu_read_lock() and - * sighand_cachep is SLAB_TYPESAFE_BY_RCU, we can safely return. - */ - if (likely(!waitqueue_active(wqh))) - return; - - /* wait_queue_entry_t->func(POLLFREE) should do remove_wait_queue() */ - wake_up_poll(wqh, POLLHUP | POLLFREE); + wake_up_pollfree(&sighand->signalfd_wqh); } struct signalfd_ctx {