Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp266413pxu; Wed, 2 Dec 2020 22:35:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJxFp8N8qojtSpkUmvuqy4il1QTEN7JONBAcEB9C4zMXoTj/L70jOc3FTYoReTxzQYaGsiag X-Received: by 2002:aa7:c303:: with SMTP id l3mr1506863edq.2.1606977322352; Wed, 02 Dec 2020 22:35:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606977322; cv=none; d=google.com; s=arc-20160816; b=RfYHv0OL9moFQh5F4doZ7IbCeShjoT84hPfUtIIh1aDvz/eoO9pbMrKOEnRnSwHt2C SnQwUh2d8BmBy8yvMc1uvoJcZiTv3sJIJJTPTYplXtCk0c7ZnDp5PiEUUhMmLtkkfI4q CBcnz5D7oL/dWwTx/hyhOGYJPNdvWX84lfxLgbOLj4jMNJhI8dDpMJieHpL2FbToeecO aF2+tWHy6xQVaSycd6srQkAXxlQ9GazKokU2c2X3r+sQaZGGdiGWNWR5VWAuAem5QSGR Ii7UbbssQte3ugH1XdecTepSoS8PX9J8XFznwiJmD0exzmIksdo8hlMMKt33328mf9wp C4Zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=qico8tW3bN9ATjTJLQoTwRdnYQYmHk03x6uakjmZsn8=; b=Ys7d4ZmPvH6U2TkOagLG3rfc0rUGmLrU7cGhYI4YxKwl98pS9i7HuL4dTtP95//siS i1MPMtjW7+HsoitsGunjp1BDN5m5+k1a2PV+FEm8fcGAdon3EK26c9k1qgKtP0QS75xS hZOOAVE1cQIOYPz7sPowDE4o0fs9PX694i+IkKjc7Z3vt8hroVJsxNfWqoeoPhZ6cEwp h1OQ27Uw5n6bWdh1ePVjYdp8MfqKMdkw27AqvSjnQCFk7GZBOqt0GW+4yroaszDTDtVG qrzinnqFB8S/MJFOXIbIoRRqSsPXF4aQZyXIXq3mNvB+GXCv8rktKi65xOw3gjwRG9oz XZHA== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r22si405291edx.445.2020.12.02.22.34.59; Wed, 02 Dec 2020 22:35:22 -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; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387452AbgLCGcF (ORCPT + 99 others); Thu, 3 Dec 2020 01:32:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:48730 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728325AbgLCGcE (ORCPT ); Thu, 3 Dec 2020 01:32:04 -0500 From: Mike Rapoport Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) 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 , 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 Subject: [PATCH v14 07/10] secretmem: add memcg accounting Date: Thu, 3 Dec 2020 08:29:46 +0200 Message-Id: <20201203062949.5484-8-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201203062949.5484-1-rppt@kernel.org> References: <20201203062949.5484-1-rppt@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 Acked-by: Roman Gushchin --- 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 249cf489f5df..cf7f1dc9f4b8 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -42,6 +42,7 @@ #include #include #include +#include #include "internal.h" #define CREATE_TRACE_POINTS @@ -844,7 +845,7 @@ static 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 52a900a135a5..2390901d3ff7 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,10 +83,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_account_pages(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) @@ -76,6 +107,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; @@ -302,6 +335,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