From patchwork Tue Nov 10 15:14:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 11894563 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 738D8697 for ; Tue, 10 Nov 2020 15:16:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4D01420781 for ; Tue, 10 Nov 2020 15:16:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZVV/hsDC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731213AbgKJPQC (ORCPT ); Tue, 10 Nov 2020 10:16:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:57352 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731462AbgKJPQC (ORCPT ); Tue, 10 Nov 2020 10:16:02 -0500 Received: from aquarius.haifa.ibm.com (nesher1.haifa.il.ibm.com [195.110.40.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 76E162151B; Tue, 10 Nov 2020 15:15:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605021361; bh=bInqIGRUfZ/ZeTiTbRPYOh4OOIeCg0iqRXozcLKRJ3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZVV/hsDCUezNW7HJaYfn2+8TPpzuUbTmBlkXTTtd0iPWzXcMg2XJiuRBilBnrHW5r 6osHn5gFkuMbrTDEYLCaYfBm053/CETLd26tjtEaEZTdOGFDF8erTIPYdD5f9a6y++ V2JYeB/L6h3bBfG8jt5nhcD0qO0X9229KjmsRECA= From: Mike Rapoport To: Andrew Morton Cc: 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" , Matthew Wilcox , Mark Rutland , Mike Rapoport , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Rick Edgecombe , 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 Subject: [PATCH v8 6/9] secretmem: add memcg accounting Date: Tue, 10 Nov 2020 17:14:41 +0200 Message-Id: <20201110151444.20662-7-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201110151444.20662-1-rppt@kernel.org> References: <20201110151444.20662-1-rppt@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Mike Rapoport Account memory consumed by secretmem to memcg. The accounting is updated when the memory is actually allocated and freed. Signed-off-by: Mike Rapoport Signed-off-by: Andrew Morton --- mm/filemap.c | 2 +- mm/secretmem.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 249cf489f5df..11387a077373 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -844,7 +844,7 @@ static noinline int __add_to_page_cache_locked(struct page *page, page->mapping = mapping; page->index = offset; - if (!huge) { + if (!huge && !page->memcg_data) { error = mem_cgroup_charge(page, current->mm, gfp); if (error) goto error; diff --git a/mm/secretmem.c b/mm/secretmem.c index 1aa2b7cffe0d..1eb7667016fa 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -49,6 +50,38 @@ struct secretmem_ctx { static struct cma *secretmem_cma; +static int secretmem_memcg_charge(struct page *page, gfp_t gfp, int order) +{ + unsigned long nr_pages = (1 << order); + int i, err; + + err = memcg_kmem_charge_page(page, gfp, order); + if (err) + return err; + + for (i = 1; i < nr_pages; i++) { + struct page *p = page + i; + + p->memcg_data = page->memcg_data; + } + + return 0; +} + +static void secretmem_memcg_uncharge(struct page *page, int order) +{ + unsigned long nr_pages = (1 << order); + int i; + + for (i = 1; i < nr_pages; i++) { + struct page *p = page + i; + + p->memcg_data = 0; + } + + memcg_kmem_uncharge_page(page, PMD_PAGE_ORDER); +} + static int secretmem_pool_increase(struct secretmem_ctx *ctx, gfp_t gfp) { unsigned long nr_pages = (1 << PMD_PAGE_ORDER); @@ -61,10 +94,14 @@ static int secretmem_pool_increase(struct secretmem_ctx *ctx, gfp_t gfp) if (!page) return -ENOMEM; - err = set_direct_map_invalid_noflush(page, nr_pages); + err = secretmem_memcg_charge(page, gfp, PMD_PAGE_ORDER); if (err) goto err_cma_release; + err = set_direct_map_invalid_noflush(page, nr_pages); + if (err) + goto err_memcg_uncharge; + addr = (unsigned long)page_address(page); err = gen_pool_add(pool, addr, PMD_SIZE, NUMA_NO_NODE); if (err) @@ -81,6 +118,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_memcg_uncharge(page, PMD_PAGE_ORDER); err_cma_release: cma_release(secretmem_cma, page, nr_pages); return err; @@ -310,6 +349,7 @@ static void secretmem_cleanup_chunk(struct gen_pool *pool, int i; set_direct_map_default_noflush(page, nr_pages); + secretmem_memcg_uncharge(page, PMD_PAGE_ORDER); for (i = 0; i < nr_pages; i++) clear_highpage(page + i);