Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp443774yba; Mon, 1 Apr 2019 09:24:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqy47QwP/wH4amWWyCAK7fF/ntLz8A4goAYJtzKiDzh01JgQSaznzjOiofsbRQtpmLAIRHOZ X-Received: by 2002:a62:2046:: with SMTP id g67mr57361354pfg.121.1554135867291; Mon, 01 Apr 2019 09:24:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554135867; cv=none; d=google.com; s=arc-20160816; b=Kke/1B3axvIkDa4hGUM0I0/SMGbI93PKbCJykqIRvULKt+I8YvXyG2L0inhuUtIjlk mTZ9GLmN2lK/RH6rk2KG3dX5av4Y/Hz0Vbxy7Un6AowUHHBjP1bG1NUbi2fsSzbMuTAU hTtr64Wy7sV/fCZoVRoosPdHyJPdosGs4k+TVUarjk28+1oNGQo5/Q/SfBF4fx92zk8E gvMYviEMd6fD3rNjF8Ui87pR2KtSX25bTV+0iJINjEF+APQO7kJAmZoC78s/1TVl5fHe rwAz+SM4vImd79s5u0t/KpJirQMRJQCto+o8krCmMm7b58mG93iJyGb6waB/EGZXHSqj onlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=aZ09BE7bZZUYfb3KOySGgcA6QPAtPnUG4ieDO4DHbmg=; b=sQMCEdiDcoGKiOuMw3LGX4PiysN/7+D1LsR+PhTSpKa0KbVO/XfbWm/RNiUXZKKtt5 dkWoZKTjZed1NUFF69MiTC0hOidxove2ajxZuHUIXmHlAx/DmSgnphskVds0JVEqtyQt ss/r6peYcZGeHn3MawhFqsSTut8F80iTCWbTFpJjVpLbNm/VnbN37vVlPBXmM6hhhgvj J0Y+ZBiPC4jaj2LzkcmHmozKoSZFgSM/AHtlp6FGTZeK3v0KbU+gj+znZ72L4mk8ANpQ fIJg5LmOIpk+0La8m7ov0pJFvokAVmjft1LGswX82J+CXuqqK1JFDsFx9v1QLePXN93A BLyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WOHsK+kv; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m3si9034855plt.310.2019.04.01.09.24.10; Mon, 01 Apr 2019 09:24:27 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=WOHsK+kv; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728640AbfDAQWD (ORCPT + 99 others); Mon, 1 Apr 2019 12:22:03 -0400 Received: from mail-ot1-f65.google.com ([209.85.210.65]:43455 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726854AbfDAQWC (ORCPT ); Mon, 1 Apr 2019 12:22:02 -0400 Received: by mail-ot1-f65.google.com with SMTP id u15so9107749otq.10 for ; Mon, 01 Apr 2019 09:22:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=aZ09BE7bZZUYfb3KOySGgcA6QPAtPnUG4ieDO4DHbmg=; b=WOHsK+kvahNR5HL4esyJ9CDkg+MZ6Oip8mpOX3usiain5wgd6GPvCJ1YNs40USOzF9 J1Q5jiUBMXf+9RrQBXhH6cLFVKcV3BPs1ugoxsN1PRV2pVgwZzteqHHDjz+lCKnIgLxH gWRmk3bkgwzrnpGZkcAHgbkDiAIyU7I9Xyz1kMP0M9/Mke1Tbh0NiBH5YRKRdqpdRXUU hTLrqGyJkL2og+M4CF0OA2oJm8RrKA1/4BFzJeFyPRdIyGaDTQpCLSWhPWiUHptFdgR/ uYPq190+KWZ13Py2WMXNJ6+D19XFphKRZ/zUELn8VwL9+Rx7KPiV2/v5QzBlKl1LPUj7 8sjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=aZ09BE7bZZUYfb3KOySGgcA6QPAtPnUG4ieDO4DHbmg=; b=PZWcNNA/GcWmObcTsg0dZoGChuM9+gyGtDO8ERvQ8uWiunJhLItTQLU4RC1MgFBAWY /SZkJFI/jADGdbRwlrZe4cmDqHetHC0axXq5Fw5FV/guu6dLgkJEZrwztb62WspmLAUD Jx9yQPcq3SHH/xyn3pkxttd+OiigJSgu9B+cIoldKprUV0y/uzegredibFSbsa3dUhJi FdqfSB6liWPb6wKh5xXWlJ7rE8GFjnEFAo18HpPTt1Ich3kQmwUVqjusJ1hz/wto4MIi KcvZyPP/wJjT/afkPgGlQTEWvUWVOUlHuNx5pux6F9KiHAx1le+utxURlBZxv28kJM4M HSHA== X-Gm-Message-State: APjAAAX8kyjBU+LAp2Dh6g4MCa0PtwTstoENU7Tg7MOa1w0Ql+/iR+vu jnQLUr/02A/wX5SRyY2M0QHx0P/Td11pfHfeGMYX5Q== X-Received: by 2002:a05:6830:144e:: with SMTP id w14mr47561203otp.170.1554135721334; Mon, 01 Apr 2019 09:22:01 -0700 (PDT) MIME-Version: 1.0 References: <20190330171215.3yrfxwodstmgzmxy@brauner.io> <132107F4-F56B-4D6E-9E00-A6F7C092E6BD@amacapital.net> <20190331211041.vht7dnqg4e4bilr2@brauner.io> <18C7FCB9-2CBA-4237-94BB-9C4395A2106B@amacapital.net> <20190401114059.7gdsvcqyoz2o5bbz@yavin> In-Reply-To: From: Daniel Colascione Date: Mon, 1 Apr 2019 09:21:49 -0700 Message-ID: Subject: Re: [PATCH v2 0/5] pid: add pidfd_open() To: Jonathan Kowalski Cc: Linus Torvalds , Aleksa Sarai , Andy Lutomirski , Christian Brauner , Jann Horn , Andrew Lutomirski , David Howells , "Serge E. Hallyn" , Linux API , Linux List Kernel Mailing , Arnd Bergmann , "Eric W. Biederman" , Konstantin Khlebnikov , Kees Cook , Alexey Dobriyan , Thomas Gleixner , Michael Kerrisk-manpages , "Dmitry V. Levin" , Andrew Morton , Oleg Nesterov , Nagarathnam Muthusamy , Al Viro , Joel Fernandes Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 1, 2019 at 9:07 AM Jonathan Kowalski wrote: > > On Mon, Apr 1, 2019 at 4:55 PM Daniel Colascione wrote: > > > > On Mon, Apr 1, 2019 at 8:36 AM Linus Torvalds > > wrote: > > > > > > On Mon, Apr 1, 2019 at 4:41 AM Aleksa Sarai wrote: > > > > > > > > Eric pitched a procfs2 which would *just* be the PIDs some time ago (in > > > > an attempt to make it possible one day to mount /proc inside a container > > > > without adding a bunch of masked paths), though it was just an idea and > > > > I don't know if he ever had a patch for it. > > > > Couldn't this mode just be a relatively simple procfs mount option > > instead of a whole new filesystem? It'd be a bit like hidepid, right? > > The internal bind mount option and the no-dotdot-traversal options > > also look good to me. > > > > > I wonder if we really want a fill procfs2, or maybe we could just make > > > the pidfd readable (yes, it's a directory file descriptor, but we > > > could allow reading). > > > > What would read(2) read? > > > > > What are the *actual* use cases for opening /proc files through it? If > > > it's really just for a small subset that android wants to do this > > > (getting basic process state like "running" etc), rather than anything > > > else, then we could skip the whole /proc linking entirely and go the > > > other way instead (ie open_pidfd() would get that limited IO model, > > > and we could make the /proc directory node get the same limited IO > > > model). > > > > We do a lot of process state inspection and manipulation, including > > reading and writing the oom killer adjustment score, reading smaps, > > and the occasional cgroup manipulation. More generally, I'd also like > > to be able to write a race-free pkill(1). Doing this work via pidfd > > would be convenient. More generally, we can't enumerate the specific > > use cases, because what we want to do with processes isn't bounded in > > advance, and we regularly find new things in /proc/pid that we want to > > read and write. I'd rather not prematurely limit the applicability of > > the pidfd interface, especially when there's a simple option (the > > procfs directory file descriptor approach) that doesn't require > > in-advance enumeration of supported process inspection and > > manipulation actions or a separate per-option pidfd equivalent. I very > > much want a general-purpose API that reuses the metadata interfaces > > the kernel already exposes. It's not clear to me how this rich > > interface could be matched by read(2) on a pidfd. > > With the POLLHUP model on a simple pidfd, you'd know when the process > you were referring to is dead (and one can map POLLPRI to dead and > POLLHUP to zombie, etc). I agree that there needs to be *some* kind of pollable file descriptor that fires on process death. Should it be the pifd itself? I've been thinking that a pollable directory would be too weird, but if that's not actually a problem, I'm fine with it. Mapping different state transitions to different poll bits is an interesting idea, but I'm also worried about making poll the "source of truth" with respect to process state as reported by a pidfd. Consider a socket: for a socket, read(2)/recv(2) is the "source of truth" and poll is just a hint that says "now is a good time to attempt a read". Some other systems even allow for spurious wakeups from poll. It's also worth keeping in mind that some pre-existing event loops let you provide "is readable" and "is writable" callbacks, but don't support the more exotic poll notification bits. That's why I've tried to keep my proposals limited to poll signaling readability. But I'm not really that picky. I just want something that works. > This is just an extension of the child process model, since you'd know > when it's dead, there's no race involved with opening the wrong > /proc/ entry. The entire thing is already non-racy for direct > children, the same model can be extended to non-direct ones. > > This simplifies a lot of things, now I am essentially just passing a > file descriptor pinning the struct pid associated with the original > task, and not process state around to others (I may even want the > other process to not read that stuff out even if it was allowed to, as > it wouldn't have been able to otherwise, due to being a in a different > mount namespace). KISS. > > The upshot is this same descriptor can be returned from clone, which > would allow you to directly register it in your event loop (like > signalfd, timerfd, file fd, sockets, etc) and POLLIN generated for you > to read back its exit status (it is arguable if non-parents should be > returned a readable instance from pidfd_open, but parents sure > should). You can disable SIGCHLD for the child, and read back exit > status much later. The entire point of waiting and reaping was that > it'd be lost, but now you have a descriptor where it is kept for you > to consume. There's a subtlety: suppose I'm a library and I want to create a private subprocess. I use the new clone facility, whatever it is, and get a pidfd back. I need to be able to read the child's exit status even if the child exits before clone returns in the parent. Doesn't this requirement imply that the pidfd, kernel-side, contain something a bit more than a struct pid?