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: 11894561 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 309DB697 for ; Tue, 10 Nov 2020 15:16:05 +0000 (UTC) Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EE44620809 for ; Tue, 10 Nov 2020 15:16:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZVV/hsDC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EE44620809 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-nvdimm-bounces@lists.01.org Received: from ml01.vlan13.01.org (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id BAC26167A588B; Tue, 10 Nov 2020 07:16:04 -0800 (PST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=198.145.29.99; helo=mail.kernel.org; envelope-from=rppt@kernel.org; receiver= Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 8E92A167A5880 for ; Tue, 10 Nov 2020 07:16:01 -0800 (PST) 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 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 Message-ID-Hash: T54WBCADQMMADIVETO2CX6OKNNJLQ4J4 X-Message-ID-Hash: T54WBCADQMMADIVETO2CX6OKNNJLQ4J4 X-MailFrom: rppt@kernel.org X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation CC: Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , 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.ker nel.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 X-Mailman-Version: 3.1.1 Precedence: list List-Id: "Linux-nvdimm developer list." Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: 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);