From patchwork Mon Apr 22 08:47:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13637946 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 A2611C07E8F for ; Mon, 22 Apr 2024 08:48:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DEE46B0082; Mon, 22 Apr 2024 04:48:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 363AD6B00A4; Mon, 22 Apr 2024 04:48:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22BCD6B00A5; Mon, 22 Apr 2024 04:48:31 -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 039D46B00A4 for ; Mon, 22 Apr 2024 04:48:30 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 568DF1C10E0 for ; Mon, 22 Apr 2024 08:48:29 +0000 (UTC) X-FDA: 82036541538.13.08DA334 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf11.hostedemail.com (Postfix) with ESMTP id 235994000E for ; Mon, 22 Apr 2024 08:48:26 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="AH9/m1aV"; spf=pass (imf11.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713775707; 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=3GWIpJQ5Q0zt874IATiQ6rzrIhl0nJD4xKAmO5R0hS/ay38kGBaFxua3OdOltzafLKPKLl 0UaKoWo/HPfw/I0DM6gbMCjOJ9KXMYZtj/0Ad5uJvitMCUb3UKTGZd3kn6kq6oc94Eke6n TadxW3d8lTT72dLhLvJlIWz4chh/csw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="AH9/m1aV"; spf=pass (imf11.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713775707; a=rsa-sha256; cv=none; b=Cr84XNHn2q1ArFuyfc52j16PFtyOga6EU9QAraP+ZbVmApv5/M06qxK9dcimAG1aHqKzZD JWjP7oNAOUb12tcw445B6ZqCASBY5YrNr/Usdx1tz0pHTRCn4/6WCjeiJQVd55VxXSmfQC Df+giJK/yMAs7wfX+8VncGOQtDPJcnI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 20FD9CE0A5E; Mon, 22 Apr 2024 08:48:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A503C4AF0A; Mon, 22 Apr 2024 08:48:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713775703; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AH9/m1aVDHGuGvLI4j3W6Er2jkmhJNQCkQxO4QUroTUv8HHvPv9ax8HkYPpnVXo0H h6zGqluiK0/MkrlNMYCJHVcZLE6rqq7/wCQ+3sa/mcoqQEa5fVvQSIKcJ2QPIMyFRU Hj2jR+oLtqdyEiJ7d0ktan3WS/LOmv1DWCbjXCJuyPzrNeHPED/3/ahlFyipBYGtOA 7pMnab4rM0rp+ru673saM/mmkvwnmefJZIKJjtxcnCRw8Bx9/07TTrjYpttQvjrHu/ IFA1udw7VXCwZW0gWcjnWISQ8yMhFl9+3c7cfvwLABBa49RyVYZnSMb/uPlxLJ4/YR 41E6wQ3VoTr9g== 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 v5 05/15] module: make module_memory_{alloc,free} more self-contained Date: Mon, 22 Apr 2024 11:47:13 +0300 Message-ID: <20240422084717.3602332-5-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240422084717.3602332-1-rppt@kernel.org> References: <20240422084717.3602332-1-rpp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X4DAavZvMdGITj0xHkasEbpNALj0uAck4IS2ZbVRCGimd4ORP7fq X19ULNte dDByVf597tJrbfDChDn1gyloWKKtT4/Quy7snP7h7LTMTp97KyoxlK6X0XslQzphb6/oSdEs+7L7kgJOYgKAoj7KjqR7p/xyrzbz/TFlGerA4XDv4jyfwHGTIuyf7u4Bfgwoo51Lu7OGzGQMTtLMY/iA8E9Tn7swNvuce5FdWbKCCMNpM+AEqwOYT1jgSEqf3MYIcnsm42VWR7TXGC4bNNVrbSKOTgeJJs6n/w88jNzu40fjEUpNg7fyT7zWw+BpYeMt9mP+3XnvJEJGIJ5LeZ2rl1afpog/jcXWOLJCrgvkvqzKshkw9TYMHfgwA8HmMzFhuIymuNpwjWzGBwPKLMGBPAe0AswD7ZNkWX/oNQgXxbEc= 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) --- 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; }