Received: by 2002:a17:90a:bc8d:0:0:0:0 with SMTP id x13csp1573324pjr; Mon, 18 May 2020 16:46:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyy+EFvC6HgBZOPAPNN/Kft6r320DMU07nkAB/12N1M+I1DoZLiIHnwg37PiBqUM8C/SQ9A X-Received: by 2002:a17:906:b08c:: with SMTP id x12mr15418974ejy.154.1589845579606; Mon, 18 May 2020 16:46:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589845579; cv=none; d=google.com; s=arc-20160816; b=l/3QoNr6QWx6w7M66wYwAzc9ZpspdtuVfHY5aWHOxlhQc9C08wm55JMjvi1kKV/lZy lYZk3eGkb1jLULdZ236oNi0CPi1OPt+nVNJTC6qweu5Da5785xzWLoDqtvNkFh/psw7B +MFsTePQzmzDVqPcK3UqmoMZ128V0OKAw8stzkk1DxjVkRBnj5H9m7U6VmfNMEOczBto cFsjkUKLb2N8lA2z9HHPP7VFPKHMCupcSz0FfEUdGqzfUNjBTGyC1UnSkKCiMuToaUhD gCXWEhOB0FBx7RHhxcB+vRbCUWeWOlt50djYtUZjM1nT0YXzPcwb8I/ytyiVd9I3be5m 3TpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=lknc8YocEIui89qyIucSrOGJ+ElgPWnEcwlidaEA3/E=; b=KK0zoJfKmjFvVGUTr632sTpaYHXCZ60nvV0xvAdtwY0wtoOCMhzuP8G+8dIGoxm/Jm TK+3rcqz30mp3/ZL5tf/mmkdmMFwIJJxYR9TDi6DsXqtBWs4jwKQnQZJy16tx9pZCpkD cMQKyswIbxZmP+bUBvrVBefPZe8+iumDBafzL0Zq1ATSLsWBoTxtWHGx90yeimJtepWa +F6/bb2fBFlbFdfLy8WQzuS4fyzjbJzGLPy5kdRw6HhLedH1KsIySTEmNU2evV54fZd4 j7TeLPQIvAodmDCoId/l+WZTGTY9aOXw/TgGllfZR/i4YyORq8dwWu3fcqgKc0e8OJNJ mYeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xGfd1mBI; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d21si7749250eja.353.2020.05.18.16.45.56; Mon, 18 May 2020 16:46:19 -0700 (PDT) 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=@kernel.org header.s=default header.b=xGfd1mBI; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732897AbgERSFI (ORCPT + 99 others); Mon, 18 May 2020 14:05:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:52342 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732400AbgERSE7 (ORCPT ); Mon, 18 May 2020 14:04:59 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 85439207D3; Mon, 18 May 2020 18:04:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589825098; bh=D5osR2tczcqEOpxUtxmuYKya3s60kgS5IqxTYFNR+dg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xGfd1mBIPClM/YrrDlPWNiAhU/V8acE2k6PBa0y0L0671XmJFceVawkTfp5xPf/xY 2Dl0c+jYPBPy7nmOoq9vkufYCYQsKaOiPo16V5TYcugHxbxntmCH7XWfN64ws8gjMe H2y/WV/1D4+X+8rEksho0g/OPywJVnYypJ1i16vM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jason Baron , Randy Dunlap , Roman Penyaev , Andrew Morton , Khazhismel Kumykov , Alexander Viro , Linus Torvalds , Sasha Levin Subject: [PATCH 5.6 121/194] epoll: call final ep_events_available() check under the lock Date: Mon, 18 May 2020 19:36:51 +0200 Message-Id: <20200518173541.688584011@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518173531.455604187@linuxfoundation.org> References: <20200518173531.455604187@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Roman Penyaev [ Upstream commit 65759097d804d2a9ad2b687db436319704ba7019 ] There is a possible race when ep_scan_ready_list() leaves ->rdllist and ->obflist empty for a short period of time although some events are pending. It is quite likely that ep_events_available() observes empty lists and goes to sleep. Since commit 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of nested epoll") we are conservative in wakeups (there is only one place for wakeup and this is ep_poll_callback()), thus ep_events_available() must always observe correct state of two lists. The easiest and correct way is to do the final check under the lock. This does not impact the performance, since lock is taken anyway for adding a wait entry to the wait queue. The discussion of the problem can be found here: https://lore.kernel.org/linux-fsdevel/a2f22c3c-c25a-4bda-8339-a7bdaf17849e@akamai.com/ In this patch barrierless __set_current_state() is used. This is safe since waitqueue_active() is called under the same lock on wakeup side. Short-circuit for fatal signals (i.e. fatal_signal_pending() check) is moved to the line just before actual events harvesting routine. This is fully compliant to what is said in the comment of the patch where the actual fatal_signal_pending() check was added: c257a340ede0 ("fs, epoll: short circuit fetching events if thread has been killed"). Fixes: 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of nested epoll") Reported-by: Jason Baron Reported-by: Randy Dunlap Signed-off-by: Roman Penyaev Signed-off-by: Andrew Morton Reviewed-by: Jason Baron Cc: Khazhismel Kumykov Cc: Alexander Viro Cc: Link: http://lkml.kernel.org/r/20200505145609.1865152-1-rpenyaev@suse.de Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/eventpoll.c | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index b0a097274cfeb..f5a481089893a 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -1857,34 +1857,33 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, * event delivery. */ init_wait(&wait); - write_lock_irq(&ep->lock); - __add_wait_queue_exclusive(&ep->wq, &wait); - write_unlock_irq(&ep->lock); + write_lock_irq(&ep->lock); /* - * We don't want to sleep if the ep_poll_callback() sends us - * a wakeup in between. That's why we set the task state - * to TASK_INTERRUPTIBLE before doing the checks. + * Barrierless variant, waitqueue_active() is called under + * the same lock on wakeup ep_poll_callback() side, so it + * is safe to avoid an explicit barrier. */ - set_current_state(TASK_INTERRUPTIBLE); + __set_current_state(TASK_INTERRUPTIBLE); + /* - * Always short-circuit for fatal signals to allow - * threads to make a timely exit without the chance of - * finding more events available and fetching - * repeatedly. + * Do the final check under the lock. ep_scan_ready_list() + * plays with two lists (->rdllist and ->ovflist) and there + * is always a race when both lists are empty for short + * period of time although events are pending, so lock is + * important. */ - if (fatal_signal_pending(current)) { - res = -EINTR; - break; + eavail = ep_events_available(ep); + if (!eavail) { + if (signal_pending(current)) + res = -EINTR; + else + __add_wait_queue_exclusive(&ep->wq, &wait); } + write_unlock_irq(&ep->lock); - eavail = ep_events_available(ep); - if (eavail) - break; - if (signal_pending(current)) { - res = -EINTR; + if (eavail || res) break; - } if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) { timed_out = 1; @@ -1905,6 +1904,15 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, } send_events: + if (fatal_signal_pending(current)) { + /* + * Always short-circuit for fatal signals to allow + * threads to make a timely exit without the chance of + * finding more events available and fetching + * repeatedly. + */ + res = -EINTR; + } /* * Try to transfer events to user space. In case we get 0 events and * there's still timeout left over, we go trying again in search of -- 2.20.1