Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3400405pxk; Mon, 5 Oct 2020 08:45:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzeSh+6Y6cx3a1gABOY3lpuukwz1UuyTpPQLgREi0mFLukplrzn+3ro/KE1BM+lWtzQYRb X-Received: by 2002:a17:906:940c:: with SMTP id q12mr318079ejx.195.1601912713673; Mon, 05 Oct 2020 08:45:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601912713; cv=none; d=google.com; s=arc-20160816; b=wlxzzx8uCs2oM6r6CGWPRHbsNXjJP/PSTGOU9rzs4xZ+//eLuKfpvIVv6wPA00fmBQ V5lxUQkC/hXErkHajt7SDRTHKDpXqQ9F/w2fHrbmyv5cJxrP3QHZB8yxW9sKkLs8d9ca /xFhyAmtLt6GaS+/2eMkRRdJNh2lq1trFZi2r9W88MyqTJz9Ey2U9zHOaYWt42lMYYzS FBDTCVXltsOni8w40wGm6D6qVuvHr9FjOWxTfd2CcLe2YbnR3YKrPBxC67M9LtogWcmB 8qfrK49ur+UdYu+wm8Kb5XBCCleWg7FSape3kT3fQvPSjzQtjbKOUv5huPR1qZjhHRlm RCjA== 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=i/esTuAQOiSiQNzx8PgisaD+wZkAJlIFCI9ToOQRE6g=; b=yNt4Pzta8O8x8vfmr8b4ZjiysOvgMyI3eUHOb1NZbpVt6WApXvudw+Cyim6Ro3ZIsF QrBtiMubVZBqzsejmyMM+ptTASWeILgboJos0MwOeSReBV1fzHwvT97RLu+/5u0ud+rH rttMmPqth8euGxSJ9sHKj95KFpUpyyo5W1usCsLuMNQqgObJ2x8IhcghhlruQcUKcLF+ zJDPFuPT8Q3u6Iz2LDFlCTOBiiMe4B0mfNDU2YlE7Q/3yqGTAeNyiyfI6KgfJg+IvR/V yGs3RLF7xQQM4r0iJo8Ro+pp5KVkbEdaK0LJ5Yaow60BzJdX9cQIAXL4PbcMq0N0jhLv PM1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b12aRwfz; 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 f4si937483ejw.642.2020.10.05.08.44.49; Mon, 05 Oct 2020 08:45:13 -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=b12aRwfz; 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 S1728769AbgJEPmf (ORCPT + 99 others); Mon, 5 Oct 2020 11:42:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:52882 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727331AbgJEP2E (ORCPT ); Mon, 5 Oct 2020 11:28:04 -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 A556E2074F; Mon, 5 Oct 2020 15:28:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601911683; bh=zXvlVSrf6VohK+ykQFuPF18aQpHLBBLC/HSdGT2mgiI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b12aRwfz96r41vfFIyew0VnIuG0YRHM4tsT1hLGXKPqgqKTzyKDmt1/lqPaIerJWI w9g8nOK6WyYD2lh+l0NiZuz6pA2bW3BNR7nxPNupDuNmiq1pAujf/Xr8k2GpRMMVj8 PbiHIR8SlcMLO2xDhHgAoTpnBrfKEwOzt+EGu7tE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Al Viro Subject: [PATCH 4.19 36/38] epoll: EPOLL_CTL_ADD: close the race in decision to take fast path Date: Mon, 5 Oct 2020 17:26:53 +0200 Message-Id: <20201005142110.413835796@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201005142108.650363140@linuxfoundation.org> References: <20201005142108.650363140@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 @@ -2074,6 +2074,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);