From patchwork Fri Apr 26 08:28:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13644227 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 AE8ECC4345F for ; Fri, 26 Apr 2024 08:30:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 432CE6B00A0; Fri, 26 Apr 2024 04:30:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BBE66B00A1; Fri, 26 Apr 2024 04:30:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25F766B00A2; Fri, 26 Apr 2024 04:30:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 081696B00A0 for ; Fri, 26 Apr 2024 04:30:13 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B14BFC079E for ; Fri, 26 Apr 2024 08:30:12 +0000 (UTC) X-FDA: 82051010664.19.693B843 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 0EF5AC0014 for ; Fri, 26 Apr 2024 08:30:10 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LwMZeOSx; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714120211; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=y49Hw3YbQx9uAvZVira3B8a8v6zI7tnQdDmrRCpQIPU=; b=BLWRcyRiRI90idy+95fKmQWR+HYXRFb0Eo78Y3HYMLYN2PtG/rMaOdUVYXLM5bcPHW8uPu oWwrmYBtYWJxSSyCBrcV580D+F25RLoimPKuSeK0kksrc+MX5fqz27FR2DYUbQ3s5u5WaI JzbIM6Y2cmITSDkcPVYERX1KYoec6J4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LwMZeOSx; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714120211; a=rsa-sha256; cv=none; b=Lc9wKPEyVTgPC/4yju3QH5K+RJfm/YSYjMwA1PzTQBYCEZSwHjLWz+rEyflYw3m6s33r6b MB9Q+Pcy839dpHLMbIMRFHySHLxq+CyIkYSAwN46NWsm08wKxOAr/SyUU4zpDRAyugTg7S FWcNDAdO8bMtJU70EwPdiK3LAxfEVLU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 54B826200F; Fri, 26 Apr 2024 08:30:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BCE9C2BD10; Fri, 26 Apr 2024 08:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714120210; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LwMZeOSxTcaopA8l7aFjn836oXymor4oh/sP036EJ9NTUjJddS/giJA53rY22kGtM KczrvrgOvL4jlS5ocJMnqvIGuQSp0OvGJ0hX9z+Tp5xcpLS9VtMNZQ3XfsGQqGJzqh 6vldDmI/hSudf9GnoBcMLm1mGO6GHJdcybJzDMOYPZI3pmfQ75uHgx+OE2DCnV+U12 lVKgZat3LQ8YKcRYxjpCf6QjFQlnNLKrfALh6wX0HX0FaFFgF+GXlRYsZACOcR95ZI GWE8siM8d7u7xPg2qxDsktAAwONYtV/flV0qRa5qeob8ziShnD/IXh7jk+Tlb9Ub81 a7RL/aC3317VA== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Alexandre Ghiti , Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Donald Dutile , Eric Chanudet , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Michael Ellerman , Mike Rapoport , Nadav Amit , Palmer Dabbelt , Peter Zijlstra , Rick Edgecombe , Russell King , Sam Ravnborg , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v6 05/16] module: make module_memory_{alloc,free} more self-contained Date: Fri, 26 Apr 2024 11:28:43 +0300 Message-ID: <20240426082854.7355-6-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240426082854.7355-1-rppt@kernel.org> References: <20240426082854.7355-1-rppt@kernel.org> MIME-Version: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/oJJaD6EW0nM8nB/ UygK9+OJ slD2Bubsftpj9vXIQ/FjnpwP4xLiu48vtDwZ7B9KxPLcn7AaEk06C2fb5YMarRF1f/6enQffQENNJz6vLHUoig9pDPeoif533Nzg05edfT7eeDczITcp8iNQ0vcmslboLUtlkuCNzqsw+T2NhgPNXtMNqf32UxGk+8Aj4gMI6fgkEzQD6VQgxPu8MdfBJzlZx/E2y4KPF9RNdE4xzL8x2HtV0jUXxR6nswb4RtZEL5A3JStvs4LyR6ODYEa2/UEYqYkTkmvAeIKphQblBNF5XkNPUOTHak6fLoCRo1Tp7FwARr2dACFn/h2xr2onf5NdoELpCdWtsbxQ3nylRwz7VyMDp71LfMqMM1NsQs1ZAjtHOPkE/uPFjGQjvf2J/jnV6nRWZ2YK4+YKth3ebXgC5Znejdn1CT1b/sd3f7Nt1cx8btSMnU/hjK7Ma3g== 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: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (IBM)" Move the logic related to the memory allocation and freeing into module_memory_alloc() and module_memory_free(). Signed-off-by: Mike Rapoport (IBM) Acked-by: Song Liu --- kernel/module/main.c | 64 +++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index e1e8a7a9d6c1..5b82b069e0d3 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1203,15 +1203,44 @@ static bool mod_mem_use_vmalloc(enum mod_mem_type type) mod_mem_type_is_core_data(type); } -static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) +static int module_memory_alloc(struct module *mod, enum mod_mem_type type) { + unsigned int size = PAGE_ALIGN(mod->mem[type].size); + void *ptr; + + mod->mem[type].size = size; + if (mod_mem_use_vmalloc(type)) - return vzalloc(size); - return module_alloc(size); + ptr = vmalloc(size); + else + ptr = module_alloc(size); + + if (!ptr) + return -ENOMEM; + + /* + * The pointer to these blocks of memory are stored on the module + * structure and we keep that around so long as the module is + * around. We only free that memory when we unload the module. + * Just mark them as not being a leak then. The .init* ELF + * sections *do* get freed after boot so we *could* treat them + * slightly differently with kmemleak_ignore() and only grey + * them out as they work as typical memory allocations which + * *do* eventually get freed, but let's just keep things simple + * and avoid *any* false positives. + */ + kmemleak_not_leak(ptr); + + memset(ptr, 0, size); + mod->mem[type].base = ptr; + + return 0; } -static void module_memory_free(void *ptr, enum mod_mem_type type) +static void module_memory_free(struct module *mod, enum mod_mem_type type) { + void *ptr = mod->mem[type].base; + if (mod_mem_use_vmalloc(type)) vfree(ptr); else @@ -1229,12 +1258,12 @@ static void free_mod_mem(struct module *mod) /* Free lock-classes; relies on the preceding sync_rcu(). */ lockdep_free_key_range(mod_mem->base, mod_mem->size); if (mod_mem->size) - module_memory_free(mod_mem->base, type); + module_memory_free(mod, type); } /* MOD_DATA hosts mod, so free it at last */ lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); - module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); + module_memory_free(mod, MOD_DATA); } /* Free a module, remove from lists, etc. */ @@ -2225,7 +2254,6 @@ static int find_module_sections(struct module *mod, struct load_info *info) static int move_module(struct module *mod, struct load_info *info) { int i; - void *ptr; enum mod_mem_type t = 0; int ret = -ENOMEM; @@ -2234,26 +2262,12 @@ static int move_module(struct module *mod, struct load_info *info) mod->mem[type].base = NULL; continue; } - mod->mem[type].size = PAGE_ALIGN(mod->mem[type].size); - ptr = module_memory_alloc(mod->mem[type].size, type); - /* - * The pointer to these blocks of memory are stored on the module - * structure and we keep that around so long as the module is - * around. We only free that memory when we unload the module. - * Just mark them as not being a leak then. The .init* ELF - * sections *do* get freed after boot so we *could* treat them - * slightly differently with kmemleak_ignore() and only grey - * them out as they work as typical memory allocations which - * *do* eventually get freed, but let's just keep things simple - * and avoid *any* false positives. - */ - kmemleak_not_leak(ptr); - if (!ptr) { + + ret = module_memory_alloc(mod, type); + if (ret) { t = type; goto out_enomem; } - memset(ptr, 0, mod->mem[type].size); - mod->mem[type].base = ptr; } /* Transfer each section which specifies SHF_ALLOC */ @@ -2296,7 +2310,7 @@ static int move_module(struct module *mod, struct load_info *info) return 0; out_enomem: for (t--; t >= 0; t--) - module_memory_free(mod->mem[t].base, t); + module_memory_free(mod, t); return ret; }