Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1159734ybi; Fri, 21 Jun 2019 15:14:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqzIaJAhlVNb60gZaEFhjyQo0ITslnExsaUk6zgum0nFLwxUDHEFDpupTkLz/OAyEcRRwkmn X-Received: by 2002:a17:90a:ac13:: with SMTP id o19mr9420193pjq.143.1561155253772; Fri, 21 Jun 2019 15:14:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561155253; cv=none; d=google.com; s=arc-20160816; b=zEKLmNLIWjjk+h+LldKxlPP+mQ+hDbp94h7xS4srjEys35l5j03JgxmXbz6hluEUto fj2Nd01WTIGLzoQdayFZ49VXYPqLyI4sTT9YuKlZ01CFjKJfW5gJmo7akUcBgyGRFxaT m9sxU5tJuFkTyrq2qpEqEgY4DfhD6rg/d+Mrj7eyU7Xexbsf1wcKg21ej1UHK7BS6Ewv rFpXNZ4ftkaBTtiXjUK/Ss83s2yzBZdpAf9LGbANhyZkzAoUVHvfT3LBMN7Jg8G3n6r6 zwE4Xuu2v7YVKrouK+J5wVlwshrgZLRskx6Hekxt9rFR+keQ2gAGTCUyZ561rkT7/ViL ORIA== 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:dkim-signature; bh=/hHk0S9u9UZeTnfOgA1tURrEwbtjqCVR5Jur9CvSRDU=; b=jv8HspMTL3286BNbO0e3no6uuSvZls/ugpBsL5iBubosxR94SzkSJaLkfQS17xI8oa 13eEl1C68IVj7QG8e/EjApDsqnj66SApCIE3v1RTrNWtuPIppSylLNqVMG/zM7/yLE1e w/tVqFc8hjbJqYIiJFLzXgWnbwG2zNFLq08xxUTOr32OC/Q+3ssvmcx6L1Qi+p646NWY MrI+mIkVzbbsIaj9z4K4EJqt+jRvQg89QclBBJY0TEIqtJ4SukpFvT8F9BCFL2tqKs0r PQFW/fHZ7kCdzVCJwrSaSq9eKMbcI73ZqN2/woFlwa2SOZVucNn2N4l5oj0iCGLQLZ8h qYIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=RblRDrzJ; 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 q6si3696369pgb.234.2019.06.21.15.13.57; Fri, 21 Jun 2019 15:14:13 -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=@brauner.io header.s=google header.b=RblRDrzJ; 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 S1726277AbfFUWNo (ORCPT + 99 others); Fri, 21 Jun 2019 18:13:44 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:34717 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726031AbfFUWNn (ORCPT ); Fri, 21 Jun 2019 18:13:43 -0400 Received: by mail-wr1-f66.google.com with SMTP id k11so7956056wrl.1 for ; Fri, 21 Jun 2019 15:13:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=/hHk0S9u9UZeTnfOgA1tURrEwbtjqCVR5Jur9CvSRDU=; b=RblRDrzJgYetJYhfs1+s8zGLEDsuGMJYIGjWEzlVg6iUUawJSAWh1cWw4zJFIa+FF9 4LeBDtgHlqAbB+vNESZVHx4UWv8R/KrQ+it0E6eaErmnu0qwSsYrYdCN+gfY4JaQaZ6P AEfu5UpaZ7fkNmoFypcWAstAxxW2RwUA9GM5+pZRNuKDG4cyfxxyolNquz5LWXzCjdWv WZGQx+GJF9rXXbWtY2VnEdqRlmCm37wHCE+rHRRw1gRhZYoKIVHVsHwEnjpfYBeEOzcJ 4Yyv7NWj8vqz102/YbR8CkCBI9EAp7NTZQQPoSiN3TbrXncG2CYCat7fRf4pFOmd8u8P OWMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=/hHk0S9u9UZeTnfOgA1tURrEwbtjqCVR5Jur9CvSRDU=; b=jng4+D1MCyDQNoxK2az08onQ/hXVy2wePQcJet+8IcFXMHJX9JiGNelWraLiUtVwqJ mrJIKy63PAfT3O0ph4cj91BHVF5uQp6s4Xv2xweb01jAVf9MowPiqJx+QNWwr7ooYWAk vRAUkcz9eLYj4rZyjWP/U/QQedHdAevbB/Tk6jgVFj9+w9R7Pg+TR/zW2VI+xD0O5h0r U7eFR3hnHQmsDKn3tptL5sJsEhX0kd+hg+M7FfcK43eWJ/0xLdo0jjEyF6Gg+EIiyHFz bxzK+J/8LXz21RnT4JORk8RmsyXIW0dqHwLS+yIAsKY19HXgzyaXQOkxEfX7F2ua6pQO sO9A== X-Gm-Message-State: APjAAAVgl3NOPUVcm6SbzO74ha4r+fJM54QdJx6xXKH8A9z8p+3vsKu2 ABp1za6uwWieEFj3FPodefM9Zw== X-Received: by 2002:a5d:4fc8:: with SMTP id h8mr9986421wrw.124.1561155220929; Fri, 21 Jun 2019 15:13:40 -0700 (PDT) Received: from brauner.io ([212.91.227.56]) by smtp.gmail.com with ESMTPSA id b6sm2547103wrx.85.2019.06.21.15.13.40 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 21 Jun 2019 15:13:40 -0700 (PDT) Date: Sat, 22 Jun 2019 00:13:39 +0200 From: Christian Brauner To: "Dmitry V. Levin" Cc: Jann Horn , Oleg Nesterov , Arnd Bergmann , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] samples: make pidfd-metadata fail gracefully on older kernels Message-ID: <20190621221339.6yj4vg4zexv4y2j7@brauner.io> References: <20190620103105.cdxgqfelzlnkmblv@brauner.io> <20190620110037.GA4998@altlinux.org> <20190620111036.asi3mbcv4ax5ekrw@brauner.io> <20190621170613.GA26182@altlinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190621170613.GA26182@altlinux.org> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 21, 2019 at 08:06:14PM +0300, Dmitry V. Levin wrote: > On Thu, Jun 20, 2019 at 01:10:37PM +0200, Christian Brauner wrote: > > On Thu, Jun 20, 2019 at 02:00:37PM +0300, Dmitry V. Levin wrote: > > > Cc'ed more people as the issue is not just with the example but > > > with the interface itself. > > > > > > On Thu, Jun 20, 2019 at 12:31:06PM +0200, Christian Brauner wrote: > > > > On Thu, Jun 20, 2019 at 06:11:44AM +0300, Dmitry V. Levin wrote: > > > > > Initialize pidfd to an invalid descriptor, to fail gracefully on > > > > > those kernels that do not implement CLONE_PIDFD and leave pidfd > > > > > unchanged. > > > > > > > > > > Signed-off-by: Dmitry V. Levin > > > > > --- > > > > > samples/pidfd/pidfd-metadata.c | 8 ++++++-- > > > > > 1 file changed, 6 insertions(+), 2 deletions(-) > > > > > > > > > > diff --git a/samples/pidfd/pidfd-metadata.c b/samples/pidfd/pidfd-metadata.c > > > > > index 14b454448429..ff109fdac3a5 100644 > > > > > --- a/samples/pidfd/pidfd-metadata.c > > > > > +++ b/samples/pidfd/pidfd-metadata.c > > > > > @@ -83,7 +83,7 @@ static int pidfd_metadata_fd(pid_t pid, int pidfd) > > > > > > > > > > int main(int argc, char *argv[]) > > > > > { > > > > > - int pidfd = 0, ret = EXIT_FAILURE; > > > > > + int pidfd = -1, ret = EXIT_FAILURE; > > > > > > > > Hm, that currently won't work since we added a check in fork.c for > > > > pidfd == 0. If it isn't you'll get EINVAL. > > > > > > Sorry, I must've missed that check. But this makes things even worse. > > > > > > > This was done to ensure that > > > > we can potentially extend CLONE_PIDFD by passing in flags through the > > > > return argument. > > > > However, I find this increasingly unlikely. Especially since the > > > > interface would be horrendous and an absolute last resort. > > > > If clone3() gets merged for 5.3 (currently in linux-next) we also have > > > > no real need anymore to extend legacy clone() this way. So either wait > > > > until (if) we merge clone3() where the check I mentioned is gone anyway, > > > > or remove the pidfd == 0 check from fork.c in a preliminary patch. > > > > Thoughts? > > > > > > Userspace needs a reliable way to tell whether CLONE_PIDFD is supported > > > by the kernel or not. > > > > Right, that's the general problem with legacy clone(): it ignores > > unknown flags... clone3() will EINVAL you if you pass any flag it > > doesn't know about. > > > > For legacy clone you can pass > > > > (CLONE_PIDFD | CLONE_DETACHED) > > > > on all relevant kernels >= 2.6.2. CLONE_DETACHED will be silently > > ignored by the kernel if specified in flags. But if you specify both > > CLONE_PIDFD and CLONE_DETACHED on a kernel that does support CLONE_PIDFD > > you'll get EINVALed. (We did this because we wanted to have the ability > > to make CLONE_DETACHED reuseable with CLONE_PIDFD.) > > Does that help? > > Yes, this is feasible, but the cost is extra syscall for new kernels > and more complicated userspace code, so... Out of curiosity: what makes the new flag different than say CLONE_NEWCGROUP or any new clone flag that got introduced? CLONE_NEWCGROUP too would not be detectable apart from the method I gave you above; same for other clone flags. Why are you so keen on being able to detect this flag when other flags didn't seem to matter that much. (Again, mere curiosity.) > > > > If CLONE_PIDFD is not supported, then pidfd remains unchanged. > > > > > > If CLONE_PIDFD is supported and fd 0 is closed, then mandatory pidfd == 0 > > > also remains unchanged, which effectively means that userspace must ensure > > > that fd 0 is not closed when invoking CLONE_PIDFD. This is ugly. > > > > > > If we can assume that clone(CLONE_PIDFD) is not going to be extended, > > > then I'm for removing the pidfd == 0 check along with recommending > > > userspace to initialize pidfd with -1. > > > > Right, I'm ok with that too. > > ... I'd prefer this variant. Please send a patch for review. Christian