From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E042AC433DB for ; Wed, 23 Dec 2020 21:22:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FBEF2246B for ; Wed, 23 Dec 2020 21:22:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729154AbgLWVWc (ORCPT ); Wed, 23 Dec 2020 16:22:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729094AbgLWVWc (ORCPT ); Wed, 23 Dec 2020 16:22:32 -0500 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8401CC0617A6 for ; Wed, 23 Dec 2020 13:21:51 -0800 (PST) Received: by mail-lf1-x130.google.com with SMTP id y19so442572lfa.13 for ; Wed, 23 Dec 2020 13:21:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wF35nXj0WqYzyWdTSecMciE07b/6u11vJiyvqK4gz6s=; b=hRMjxvObgBylB6uesmR8k4mhN2Pag25RaGN4pEInOqMyvL7N0xNsE42tWYXG4UfS/K +XsNltY2tXUStsjEFN1DCG4drR9Yei1e1pwXBxYqxPOOmhk+RzpuwJEBcJEJBYbBaIWu THr2sXxeIA+/WiiHyFx6LUFoHZYjin8MgtY/Fqd5CQJswDcVyzUMDtXTGiPAs8ZJBB4L 1CoQWXGNI/OdkwADgVzfhifxid1LxM5XDGRYxrB3/R4eaYk1AtIE4JAQz8bGZBRTVKOW aQIZawR3t/pSfsVITaeUVJCoYWC5LAIFLAm210rOtmgvOq0Z8rwRmedSpAVH38lM7HqO bQog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wF35nXj0WqYzyWdTSecMciE07b/6u11vJiyvqK4gz6s=; b=cbAoN1/QbK+vzujeBL7p4hRNYdyPHYKrtIjkF/QymQ9SWGEutYUrZixZuiTFOUG0+N fxf76WIRVM3ym+1xpbenBk2Lzo/2140np5CHMaggSljCVt+9bIqwNmKt8OQd9Dgoz7fu U2DD7Zue4R3+3ploWgNSmvNuMmJo/RKpkj0wgz+DQftVQakD8NQwJJukZWeJw6Hch4IF zGaMIP+CU3SnlOPaq7lyeOHQXbaq+3piMS78PYRWajc7qzijFOIcdxPg9oKGhaB7mMSb 3XQmP6YAzi6WFkmocWglNKDlqXqSZBTYfsPRQn/zMtfPodLhf9bsa9UCkjEeLxpHAD2o toOQ== X-Gm-Message-State: AOAM532j3wytVD5Gauu+t0Xn8WDMYSChOZZRxEw1U6+3Gt/Y6oXynGm1 cxxul10IP2l43u6xqn4S5KkWO4ZtlCXi5pRHRE1MKg== X-Google-Smtp-Source: ABdhPJzHt5QoMR2hM01ksgvBri5qImtglXl7UpYz9rRyEvNbcOkgf4IEhhYCab297w4wPrrjMQKaK7bSjP/7n6Ddrt4= X-Received: by 2002:a05:6512:32ad:: with SMTP id q13mr3919043lfe.83.1608758509561; Wed, 23 Dec 2020 13:21:49 -0800 (PST) MIME-Version: 1.0 References: <20201217034356.4708-1-songmuchun@bytedance.com> <20201217034356.4708-8-songmuchun@bytedance.com> In-Reply-To: <20201217034356.4708-8-songmuchun@bytedance.com> From: Shakeel Butt Date: Wed, 23 Dec 2020 13:21:38 -0800 Message-ID: Subject: Re: [PATCH v5 7/7] mm: memcontrol: make the slab calculation consistent To: Muchun Song Cc: Greg Kroah-Hartman , rafael@kernel.org, Alexey Dobriyan , Andrew Morton , Johannes Weiner , Michal Hocko , Vladimir Davydov , Hugh Dickins , Roman Gushchin , Sami Tolvanen , Feng Tang , Neil Brown , Joonsoo Kim , Randy Dunlap , LKML , linux-fsdevel , Linux MM , Cgroups Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 16, 2020 at 7:46 PM Muchun Song wrote: > > Although the ratio of the slab is one, we also should read the ratio > from the related memory_stats instead of hard-coding. And the local > variable of size is already the value of slab_unreclaimable. So we > do not need to read again. > > To do this we need some code like below: > > if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { > - size = memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + > - memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B); > + size += memcg_page_state(memcg, memory_stats[i - 1].idx) * > + memory_stats[i - 1].ratio; > > It requires a series of BUG_ONs or comments to ensure these two > items are actually adjacent and in the right order. So it would > probably be easier to implement this using a wrapper that has a > big switch() for unit conversion. > > This would fix the ratio inconsistency and get rid of the order > guarantee. > The commit message is really confusing. It is explaining a situation which it did not do. I don't see any benefit of mentioning BUG_ONs or [i-1]s in the message. The patch makes sure that we use the right ratio for slab. Can you rewrite the commit message and motivate in just that regard? > Signed-off-by: Muchun Song > --- > mm/memcontrol.c | 105 +++++++++++++++++++++++++++++++++++--------------------- > 1 file changed, 66 insertions(+), 39 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index a40797a27f87..eec44918d373 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -1511,49 +1511,71 @@ static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) > > struct memory_stat { > const char *name; > - unsigned int ratio; > unsigned int idx; > }; > > static const struct memory_stat memory_stats[] = { > - { "anon", PAGE_SIZE, NR_ANON_MAPPED }, > - { "file", PAGE_SIZE, NR_FILE_PAGES }, > - { "kernel_stack", 1024, NR_KERNEL_STACK_KB }, > - { "pagetables", PAGE_SIZE, NR_PAGETABLE }, > - { "percpu", 1, MEMCG_PERCPU_B }, > - { "sock", PAGE_SIZE, MEMCG_SOCK }, > - { "shmem", PAGE_SIZE, NR_SHMEM }, > - { "file_mapped", PAGE_SIZE, NR_FILE_MAPPED }, > - { "file_dirty", PAGE_SIZE, NR_FILE_DIRTY }, > - { "file_writeback", PAGE_SIZE, NR_WRITEBACK }, > + { "anon", NR_ANON_MAPPED }, > + { "file", NR_FILE_PAGES }, > + { "kernel_stack", NR_KERNEL_STACK_KB }, > + { "pagetables", NR_PAGETABLE }, > + { "percpu", MEMCG_PERCPU_B }, > + { "sock", MEMCG_SOCK }, > + { "shmem", NR_SHMEM }, > + { "file_mapped", NR_FILE_MAPPED }, > + { "file_dirty", NR_FILE_DIRTY }, > + { "file_writeback", NR_WRITEBACK }, > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > - { "anon_thp", PAGE_SIZE, NR_ANON_THPS }, > - { "file_thp", PAGE_SIZE, NR_FILE_THPS }, > - { "shmem_thp", PAGE_SIZE, NR_SHMEM_THPS }, > + { "anon_thp", NR_ANON_THPS }, > + { "file_thp", NR_FILE_THPS }, > + { "shmem_thp", NR_SHMEM_THPS }, > #endif > - { "inactive_anon", PAGE_SIZE, NR_INACTIVE_ANON }, > - { "active_anon", PAGE_SIZE, NR_ACTIVE_ANON }, > - { "inactive_file", PAGE_SIZE, NR_INACTIVE_FILE }, > - { "active_file", PAGE_SIZE, NR_ACTIVE_FILE }, > - { "unevictable", PAGE_SIZE, NR_UNEVICTABLE }, > - > - /* > - * Note: The slab_reclaimable and slab_unreclaimable must be > - * together and slab_reclaimable must be in front. > - */ > - { "slab_reclaimable", 1, NR_SLAB_RECLAIMABLE_B }, > - { "slab_unreclaimable", 1, NR_SLAB_UNRECLAIMABLE_B }, > + { "inactive_anon", NR_INACTIVE_ANON }, > + { "active_anon", NR_ACTIVE_ANON }, > + { "inactive_file", NR_INACTIVE_FILE }, > + { "active_file", NR_ACTIVE_FILE }, > + { "unevictable", NR_UNEVICTABLE }, > + { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, > + { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, > > /* The memory events */ > - { "workingset_refault_anon", 1, WORKINGSET_REFAULT_ANON }, > - { "workingset_refault_file", 1, WORKINGSET_REFAULT_FILE }, > - { "workingset_activate_anon", 1, WORKINGSET_ACTIVATE_ANON }, > - { "workingset_activate_file", 1, WORKINGSET_ACTIVATE_FILE }, > - { "workingset_restore_anon", 1, WORKINGSET_RESTORE_ANON }, > - { "workingset_restore_file", 1, WORKINGSET_RESTORE_FILE }, > - { "workingset_nodereclaim", 1, WORKINGSET_NODERECLAIM }, > + { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, > + { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, > + { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, > + { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, > + { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, > + { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, > + { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, > }; > > +/* Translate stat items to the correct unit for memory.stat output */ > +static int memcg_page_state_unit(int item) > +{ > + switch (item) { > + case MEMCG_PERCPU_B: > + case NR_SLAB_RECLAIMABLE_B: > + case NR_SLAB_UNRECLAIMABLE_B: > + case WORKINGSET_REFAULT_ANON: > + case WORKINGSET_REFAULT_FILE: > + case WORKINGSET_ACTIVATE_ANON: > + case WORKINGSET_ACTIVATE_FILE: > + case WORKINGSET_RESTORE_ANON: > + case WORKINGSET_RESTORE_FILE: > + case WORKINGSET_NODERECLAIM: > + return 1; > + case NR_KERNEL_STACK_KB: > + return SZ_1K; > + default: > + return PAGE_SIZE; > + } > +} > + > +static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg, > + int item) > +{ > + return memcg_page_state(memcg, item) * memcg_page_state_unit(item); > +} > + > static char *memory_stat_format(struct mem_cgroup *memcg) > { > struct seq_buf s; > @@ -1577,13 +1599,12 @@ static char *memory_stat_format(struct mem_cgroup *memcg) > for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { > u64 size; > > - size = memcg_page_state(memcg, memory_stats[i].idx); > - size *= memory_stats[i].ratio; > + size = memcg_page_state_output(memcg, memory_stats[i].idx); > seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size); > > if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { > - size = memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + > - memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B); > + size += memcg_page_state_output(memcg, > + NR_SLAB_RECLAIMABLE_B); > seq_buf_printf(&s, "slab %llu\n", size); > } > } > @@ -6377,6 +6398,12 @@ static int memory_stat_show(struct seq_file *m, void *v) > } > > #ifdef CONFIG_NUMA > +static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, > + int item) > +{ > + return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item); > +} > + No need to have lruvec_page_state_output() separately as there is just one user. Just inline it. > static int memory_numa_stat_show(struct seq_file *m, void *v) > { > int i; > @@ -6394,8 +6421,8 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) > struct lruvec *lruvec; > > lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); > - size = lruvec_page_state(lruvec, memory_stats[i].idx); > - size *= memory_stats[i].ratio; > + size = lruvec_page_state_output(lruvec, > + memory_stats[i].idx); > seq_printf(m, " N%d=%llu", nid, size); > } > seq_putc(m, '\n'); > -- > 2.11.0 > From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB1AEC433DB for ; Wed, 23 Dec 2020 21:21:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2E44E22273 for ; Wed, 23 Dec 2020 21:21:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2E44E22273 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6ED096B00C0; Wed, 23 Dec 2020 16:21:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 673F96B00C1; Wed, 23 Dec 2020 16:21:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53C628D0026; Wed, 23 Dec 2020 16:21:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0111.hostedemail.com [216.40.44.111]) by kanga.kvack.org (Postfix) with ESMTP id 374D96B00C0 for ; Wed, 23 Dec 2020 16:21:52 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F25481EE6 for ; Wed, 23 Dec 2020 21:21:51 +0000 (UTC) X-FDA: 77625819222.03.waste44_5f014662746b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id C6A0A28A4E8 for ; Wed, 23 Dec 2020 21:21:51 +0000 (UTC) X-HE-Tag: waste44_5f014662746b X-Filterd-Recvd-Size: 12379 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Wed, 23 Dec 2020 21:21:51 +0000 (UTC) Received: by mail-lf1-f48.google.com with SMTP id m12so521041lfo.7 for ; Wed, 23 Dec 2020 13:21:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wF35nXj0WqYzyWdTSecMciE07b/6u11vJiyvqK4gz6s=; b=hRMjxvObgBylB6uesmR8k4mhN2Pag25RaGN4pEInOqMyvL7N0xNsE42tWYXG4UfS/K +XsNltY2tXUStsjEFN1DCG4drR9Yei1e1pwXBxYqxPOOmhk+RzpuwJEBcJEJBYbBaIWu THr2sXxeIA+/WiiHyFx6LUFoHZYjin8MgtY/Fqd5CQJswDcVyzUMDtXTGiPAs8ZJBB4L 1CoQWXGNI/OdkwADgVzfhifxid1LxM5XDGRYxrB3/R4eaYk1AtIE4JAQz8bGZBRTVKOW aQIZawR3t/pSfsVITaeUVJCoYWC5LAIFLAm210rOtmgvOq0Z8rwRmedSpAVH38lM7HqO bQog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wF35nXj0WqYzyWdTSecMciE07b/6u11vJiyvqK4gz6s=; b=uTlJ3QiC/aEa7jiFOWh7bLjLv8c+UD28huHLCbaai+s7PVyN9x8hi4F4lev72y1m3b J8mGcWk0VyKZXMfomEZa9+hbREJSouuhAceFkwiYWdWu96vAsi6IgjS4nvqKr8qDK1R4 EABXPHeVWJ3p1pyh/bXUfhPxVpeiiofh8nHuBma7jtyhrKNzI965ta9wKsbWWhhhOr4Q oP147yWn/zlgVaOWspfpmSaWMYiz443TB6QUZT3/mLmBbzv1iF3n4UhNKJnJ89HQ4VdD A1p6X5C4KHTu+rvtwtE57le9QmcTzAGjQJdZzCYhjsma5rf1Mank0jHHGtFf9+oZgi0K Vhdg== X-Gm-Message-State: AOAM532O6yXD92e06begZM51TBsvv8BL4vKHP/3qkuT1YxtChLR06A5u 8Rl4a1Qs0hUAF/TthCiBA6pfh33ZWQePuNMl5emqlg== X-Google-Smtp-Source: ABdhPJzHt5QoMR2hM01ksgvBri5qImtglXl7UpYz9rRyEvNbcOkgf4IEhhYCab297w4wPrrjMQKaK7bSjP/7n6Ddrt4= X-Received: by 2002:a05:6512:32ad:: with SMTP id q13mr3919043lfe.83.1608758509561; Wed, 23 Dec 2020 13:21:49 -0800 (PST) MIME-Version: 1.0 References: <20201217034356.4708-1-songmuchun@bytedance.com> <20201217034356.4708-8-songmuchun@bytedance.com> In-Reply-To: <20201217034356.4708-8-songmuchun@bytedance.com> From: Shakeel Butt Date: Wed, 23 Dec 2020 13:21:38 -0800 Message-ID: Subject: Re: [PATCH v5 7/7] mm: memcontrol: make the slab calculation consistent To: Muchun Song Cc: Greg Kroah-Hartman , rafael@kernel.org, Alexey Dobriyan , Andrew Morton , Johannes Weiner , Michal Hocko , Vladimir Davydov , Hugh Dickins , Roman Gushchin , Sami Tolvanen , Feng Tang , Neil Brown , Joonsoo Kim , Randy Dunlap , LKML , linux-fsdevel , Linux MM , Cgroups Content-Type: text/plain; charset="UTF-8" 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: On Wed, Dec 16, 2020 at 7:46 PM Muchun Song wrote: > > Although the ratio of the slab is one, we also should read the ratio > from the related memory_stats instead of hard-coding. And the local > variable of size is already the value of slab_unreclaimable. So we > do not need to read again. > > To do this we need some code like below: > > if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { > - size = memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + > - memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B); > + size += memcg_page_state(memcg, memory_stats[i - 1].idx) * > + memory_stats[i - 1].ratio; > > It requires a series of BUG_ONs or comments to ensure these two > items are actually adjacent and in the right order. So it would > probably be easier to implement this using a wrapper that has a > big switch() for unit conversion. > > This would fix the ratio inconsistency and get rid of the order > guarantee. > The commit message is really confusing. It is explaining a situation which it did not do. I don't see any benefit of mentioning BUG_ONs or [i-1]s in the message. The patch makes sure that we use the right ratio for slab. Can you rewrite the commit message and motivate in just that regard? > Signed-off-by: Muchun Song > --- > mm/memcontrol.c | 105 +++++++++++++++++++++++++++++++++++--------------------- > 1 file changed, 66 insertions(+), 39 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index a40797a27f87..eec44918d373 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -1511,49 +1511,71 @@ static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) > > struct memory_stat { > const char *name; > - unsigned int ratio; > unsigned int idx; > }; > > static const struct memory_stat memory_stats[] = { > - { "anon", PAGE_SIZE, NR_ANON_MAPPED }, > - { "file", PAGE_SIZE, NR_FILE_PAGES }, > - { "kernel_stack", 1024, NR_KERNEL_STACK_KB }, > - { "pagetables", PAGE_SIZE, NR_PAGETABLE }, > - { "percpu", 1, MEMCG_PERCPU_B }, > - { "sock", PAGE_SIZE, MEMCG_SOCK }, > - { "shmem", PAGE_SIZE, NR_SHMEM }, > - { "file_mapped", PAGE_SIZE, NR_FILE_MAPPED }, > - { "file_dirty", PAGE_SIZE, NR_FILE_DIRTY }, > - { "file_writeback", PAGE_SIZE, NR_WRITEBACK }, > + { "anon", NR_ANON_MAPPED }, > + { "file", NR_FILE_PAGES }, > + { "kernel_stack", NR_KERNEL_STACK_KB }, > + { "pagetables", NR_PAGETABLE }, > + { "percpu", MEMCG_PERCPU_B }, > + { "sock", MEMCG_SOCK }, > + { "shmem", NR_SHMEM }, > + { "file_mapped", NR_FILE_MAPPED }, > + { "file_dirty", NR_FILE_DIRTY }, > + { "file_writeback", NR_WRITEBACK }, > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > - { "anon_thp", PAGE_SIZE, NR_ANON_THPS }, > - { "file_thp", PAGE_SIZE, NR_FILE_THPS }, > - { "shmem_thp", PAGE_SIZE, NR_SHMEM_THPS }, > + { "anon_thp", NR_ANON_THPS }, > + { "file_thp", NR_FILE_THPS }, > + { "shmem_thp", NR_SHMEM_THPS }, > #endif > - { "inactive_anon", PAGE_SIZE, NR_INACTIVE_ANON }, > - { "active_anon", PAGE_SIZE, NR_ACTIVE_ANON }, > - { "inactive_file", PAGE_SIZE, NR_INACTIVE_FILE }, > - { "active_file", PAGE_SIZE, NR_ACTIVE_FILE }, > - { "unevictable", PAGE_SIZE, NR_UNEVICTABLE }, > - > - /* > - * Note: The slab_reclaimable and slab_unreclaimable must be > - * together and slab_reclaimable must be in front. > - */ > - { "slab_reclaimable", 1, NR_SLAB_RECLAIMABLE_B }, > - { "slab_unreclaimable", 1, NR_SLAB_UNRECLAIMABLE_B }, > + { "inactive_anon", NR_INACTIVE_ANON }, > + { "active_anon", NR_ACTIVE_ANON }, > + { "inactive_file", NR_INACTIVE_FILE }, > + { "active_file", NR_ACTIVE_FILE }, > + { "unevictable", NR_UNEVICTABLE }, > + { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, > + { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, > > /* The memory events */ > - { "workingset_refault_anon", 1, WORKINGSET_REFAULT_ANON }, > - { "workingset_refault_file", 1, WORKINGSET_REFAULT_FILE }, > - { "workingset_activate_anon", 1, WORKINGSET_ACTIVATE_ANON }, > - { "workingset_activate_file", 1, WORKINGSET_ACTIVATE_FILE }, > - { "workingset_restore_anon", 1, WORKINGSET_RESTORE_ANON }, > - { "workingset_restore_file", 1, WORKINGSET_RESTORE_FILE }, > - { "workingset_nodereclaim", 1, WORKINGSET_NODERECLAIM }, > + { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, > + { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, > + { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, > + { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, > + { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, > + { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, > + { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, > }; > > +/* Translate stat items to the correct unit for memory.stat output */ > +static int memcg_page_state_unit(int item) > +{ > + switch (item) { > + case MEMCG_PERCPU_B: > + case NR_SLAB_RECLAIMABLE_B: > + case NR_SLAB_UNRECLAIMABLE_B: > + case WORKINGSET_REFAULT_ANON: > + case WORKINGSET_REFAULT_FILE: > + case WORKINGSET_ACTIVATE_ANON: > + case WORKINGSET_ACTIVATE_FILE: > + case WORKINGSET_RESTORE_ANON: > + case WORKINGSET_RESTORE_FILE: > + case WORKINGSET_NODERECLAIM: > + return 1; > + case NR_KERNEL_STACK_KB: > + return SZ_1K; > + default: > + return PAGE_SIZE; > + } > +} > + > +static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg, > + int item) > +{ > + return memcg_page_state(memcg, item) * memcg_page_state_unit(item); > +} > + > static char *memory_stat_format(struct mem_cgroup *memcg) > { > struct seq_buf s; > @@ -1577,13 +1599,12 @@ static char *memory_stat_format(struct mem_cgroup *memcg) > for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { > u64 size; > > - size = memcg_page_state(memcg, memory_stats[i].idx); > - size *= memory_stats[i].ratio; > + size = memcg_page_state_output(memcg, memory_stats[i].idx); > seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size); > > if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { > - size = memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + > - memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B); > + size += memcg_page_state_output(memcg, > + NR_SLAB_RECLAIMABLE_B); > seq_buf_printf(&s, "slab %llu\n", size); > } > } > @@ -6377,6 +6398,12 @@ static int memory_stat_show(struct seq_file *m, void *v) > } > > #ifdef CONFIG_NUMA > +static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, > + int item) > +{ > + return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item); > +} > + No need to have lruvec_page_state_output() separately as there is just one user. Just inline it. > static int memory_numa_stat_show(struct seq_file *m, void *v) > { > int i; > @@ -6394,8 +6421,8 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) > struct lruvec *lruvec; > > lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); > - size = lruvec_page_state(lruvec, memory_stats[i].idx); > - size *= memory_stats[i].ratio; > + size = lruvec_page_state_output(lruvec, > + memory_stats[i].idx); > seq_printf(m, " N%d=%llu", nid, size); > } > seq_putc(m, '\n'); > -- > 2.11.0 > From mboxrd@z Thu Jan 1 00:00:00 1970 From: Shakeel Butt Subject: Re: [PATCH v5 7/7] mm: memcontrol: make the slab calculation consistent Date: Wed, 23 Dec 2020 13:21:38 -0800 Message-ID: References: <20201217034356.4708-1-songmuchun@bytedance.com> <20201217034356.4708-8-songmuchun@bytedance.com> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wF35nXj0WqYzyWdTSecMciE07b/6u11vJiyvqK4gz6s=; b=hRMjxvObgBylB6uesmR8k4mhN2Pag25RaGN4pEInOqMyvL7N0xNsE42tWYXG4UfS/K +XsNltY2tXUStsjEFN1DCG4drR9Yei1e1pwXBxYqxPOOmhk+RzpuwJEBcJEJBYbBaIWu THr2sXxeIA+/WiiHyFx6LUFoHZYjin8MgtY/Fqd5CQJswDcVyzUMDtXTGiPAs8ZJBB4L 1CoQWXGNI/OdkwADgVzfhifxid1LxM5XDGRYxrB3/R4eaYk1AtIE4JAQz8bGZBRTVKOW aQIZawR3t/pSfsVITaeUVJCoYWC5LAIFLAm210rOtmgvOq0Z8rwRmedSpAVH38lM7HqO bQog== In-Reply-To: <20201217034356.4708-8-songmuchun-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Muchun Song Cc: Greg Kroah-Hartman , rafael-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, Alexey Dobriyan , Andrew Morton , Johannes Weiner , Michal Hocko , Vladimir Davydov , Hugh Dickins , Roman Gushchin , Sami Tolvanen , Feng Tang , Neil Brown , Joonsoo Kim , Randy Dunlap , LKML , linux-fsdevel , Linux MM , Cgroups On Wed, Dec 16, 2020 at 7:46 PM Muchun Song wrote: > > Although the ratio of the slab is one, we also should read the ratio > from the related memory_stats instead of hard-coding. And the local > variable of size is already the value of slab_unreclaimable. So we > do not need to read again. > > To do this we need some code like below: > > if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { > - size = memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + > - memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B); > + size += memcg_page_state(memcg, memory_stats[i - 1].idx) * > + memory_stats[i - 1].ratio; > > It requires a series of BUG_ONs or comments to ensure these two > items are actually adjacent and in the right order. So it would > probably be easier to implement this using a wrapper that has a > big switch() for unit conversion. > > This would fix the ratio inconsistency and get rid of the order > guarantee. > The commit message is really confusing. It is explaining a situation which it did not do. I don't see any benefit of mentioning BUG_ONs or [i-1]s in the message. The patch makes sure that we use the right ratio for slab. Can you rewrite the commit message and motivate in just that regard? > Signed-off-by: Muchun Song > --- > mm/memcontrol.c | 105 +++++++++++++++++++++++++++++++++++--------------------- > 1 file changed, 66 insertions(+), 39 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index a40797a27f87..eec44918d373 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -1511,49 +1511,71 @@ static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) > > struct memory_stat { > const char *name; > - unsigned int ratio; > unsigned int idx; > }; > > static const struct memory_stat memory_stats[] = { > - { "anon", PAGE_SIZE, NR_ANON_MAPPED }, > - { "file", PAGE_SIZE, NR_FILE_PAGES }, > - { "kernel_stack", 1024, NR_KERNEL_STACK_KB }, > - { "pagetables", PAGE_SIZE, NR_PAGETABLE }, > - { "percpu", 1, MEMCG_PERCPU_B }, > - { "sock", PAGE_SIZE, MEMCG_SOCK }, > - { "shmem", PAGE_SIZE, NR_SHMEM }, > - { "file_mapped", PAGE_SIZE, NR_FILE_MAPPED }, > - { "file_dirty", PAGE_SIZE, NR_FILE_DIRTY }, > - { "file_writeback", PAGE_SIZE, NR_WRITEBACK }, > + { "anon", NR_ANON_MAPPED }, > + { "file", NR_FILE_PAGES }, > + { "kernel_stack", NR_KERNEL_STACK_KB }, > + { "pagetables", NR_PAGETABLE }, > + { "percpu", MEMCG_PERCPU_B }, > + { "sock", MEMCG_SOCK }, > + { "shmem", NR_SHMEM }, > + { "file_mapped", NR_FILE_MAPPED }, > + { "file_dirty", NR_FILE_DIRTY }, > + { "file_writeback", NR_WRITEBACK }, > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > - { "anon_thp", PAGE_SIZE, NR_ANON_THPS }, > - { "file_thp", PAGE_SIZE, NR_FILE_THPS }, > - { "shmem_thp", PAGE_SIZE, NR_SHMEM_THPS }, > + { "anon_thp", NR_ANON_THPS }, > + { "file_thp", NR_FILE_THPS }, > + { "shmem_thp", NR_SHMEM_THPS }, > #endif > - { "inactive_anon", PAGE_SIZE, NR_INACTIVE_ANON }, > - { "active_anon", PAGE_SIZE, NR_ACTIVE_ANON }, > - { "inactive_file", PAGE_SIZE, NR_INACTIVE_FILE }, > - { "active_file", PAGE_SIZE, NR_ACTIVE_FILE }, > - { "unevictable", PAGE_SIZE, NR_UNEVICTABLE }, > - > - /* > - * Note: The slab_reclaimable and slab_unreclaimable must be > - * together and slab_reclaimable must be in front. > - */ > - { "slab_reclaimable", 1, NR_SLAB_RECLAIMABLE_B }, > - { "slab_unreclaimable", 1, NR_SLAB_UNRECLAIMABLE_B }, > + { "inactive_anon", NR_INACTIVE_ANON }, > + { "active_anon", NR_ACTIVE_ANON }, > + { "inactive_file", NR_INACTIVE_FILE }, > + { "active_file", NR_ACTIVE_FILE }, > + { "unevictable", NR_UNEVICTABLE }, > + { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, > + { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, > > /* The memory events */ > - { "workingset_refault_anon", 1, WORKINGSET_REFAULT_ANON }, > - { "workingset_refault_file", 1, WORKINGSET_REFAULT_FILE }, > - { "workingset_activate_anon", 1, WORKINGSET_ACTIVATE_ANON }, > - { "workingset_activate_file", 1, WORKINGSET_ACTIVATE_FILE }, > - { "workingset_restore_anon", 1, WORKINGSET_RESTORE_ANON }, > - { "workingset_restore_file", 1, WORKINGSET_RESTORE_FILE }, > - { "workingset_nodereclaim", 1, WORKINGSET_NODERECLAIM }, > + { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, > + { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, > + { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, > + { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, > + { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, > + { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, > + { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, > }; > > +/* Translate stat items to the correct unit for memory.stat output */ > +static int memcg_page_state_unit(int item) > +{ > + switch (item) { > + case MEMCG_PERCPU_B: > + case NR_SLAB_RECLAIMABLE_B: > + case NR_SLAB_UNRECLAIMABLE_B: > + case WORKINGSET_REFAULT_ANON: > + case WORKINGSET_REFAULT_FILE: > + case WORKINGSET_ACTIVATE_ANON: > + case WORKINGSET_ACTIVATE_FILE: > + case WORKINGSET_RESTORE_ANON: > + case WORKINGSET_RESTORE_FILE: > + case WORKINGSET_NODERECLAIM: > + return 1; > + case NR_KERNEL_STACK_KB: > + return SZ_1K; > + default: > + return PAGE_SIZE; > + } > +} > + > +static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg, > + int item) > +{ > + return memcg_page_state(memcg, item) * memcg_page_state_unit(item); > +} > + > static char *memory_stat_format(struct mem_cgroup *memcg) > { > struct seq_buf s; > @@ -1577,13 +1599,12 @@ static char *memory_stat_format(struct mem_cgroup *memcg) > for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { > u64 size; > > - size = memcg_page_state(memcg, memory_stats[i].idx); > - size *= memory_stats[i].ratio; > + size = memcg_page_state_output(memcg, memory_stats[i].idx); > seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size); > > if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { > - size = memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + > - memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B); > + size += memcg_page_state_output(memcg, > + NR_SLAB_RECLAIMABLE_B); > seq_buf_printf(&s, "slab %llu\n", size); > } > } > @@ -6377,6 +6398,12 @@ static int memory_stat_show(struct seq_file *m, void *v) > } > > #ifdef CONFIG_NUMA > +static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, > + int item) > +{ > + return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item); > +} > + No need to have lruvec_page_state_output() separately as there is just one user. Just inline it. > static int memory_numa_stat_show(struct seq_file *m, void *v) > { > int i; > @@ -6394,8 +6421,8 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) > struct lruvec *lruvec; > > lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); > - size = lruvec_page_state(lruvec, memory_stats[i].idx); > - size *= memory_stats[i].ratio; > + size = lruvec_page_state_output(lruvec, > + memory_stats[i].idx); > seq_printf(m, " N%d=%llu", nid, size); > } > seq_putc(m, '\n'); > -- > 2.11.0 >