From patchwork Fri Jul 8 08:21:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 12910722 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 BCE5AC43334 for ; Fri, 8 Jul 2022 08:22:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 592EA6B0073; Fri, 8 Jul 2022 04:22:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 51E1E6B0074; Fri, 8 Jul 2022 04:22:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3953E8E0001; Fri, 8 Jul 2022 04:22:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2C0A76B0073 for ; Fri, 8 Jul 2022 04:22:56 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id E925D805F8 for ; Fri, 8 Jul 2022 08:22:55 +0000 (UTC) X-FDA: 79663241910.27.91D9E8D Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf29.hostedemail.com (Postfix) with ESMTP id 8E33012004C for ; Fri, 8 Jul 2022 08:22:55 +0000 (UTC) Received: by mail-pj1-f46.google.com with SMTP id w24so17462462pjg.5 for ; Fri, 08 Jul 2022 01:22:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KWf5dtXssMqsGOz7VAfXoh+v1Z2hqWDoP8+0nbo6wHo=; b=m0PYz6XwlosOdsP+9iwryUoydSkHiDwsygkPzmDiJ8OAMhJc7xJghUUzA7I2zcptug jqR+u+fNiLVVZ33aytEaErpcgQwFvATwbeNUs5ygSQDeyx2nP73Pc2ZOA8RjiHxvUjc8 tXuKr6alGYPRffom/o3tmmrWW27BX/q82+CZEBpY1UXlMHgJiNHt0cIjtUBA6Nox/NlE W4q1PS1pjn+6e6A2xxNP3/p/G3K/iuWp07vpnVmbKtiC+FEixbZ/JquZPPJj1Gk1q4i0 nXWIYhpROF75IR6nVRGDkPzlhfT+MNGx+7x1qU5olypFlgu1PqydGNNBJgcdQ8Cnn6K2 IhtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KWf5dtXssMqsGOz7VAfXoh+v1Z2hqWDoP8+0nbo6wHo=; b=ZX6Cc+bBuZTJM7zsVbbMQMFUknGoqOGn7Xz7LbSXSfTwIln7UIyDTTbuxFaWvZI87p n9IIC8EPsT/WbJEG2o3LxwI78N0OEGp7+zL0mBAe8I8zgUfrPBLsfUx1cyvoE5LT3vcC lbPwxV/V6buyEdsZfYeSHhwwCU3SuOq3REbn5CQTUAKHVW/pq/EG2SO9K10IYhZP3Je6 dDurFiLRjhC9d4JDHXIDWVqmr1Mu3AlrIAmTb4C/GDA1U0PUnXZNX2abRMlKxbKyHzaB 0eIcAA6Q+A19ozId3c/6JgLG+b1ti6jsQ13TjH+G/MYqPqXXgOpgpyNAeSaB6d8VSxS1 hE3A== X-Gm-Message-State: AJIora9mQGBPRDxXb1nwxXgxcPb2Q0ZmtUa2VgDBKt7mb/kX5KNZ1f7e GrEVV191zPMMxZktmrE4/Afmyg== X-Google-Smtp-Source: AGRyM1sXw4OaPJ/ttATPWPzLoQNiXr1HO7oR/PrceAT9cX2L07mrACBC5lCbCAIytcQOPDZR6J8YpA== X-Received: by 2002:a17:902:7618:b0:16a:23ec:75f6 with SMTP id k24-20020a170902761800b0016a23ec75f6mr2465799pll.158.1657268574396; Fri, 08 Jul 2022 01:22:54 -0700 (PDT) Received: from C02FT5A6MD6R.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id x65-20020a636344000000b00412b1043f33sm3329291pgb.39.2022.07.08.01.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 01:22:53 -0700 (PDT) From: Gang Li To: mhocko@suse.com, akpm@linux-foundation.org, surenb@google.com Cc: hca@linux.ibm.com, gor@linux.ibm.com, agordeev@linux.ibm.com, borntraeger@linux.ibm.com, svens@linux.ibm.com, viro@zeniv.linux.org.uk, ebiederm@xmission.com, keescook@chromium.org, rostedt@goodmis.org, mingo@redhat.com, peterz@infradead.org, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, david@redhat.com, imbrenda@linux.ibm.com, adobriyan@gmail.com, yang.yang29@zte.com.cn, brauner@kernel.org, stephen.s.brennan@oracle.com, zhengqi.arch@bytedance.com, haolee.swjtu@gmail.com, xu.xin16@zte.com.cn, Liam.Howlett@Oracle.com, ohoono.kwon@samsung.com, peterx@redhat.com, arnd@arndb.de, shy828301@gmail.com, alex.sierra@amd.com, xianting.tian@linux.alibaba.com, willy@infradead.org, ccross@google.com, vbabka@suse.cz, sujiaxun@uniontech.com, sfr@canb.auug.org.au, vasily.averin@linux.dev, mgorman@suse.de, vvghjk1234@gmail.com, tglx@linutronix.de, luto@kernel.org, bigeasy@linutronix.de, fenghua.yu@intel.com, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-perf-users@vger.kernel.org, Gang Li Subject: [PATCH v2 2/5] mm: add numa_count field for rss_stat Date: Fri, 8 Jul 2022 16:21:26 +0800 Message-Id: <20220708082129.80115-3-ligang.bdlg@bytedance.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220708082129.80115-1-ligang.bdlg@bytedance.com> References: <20220708082129.80115-1-ligang.bdlg@bytedance.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=m0PYz6Xw; spf=pass (imf29.hostedemail.com: domain of ligang.bdlg@bytedance.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=ligang.bdlg@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657268575; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KWf5dtXssMqsGOz7VAfXoh+v1Z2hqWDoP8+0nbo6wHo=; b=OKxcj6WJQ8JGhMpuwbJnwSW9GGZ0epop0Gq/+F5R+V8hUKkRxPD53uhCXBZCDvlniD5pgi f51cdQP1yEZm/Hwv5E+tRmlR51RmtBeUvgVSQft9jlbYcpOkX8o5q3a60WbbPcu5ZFr4V1 oREGVUssDFocWDlqtIbhufBo8zAbF1o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657268575; a=rsa-sha256; cv=none; b=pBaJEzoW4YfLb9eQmGCWRp81bIHflsfUYtSKoS7K1ZMxKobxJ+oKmtrSLcaMymW/ONSIcJ egmrbMMIK6e2FN9kb96bG2gZO4PtsWxIPJDKBq6Zf9g1HPDSVuZLDtB7JwElJayv0uEQcp rMS2z/vRKo3/wTiSMePK3dOJFKW5Fm0= X-Rspam-User: X-Stat-Signature: d3omsksn3qa53etna3dx4derroesay6x X-Rspamd-Queue-Id: 8E33012004C Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=m0PYz6Xw; spf=pass (imf29.hostedemail.com: domain of ligang.bdlg@bytedance.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=ligang.bdlg@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam11 X-HE-Tag: 1657268575-644756 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This patch add new fields `numa_count` for mm_rss_stat and task_rss_stat. `numa_count` are in the size of `sizeof(long) * num_possible_numa()`. To reduce mem consumption, they only contain the sum of rss which is needed by `oom_badness` instead of recording different kinds of rss sepratly. Signed-off-by: Gang Li --- include/linux/mm_types_task.h | 6 +++ kernel/fork.c | 70 +++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h index 32512af31721..9fd34ab484f4 100644 --- a/include/linux/mm_types_task.h +++ b/include/linux/mm_types_task.h @@ -52,11 +52,17 @@ enum { struct task_rss_stat { int events; /* for synchronization threshold */ int count[NR_MM_COUNTERS]; +#ifdef CONFIG_NUMA + int *numa_count; +#endif }; #endif /* USE_SPLIT_PTE_PTLOCKS */ struct mm_rss_stat { atomic_long_t count[NR_MM_COUNTERS]; +#ifdef CONFIG_NUMA + atomic_long_t *numa_count; +#endif }; struct page_frag { diff --git a/kernel/fork.c b/kernel/fork.c index 23f0ba3affe5..f4f93d6fecd5 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -140,6 +140,10 @@ DEFINE_PER_CPU(unsigned long, process_counts) = 0; __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ +#if (defined SPLIT_RSS_COUNTING) && (defined CONFIG_NUMA) +#define SPLIT_RSS_NUMA_COUNTING +#endif + #ifdef CONFIG_PROVE_RCU int lockdep_tasklist_lock_is_held(void) { @@ -757,6 +761,16 @@ static void check_mm(struct mm_struct *mm) mm, resident_page_types[i], x); } +#ifdef CONFIG_NUMA + for (i = 0; i < num_possible_nodes(); i++) { + long x = atomic_long_read(&mm->rss_stat.numa_count[i]); + + if (unlikely(x)) + pr_alert("BUG: Bad rss-counter state mm:%p node:%d val:%ld\n", + mm, i, x); + } +#endif + if (mm_pgtables_bytes(mm)) pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", mm_pgtables_bytes(mm)); @@ -769,6 +783,29 @@ static void check_mm(struct mm_struct *mm) #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) +#ifdef CONFIG_NUMA +static inline void mm_free_rss_stat(struct mm_struct *mm) +{ + kfree(mm->rss_stat.numa_count); +} + +static inline int mm_init_rss_stat(struct mm_struct *mm) +{ + memset(&mm->rss_stat.count, 0, sizeof(mm->rss_stat.count)); + mm->rss_stat.numa_count = kcalloc(num_possible_nodes(), sizeof(atomic_long_t), GFP_KERNEL); + if (unlikely(!mm->rss_stat.numa_count)) + return -ENOMEM; + return 0; +} +#else +static inline void mm_free_rss_stat(struct mm_struct *mm) {} +static inline int mm_init_rss_stat(struct mm_struct *mm) +{ + memset(&mm->rss_stat.count, 0, sizeof(mm->rss_stat.count)); + return 0; +} +#endif + /* * Called when the last reference to the mm * is dropped: either by a lazy thread or by @@ -783,6 +820,7 @@ void __mmdrop(struct mm_struct *mm) destroy_context(mm); mmu_notifier_subscriptions_destroy(mm); check_mm(mm); + mm_free_rss_stat(mm); put_user_ns(mm->user_ns); mm_pasid_drop(mm); free_mm(mm); @@ -824,12 +862,22 @@ static inline void put_signal_struct(struct signal_struct *sig) free_signal_struct(sig); } +#ifdef SPLIT_RSS_NUMA_COUNTING +void rss_stat_free(struct task_struct *p) +{ + kfree(p->rss_stat.numa_count); +} +#else +void rss_stat_free(struct task_struct *p) {} +#endif + void __put_task_struct(struct task_struct *tsk) { WARN_ON(!tsk->exit_state); WARN_ON(refcount_read(&tsk->usage)); WARN_ON(tsk == current); + rss_stat_free(tsk); io_uring_free(tsk); cgroup_free(tsk); task_numa_free(tsk, true); @@ -956,6 +1004,7 @@ void set_task_stack_end_magic(struct task_struct *tsk) static struct task_struct *dup_task_struct(struct task_struct *orig, int node) { struct task_struct *tsk; + int *numa_count __maybe_unused; int err; if (node == NUMA_NO_NODE) @@ -977,9 +1026,16 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) #endif account_kernel_stack(tsk, 1); +#ifdef SPLIT_RSS_NUMA_COUNTING + numa_count = kcalloc(num_possible_nodes(), sizeof(int), GFP_KERNEL); + if (!numa_count) + goto free_stack; + tsk->rss_stat.numa_count = numa_count; +#endif + err = scs_prepare(tsk, node); if (err) - goto free_stack; + goto free_rss_stat; #ifdef CONFIG_SECCOMP /* @@ -1045,6 +1101,10 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) return tsk; +free_rss_stat: +#ifdef SPLIT_RSS_NUMA_COUNTING + kfree(numa_count); +#endif free_stack: exit_task_stack_account(tsk); free_thread_stack(tsk); @@ -1114,7 +1174,6 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, mm->map_count = 0; mm->locked_vm = 0; atomic64_set(&mm->pinned_vm, 0); - memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); spin_lock_init(&mm->page_table_lock); spin_lock_init(&mm->arg_lock); mm_init_cpumask(mm); @@ -1141,6 +1200,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, if (mm_alloc_pgd(mm)) goto fail_nopgd; + if (mm_init_rss_stat(mm)) + goto fail_nocontext; + if (init_new_context(p, mm)) goto fail_nocontext; @@ -2142,7 +2204,9 @@ static __latent_entropy struct task_struct *copy_process( p->io_uring = NULL; #endif -#if defined(SPLIT_RSS_COUNTING) +#ifdef SPLIT_RSS_NUMA_COUNTING + memset(&p->rss_stat, 0, sizeof(p->rss_stat) - sizeof(p->rss_stat.numa_count)); +#else memset(&p->rss_stat, 0, sizeof(p->rss_stat)); #endif