From patchwork Tue Mar 8 13:10:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12773791 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06571C433F5 for ; Tue, 8 Mar 2022 13:11:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE6118D0008; Tue, 8 Mar 2022 08:11:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E94B68D0001; Tue, 8 Mar 2022 08:11:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0BA88D0008; Tue, 8 Mar 2022 08:11:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id C3CE68D0001 for ; Tue, 8 Mar 2022 08:11:20 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9EA01248BB for ; Tue, 8 Mar 2022 13:11:20 +0000 (UTC) X-FDA: 79221255120.05.FFEF707 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf21.hostedemail.com (Postfix) with ESMTP id 2D6F51C0012 for ; Tue, 8 Mar 2022 13:11:19 +0000 (UTC) Received: by mail-pf1-f170.google.com with SMTP id z16so17294183pfh.3 for ; Tue, 08 Mar 2022 05:11:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=z98OQ6AdalG5oMDKVv5aA3FO3T1l3cK2nxdIAVKfKFQ=; b=YdbOfZYq1mOC7LrS/ckJnfEBQ+MAblMP+dx7AWe6RKa98jAA9r1n44yEApW3RKGMd5 zRC97/VTsM7EsdnHMDO7hE6yzmYjY+E4jfZQL8n8DTe/cfFkXDN5zun4HeZTuag2VUiI 1uVbJRLZgyBFHI3LI09GiKz5UK1PMeMrNKAI4k6YNc4E1md3TxXvNZpzuFtYIbfkIFOJ i797KhWbBX2INTNoR3rd6zalgu7CvEkKZbVypUJ8AUSn5KpA5CEVOMo06zFbjaj2nm8t BELs1qacF+x+Y92yqHN5epNG5LlOKvH5ZD7MfFAEnqr8Z3Ff6F3wXJTKxppyuwulT8mY r62Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z98OQ6AdalG5oMDKVv5aA3FO3T1l3cK2nxdIAVKfKFQ=; b=OunBFut7I9vhCf4kaTblQR7RwJgT5AP5ZxA5gTl96D4bCnwiMf9lCv6ywjxfpq6PDh yXFwJhPkZ6cbdEBWeUE0uk/om/VqjLscd1bbPRd1rXi0Yg40VySjYsqXNFOAOtRw3w9I giEvAOR+E/MCn3Emlit3nIujLt3umy7RrBlhGEU4Ml57KEai92eQ6jCv722MUWLgEVvv uLcLOdgDy2Tv0wEnY1IqH0KD3QiYtAOAhctNUqFd6csHVUP0vfzhxlJhtPj4oidttrQu F0wt8NuN66aODz/74mL0H0i1VJw2zn9frTHAQLNAh8arQPpXKnh/mPHNK9tCO7g6Fkmq x51A== X-Gm-Message-State: AOAM531tKxb6koGTkdVonU/dfahYKOQ1vKn6GrkaOScaOgsPXyrH9mRl q7SqviEyFUZj/CepfWyl9nQ= X-Google-Smtp-Source: ABdhPJxrM0tl1/gYxlXQIsaOe+AjwvJr57dfg0zKANngu7XWWfwFpew1AJotBmNiE7RcN7WhSYaheA== X-Received: by 2002:a63:b30d:0:b0:378:c5ee:afda with SMTP id i13-20020a63b30d000000b00378c5eeafdamr14297243pgf.385.1646745079189; Tue, 08 Mar 2022 05:11:19 -0800 (PST) Received: from vultr.guest ([149.248.19.67]) by smtp.gmail.com with ESMTPSA id s20-20020a056a00179400b004f709998d13sm7378598pfg.10.2022.03.08.05.11.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 05:11:18 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, akpm@linux-foundation.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, guro@fb.com Cc: linux-mm@kvack.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [PATCH RFC 4/9] mm: add methord to charge vmalloc-ed address Date: Tue, 8 Mar 2022 13:10:51 +0000 Message-Id: <20220308131056.6732-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220308131056.6732-1-laoar.shao@gmail.com> References: <20220308131056.6732-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2D6F51C0012 X-Stat-Signature: ojub6535pk4pdug7si7grx37wxt5owj5 X-Rspam-User: Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YdbOfZYq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspamd-Server: rspam03 X-HE-Tag: 1646745079-40586 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This patch adds a methord to charge or uncharge a given vmalloc-ed address. It is similar to vfree, except that it doesn't touch the related pages while does account only. Signed-off-by: Yafang Shao --- include/linux/slab.h | 1 + include/linux/vmalloc.h | 1 + mm/util.c | 9 +++++++++ mm/vmalloc.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/include/linux/slab.h b/include/linux/slab.h index ae82e23..7173354 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -759,6 +759,7 @@ extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flag __alloc_size(3); extern void kvfree(const void *addr); extern void kvfree_sensitive(const void *addr, size_t len); +void kvcharge(const void *addr, bool charge); unsigned int kmem_cache_size(struct kmem_cache *s); void __init kmem_cache_init_late(void); diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 880227b..b48d941 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -161,6 +161,7 @@ void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, extern void vfree(const void *addr); extern void vfree_atomic(const void *addr); +void vcharge(const void *addr, bool charge); extern void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot); diff --git a/mm/util.c b/mm/util.c index 7e433690..f5f5e05 100644 --- a/mm/util.c +++ b/mm/util.c @@ -614,6 +614,15 @@ void kvfree(const void *addr) } EXPORT_SYMBOL(kvfree); +void kvcharge(const void *addr, bool charge) +{ + if (is_vmalloc_addr(addr)) + vcharge(addr, charge); + else + kcharge(addr, charge); +} +EXPORT_SYMBOL(kvcharge); + /** * kvfree_sensitive - Free a data object containing sensitive information. * @addr: address of the data object to be freed. diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 4165304..6fc2295 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2715,6 +2715,35 @@ void vfree(const void *addr) } EXPORT_SYMBOL(vfree); +void vcharge(const void *addr, bool charge) +{ + unsigned int page_order; + struct vm_struct *area; + int i; + + WARN_ON(!in_task()); + + if (!addr) + return; + + area = find_vm_area(addr); + if (unlikely(!area)) + return; + + page_order = vm_area_page_order(area); + for (i = 0; i < area->nr_pages; i += 1U << page_order) { + struct page *page = area->pages[i]; + + WARN_ON(!page); + if (charge) + memcg_kmem_charge_page(page, GFP_KERNEL, page_order); + else + memcg_kmem_uncharge_page(page, page_order); + cond_resched(); + } +} +EXPORT_SYMBOL(vcharge); + /** * vunmap - release virtual mapping obtained by vmap() * @addr: memory base address