From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 29C7A3858C2B for ; Thu, 24 Aug 2023 17:00:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 29C7A3858C2B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896410; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=m7qC3tObqDvXgD2uPBGrWPJ1uLcuJrgs/0ytM8B/+MM=; b=DfmlUJYnpwCOBXlEaVIoCs3T2ylwe5Z7IsMEt4SaZYJRTliHGgBm/9bjr06GYY2UOUFrrY 8EhLTBB+M8nfiHUHY1ucbSfiWc66sciHwWxa2WpK51Hz1bBGqchZHX9iEKI7697BuW/yky iHTUPkT6Bdg4TIDGfp8qvdIjej7nals= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-124-9NfYh4kjMZqMnFUv-5fWyQ-1; Thu, 24 Aug 2023 13:00:08 -0400 X-MC-Unique: 9NfYh4kjMZqMnFUv-5fWyQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 29204858EED; Thu, 24 Aug 2023 17:00:08 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AA74F40C6F4C; Thu, 24 Aug 2023 17:00:07 +0000 (UTC) From: Florian Weimer To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v8 5/7] posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349) References: <20230818140642.1623571-1-adhemerval.zanella@linaro.org> <20230818140642.1623571-6-adhemerval.zanella@linaro.org> <875y5429r4.fsf@oldenburg.str.redhat.com> <095993ea-8773-fafd-7d0c-4750517c76e9@linaro.org> Date: Thu, 24 Aug 2023 19:00:06 +0200 In-Reply-To: <095993ea-8773-fafd-7d0c-4750517c76e9@linaro.org> (Adhemerval Zanella Netto's message of "Thu, 24 Aug 2023 12:43:17 -0300") Message-ID: <87a5ugz889.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: * Adhemerval Zanella Netto: > On 24/08/23 04:13, Florian Weimer wrote: >> * Adhemerval Zanella: >> >>> Returning a pidfd allows a process to keep a race-free handle for a >>> child process, otherwise, the caller will need to either use pidfd_open >>> (which still might be subject to TOCTOU) or keep the old racy interface >>> base on pid_t. >>> >>> The implementation makes sure that kernel must support the complete >>> pidfd interface, meaning that waitid (P_PIDFD) should be supported >>> (added on Linux 5.4). It ensures that a non-racy workaround is required >>> (such as reading procfs fdinfo pid to use along with wait interfaces). >> >> Sorry, I don't understand the second sentence. > > It is indeed confusing, I will change to: > > To correctly use pifd_spawn, the kernel must support not only returning > the pidfd with clone/clone3 but also waitid (P_PIDFD) (added on Linux 5.4). > If the kernel does not support the waitid, pidfd returns ENOSYS. It avoids > the need for racy workarounds, such as reading the procfs fdinfo to get the > pid to use along with other wait interfaces. Okay. >>> diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c >>> index f0d4c62ae6..d4ff23d955 100644 >>> --- a/sysdeps/unix/sysv/linux/spawni.c >>> +++ b/sysdeps/unix/sysv/linux/spawni.c >> >>> internal_signal_block_all (&args.oldmask); >>> @@ -386,13 +399,16 @@ __spawnix (pid_t * pid, const char *file, >>> /* Unsupported flags like CLONE_CLEAR_SIGHAND will be cleared up by >>> __clone_internal_fallback. */ >>> .flags = (set_cgroup ? CLONE_INTO_CGROUP : 0) >>> + | (use_pidfd ? CLONE_PIDFD : 0) >>> | CLONE_CLEAR_SIGHAND >>> | CLONE_VM >>> | CLONE_VFORK, >>> .exit_signal = SIGCHLD, >>> .stack = (uintptr_t) stack, >>> .stack_size = stack_size, >>> - .cgroup = (set_cgroup ? attrp->__cgroup : 0) >>> + .cgroup = (set_cgroup ? attrp->__cgroup : 0), >>> + .pidfd = use_pidfd ? (uintptr_t) &args.pidfd : 0, >>> + .parent_tid = use_pidfd ? (uintptr_t) &args.pidfd : 0, >> >> The .parent_tid line looks wrong? > > It is required for clone (and that's why you can't use CLONE_PIDFD with > CLONE_PARENT_SETTID). It could only set parent_tid on clone fallback, > but I think this is simpler. I will add a comment. Please use a separate variable, not args.pidfd, though. The current code depends on the order the kernel sets these fields, I think. Thanks, Florian