From patchwork Sun May 5 14:25:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13654316 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 0AFDFC4345F for ; Sun, 5 May 2024 14:27:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63EF56B0098; Sun, 5 May 2024 10:27:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EF366B0099; Sun, 5 May 2024 10:27:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DE396B009A; Sun, 5 May 2024 10:27:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 307226B0098 for ; Sun, 5 May 2024 10:27:25 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AEE4FA04F4 for ; Sun, 5 May 2024 14:27:24 +0000 (UTC) X-FDA: 82084570008.12.13848EC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 04A7280002 for ; Sun, 5 May 2024 14:27:22 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Xl5iD4lu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.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=1714919243; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IIBGIsCSUWlsjH08h6Yqq1vKj8aLzQai1EKlJHl3zyY=; b=YmTGli1RTyMqlXsiqn++f34ZbcvPo6kGB9zq7TYvpI2HuQe4XwDitvfSYjn7NEsjuK9usS r+Z/9J4jUbdZyB0oFqeCdRw86ipRPfvFZGQMw1sbxwjU7JP1Wy0ExxLo/osSf9X0dQNUxr K4z1JpbqaQMxdcIh6gil0Ru2XOBB1K0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Xl5iD4lu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.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=1714919243; a=rsa-sha256; cv=none; b=IIlgRmye5NJXvqVl0glqDTJup/4ysL/t54dR4K2Slb61eTw6NN3imnIF9odAGJxLmmBzs5 14+NbCQVa5sNGIiAWsSLKDNVHKgCxZqVhqddY6yGAqBlN3THZVva1IQwWJugVCrU0fwowO h81/a5x1rQknjkYDtYc8gPWMZgNCzDk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 4683A60C8B; Sun, 5 May 2024 14:27:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D95EC4AF67; Sun, 5 May 2024 14:27:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714919241; bh=vbQmVIViLlnHnV/HmsoDNHPWB+6N7x9rENWps4lsXos=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xl5iD4lueG8tIPXrO/gEXU39XDfM3YC/JMMh3852CFxF2oYh+paX9vBtiDNHobolr fT4G8IF9fkbLUdGv2ikdeuc6+J2xDFH0KR/VTbUG+Hv/QdKrBr+pSDv9DFzfUPP2I3 bP2It0ZiWqKrpaFsBerSAvvYt92y2em8jZ93u0QN8uW5A3MxYvy1JsUox9JZRQ11j8 hWmui1dlwkA0uUH0DLGz65sT+kLHQX0p9W2t3k9zMl14nQC7TMegAfhWBFTdDhkJMw rp4g/OdSjIa699sH+T7Z+caC4c3erFuVC8VvAkXFGQHM++7hzBOqRdtcFRKejSaFcx 8h5mYj3Qiu98Q== 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 , Liviu Dudau , 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 v8 05/17] module: make module_memory_{alloc,free} more self-contained Date: Sun, 5 May 2024 17:25:48 +0300 Message-ID: <20240505142600.2322517-6-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240505142600.2322517-1-rppt@kernel.org> References: <20240505142600.2322517-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Stat-Signature: eh39pcmdgmawia3hich397kwz5e4dkg5 X-Rspam-User: X-Rspamd-Queue-Id: 04A7280002 X-HE-Tag: 1714919242-359474 X-HE-Meta: U2FsdGVkX1+yDUMwfOmAzetvaHbhmY4zMjD7sseJSW30SO17IYSU07QXqJrCd5E43O8GnuJasca8DIPSKP/dqTLCLJ3jNB3aWFzqrH7Jgn0lb5QiiJg7ueEx0G++KoWSsutS3GQGBeW5uG/3ZCtHHSEKw11To99CBw+NJEaDnWr0YvYqfXpWvnJq5s/H1juVAljGsuEjVbxjgQnsA7gL/hCbVHkrYWPmyt7D7Ev+LZyaloLoBpQxQ5Zl+t6JsmcktbuNXR+0y4fZ8ekPNmEHMhf/3ZvMFHqg31yz+3Dd7n8DK5TGHSHa8rfSg6p8MxxSuDuXa4NDVPdYB8hCUqkNY1AQS0rFJTsri9+cJxRhs9spTw7twvBlSIbvXmuKOQz+jU+i0Fo0QYxqgtaNCrcFIPEa2HMl0gn7oygIe6Zw6Tq9HPiI3GY7hWURzkr5Q2dkSUrqdKzx4h/tDFACkjvMqG2r3MrU3bsgTL8CGCTuxv7E3gyBU2WrMoTuYBFGhqcsL8nLzZmmF6eYRIAC0mXGzO/JQ36nV7wKH6pNG6qSfVqAjh5pzqkffSaYlHcwlx42J84r4Kbxj1CphDiQDrtJJ8j86nEJYZBrKSz3l+4cI3Sppk3K0aNUcCywmRFiHmDi4a1MKQrWWUYuLm2m6HhwMzQtN2H0/n3F2Aw5mAKS/VjLkhDC67fM1bojTbbqznY4haMlD8eH9XA5FjuVJHO14yqOUlvSjaIjaHX9K65hM6+LnUvDtisKNaxpIsPl/x2FazExP1Qo13CpReJlLd8Ro9Ajwba1ZIrHCZprqhsTCNnNkFaJ7eDOAwZsy3kbw75/mZDRIDoJxfHkt4FvRUTJViUF4AkFE+HGoW8kew2j1JyO5l5Ixhm+vHdaM6JtBwiPSp2U8Q6Y8d30a3bIE5Cwh1GoYIhIUuIqwtir8HlhoEn0yPvrbqEpZprgEYsHOOXcb6dZEoiGjAIHFWMNwwf 7VdeFDHt Tos6VmA+GJKjNRRJvwjiMWQYlLU/58TlaDLmCPBPYAyFg16NZzBcTvcYiANGjIkVuIKPWmJEEaf1NhQj+B+y8YyAs3KF+2rg+Lbg2JjnaVt9SUIRXD/sthyReHwhU8RUMRizq2XWzKRG1ey78ABxEwxQqPVL1ITyZuVC30vGaYWRg90LAlZ7WY3xTMY77S1lBkQJZtVPUm3CrZzcvDuVdciceXd/hEpBghs30pkjN5vSvmsZAbpcg3ueu0izx+FVMMjFzwslmIEFWx0UL8FFgj/cBnb1HjSLs6bIIOfCJGY3SPnZU7I/24+hb+r0C6UTmR0r8IEjggaee0wxUOh9D0TWxTdmp4q73G/Kz3Elov2YKprv/PXoN9tcMags8pspciF0b5wXbEzGISL2ZREm1Gy+h2z/vqaIlireQk/ktOONrIwG7gsJ7kgB/izm1lWU0Umgu+J+mEp3gkXRZg2RlqBkrXITt6KsDhUI6cnOBI/MMpk2XeANjl6ljEzUxDtlLnTl2tUHRIfLJd+vbkYEq/70rHg== 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; }