Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4567081ybi; Tue, 11 Jun 2019 08:43:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqyH0jFc/RQR6GKCY5UvirMTbJRUTRy2yUrp11oqo4/tIr1UA7st9KhgseCz97eQnDTVwLKh X-Received: by 2002:a17:902:9885:: with SMTP id s5mr76394615plp.102.1560267790535; Tue, 11 Jun 2019 08:43:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560267790; cv=none; d=google.com; s=arc-20160816; b=kwmYPRRyHuqXrDbNn2Mmj5+FeBVtUe7vMgryPCg3S96hlezsvzAxfINdOdbf/H++pS IzYrMGuFyoVug2l7EjIJwSOHX1xEPtHxu3AMnCOlukGK0mTSSSdtF2ZN1QSQmiKOO1ii 640UeEnVYyvVdvN0TsZ+P30AOkPdgKSOU5XJ2zO2eM3yOEJZRNJ/rMpO4UZP/xKtt3H9 lH2aMhIWBFv78AfVis76HBhgaVHmMU2vC2YO4HLpWLopJaFTKPaD4kZbulfi25CDHlO0 ViIlDsmPMtlZWPXZasR/1NRTEw3Z80ES60dOoJ3ytoqfWrn39sIKxKhbfF8O33rmc8Be bIYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from; bh=4GUtpsSMudY2PB5P/TiYu6p66nmlgzB4aNff6wMb2cM=; b=uyVHS1lEbBwXw8NfpMxAWa5pEAPsbsa5GgHpwF/5EuQroow4cV9V03glhs1yKbp7Et ZJnqiaG729x05YIWRt6W0YwJDhZKk6XZbKUE5+r/y5SEt010OawR3duGrdsEds0C6vh7 YrcXTng5sWrX0hnTMnfAAPvduORSc0wz3oUzdjjR5jItnJMZy31cjg3c/e4Zx46I/lBW z8olrFMZuzmnmqQelXTmBJhz5Ef30IY7orTRhNSxG/WbMoMyaLv2gzof9VgWfiDcjrFD JXcW9Uxt2Iohqg+PptsanFIKjs9GskYgEVJCyHSkuB7QLcE03/sv80lhmbQtsaHmh3YH OTYA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k7si12346042pll.145.2019.06.11.08.42.55; Tue, 11 Jun 2019 08:43:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387610AbfFKOz0 (ORCPT + 99 others); Tue, 11 Jun 2019 10:55:26 -0400 Received: from mx2.suse.de ([195.135.220.15]:52556 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2391625AbfFKOzQ (ORCPT ); Tue, 11 Jun 2019 10:55:16 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id BCAC6ABD2; Tue, 11 Jun 2019 14:55:15 +0000 (UTC) From: Roman Penyaev Cc: Roman Penyaev , Andrew Morton , Al Viro , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 07/14] epoll: call ep_add_event_to_uring() from ep_poll_callback() Date: Tue, 11 Jun 2019 16:54:51 +0200 Message-Id: <20190611145458.9540-8-rpenyaev@suse.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190611145458.9540-1-rpenyaev@suse.de> References: <20190611145458.9540-1-rpenyaev@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Each ep_poll_callback() is called when fd calls wakeup() on epfd. So account new event in user ring. The tricky part here is EPOLLONESHOT. Since we are lockless we have to be deal with ep_poll_callbacks() called in paralle, thus use cmpxchg to clear public event bits and filter out concurrent call from another cpu. Signed-off-by: Roman Penyaev Cc: Andrew Morton Cc: Al Viro Cc: Linus Torvalds Cc: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- fs/eventpoll.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index 545d1769fa0f..bcd57ca47564 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -1539,6 +1539,29 @@ struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd, } #endif /* CONFIG_CHECKPOINT_RESTORE */ +/** + * Atomically clear public event bits and return %true if the old value has + * public event bits set. + */ +static inline bool ep_clear_public_event_bits(struct epitem *epi) +{ + __poll_t old, flags; + + /* + * Here we race with ourselves and with ep_modify(), which can + * change the event bits. In order not to override events updated + * by ep_modify() we have to do cmpxchg. + */ + + old = READ_ONCE(epi->event.events); + do { + flags = old; + } while ((old = cmpxchg(&epi->event.events, flags, + flags & EP_PRIVATE_BITS)) != flags); + + return flags & ~EP_PRIVATE_BITS; +} + /** * Adds a new entry to the tail of the list in a lockless way, i.e. * multiple CPUs are allowed to call this function concurrently. @@ -1658,6 +1681,20 @@ static int ep_poll_callback(struct epitem *epi, __poll_t pollflags) if (pollflags && !(pollflags & epi->event.events)) goto out_unlock; + if (ep_polled_by_user(ep)) { + /* + * For polled descriptor from user we have to disable events on + * callback path in case of one-shot. + */ + if ((epi->event.events & EPOLLONESHOT) && + !ep_clear_public_event_bits(epi)) + /* Race is lost, another callback has cleared events */ + goto out_unlock; + + ep_add_event_to_uring(epi, pollflags); + goto wakeup; + } + /* * If we are transferring events to userspace, we can hold no locks * (because we're accessing user memory, and because of linux f_op->poll() @@ -1677,6 +1714,7 @@ static int ep_poll_callback(struct epitem *epi, __poll_t pollflags) ep_pm_stay_awake_rcu(epi); } +wakeup: /* * Wake up ( if active ) both the eventpoll wait list and the ->poll() * wait list. -- 2.21.0