Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4812995yba; Tue, 30 Apr 2019 04:56:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqx45Ckyw4Q0FQeduZ3UrwnGKo/lewHEHJVzpB8z41RqJKPEvLhmkQv7EdBV8AcKEMc5go6T X-Received: by 2002:a63:b53:: with SMTP id a19mr22073518pgl.216.1556625412793; Tue, 30 Apr 2019 04:56:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556625412; cv=none; d=google.com; s=arc-20160816; b=LVUHWKPY07QP/cn4Bxj9+Dhk/fZj951GfLcvY57iy3YNSLtfD3PLZ6ie6Vz1g1kTlj KroC1ASUKtjRJwoBgtWsaYWZaFXBSvNWWRLz3i9jCAwGPLXQa9b5KX2cklnh2LcNRD9v QXpW3+qkVoSphDYwMzBQUS4ECjgc7AWwLqbbAvmfeHDYf1JV03Tr34GHt9dviTrAkcuD H3SVeC5TjUhHbnzC6NEIEe/T0+ArDUCLnJ/3bWkUcSUquoseQhyB8BsvLu43LMmZ5qzD RjldYtixZhWGrNLcoYaZEUTMs58gzljamsro7w/YsIJvWi73UBx7uSLbU6nyFRLVHnYU mUQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=huGc6LV+7dk2SVCUT87SLIpJHkaTc9qrqEM+vd75q9E=; b=uYHcOPyC7iiIp61fpoflRGD8eKMshL8XCtYvi7B7PFfuWX8YCaKmVoOtoMlfis1sB/ m3eq9A0jDCwcadX0GOH2rfnBdBZijeGfY7yNwZ1ALdN6nTmAH+WDdorE6PM53ZyZ2647 Fx7GE6kTYomGYGR0VXVEw3DlC0gRrEXY5piEldeEmGbBItsoqD9fWgP4/ZdSzRpBvAjE CFIzx5dAX1xGDK+pI8Kya7bl2ZVVpry1JgZFvWNGv//GTCQKtdiX7lZqSi9V+dixu6Fd EHRtfElqXgKakNf5s44lTsrxYT3eMTlIBmStREgrS/Y1l3cRdpuXhSOnOzTSrHdVz33G iKgQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f64si38139122pfc.168.2019.04.30.04.56.37; Tue, 30 Apr 2019 04:56:52 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731043AbfD3Lxu (ORCPT + 99 others); Tue, 30 Apr 2019 07:53:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50050 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730755AbfD3Lxn (ORCPT ); Tue, 30 Apr 2019 07:53:43 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 76888859FB; Tue, 30 Apr 2019 11:53:42 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.43.17.159]) by smtp.corp.redhat.com (Postfix) with SMTP id A86E22B3B5; Tue, 30 Apr 2019 11:53:35 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 30 Apr 2019 13:53:40 +0200 (CEST) Date: Tue, 30 Apr 2019 13:53:33 +0200 From: Oleg Nesterov To: Joel Fernandes Cc: Christian Brauner , linux-kernel@vger.kernel.org, luto@amacapital.net, rostedt@goodmis.org, dancol@google.com, sspatil@google.com, jannh@google.com, surenb@google.com, timmurray@google.com, Jonathan Kowalski , torvalds@linux-foundation.org, kernel-team@android.com, Andrew Morton , Arnd Bergmann , "Eric W. Biederman" , Greg Kroah-Hartman , Ingo Molnar , Jann Horn , linux-kselftest@vger.kernel.org, Michal Hocko , "Peter Zijlstra (Intel)" , Serge Hallyn , Shuah Khan , Stephen Rothwell , Thomas Gleixner , Tycho Andersen , viro@zeniv.linux.org.uk, linux-api@vger.kernel.org Subject: Re: [PATCH v1 1/2] Add polling support to pidfd Message-ID: <20190430115332.GB23020@redhat.com> References: <20190425190010.46489-1-joel@joelfernandes.org> <20190425222359.sqhboc4x4daznr6r@brauner.io> <20190428162405.GA6757@redhat.com> <20190429140245.GB233442@google.com> <20190429142030.GA17715@redhat.com> <20190429163259.GA201155@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190429163259.GA201155@google.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 30 Apr 2019 11:53:43 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04/29, Joel Fernandes wrote: > > On Mon, Apr 29, 2019 at 04:20:30PM +0200, Oleg Nesterov wrote: > > On 04/29, Joel Fernandes wrote: > > > > > > However, in your code above, it is avoided because we get: > > > > > > Task A (poller) Task B (exiting task being polled) > > > ------------ ---------------- > > > poll() called > > > add_wait_queue() > > > exit_state is set to non-zero > > > read exit_state > > > remove_wait_queue() > > > wake_up_all() > > > > just to clarify... No, sys_poll() path doesn't do remove_wait_queue() until > > it returns to user mode, and that is why we can't race with set-exit_code + > > wake_up(). > > I didn't follow what you mean, the removal from the waitqueue happens in > free_poll_entry() called from poll_freewait() which happens from > do_sys_poll() which is before the syscall returns to user mode. Could you > explain more? Hmm. I do not really understand the question... Sure, do_sys_poll() does poll_freewait() before sysret or even before return from syscall, but why does this matter? This is the exit path, it frees the memory, does fput(), etc, f_op->poll() won't be call after that. > > pidfd_poll() can race with the exiting task, miss exit_code != 0, and return > > zero. However, do_poll() won't block after that and pidfd_poll() will be called > > again. > > Here also I didn't follow what you mean. If exit_code is read as 0 in > pidfd_poll(), then in do_poll() the count will be 0 and it will block in > poll_schedule_timeout(). Right? No. Please note the pwq->triggered check and please read __pollwake(). But if you want to understand this you can forget about poll/select. It is a bit complicated, in particular because it has to do set_current_state() right before schedule() and thus it plays games with pwq->triggered. But in essence this doesn't differ too much from the plain wait_event-like code (although you can also look at wait_woken/woken_wake_function). If remove_wait_queue() could happem before wake_up_all() (like in your pseudo- code above), then pidfd_poll() or any other ->poll() method could miss _both_ the condition and wakeup. But sys_poll() doesn't do this, so it is fine to miss the condition and rely on wake_up_all() which ensures we won't block and the next iteration must see condition == T. Oleg.