From patchwork Mon Apr 29 12:16:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13646778 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 8BE8EC25B10 for ; Mon, 29 Apr 2024 12:18:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0204F6B008A; Mon, 29 Apr 2024 08:18:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F10326B008C; Mon, 29 Apr 2024 08:18:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFEF66B0092; Mon, 29 Apr 2024 08:18:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BB10F6B008A for ; Mon, 29 Apr 2024 08:18:09 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7818041AD2 for ; Mon, 29 Apr 2024 12:18:09 +0000 (UTC) X-FDA: 82062471498.01.6BF3E60 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id 6105B8004A for ; Mon, 29 Apr 2024 12:18:07 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sKwLHX22; spf=pass (imf02.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 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=1714393087; 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=cS143DTworVPj5oexIsTlVbsugErcV3YsNWQh+kZ7QPPzTKujdCfRoiRvaj4r+Gg9ZS6am wocNKpWWD44++QzjlWEKZWEKa1S5hmAb2ntlQllc7s6tVIy/hhF92K82gBBr2wOVr42/Kt moCWA2SMur+0+VfbgtM3LiG7a7Z9OpM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sKwLHX22; spf=pass (imf02.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 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=1714393087; a=rsa-sha256; cv=none; b=5L/1XS69soYe/mboXSII8UpP7QePaWgapEfLmsHKTwtzrv1mHhlh2mRpyQrpLNI0gMTTt+ 5Sr7fDR7514bdBaa/GNOGrVeghzZHevhc1tf7s6SMPgLPxCQHpkT84LQQXhCgk1cHyfMeQ PGoO5eTSC/kaLaPWW3rdz30FqkDbWZ4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 93C0160C8B; Mon, 29 Apr 2024 12:17:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98887C4AF19; Mon, 29 Apr 2024 12:17:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714393056; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sKwLHX22IdeW62736wZ9r1Ikc2SqCJi9L5yjGR/7xI3GLyv4B4elzDchb1SGl++Wj hXWds1Pah1tt9zbAlLSRB2sxYnf5jMZYuc72yLTyQybTOBcpZ4kHzM8kl0uxUW68wJ bMhxoiVvvVzrkF7bXlTe8iq0Ts/IbyOLtbViwz2+g5SMGZezqxFY1WPTCsn82Q6rCL 40jrR0zhHdgWUMckJl4r/6m3yRk0TwmQEqcecKSZfwHa8lk0f+PQ87hN6JGasuPxOs 1L5Ek4JLmOmVkLuztUzltVHerGBETMLdq9JzO8Y60eATC+Y6+We/ipxX/x3WGFdGlw RmkGLgOWCS7Vg== 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 , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , 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 v7 05/16] module: make module_memory_{alloc,free} more self-contained Date: Mon, 29 Apr 2024 15:16:09 +0300 Message-ID: <20240429121620.1186447-6-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240429121620.1186447-1-rppt@kernel.org> References: <20240429121620.1186447-1-rppt@kernel.org> MIME-Version: 1.0 X-Stat-Signature: rqwj1ybqf3n6tkhuo3pkm1ue6bybokyo X-Rspamd-Queue-Id: 6105B8004A X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1714393087-459929 X-HE-Meta: U2FsdGVkX1/e8nge9+y+yCXD0GOVyjHaDjz1bMM8PvHgfU9rtZHFfUMwdQ8IKI4RrgAxP36aF2NNVmt8AVWvu/ixeoxNMsNyLckVl3xGgeF6lCcOTNaTqzOcnaSlDUEA4wWI8L/zRsMTUHd+vH6iRWdaSSB7Q4eF5Ru2OXgKJHP35Q2L5MwQ8KOsL9uzcqZOa87Xo8eB/XaxWQIqo2LDgVlPgK4xZ/FMkDBGEayBjZRDBXI2KyQREbhF06i28MUphOjKUdOpsgornoUis82oFfYtqRtccc0TAUzxWTAIBfgyz+U7jmFB7tSiGUwTd38lqs/zAJhlmYtdP2gF6AGeU9IaME0aIDagvv2x7YFUVHazSyfpQ8pjWyU+HUYm8iKEU0zbSMFXvU3ETAUXj+G7Utzp/TP9IHdTMsIWKyrWDQbWIepJLGOJpjlwe7CCGX1ocF1FneWRLp25eWONXA/VKv0nth8KOGtRTstti9f0j6/7hJTXvIP6MF3TB+lHacjeeewzrrzIhE+3DNRRf+poIsXYqHsobH9Ii/lmDIkJ/cc0b+27k6a61ZC0WCs0W5kUAZF0a50nwoj8YJJ1FuuiFDFhFJO0QjyY3e5S7Oof1k5dQ/YiuSukDRmC0cIE8AdndxKR+A7Y0R/JXK2mrQdg/mFEe6gfMIqcm2xrcOgBGVW3S9gBBuykZLScTLzWLMoDSHaPc4ZPsAdEMIxGC6JRg1BhQPh0Ban90v82Zu3PkQln2mdIhkDwTDTe9gcYfnZRf7g84ocj7t5kTjVENbDniP+iNy6yX1jek+I3ZZllYcKokfJsh7ry4M4FKK2dhxhyOatFbAAS4nX6Sm230CjUsmBBfJ/Oglr8oCxb/wHmftxSF26WU+vbk0k1FfGZYQXYUiclFkJrdQsYsQl7C5DO2ZbsZdN7rbtBetSKNl58yUfwcM9lsU0x8kwB92Cg7yFaNyBPseLB04Ps1Us4Rud zALDBE62 c0EbTaqTw9LWpnBmBRyWgYvU7E0OUhTLYG+ccE2VqFCeMrpY+dxYABmyjSqI0oSmv0bM1+1eD0inAulcaNLHf9B0YyQaI2fvyV4oHMMKViwSPTK6Sz8/lhLYO3+4e995AgLHm0zTiWgf5oG9TwYtPzsouQdFslRLtvo7gf8X5dqghRVxtIqYVPLC9dH6O4kVjE7C2ovtQyiz4G9ITnooY7JntZ1KT02PFAfDoh3X+HtdhMCsff0hDjYZYty4+NdZVL4R0GR0Exb5vwRSs4aX4BhvQF1XQDXAOqI4xApTN4ooZ1zs7AP1UlvLMoawEknf2h03J6XzpiPCbacyPAaaDPoU0t4YgKk3Mk0uV3k4IB81kzd7RMyAO+wt3dZdOjRsUU3HKUPFGuwUK7f/YXPg42bntXMoFl7Qj3gRdtRTJJWDwHLCC1W4CykgvOA== 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) Reviewed-by: Philippe Mathieu-Daudé --- 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; }