Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3465099pxb; Mon, 25 Jan 2021 17:41:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJx+kKUsyBboqBYyT08ADHAzSqJP+nagr8HlFK2ATGbyUZEsz8FlZJzEGQnWx/QIaq7eofh4 X-Received: by 2002:aa7:c485:: with SMTP id m5mr2673873edq.320.1611625286236; Mon, 25 Jan 2021 17:41:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611625286; cv=none; d=google.com; s=arc-20160816; b=M/aAi23fa16iEXNUSjSpS2pDp5938aHafuoZsIS6648071qqBsw3kX8WaM6JBj+tzC VHW8RbEyKUl/Nunuav7NeJ2PmhjTyNJyEwJcc2gTyoOmTxwUPpMgXP8z9rJENznNiCOU yDb+coYf/sEeD6w3yVjlAb8O1RROLb8Cziu6f8XP5l7oCPuZztrxHjB+67b8AEQpP6Dw Rbx2K+A91T09czPWowIc7S8Rn8XXExBmzYzg0Alvg4i2AhP6PJzXZOsrcEhOu/rWSgaP U4hgqTcpzAfLI2RFxUm8KeVrVSw5zXk4t3FQVibXs3tPUIBEe0PKReJGa3wLHbwkHeNL EEjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=IJWSMxQ0cSD59sZdRy00Q/p6Rk88W9Y0AQrU4XxStZg=; b=lZ1LGgZ7ZeUo/1jQcuulhg/mybSZqzfRDwsOLOSnghLXtA+0wzrw9mcNdLpLPfQ43N Tm8ty/ejrvVczrv9XVtpJiPQSMO13h7L/uawMWqMXjeIk344536da5fvmHsBaGmhVvPx 37p53R44E5X1SlnSNHmTYOPchtEYAmw8Qpsho2xeJdrbvzp+SIgTkYyUecdGA/LBWULO 8XhaHVo7Tchxe4EWXRfUBuqJ3HGJxMhFcGfdmoE6BpxsslMpdBTEP71tvnnQoBshQ0yR rubANUs01EHgY+81VtadM1w4C+0erVzjNEqxgxsIVuIfm/ohNuqjNy3xcYyBcvN6JxRn fdSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=PuPaQ600; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z7si8534241edm.477.2021.01.25.17.41.01; Mon, 25 Jan 2021 17:41:26 -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; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=PuPaQ600; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730658AbhAYQVo (ORCPT + 99 others); Mon, 25 Jan 2021 11:21:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730628AbhAYQVe (ORCPT ); Mon, 25 Jan 2021 11:21:34 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 266FCC06178C; Mon, 25 Jan 2021 08:20:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=IJWSMxQ0cSD59sZdRy00Q/p6Rk88W9Y0AQrU4XxStZg=; b=PuPaQ600mHPoHQdKyO6XI5TBrm CnXgjHw3HKWzCm5rtjfhkN6pBiyt2h/9yeDanzks13gq46MuMu5ie6WAiuNN2m/RkKR/rpK0Ta+ng fHJqJV/J5ZL7Mm56BAbERsDJQvtiznZxtFYP1h0WIVW4cRUSWOgeXte9ZAwEUfG7Limi/SljDPwHf 3LMXYk3STX6IxUUL3ZYWCYEFfmPendGeUlO+ZrAdcgyCMSmB3rEIdQrHxVOnGrHC53/rZBPfRUW94 buoqllvpB2FvdrEAlQBBWi7qPaBsXDmTgOiFPH1Y4lZIdk+po0IBNUEGXYPVncthQFPVdxLrQWtZK FRfe61ig==; Received: from willy by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1l44Y6-004OSf-8C; Mon, 25 Jan 2021 16:17:11 +0000 Date: Mon, 25 Jan 2021 16:17:06 +0000 From: Matthew Wilcox To: Mike Rapoport Cc: Andrew Morton , Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , Dan Williams , Dave Hansen , David Hildenbrand , Elena Reshetova , "H. Peter Anvin" , Ingo Molnar , James Bottomley , "Kirill A. Shutemov" , Mark Rutland , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Rick Edgecombe , Roman Gushchin , Shakeel Butt , Shuah Khan , Thomas Gleixner , Tycho Andersen , Will Deacon , linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-riscv@lists.infradead.org, x86@kernel.org, Hagen Paul Pfeifer , Palmer Dabbelt Subject: Re: [PATCH v16 08/11] secretmem: add memcg accounting Message-ID: <20210125161706.GE308988@casper.infradead.org> References: <20210121122723.3446-1-rppt@kernel.org> <20210121122723.3446-9-rppt@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210121122723.3446-9-rppt@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 21, 2021 at 02:27:20PM +0200, Mike Rapoport wrote: > From: Mike Rapoport > > Account memory consumed by secretmem to memcg. The accounting is updated > when the memory is actually allocated and freed. I think this is wrong. It fails to account subsequent allocators from the same PMD. If you want to track like this, you need separate pools per memcg. I think you shouldn't try to track like this; better to just track on a per-page basis. After all, the page allocator doesn't track order-10 pages to the memcg that initially caused them to be split. > Signed-off-by: Mike Rapoport > Acked-by: Roman Gushchin > Reviewed-by: Shakeel Butt > Cc: Alexander Viro > Cc: Andy Lutomirski > Cc: Arnd Bergmann > Cc: Borislav Petkov > Cc: Catalin Marinas > Cc: Christopher Lameter > Cc: Dan Williams > Cc: Dave Hansen > Cc: David Hildenbrand > Cc: Elena Reshetova > Cc: Hagen Paul Pfeifer > Cc: "H. Peter Anvin" > Cc: Ingo Molnar > Cc: James Bottomley > Cc: "Kirill A. Shutemov" > Cc: Mark Rutland > Cc: Matthew Wilcox > Cc: Michael Kerrisk > Cc: Palmer Dabbelt > Cc: Palmer Dabbelt > Cc: Paul Walmsley > Cc: Peter Zijlstra > Cc: Rick Edgecombe > Cc: Shuah Khan > Cc: Thomas Gleixner > Cc: Tycho Andersen > Cc: Will Deacon > --- > mm/filemap.c | 3 ++- > mm/secretmem.c | 36 +++++++++++++++++++++++++++++++++++- > 2 files changed, 37 insertions(+), 2 deletions(-) > > diff --git a/mm/filemap.c b/mm/filemap.c > index 2d0c6721879d..bb28dd6d9e22 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -42,6 +42,7 @@ > #include > #include > #include > +#include > #include "internal.h" > > #define CREATE_TRACE_POINTS > @@ -839,7 +840,7 @@ noinline int __add_to_page_cache_locked(struct page *page, > page->mapping = mapping; > page->index = offset; > > - if (!huge) { > + if (!huge && !page_is_secretmem(page)) { > error = mem_cgroup_charge(page, current->mm, gfp); > if (error) > goto error; > diff --git a/mm/secretmem.c b/mm/secretmem.c > index 469211c7cc3a..05026460e2ee 100644 > --- a/mm/secretmem.c > +++ b/mm/secretmem.c > @@ -18,6 +18,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -44,6 +45,32 @@ struct secretmem_ctx { > > static struct cma *secretmem_cma; > > +static int secretmem_account_pages(struct page *page, gfp_t gfp, int order) > +{ > + int err; > + > + err = memcg_kmem_charge_page(page, gfp, order); > + if (err) > + return err; > + > + /* > + * seceremem caches are unreclaimable kernel allocations, so treat > + * them as unreclaimable slab memory for VM statistics purposes > + */ > + mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, > + PAGE_SIZE << order); > + > + return 0; > +} > + > +static void secretmem_unaccount_pages(struct page *page, int order) > +{ > + > + mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, > + -PAGE_SIZE << order); > + memcg_kmem_uncharge_page(page, order); > +} > + > static int secretmem_pool_increase(struct secretmem_ctx *ctx, gfp_t gfp) > { > unsigned long nr_pages = (1 << PMD_PAGE_ORDER); > @@ -56,6 +83,10 @@ static int secretmem_pool_increase(struct secretmem_ctx *ctx, gfp_t gfp) > if (!page) > return -ENOMEM; > > + err = secretmem_account_pages(page, gfp, PMD_PAGE_ORDER); > + if (err) > + goto err_cma_release; > + > /* > * clear the data left from the prevoius user before dropping the > * pages from the direct map > @@ -65,7 +96,7 @@ static int secretmem_pool_increase(struct secretmem_ctx *ctx, gfp_t gfp) > > err = set_direct_map_invalid_noflush(page, nr_pages); > if (err) > - goto err_cma_release; > + goto err_memcg_uncharge; > > addr = (unsigned long)page_address(page); > err = gen_pool_add(pool, addr, PMD_SIZE, NUMA_NO_NODE); > @@ -83,6 +114,8 @@ static int secretmem_pool_increase(struct secretmem_ctx *ctx, gfp_t gfp) > * won't fail > */ > set_direct_map_default_noflush(page, nr_pages); > +err_memcg_uncharge: > + secretmem_unaccount_pages(page, PMD_PAGE_ORDER); > err_cma_release: > cma_release(secretmem_cma, page, nr_pages); > return err; > @@ -314,6 +347,7 @@ static void secretmem_cleanup_chunk(struct gen_pool *pool, > int i; > > set_direct_map_default_noflush(page, nr_pages); > + secretmem_unaccount_pages(page, PMD_PAGE_ORDER); > > for (i = 0; i < nr_pages; i++) > clear_highpage(page + i); > -- > 2.28.0 >