Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4005160pxu; Mon, 12 Oct 2020 07:09:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzB0YxUCAdIsPMZSw0/zMc0ri95Jt4wMRYT6XeaX+LauEcnd59gidaRDkgl5E6DEHxAVMpr X-Received: by 2002:aa7:dc12:: with SMTP id b18mr14329565edu.295.1602511799722; Mon, 12 Oct 2020 07:09:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602511799; cv=none; d=google.com; s=arc-20160816; b=M+/HQ22dorBHwgp88MNESVw1HeojATsyfeT5vTt2YvKwplBOnD6W6GP/VQADqJ58mV a5jN9XEQL2IT/a1oyKJFSpiAALqnSKJu5rjwShRhKKY8w6MxYavHdPJgGRj9z86iJBz5 wxEI/Ur5P6yEfdjnhvBQK+EXQY7+WFxQNGKZhZegrSRQRi38G0NhaGalCj7JOVsByE7U +C4jWvTmQT90IVQRgsQocwdtVQZxoHliIIo/ExwOtLUAlpxSlep3tK6I51pIOJYU7JGS rkI07qdWRaJTTxa3D9yezkwx+ut+GSA5CbuITtN8ULdpCHgtY7pgVI+EQd6YiPhAPwF8 BX8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eattHu5nRFbTCh9RsjsJT/58Om4CDbBzcRpp/8JYU4Q=; b=PBpwyjZSUbtcWTHNmN69HwGU23IR9UdN9Vc/JHwcsa6mflyKdD+/MGJOBm1UvJ/1KI VoAm+DasTyFV1yu5YkVltYbZf4gMS5gDgOcJ4gWHohlM7+uMOjfZ0zzNdaBhR0lFavVe V1lozyASbrN68MN31UxCW5BVfEs2u4LKAd3ZdnWhlHJ3EV+MZe0s/lnvpT6hoAZ4rjdi Zx76VmdKGJZxW4sQ0f5dYD3R7UPiXv4zq8ErTzno98OszhILWb/wvumK9DSSjmpuN09I dkk4VLRn1cWt/5hsuhieuflMa0VjHdegclkyXmAIuWL6VesKu/UdyqM5PqCFEiGuqOTi 4+0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gFvza9x5; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v10si3762373edw.7.2020.10.12.07.09.36; Mon, 12 Oct 2020 07:09:59 -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=gFvza9x5; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388873AbgJLOFa (ORCPT + 99 others); Mon, 12 Oct 2020 10:05:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:37700 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730773AbgJLNfI (ORCPT ); Mon, 12 Oct 2020 09:35:08 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 2E0F521D81; Mon, 12 Oct 2020 13:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602509707; bh=4kvoDMx0jyu3IrKxPP+HTDN8m6lGqaGA1umUbdi0RKk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gFvza9x5qLHckRXQPEgJkbJ54A5/58ihhPmxevcfoc0jh038nAAvxX/4oHTnVM3iw xgf8SERfrZ0oZiIf/KcPmM4kt5FObBtRxtGUFPjSGsJn91ZNY5h21aAtk4KzgbreQ6 1Jq9r50QWuh8MM1wZDRGhtDNK6YH7GrmLEtze8kg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro Subject: [PATCH 4.9 20/54] epoll: EPOLL_CTL_ADD: close the race in decision to take fast path Date: Mon, 12 Oct 2020 15:26:42 +0200 Message-Id: <20201012132630.538213666@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201012132629.585664421@linuxfoundation.org> References: <20201012132629.585664421@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Al Viro commit fe0a916c1eae8e17e86c3753d13919177d63ed7e upstream. Checking for the lack of epitems refering to the epoll we want to insert into is not enough; we might have an insertion of that epoll into another one that has already collected the set of files to recheck for excessive reverse paths, but hasn't gotten to creating/inserting the epitem for it. However, any such insertion in progress can be detected - it will update the generation count in our epoll when it's done looking through it for files to check. That gets done under ->mtx of our epoll and that allows us to detect that safely. We are *not* holding epmutex here, so the generation count is not stable. However, since both the update of ep->gen by loop check and (later) insertion into ->f_ep_link are done with ep->mtx held, we are fine - the sequence is grab epmutex bump loop_check_gen ... grab tep->mtx // 1 tep->gen = loop_check_gen ... drop tep->mtx // 2 ... grab tep->mtx // 3 ... insert into ->f_ep_link ... drop tep->mtx // 4 bump loop_check_gen drop epmutex and if the fastpath check in another thread happens for that eventpoll, it can come * before (1) - in that case fastpath is just fine * after (4) - we'll see non-empty ->f_ep_link, slow path taken * between (2) and (3) - loop_check_gen is stable, with ->mtx providing barriers and we end up taking slow path. Note that ->f_ep_link emptiness check is slightly racy - we are protected against insertions into that list, but removals can happen right under us. Not a problem - in the worst case we'll end up taking a slow path for no good reason. Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/eventpoll.c | 1 + 1 file changed, 1 insertion(+) --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -1926,6 +1926,7 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, in mutex_lock_nested(&ep->mtx, 0); if (op == EPOLL_CTL_ADD) { if (!list_empty(&f.file->f_ep_links) || + ep->gen == loop_check_gen || is_file_epoll(tf.file)) { full_check = 1; mutex_unlock(&ep->mtx);