Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp126818pxu; Wed, 6 Jan 2021 23:57:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5+rfgFXtfgIUfqAJ0kN/8Pne+GLu+NU+SueukYFBr41tihG1s37QpKTxUgDRMu16ymi+J X-Received: by 2002:a17:906:94c5:: with SMTP id d5mr5264337ejy.427.1610006250377; Wed, 06 Jan 2021 23:57:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610006250; cv=none; d=google.com; s=arc-20160816; b=izcydi6ETIN+/7zgN0bD8SFGUsAmChA2xJ1017T02tBYwhK4bE9DAuLDgAvWH0bfP9 ZMSgguSiU1VcAqdbwLY3FE3YHcMNO0xnEbmcMsAQ9qUUo0VHajljXZS6Cfuv8j5ZPJgb +G5K3X+bGVnANlRis8/BRXSfS/7WlASv5OrLgwpZggEax/KlMIEFiD/qeog6oDwR5N/5 O7xyRUh8pqeu1EVZhRLBLpVU+AVa9st6F6VxyDJr5q0SJ65mH78c/iTzJCztjzN7faGx 9qvwzYd6mVKfKZTC5PCbqOzlG67cK/f5Kl2QvIsu7nbxHG/FAGP6EbStF68+22slYa+c TuSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Pp8uExFY3AbD9xuZZKWOBwOzO08wXO030h1ZIqCAUgk=; b=0GdhnE5ZuAI/SKva0rCk01UZmjFhOJSqOZHtsp5vtCpQHoSNOSurFOY9TBg7dSQnRo 2wPGgGgm2GbKckBYQA1iLHBk4h9ufplSRXTXEyvP1f92S4CSCBo964rCmmOQSdYt6MBS oUFTST6KT/D8bPBGsC6AZV7rsJk0cLS9S2N07e2uWlObAN34Vhj9k2gdEZDZwiMIxYMK Qfx0s9P+ja3U2ese88rwZZbJBrthrxErMabqn3/bhRP2wF/m6EwLvDAEILXkn8xMExri KYiY+o6FD+YMr3a7+svWfoJ/y8K3l4hxxlEp3tJQ5H2zxxBZF/n2VfK8DgfT0wSdOGFu XdnQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oy13si1846564ejb.397.2021.01.06.23.57.07; Wed, 06 Jan 2021 23:57:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727466AbhAGHyF (ORCPT + 99 others); Thu, 7 Jan 2021 02:54:05 -0500 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:52721 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727415AbhAGHyD (ORCPT ); Thu, 7 Jan 2021 02:54:03 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R261e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04394;MF=wenyang@linux.alibaba.com;NM=1;PH=DS;RN=20;SR=0;TI=SMTPD_---0UKzE3SA_1610005997; Received: from localhost(mailfrom:wenyang@linux.alibaba.com fp:SMTPD_---0UKzE3SA_1610005997) by smtp.aliyun-inc.com(127.0.0.1); Thu, 07 Jan 2021 15:53:17 +0800 From: Wen Yang To: Greg Kroah-Hartman , Sasha Levin Cc: Xunlei Pang , linux-kernel@vger.kernel.org, "Joel Fernandes (Google)" , Andy Lutomirski , Steven Rostedt , Daniel Colascione , Jann Horn , Tim Murray , Jonathan Kowalski , Linus Torvalds , Al Viro , Kees Cook , David Howells , Oleg Nesterov , kernel-team@android.com, Christian Brauner , stable@vger.kernel.org, Wen Yang Subject: [PATCH 4.19 2/7] pidfd: add polling support Date: Thu, 7 Jan 2021 15:53:09 +0800 Message-Id: <20210107075314.62683-3-wenyang@linux.alibaba.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20210107075314.62683-1-wenyang@linux.alibaba.com> References: <20210107075314.62683-1-wenyang@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Joel Fernandes (Google)" [ Upstream commit b53b0b9d9a613c418057f6cb921c2f40a6f78c24 ] This patch adds polling support to pidfd. Android low memory killer (LMK) needs to know when a process dies once it is sent the kill signal. It does so by checking for the existence of /proc/pid which is both racy and slow. For example, if a PID is reused between when LMK sends a kill signal and checks for existence of the PID, since the wrong PID is now possibly checked for existence. Using the polling support, LMK will be able to get notified when a process exists in race-free and fast way, and allows the LMK to do other things (such as by polling on other fds) while awaiting the process being killed to die. For notification to polling processes, we follow the same existing mechanism in the kernel used when the parent of the task group is to be notified of a child's death (do_notify_parent). This is precisely when the tasks waiting on a poll of pidfd are also awakened in this patch. We have decided to include the waitqueue in struct pid for the following reasons: 1. The wait queue has to survive for the lifetime of the poll. Including it in task_struct would not be option in this case because the task can be reaped and destroyed before the poll returns. 2. By including the struct pid for the waitqueue means that during de_thread(), the new thread group leader automatically gets the new waitqueue/pid even though its task_struct is different. Appropriate test cases are added in the second patch to provide coverage of all the cases the patch is handling. Cc: Andy Lutomirski Cc: Steven Rostedt Cc: Daniel Colascione Cc: Jann Horn Cc: Tim Murray Cc: Jonathan Kowalski Cc: Linus Torvalds Cc: Al Viro Cc: Kees Cook Cc: David Howells Cc: Oleg Nesterov Cc: kernel-team@android.com Reviewed-by: Oleg Nesterov Co-developed-by: Daniel Colascione Signed-off-by: Daniel Colascione Signed-off-by: Joel Fernandes (Google) Signed-off-by: Christian Brauner Cc: # 4.19.x Signed-off-by: Wen Yang --- include/linux/pid.h | 3 +++ kernel/fork.c | 26 ++++++++++++++++++++++++++ kernel/pid.c | 2 ++ kernel/signal.c | 11 +++++++++++ 4 files changed, 42 insertions(+) diff --git a/include/linux/pid.h b/include/linux/pid.h index 29c0a99..a82d2f7 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -3,6 +3,7 @@ #define _LINUX_PID_H #include +#include enum pid_type { @@ -60,6 +61,8 @@ struct pid unsigned int level; /* lists of tasks that use this pid */ struct hlist_head tasks[PIDTYPE_MAX]; + /* wait queue for pidfd notifications */ + wait_queue_head_t wait_pidfd; struct rcu_head rcu; struct upid numbers[1]; }; diff --git a/kernel/fork.c b/kernel/fork.c index e419891..33dc746 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1688,8 +1688,34 @@ static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) } #endif +/* + * Poll support for process exit notification. + */ +static unsigned int pidfd_poll(struct file *file, struct poll_table_struct *pts) +{ + struct task_struct *task; + struct pid *pid = file->private_data; + int poll_flags = 0; + + poll_wait(file, &pid->wait_pidfd, pts); + + rcu_read_lock(); + task = pid_task(pid, PIDTYPE_PID); + /* + * Inform pollers only when the whole thread group exits. + * If the thread group leader exits before all other threads in the + * group, then poll(2) should block, similar to the wait(2) family. + */ + if (!task || (task->exit_state && thread_group_empty(task))) + poll_flags = POLLIN | POLLRDNORM; + rcu_read_unlock(); + + return poll_flags; +} + const struct file_operations pidfd_fops = { .release = pidfd_release, + .poll = pidfd_poll, #ifdef CONFIG_PROC_FS .show_fdinfo = pidfd_show_fdinfo, #endif diff --git a/kernel/pid.c b/kernel/pid.c index b88fe5e..3ba6fcb 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -214,6 +214,8 @@ struct pid *alloc_pid(struct pid_namespace *ns) for (type = 0; type < PIDTYPE_MAX; ++type) INIT_HLIST_HEAD(&pid->tasks[type]); + init_waitqueue_head(&pid->wait_pidfd); + upid = pid->numbers + ns->level; spin_lock_irq(&pidmap_lock); if (!(ns->pid_allocated & PIDNS_ADDING)) diff --git a/kernel/signal.c b/kernel/signal.c index a02a25a..22a04795 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1810,6 +1810,14 @@ int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) return ret; } +static void do_notify_pidfd(struct task_struct *task) +{ + struct pid *pid; + + pid = task_pid(task); + wake_up_all(&pid->wait_pidfd); +} + /* * Let a parent know about the death of a child. * For a stopped/continued status change, use do_notify_parent_cldstop instead. @@ -1833,6 +1841,9 @@ bool do_notify_parent(struct task_struct *tsk, int sig) BUG_ON(!tsk->ptrace && (tsk->group_leader != tsk || !thread_group_empty(tsk))); + /* Wake up all pidfd waiters */ + do_notify_pidfd(tsk); + if (sig != SIGCHLD) { /* * This is only possible if parent == real_parent. -- 1.8.3.1