Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp691899yba; Thu, 16 May 2019 07:29:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqwE78H1of5xpt4knXCOHeTXfisFr0SVisTpCBmTtftrwtaBdS/vAXSkbQJB4VXmXABeLcmP X-Received: by 2002:a63:2ace:: with SMTP id q197mr50098010pgq.371.1558016958793; Thu, 16 May 2019 07:29:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558016958; cv=none; d=google.com; s=arc-20160816; b=y3NpAyPOgUeQm8A6fvaKOs4d+Q1d/W3f4qGfPcq5KzIjWe6KO8a++CvMGqDcyqRQOw 3XME3Cxp4UMa6W0xMhvTtRfEqvDJMpUzR2GMIU8CNEXE2oAj0LYqumZXElnl2LEUVsv4 iZcaXlzqEUGwp/MEUDip24je7HFqyUhBGU4DykyEByakRFX0WPBraCIgu4jcTazsuBL2 wn6zIwwDo5l41Iun4ZgV2PHx1gpvGHOZEliH6xl0O8k7zLM1zFR2+tLblhOX3H1Ja7iK vrU1UXSuA2nm9kHxvYrB2StF1+gFrXYhEaYVijGaYZ1dLYkoa3KA2yNNXKOHdingZ0Gd hDWg== 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=qtDy1SuyllSXQng3se/WZniHteqFeSmT2iYfozkh5Go=; b=pvUdbAr+RhNxBEpARzXgSar8QfZNvsdQP9c44Zz8XL+79w3646OBcGgo4Ji6CModbc L2QFXBnrN90vwIyViT3hQyI5CIy08zHJ9PztyCuRbNLM/zYvHsbFATjLgK0dox7NeVfN FMhiHTFM2+CWwcth6VYx6Vz3abObsDSd/15eZ2MG3kyBPjJDqw5VOLG/5QoPGLrRVLz9 1wd6Hv1DnTRSpR4jr36ynXH9tbnqBfad0RKRDkpA8NT3QXGN6hf4pBkbr5aW04KwM60x waxxJOv1uPcDFpGCPYEND1eC+nXhW6BYFWvlsnbI6PJZ+6hBqonGWH+Z/O3AKbJ6X/df AMHQ== 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 f7si4966659plm.427.2019.05.16.07.29.03; Thu, 16 May 2019 07:29:18 -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 S1727434AbfEPO1Q (ORCPT + 99 others); Thu, 16 May 2019 10:27:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37447 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727021AbfEPO1P (ORCPT ); Thu, 16 May 2019 10:27:15 -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 7E7E388E5D; Thu, 16 May 2019 14:27:11 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.43.17.159]) by smtp.corp.redhat.com (Postfix) with SMTP id 144FC341E2; Thu, 16 May 2019 14:27:01 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Thu, 16 May 2019 16:27:10 +0200 (CEST) Date: Thu, 16 May 2019 16:27:00 +0200 From: Oleg Nesterov To: Christian Brauner Cc: jannh@google.com, viro@zeniv.linux.org.uk, torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, cyphar@cyphar.com, dhowells@redhat.com, ebiederm@xmission.com, elena.reshetova@intel.com, keescook@chromium.org, luto@amacapital.net, luto@kernel.org, tglx@linutronix.de, linux-alpha@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-xtensa@linux-xtensa.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, joel@joelfernandes.org, dancol@google.com, serge@hallyn.com, Geert Uytterhoeven Subject: Re: [PATCH v1 1/2] pid: add pidfd_open() Message-ID: <20190516142659.GB22564@redhat.com> References: <20190516135944.7205-1-christian@brauner.io> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190516135944.7205-1-christian@brauner.io> 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.25]); Thu, 16 May 2019 14:27:14 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/16, Christian Brauner wrote: > > With the introduction of pidfds through CLONE_PIDFD it is possible to > created pidfds at process creation time. Now I am wondering why do we need CLONE_PIDFD, you can just do pid = fork(); pidfd_open(pid); > +SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags) > +{ > + int fd, ret; > + struct pid *p; > + struct task_struct *tsk; > + > + if (flags) > + return -EINVAL; > + > + if (pid <= 0) > + return -EINVAL; > + > + p = find_get_pid(pid); > + if (!p) > + return -ESRCH; > + > + ret = 0; > + rcu_read_lock(); > + /* > + * If this returns non-NULL the pid was used as a thread-group > + * leader. Note, we race with exec here: If it changes the > + * thread-group leader we might return the old leader. > + */ > + tsk = pid_task(p, PIDTYPE_TGID); > + if (!tsk) > + ret = -ESRCH; > + rcu_read_unlock(); > + > + fd = ret ?: pidfd_create(p); > + put_pid(p); > + return fd; > +} Looks correct, feel free to add Reviewed-by: Oleg Nesterov But why do we need task_struct *tsk? rcu_read_lock(); if (!pid_task(PIDTYPE_TGID)) ret = -ESRCH; rcu_read_unlock(); and in fact we do not even need rcu_read_lock(), we could do // shut up rcu_dereference_check() rcu_lock_acquire(&rcu_lock_map); if (!pid_task(PIDTYPE_TGID)) ret = -ESRCH; rcu_lock_release(&rcu_lock_map); Well... I won't insist, but the comment about the race with exec looks a bit confusing to me. It is true, but we do not care at all, we are not going to use the task_struct returned by pid_task(). Oleg.