From patchwork Thu Feb 9 07:14:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: huyd12@chinatelecom.cn X-Patchwork-Id: 13134077 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7AFD9C636D7 for ; Thu, 9 Feb 2023 07:15:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E6D196B0071; Thu, 9 Feb 2023 02:15:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E1D926B0074; Thu, 9 Feb 2023 02:15:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBE656B0075; Thu, 9 Feb 2023 02:15:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B92C26B0071 for ; Thu, 9 Feb 2023 02:15:23 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8BEDF12012A for ; Thu, 9 Feb 2023 07:15:23 +0000 (UTC) X-FDA: 80446892526.20.DA1BC05 Received: from chinatelecom.cn (prt-mail.chinatelecom.cn [42.123.76.220]) by imf26.hostedemail.com (Postfix) with ESMTP id 6C5DC140006 for ; Thu, 9 Feb 2023 07:15:19 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf26.hostedemail.com: domain of huyd12@chinatelecom.cn designates 42.123.76.220 as permitted sender) smtp.mailfrom=huyd12@chinatelecom.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675926920; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/Sn0xTmataHHF3jjuxjbQTXAAPr4h0fwMye5ZieihX0=; b=XU3pV73FznvLR+4M0PEgzgEeKXlLv/DRdRlLcW8R0K3T4koGaRidCB3Tpe8kwrzfX7+zvf S6nE9scvm+Ab1vPMx6DHOYBWk1mSky94KvPw/YH+ue7uwvPjZ8SZmq26otjKJrsEGjKLE0 UadrfbWlK2eh5NqT5mWVcG5N0kzTAEI= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf26.hostedemail.com: domain of huyd12@chinatelecom.cn designates 42.123.76.220 as permitted sender) smtp.mailfrom=huyd12@chinatelecom.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675926920; a=rsa-sha256; cv=none; b=wejYhgJIUBgj68I9qkGQqutQH/QLDHYm9JqFErYGq4b9UOn4A8sA4I+2IJP0+Oai2yV4sC yugjG2WQes8N555stF27Adx1e5QemqVjGq4D+8sr1iAMw1MYogmVxjRn2ZbOyS1VQAjPsn NLb1jt9SbeqXuh/ozkKjy6VC1fe4kzE= HMM_SOURCE_IP: 172.18.0.188:33460.1299675246 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP Received: from clientip-10.133.8.199 (unknown [172.18.0.188]) by chinatelecom.cn (HERMES) with SMTP id 68AA52800BD; Thu, 9 Feb 2023 15:14:56 +0800 (CST) X-189-SAVE-TO-SEND: huyd12@chinatelecom.cn Received: from ([10.133.8.199]) by app0023 with ESMTP id 79a6d3fc04714ff6ba7bdd059d93cc5d for agruenba@redhat.com; Thu, 09 Feb 2023 15:15:12 CST X-Transaction-ID: 79a6d3fc04714ff6ba7bdd059d93cc5d X-Real-From: huyd12@chinatelecom.cn X-Receive-IP: 10.133.8.199 X-MEDUSA-Status: 0 From: To: , , , "'Christian Brauner'" , "'Michal Hocko'" , "'Andrew Morton'" Cc: , , References: <20230208094905.373-1-liuq131@chinatelecom.cn> In-Reply-To: <20230208094905.373-1-liuq131@chinatelecom.cn> Subject: =?eucgb2312_cn?b?u9i4tDogW1BBVENIXSBwaWQ6IGFkZCBoYW5kbGluZyBvZiB0b28gbWFu?= =?eucgb2312_cn?b?eSB6b21iaWUgcHJvY2Vzc2Vz?= Date: Thu, 9 Feb 2023 15:14:57 +0800 Message-ID: <000e01d93c56$3a4bcb00$aee36100$@chinatelecom.cn> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQDiPQqBEqp6eGY2GPbOHeYBBQy2tLC0MRXw Content-Language: zh-cn X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6C5DC140006 X-Stat-Signature: 3zx7h47kg6kb4ke7dpkess73ru8hx81d X-HE-Tag: 1675926919-883590 X-HE-Meta: U2FsdGVkX19qa3HofocKotGvEzRNlXEJZ0DeLZQs+ShvI8sAFl900GcB2GFc3vl93N5Mt6h/aa7/JRKLqVyjt3Jmbucw6XU7xwWJsTz/pDysamRjdvn5kPUA9t7PwWrS0iWIPYvnQ6EC59dEc+cMnB3+owJuMB98Dk5JvHrbbPhhg43XtWRgdqW0j4ngUhClvWtk8UIlAJ2MR6WKzPVxJk/vDRoM+d83PP6iu5BgfI0ilmHGkwbH1biod066dUDccaeepTSdlzQPudXjUAE5XqtfHnzx8ZzqgyuMSg0BzHhQV34d2pY3a3IsxWMwRoUGLVr+OAIjmPtdFK43kN8MU6K9COHVnNWg7NTtkOuolr14H1GJCUTKumUOjsKJzjl7QzhP1YbXIr34TdmijYN8ZoDGLA8owWPuM6QoTQDeR+tsYkdqU2qhrAu8xfRxO9zitOGs1+NYrPKVcer7Zg69CGpTX8wKtMbr2iby+00sI7ZPOLd7HzccsgFcdT/s5Wb1sPkl1BI71GB51sN9LpOHsHlr+2+sbppriUrf3WnvK9YBTWBXKVtxQYECTp60tT9YSySNiKEdmOGS5EeG+xfaGOwks/u3JXYqt0/XtKvt72Obfw5+ThL1EhA4zURz81KtdRCZGhwI3Cf0QS9jwzVtVKWrXI4XgVk5iJJxfgBRvyFoNxaCc7ZZ4TbkLdXvdKbWJuTn2wWoTT1yy5Cwv65AUs6YzrHQRmfHBJA0hqBmBd6ByGjalZLTm3NQNfXCYCbnsWJZpgNB8Dr8eOQrUd2d49jApWOvBSJkps1jB6LAdyhvO2ZrFbKT8ofKZUxIafjd4l2OsSS2vOAYK3SIxiYY9Mm37eP+Zli1ZKfLe3tUYKMbzmBYsrC3r8XcqthhrlOfPcm7KbgcD8k2s/pZbohUuP8IbGJGQ8ptV9SUtnhD0yoyfe52uF4Ki9Hqoh7rHFhMFDuJiQzRxW4IlqaZJWj M0KiSe39 LiyI0qwA/vklou1iDMUP0o9y6PR1rOxK0plNxE5cc0VDTtxDkVuN1vA6sEaDUj/O/n1NB1T3z+7lBrtCGwwRd/OKWHUYjDpuLEC+kVj3QnRraeRFBVITKAYHZgDsnfmkqciUDzQ+Xhm2tCwlmK/kRpmOY3ZgMln9QcMqGa6SwKsbDjkix8CEIunf4hfrvO4+F6jy5yLX989GwlywXgIG1lAh1COrl1k+XE95DQ8Dp0yCXRdj+lIh4I5Dw8h3jvR8udYtKsv+oL0otWkX/i6Iiwa6i63tT73Fnf7uRn1AoPOlLpGqvIk27zd56jq5/2sT49nYYxmmtXQTlSr9R2ehDs5VpDTDQ7J3pEjN1/9qbPDNqCSHNhbXVsjcjHLWR0Xq7v/kZWKb5W3y867M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Any comments will be appreciated. -----邮件原件----- 发件人: liuq131@chinatelecom.cn 发送时间: 2023年2月8日 17:49 收件人: akpm@linux-foundation.org 抄送: agruenba@redhat.com; linux-mm@kvack.org; linux-kernel@vger.kernel.org; huyd12@chinatelecom.cn; liuq 主题: [PATCH] pid: add handling of too many zombie processes There is a common situation that a parent process forks many child processes to execute tasks, but the parent process does not execute wait/waitpid when the child process exits, resulting in a large number of child processes becoming zombie processes. At this time, if the number of processes in the system out of kernel.pid_max, the new fork syscall will fail, and the system will not be able to execute any command at this time (unless an old process exits) eg: [root@lq-workstation ~]# ls -bash: fork: retry: Resource temporarily unavailable -bash: fork: retry: Resource temporarily unavailable -bash: fork: retry: Resource temporarily unavailable -bash: fork: retry: Resource temporarily unavailable -bash: fork: Resource temporarily unavailable [root@lq-workstation ~]# reboot -bash: fork: retry: Resource temporarily unavailable -bash: fork: retry: Resource temporarily unavailable -bash: fork: retry: Resource temporarily unavailable -bash: fork: retry: Resource temporarily unavailable -bash: fork: Resource temporarily unavailable I dealt with this situation in the alloc_pid function, and found a process with the most zombie subprocesses, and more than 10(or other reasonable values?) zombie subprocesses, so I tried to kill this process to release the pid resources. Signed-off-by: liuq --- include/linux/mm.h | 2 ++ kernel/pid.c | 6 +++- mm/oom_kill.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f857163ac89..afcff08a3878 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1940,6 +1940,8 @@ static inline void clear_page_pfmemalloc(struct page *page) * Can be called by the pagefault handler when it gets a VM_FAULT_OOM. */ extern void pagefault_out_of_memory(void); +extern void pid_max_oom_check(struct pid_namespace *ns); + #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK) #define offset_in_thp(page, p) ((unsigned long)(p) & (thp_size(page) - 1)) diff --git a/kernel/pid.c b/kernel/pid.c index 3fbc5e46b721..1a9a60e19ab6 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -237,7 +237,11 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, idr_preload_end(); if (nr < 0) { - retval = (nr == -ENOSPC) ? -EAGAIN : nr; + retval = nr; + if (nr == -ENOSPC) { + retval = -EAGAIN; + pid_max_oom_check(tmp); + } goto out_free; } diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 1276e49b31b0..18d05d706f48 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1260,3 +1260,73 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) return -ENOSYS; #endif /* CONFIG_MMU */ } + +static void oom_pid_evaluate_task(struct task_struct *p, + struct task_struct **max_zombie_task, int *max_zombie_num) { + struct task_struct *child; + int zombie_num = 0; + + list_for_each_entry(child, &p->children, sibling) { + if (child->exit_state == EXIT_ZOMBIE) + zombie_num++; + } + if (zombie_num > *max_zombie_num) { + *max_zombie_num = zombie_num; + *max_zombie_task = p; + } +} +#define MAX_ZOMBIE_NUM 10 +struct task_struct *pid_max_bad_process(struct pid_namespace *ns) { + int max_zombie_num = 0; + struct task_struct *max_zombie_task = &init_task; + struct task_struct *p; + + rcu_read_lock(); + for_each_process(p) + oom_pid_evaluate_task(p, &max_zombie_task, &max_zombie_num); + rcu_read_unlock(); + + if (max_zombie_num > MAX_ZOMBIE_NUM) { + pr_info("process %d has %d zombie child\n", + task_pid_nr_ns(max_zombie_task, ns), max_zombie_num); + return max_zombie_task; + } + + return NULL; +} + +void pid_max_oom_kill_process(struct task_struct *task) { + struct oom_control oc = { + .zonelist = NULL, + .nodemask = NULL, + .memcg = NULL, + .gfp_mask = 0, + .order = 0, + }; + + get_task_struct(task); + oc.chosen = task; + + if (mem_cgroup_oom_synchronize(true)) + return; + + if (!mutex_trylock(&oom_lock)) + return; + + oom_kill_process(&oc, "Out of pid max(oom_kill_allocating_task)"); + mutex_unlock(&oom_lock); +} + +void pid_max_oom_check(struct pid_namespace *ns) { + struct task_struct *p; + + p = pid_max_bad_process(ns); + if (p) { + pr_info("oom_kill process %d\n", task_pid_nr_ns(p, ns)); + pid_max_oom_kill_process(p); + } +} -- 2.27.0