From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EAC03CAE7D; Sun, 3 May 2026 13:24:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777814666; cv=none; b=Tj69GvaeMFw8c9Adb/v7nL22PyBF1xa2HeOKgRpBNJHocFJ/aeckmrAR+tHki1OfE4ATV5rGk9tRxAzmwGo10C09iz8zrm4YLMuEISYqdfcRDnteljVNO3H/x2kqa2tuYPvmkAvxbfbg/7PMKkGwuBWc0YJbhlt7SvZCKH9PATY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777814666; c=relaxed/simple; bh=hXJkb9zeGBnqy8Bqmj5jsUWVyNOcIs9SNKVV5AIb5VM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=BUm/N/20PRUCusxN37XAIkBeoLJgCfrpuW8GSjni/EuCsHhT5RwGYIBFJHYrr6DQHKqkWf9BQKP7S7fRxlvab2wxe27RK0CgcHxVHzmyFFkj1Un3AcLsS9BgJpRwViISCetRcpIwnGzsTOXcvXdaAhnGjmK7V15ldvr9wyycv8o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Vp9qxu1m; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JMnckAOp; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Vp9qxu1m"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JMnckAOp" From: Nam Cao DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1777814656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Tz8my8xNlqK7bwfmWQRlThnku6PbexVIGoppmPblUuk=; b=Vp9qxu1mT/Oh2DbsI8QMvVXYSiRlU6u4bjDfoV/yTMq8J3WS2mAHx1tWP4tNmNmEK/N7mw l5AdP4IwCTdzgV9adKYAzO7ffWy0WcU3u3cCB5sZB8q2Xk9gohFFQN6995mMAL3C1RhNup Exm/qr0JNtawUgNmzpxI5Z4WBRUIHYiMxBDLF7Pp0v+AEgD08FZyYtxNTP4UMwIeyxn/l4 lrW7i9hzaxFuuE9xs3uUiLacHD7s8/sFjNf05V6brOJiHtBTP5L9VaHHkb4Dy6uI/o4cA9 4BU1brqlbx6g5/0oskH1IJrcemnruoDuO6SlG39uSGvePWnYdkPMDTE/Skoo9g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1777814656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Tz8my8xNlqK7bwfmWQRlThnku6PbexVIGoppmPblUuk=; b=JMnckAOp+EHGs30bBWElkNK1KSD9wAP5DDGdbEycT/kSpN+bTLhVUPu8Lcj49TLIeCDL38 R9uZw3v3eCTc9RAw== To: Mateusz Guzik Cc: Christian Brauner , Soheil Hassas Yeganeh , Alexander Viro , Jan Kara , Shuah Khan , Davidlohr Bueso , Khazhismel Kumykov , Willem de Bruijn , Eric Dumazet , Jens Axboe , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 2/2] eventpoll: Fix epoll_wait() report false negative In-Reply-To: References: <43d64ad765e2c47e958f01246320359b11379466.1752824628.git.namcao@linutronix.de> <20250718085948.3xXGcxeQ@linutronix.de> <20260429-november-speisen-3084d769d316@brauner> <87340exm2o.fsf@yellow.woof> Date: Sun, 03 May 2026 15:24:14 +0200 Message-ID: <87cxzc62yp.fsf@yellow.woof> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Mateusz Guzik writes: > Strictly speaking more error prone than the seq approach, but should be > faster on weaker-ordered archs thanks to avoided fences. > > I'm definitely not going to protest the seqc route. Linus probably wouldn't be thrilled if I break epoll again, so let's stay with the simpler seqcount route. Nam diff --git a/fs/eventpoll.c b/fs/eventpoll.c index a3090b446af1..22c3f0186476 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -38,6 +38,7 @@ #include #include #include +#include #include /* @@ -190,6 +191,9 @@ struct eventpoll { /* Lock which protects rdllist and ovflist */ spinlock_t lock; + /* Protect switching between rdllist and ovflist */ + seqcount_spinlock_t seq; + /* RB tree root used to store monitored fd structs */ struct rb_root_cached rbr; @@ -382,8 +386,17 @@ static inline struct epitem *ep_item_from_wait(wait_queue_entry_t *p) */ static inline int ep_events_available(struct eventpoll *ep) { - return !list_empty_careful(&ep->rdllist) || - READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR; + bool events_available; + unsigned int seq; + + do { + seq = read_seqcount_begin(&ep->seq); + + events_available = !list_empty_careful(&ep->rdllist) || + READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR; + } while (read_seqcount_retry(&ep->seq, seq)); + + return events_available; } #ifdef CONFIG_NET_RX_BUSY_POLL @@ -735,8 +748,12 @@ static void ep_start_scan(struct eventpoll *ep, struct list_head *txlist) */ lockdep_assert_irqs_enabled(); spin_lock_irq(&ep->lock); + write_seqcount_begin(&ep->seq); + list_splice_init(&ep->rdllist, txlist); WRITE_ONCE(ep->ovflist, NULL); + + write_seqcount_end(&ep->seq); spin_unlock_irq(&ep->lock); } @@ -768,6 +785,9 @@ static void ep_done_scan(struct eventpoll *ep, ep_pm_stay_awake(epi); } } + + write_seqcount_begin(&ep->seq); + /* * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after * releasing the lock, events will be queued in the normal way inside @@ -779,6 +799,9 @@ static void ep_done_scan(struct eventpoll *ep, * Quickly re-inject items left on "txlist". */ list_splice(txlist, &ep->rdllist); + + write_seqcount_end(&ep->seq); + __pm_relax(ep->ws); if (!list_empty(&ep->rdllist)) { @@ -1155,6 +1178,7 @@ static int ep_alloc(struct eventpoll **pep) mutex_init(&ep->mtx); spin_lock_init(&ep->lock); + seqcount_spinlock_init(&ep->seq, &ep->lock); init_waitqueue_head(&ep->wq); init_waitqueue_head(&ep->poll_wait); INIT_LIST_HEAD(&ep->rdllist);