From patchwork Mon Apr 22 09:44:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13638182 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 F19F1C4345F for ; Mon, 22 Apr 2024 09:46:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 786F56B00C0; Mon, 22 Apr 2024 05:46:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70FE36B00C1; Mon, 22 Apr 2024 05:46:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 58AE26B00C2; Mon, 22 Apr 2024 05:46:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3803A6B00C0 for ; Mon, 22 Apr 2024 05:46:02 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E7921140A6D for ; Mon, 22 Apr 2024 09:46:01 +0000 (UTC) X-FDA: 82036686522.03.007B232 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf06.hostedemail.com (Postfix) with ESMTP id 999FE180004 for ; Mon, 22 Apr 2024 09:45:59 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=S9gHw89B; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 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=1713779160; 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=F6scC+QgvPY3XCZFaaO7k/nEtu+A/WF72gk7udJsEWq9jQE3E01WNPKQ9KJMAl9GKXNUqp 1FXPe2j+kYD4TZKmcZoZESaldlvkH78bL0jsdke9JZhxiqOwpKZkevbjDboX88YYv2BG+G O+stw+/y1kf3BeVUeb0PCkgs6iHizHY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=S9gHw89B; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713779160; a=rsa-sha256; cv=none; b=md/oAidAFQJzliQ9qrZp971fbU2HHnGdXH4cG4tPWOBPwPhtrmSL9s6WeLVsGi3UUD8+1I l3c2xnHUNnVPODcczt78oDXZk1Z95TQW+/Om3Ysu7s99XY9QUO0j3lCmIaQ0He7Z8BWGbA wGwLU58KbmPBojMPQHlonWyazOQSQM0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 4C10BCE01F6; Mon, 22 Apr 2024 09:45:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F818C113CC; Mon, 22 Apr 2024 09:45:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713779155; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S9gHw89Bc9FpvcyRqz2mylEtx/ySy/xrGeDZB0E1w/mlVeYp57p8f65E3+aGymYYJ XKt5jDenmlZj/yN00ft00xley2ttD+fyeV1ulT5GJwKMmJAnKPaZ43VUDgY10/lPgl xq7smX4zzV1VmOO7xtt/0tf4Xzz9cuQTdGAdf02PcdSYIbjI5d1BNNiRdHdJLAkTyv /rY0xkEOYSxAdAG0VEZjjTe53DFeIklhl2TLWFi29vxWAAuKMqC99Fb0TZKLwLStad u/q6dvUUoD7MUn8hF8tm5qV04iMVNgrNkP1fE0itwOpJPCQi6b3LjfAWzNJoEYHT0X lFMpN921OwEvA== 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 12:44:26 +0300 Message-ID: <20240422094436.3625171-6-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240422094436.3625171-1-rppt@kernel.org> References: <20240422094436.3625171-1-rppt@kernel.org> MIME-Version: 1.0 X-Rsp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Cbkz8+8rlcb 4xk7BIWn x6ELMGDkqerHv4vwzLCaKkOYuqAYqQtVL+Uz+OBqdJQdT548431j2lMqy0USTNmW7VVLQaX4GmF/xXnDSZIbKHP8467Cvs4+pY0VmcyKR1u7JUV66rC6TsOL0c9wUSQWj96CEDVD9xns+QGJVt6AHukXEG8DadCJ3dYfzffimOBlEpVBJq4PfJ2kDR/3r02m3pY79JkDsnZJfZXXMmKS998FirJLJsbiGX+NX/RQFpJry6/XexM/A6QFYDnCCb3uTLj4qXcY4Hgh2Ar3RgCCc9fT2HJVGjUIU70RtujGFa2c5PpH0i54tShC+nmYYb6Pox8Isph/Vhvj61MaeV6XE64S7mhd/vM5dmmlxlBTqCp0ZNcI= 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; }