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: 11894603 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 0703916C1 for ; Tue, 10 Nov 2020 15:16:55 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (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 B93EC20797 for ; Tue, 10 Nov 2020 15:16:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="rGQTl61s"; 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 B93EC20797 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-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=3sVsDrRSIoHSCXyALyuh1qUPx/YQFIFNFi5uEQUSJBQ=; b=rGQTl61s5Drw0aTNhhEni/54y WL1inqrq1bdHEbACoh2JUBZErXkQMnHuklFYq9yS7AgKreHU2FvOUII1o0XmGrmp1wHd3vX5UsVCf FMGdmWVhFguyXXiH45prbjLyPeUzE9pE/M9Xle8M0K63GZjptn9KrfAKp6wm7AT6lvzF7BzIcDesx d+9Wybj2Ct4uA83IxKJHRX7A0kckWNuRESvoXi2N+LtY7V//k4QxGUx7wKVcYY3pJtDWP+cS+k5pF LKWVEFCQ/v6tlE39oUSD07v9GbyQpTex4R0SFUMw4frgP4u8WkE6he6/TWzg+ticR/xffradKFmI6 zeq1kKuEw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kcVO7-0001E7-8D; Tue, 10 Nov 2020 15:16:51 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kcVNK-0000iT-8U; Tue, 10 Nov 2020 15:16:05 +0000 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201110_101603_054966_4214C05D X-CRM114-Status: GOOD ( 23.55 ) X-Spam-Score: -5.2 (-----) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-5.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at https://www.dnswl.org/, high trust [198.145.29.99 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 DKIMWL_WL_HIGH DKIMwl.org - High trust sender X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , David Hildenbrand , Peter Zijlstra , Catalin Marinas , Dave Hansen , linux-mm@kvack.org, linux-kselftest@vger.kernel.org, "H. Peter Anvin" , Christopher Lameter , Shuah Khan , Thomas Gleixner , Elena Reshetova , linux-arch@vger.kernel.org, Tycho Andersen , linux-nvdimm@lists.01.org, Will Deacon , x86@kernel.org, Matthew Wilcox , Mike Rapoport , Ingo Molnar , Michael Kerrisk , Arnd Bergmann , James Bottomley , Borislav Petkov , Alexander Viro , Andy Lutomirski , Paul Walmsley , "Kirill A. Shutemov" , Dan Williams , linux-arm-kernel@lists.infradead.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Palmer Dabbelt , linux-fsdevel@vger.kernel.org, Rick Edgecombe , Mike Rapoport Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.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);