Message ID | 20240131132541.GA23632@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-46494-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1886774dyb; Wed, 31 Jan 2024 05:29:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAZeBvBqewXhgwBDXKayEY7kQalBeUoD/39hehOHc8f3WKeB9VlwQC+S+/7j95Bxg4/rhi X-Received: by 2002:a17:90b:2395:b0:294:6b8b:369a with SMTP id mr21-20020a17090b239500b002946b8b369amr1640448pjb.20.1706707767386; Wed, 31 Jan 2024 05:29:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706707767; cv=pass; d=google.com; s=arc-20160816; b=xldLyDCft/akqp7jhweQGL/xEjeJLgMlRRxPbEqCfuGfxclpc1i+rWLxPM9H0S/8hf GqWtt6wG3f7tT1dhoG4J/GidpfQfzHnTbmh/H7jEsbS3d6wBRf9RUkEwWBsdQF71fc2F P8kQ9xi7kobz56qlI4Hs2ST0SQs9+avU+dHkyk12FVt+3M+/aXjBsivMhPUSkELGF3B5 8qdblZZK09SEJbWssSHCdqa4gk2c7BeStYS2nK6BCdKerH65vjG+ZsmBnRotCODYLXHF Wqp9gmgvmquTrZe7Pv4YGWneipjjdgPcXcBCNBjeumWkkEQgcyUzzJnDQ9D04RTsrMcA 98Aw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:subject:cc:to:from :date:dkim-signature; bh=AEoHzKgqw9sGTXanl1cSpEXOm2keGIi49ypqIIavTQY=; fh=cMuqLFH3cbUbplns8vkBBmO6zzUPtSwB8XkfXbdR92c=; b=NL7x54R4wdM4EvoY9CTwNVaq/XEyFi9N1AgyHT5hS1rL4rdYwhNVBJQLA4s1zeqLIF ilSUNXMDKFC70j384+QZ9WeHCF39R6OT1Gr9ZsMsoYHSjPNW44A7n1BugplwaeSwqNT3 yNiBWq86cpXF+BNZu6fp4j1Ylr4HdjB/CxjDziCd3w0oVjo5DvwEiK6w+8fUn6v5afbF 27c1EKYFxl9RYP5avVE8egsmB/+uiV7gj9bz+XkAYsV1YNe5RuzuxkmtsC59VIX0AQIC Y0bC0TA8aGACwG7Cwnum1iA/77UKY6+hbjwKCBePniqaQgEHHZBj7iuPPgXMXMlYwHry GAcg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QAr2T3qJ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-46494-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46494-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCUeDt0wshU0Ci63o8X9Xh/B4S45+oPUQYQ44eM422gZNTJCMkYLKWnX61eCoBGr40PwuSH218oda1UUFwiesHIvrrurgA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b7-20020a17090a12c700b0028ffc9c1453si1235001pjg.21.2024.01.31.05.29.27 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 05:29:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46494-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QAr2T3qJ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-46494-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46494-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7F27A287901 for <ouuuleilei@gmail.com>; Wed, 31 Jan 2024 13:27:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F420D7F462; Wed, 31 Jan 2024 13:27:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QAr2T3qJ" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 963ED7BB03 for <linux-kernel@vger.kernel.org>; Wed, 31 Jan 2024 13:27:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706707625; cv=none; b=mASPmnW8Iu4rl3DnjhgI7XR/jj29oXL3EP6TgoonuZ80rNAkZeb5N4dhGWw7XMZ4RtofypV8eqjq7YREOPFciOBJoY5rj8g/yBXADGYzdmzLEv/UTBTHWVMthRnTdpj1FhlHUGIyPLzmDk60qX6ZSZkvZ4kN6oscImws3qBS5Ow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706707625; c=relaxed/simple; bh=x2alkTFdX9GtAAjAdf9d5pBfRjBGsbqEAc0JOC49oYY=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=F4HH/clEBRYJrUkOQXPWQytGFPkmXGmK7sG5NsaQ6ccd/1M2wzbyZJRXPk1gd/P5gs4lj6jOOB8COoYb6A1PwkX5rB1SUEiVQg87urcLUNMTZbkiaegS4VPxdk3VoGzhz94TPVAsLMU4PDdTLhtZVAaQbMRRk5vErmrx1oaUqtQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QAr2T3qJ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706707622; 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; bh=AEoHzKgqw9sGTXanl1cSpEXOm2keGIi49ypqIIavTQY=; b=QAr2T3qJ4T+rqV6FPAgKZXl+sKCdxgVaDuq730gcnGxwv1rCTuF6gPZTlzrj09wTRIdqmz qsOZwsDLZMfUEDsu1UEO74i8a35a2snAd5S3zDLE8V/DnWTduCXeNsQSrzkRyPjwMrO7C7 lcAz21UGL1SSCPfoAm995Hke8j+OpCw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-466-ryf6m05BOHuqu6haSIRe3w-1; Wed, 31 Jan 2024 08:26:58 -0500 X-MC-Unique: ryf6m05BOHuqu6haSIRe3w-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5EFE0101A526; Wed, 31 Jan 2024 13:26:58 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.249]) by smtp.corp.redhat.com (Postfix) with SMTP id 0D16140C122E; Wed, 31 Jan 2024 13:26:56 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Wed, 31 Jan 2024 14:25:43 +0100 (CET) Date: Wed, 31 Jan 2024 14:25:41 +0100 From: Oleg Nesterov <oleg@redhat.com> To: Christian Brauner <brauner@kernel.org> Cc: "Eric W. Biederman" <ebiederm@xmission.com>, Tycho Andersen <tycho@tycho.pizza>, linux-kernel@vger.kernel.org Subject: [PATCH v3 0/1] pidfd: implement PIDFD_THREAD flag for pidfd_open() Message-ID: <20240131132541.GA23632@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789612803862687629 X-GMAIL-MSGID: 1789612803862687629 |
Commit Message
Oleg Nesterov
Jan. 31, 2024, 1:25 p.m. UTC
Please see the interdiff below. Also, I updated the changelog to document that the behaviour of pidfd_poll(PIDFD_THREAD, pid-of-group-leader) is not well defined if a sub-thread execs. Do you agree with this semantics? Oleg. ---
Comments
On Wed, Jan 31, 2024 at 02:25:41PM +0100, Oleg Nesterov wrote: > Please see the interdiff below. > > Also, I updated the changelog to document that the behaviour of > pidfd_poll(PIDFD_THREAD, pid-of-group-leader) is not well defined > if a sub-thread execs. > > Do you agree with this semantics? Yeah, that seems fine to me.
Before I forget this... After this patch we can easily add another feature, pidfd_poll() can add, say, POLLHUP to poll_flags if the pid is "dead". So the user can do poll(pidfd, { .revents = POLLHUP }); and it will block until release_task() is called and this pid is no longer in use (pid_task() == NULL). Do you think this can be useful? Oleg.
On Wed, Jan 31, 2024 at 03:12:04PM +0100, Oleg Nesterov wrote: > Before I forget this... > > After this patch we can easily add another feature, pidfd_poll() > can add, say, POLLHUP to poll_flags if the pid is "dead". > > So the user can do > > poll(pidfd, { .revents = POLLHUP }); > > and it will block until release_task() is called and this pid is > no longer in use (pid_task() == NULL). > > Do you think this can be useful? Yeah, I think this is something that people would find useful. IIUC, it would essentially allow them to do things like wait until a task has been waited upon which for service managers is very useful information. --- Btw, bigger picture for a second. You probably haven't kept track of this but the fact that we got pidfds - thanks a lot to your review and help - has made quite a huge difference in userspace. Since we last did any meaningful work we got: * systemd completely relying on pidfds to manage services to guard against any pid races. * Extended dbus to allow authentication via pidfds. * Extended policy kit to enable secure authentication of processes via pidfds. * Language support for pidfds: Go, Rust etc. * An endless number of tools that added support for them. * glibc support for pidfd apis. There's a bunch more. That literally obliterated whole bug classes. I think with PIDFD_THREAD support it might make it interesting to use it for some pthread management as well.
On 01/31, Christian Brauner wrote: > > On Wed, Jan 31, 2024 at 03:12:04PM +0100, Oleg Nesterov wrote: > > > > After this patch we can easily add another feature, pidfd_poll() > > can add, say, POLLHUP to poll_flags if the pid is "dead". > > > > So the user can do > > > > poll(pidfd, { .revents = POLLHUP }); > > > > and it will block until release_task() is called and this pid is > > no longer in use (pid_task() == NULL). > > > > Do you think this can be useful? > > Yeah, I think this is something that people would find useful. IIUC, it > would essentially allow them to do things like wait until a task has > been waited upon Exactly. OK. I'll try to make the (hopefully simple) patch on top of this one on Friday, if Tycho agrees with V3. Will be busy tomorrow. > * systemd completely relying on pidfds to manage services to guard > against any pid races. > * Extended dbus to allow authentication via pidfds. > * Extended policy kit to enable secure authentication of processes via pidfds. > * Language support for pidfds: Go, Rust etc. > * An endless number of tools that added support for them. > * glibc support for pidfd apis. > > There's a bunch more. That literally obliterated whole bug classes. Thanks for this info! Not that I ever thouhgt that pidfd is "useless", not at all, but as I said (and as a Perl progammer ;) I simply do not know what people actually do with pidfds ;) Oleg.
diff --git a/fs/exec.c b/fs/exec.c index 73e4045df271..0fd7e668c477 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1143,7 +1143,11 @@ static int de_thread(struct task_struct *tsk) BUG_ON(leader->exit_state != EXIT_ZOMBIE); leader->exit_state = EXIT_DEAD; - + /* + * leader and tsk exhanged their pids, the old pid dies, + * wake up the PIDFD_THREAD waiters. + */ + do_notify_pidfd(leader); /* * We are going to release_task()->ptrace_unlink() silently, * the tracer can sleep in do_wait(). EXIT_DEAD guarantees diff --git a/include/linux/pid.h b/include/linux/pid.h index e6a041cb8bac..8124d57752b9 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -70,10 +70,11 @@ extern const struct file_operations pidfd_fops; struct file; -extern struct pid *pidfd_pid(const struct file *file); +struct pid *pidfd_pid(const struct file *file); struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags); struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags); int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret); +void do_notify_pidfd(struct task_struct *task); static inline struct pid *get_pid(struct pid *pid) { diff --git a/kernel/signal.c b/kernel/signal.c index 5f48d2c4b409..9b40109f0c56 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -2019,7 +2019,7 @@ int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) return ret; } -static void do_notify_pidfd(struct task_struct *task) +void do_notify_pidfd(struct task_struct *task) { struct pid *pid;