From patchwork Sun May 5 16:06:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13654467 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 5E1A5C4345F for ; Sun, 5 May 2024 16:07:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF7E76B0093; Sun, 5 May 2024 12:07:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EA8DF6B0095; Sun, 5 May 2024 12:07:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D21246B0096; Sun, 5 May 2024 12:07:50 -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 B42E66B0093 for ; Sun, 5 May 2024 12:07:50 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 43E68120496 for ; Sun, 5 May 2024 16:07:50 +0000 (UTC) X-FDA: 82084823100.28.83D666D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id 8FA6AC0005 for ; Sun, 5 May 2024 16:07:48 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aAa5MSgJ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1714925268; 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=Y3zbGa3AqthnPnV53jg1B0GJtS4Ed2dmU1fA8WJ7JBT0IxBI/cF32q0VbgT4Ut1TcilRB/ 8aEKqoi2H0TwYDVg6Y3KoK459z/2GMHNo+v0dUahiJCAHVHmnDDS+2LYNNlilS3XDzXBI5 yFVjjIkx8kk6fgPEPekHOCDT30F16Os= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aAa5MSgJ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1714925268; a=rsa-sha256; cv=none; b=BujRM9DLQn4Ja5Bp53mugKDHPPuuyWZHnONwm71SY1Tps2B9oeR41y1UCqWYGzC4SAmm1v b+Yrx1vaK0WYJeVWmmPfTndno2qevfF400ilczdX6Bjqy9KH2sSyMZ99cFkW9vbHW+Jefe k2g/s6HEsM56HUc5fSE7l4hhNl/nwOg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id B1F8F60CBB; Sun, 5 May 2024 16:07:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21FD0C4DDE7; Sun, 5 May 2024 16:07:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714925267; bh=vbQmVIViLlnHnV/HmsoDNHPWB+6N7x9rENWps4lsXos=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aAa5MSgJQJM45G3HokYLiiJiOZDEeE2ajV8iw/yf1DX/fQ3Kmw6A3USHH0Ot5m3Op TVZYg1AWKghmbLAQnkVMS+GBWsm8aShhkYMk2oUWFTCwZ221sTIuktvc3sHgn5Di67 0I35GiFLFgXviW5AdRB+u7RdDHkgSpW6HhkZwMFM/gPwvOTBBTyPfyI73Civ8D9YvS rNWePZxZHMorQMyuKc+VCAWVVq3t2idHccNWlW0YiN3gXtGTvkETcE/v5FdYXJ2KJf LyxaKhxqABmwc9SmSM4tJ4eoIiC7UL7HsecM0w4e+PWxQYptDGBGAdqaW44NkUBYgt DP/ubOUkGv4RQ== 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 RESEND v8 05/16] module: make module_memory_{alloc,free} more self-contained Date: Sun, 5 May 2024 19:06:17 +0300 Message-ID: <20240505160628.2323363-6-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240505160628.2323363-1-rppt@kernel.org> References: <20240505160628.2323363-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8FA6AC0005 X-Stat-Signature: gm4nj6orp5kt1kqxwk41kjbscyrnd13o X-Rspam-User: X-HE-Tag: 1714925268-62191 X-HE-Meta: U2FsdGVkX19iHl3FxFqJ8qt2VYJRNRt9JyI96qDCc+eycpWPg5MzYdc1nrPUsa7nmYW0pKwJRPrVTz+J2i4OXhYiAxQrfSB5IUM20P/DHCyt0eBTjwx7oyjlSw+uZj1g0TYA3LPuodjTEMO6p/zi2T5jkqIBLlO00BEc/GhpW7wUKIbI7p6YiC16nRDuCJuE2h/MsyBUAM1eEazykuEQl3dsynRtfE3yye9D6u7KeL24EGzjTRo4WyNGMic8Q29K2LoVAoKd9vJdoQDTiAIxyOEbCyef3iQnRHM1GVkzLtN/fdqsv+lejjfGdAUtDAHZYuN+2+TxU7U1zBUqp79ABo7IrzDZ7/Jg02Ux1NjcY68qPO7to7q7QcOWT6JYdTgG5JGLUX9Uvcrf2hnuCs00kHbnXWuZVcxQQhUgTWFG2pkrEHvO4591JyQhpQjfjDUkpT+d23W+nJPiBI75qJAyfnK21+O3RCWlIw1wng3fBnQw69TkDSmIJBVHrSa3GeDDRQuytSMbi484Q4ZwwFJ0mIVYPyiYb5NvjEvOfI4oA33ABi0SWPlMF6ZNqLRnwPFpJdImNgKv6SDUQ3NLbJzQ6/r/sr4aug7JKsD56I51sNyLjxupfmUP1aMuGGfqZw+5nl+vvjGNMYnh/e3UYQ0QknnC/ZdFpSU2RkotOc7tD+/4hkvhaHRExXSfnqPN9x9FPYvRfkrj45B1Eb6liaUnLzWQzXpZYmi/02YdWCDgGNWL37yj5kZi6BXazo1fJwQSwsbDYYD7WyoRw8YndXIs7CzWMzeEwt2QcOqTW2HMNB/7YYAkgYgJYb7/w4t2GJ1XMK/LLcbawCwr20PGeLnisw+p7C5NBDil7kSrTxiTbUQt5Y5zVMNPAcv29F3XP60eP3MtDDo+qmw4VF7PQnB6dR6Bq9xEPdpJiBJmD5UnmrPPGI3ptT0DAq5ZRkdteB6Zd9AXSDYTanAGWC+TDsg nHndWVRw MLwBiQGLH4XL5/LlrLUQaXBamchDeBcbAv6Aa/Q0raNktbOYLKDuLwfhQBp/9VPEFHkr3kaiMRm871Y5lQlho5nPsjyMjZDoj+GEuNquzP8B04crlQPAet8kHfA3DSr09NIyLup74lxy4NzYwF2rWCYsFwpV4sxaRC5xme9pxelZJFyc+1bxc1sfzqbjIGfVgttLFIzLQo3nGPfuxpEwQWb+uy7DzjnVP8pjeXkQGFz6iVC1DVV30wRiZeQmgCz4f16gMFZ5nz3P1RVlpbDj9Xvta6J6sCWUPN87ukW+Xzmw5pFqQ2dwnp/fp+JXrKAMB6xk3jz67JVfRBwzUOxj89BBrBWlSoSgQT+IvrDi6Oo1Q6CpU8RafMJMNg4IMHOVkX6Up8KLAeLCuGFqk9xKLHdVbZhaUmKlafxvtRQd+cnQtgavhzy3vGBcww3lXLgKzzJRms3S7C8rlqyFe5QMDVo0AOfDhPgqrJGjxmTcETpeGuz1t2PpvQ5v9sCBKLyOHcJ4orHUffq3gZChvWByboZHzSg== 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é Reviewed-by: Masami Hiramatsu (Google) --- 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; }