Received: by 2002:ab2:3c46:0:b0:1f5:f2ab:c469 with SMTP id x6csp128039lqf; Fri, 26 Apr 2024 01:32:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUcJ6aW4StdkKTOZYCyVib3KT1XMZIS9HLJ26Wb4prjt5pXNnH1tqgfSYnnGqmqWEvoYmLHwSdIDMyJjTQHo/nfgFHYXdji7RXu5SBF/w== X-Google-Smtp-Source: AGHT+IGI+q2O26GcTVLymNIJ5giDIidNaTaDg0th14PvQvarODiNFYmESCQ9ODKLlMTJbSKA4Aan X-Received: by 2002:a17:90a:ca91:b0:2a6:217b:4492 with SMTP id y17-20020a17090aca9100b002a6217b4492mr3159497pjt.3.1714120343243; Fri, 26 Apr 2024 01:32:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714120343; cv=pass; d=google.com; s=arc-20160816; b=xr24lSuhgKRFY2jehrPb4tCxUBDk6iHQznULzowOZ5Znixr143LCpiOxwY0HFgOosF 2m5OFARiUj/2Zrmi3IVH2lRHz0phR5wPGGUlXhdHGyAGY8+O+lu0Pdk/gbF0NJ06KtZI Y6xpoKIsJfvHL4IEEHlUOn1rLqqIfz2iIEzdewGwasxJ6V4KIWNGTglzhCDwOzjEOMpH KbPIcP7lfjNEx0nhBQbyPbCdMZUeKM5eJfUfu3ysJ1KpyzDSpgUsgBzcMy4XdOpSYNHa UwtMMOZMU3kCKSttFuEVkHvG8dE3KdpiZJVzLv4rT5CQDvhKu3XUWUzfI+uEL0eJQ543 5L6w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=y49Hw3YbQx9uAvZVira3B8a8v6zI7tnQdDmrRCpQIPU=; fh=CzSJNj8w8iUnmlgWvNYhDvD5pl9OAXL/ljrL7Y6PMxw=; b=vvhkHoz7dr0MjzSuWw54zf7D9o7otaQ470CfUC44g9nFbQIDYDSYrBdTjLY8kd4W6y sXJIJckdVU00Hc7rMzzYo8QTe8LkYiEbLmk3STsnXOXmo36jLDwftVAM2NM/5J6hIb7Z u9B9ALquj44C+/vDzNsaKxbRdJUS0s66715dGgeZHhgqlZMvEM7Jq9h/Xf5nSC7HRkk7 jKkAOf7uW4BWinlRdWoky+hF3pRzXMYjeKAb1/9vxYZn+8Qgk1vJ38ackBUIpfxARQ3c 0jVlcmScORa1zs30PvIqT9plPUKzX3vh8yIjaKdVySt02Sn0Fsq/fc/EdwWufoTiz6/6 kEfw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LwMZeOSx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-159701-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159701-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id me12-20020a17090b17cc00b002ae501aa965si7604198pjb.26.2024.04.26.01.32.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 01:32:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159701-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LwMZeOSx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-159701-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159701-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D30F528543F for ; Fri, 26 Apr 2024 08:32:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A927414037C; Fri, 26 Apr 2024 08:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LwMZeOSx" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CF9E13D26A; Fri, 26 Apr 2024 08:30:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714120210; cv=none; b=CPJs2QTEXvaWcw57EbbLPrgjNgW3KMc0r/Y8ECO/hlVrMcalgASyCkxCJ7V6p8AWN9GMRrDk7Aqpf5Rp4Jle5RCrwq5mnqwLqzxYlSiLJhsTc0aYzuzOHSgOyikMOo3L4K/ni3S3DNr1EfEF3VbiJ8vXfn4uMTLHEnonqVBQB7Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714120210; c=relaxed/simple; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QIhhJ2xJ6JQcqTbVpm3ijZyipFicptrlO+9NIwrnu0jjcpORSj6g9jyDs59TC4sfGsc2HNL5+AI0bJvRfRao3cllPA428DqHNIL2DGQLOoWwl1dqVMHuUQBAKZyFGnDWQXZGBAuSwM9I5r402+bkLK7FXqQ5oICVDNsfVFdE/go= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LwMZeOSx; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BCE9C2BD10; Fri, 26 Apr 2024 08:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714120210; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LwMZeOSxTcaopA8l7aFjn836oXymor4oh/sP036EJ9NTUjJddS/giJA53rY22kGtM KczrvrgOvL4jlS5ocJMnqvIGuQSp0OvGJ0hX9z+Tp5xcpLS9VtMNZQ3XfsGQqGJzqh 6vldDmI/hSudf9GnoBcMLm1mGO6GHJdcybJzDMOYPZI3pmfQ75uHgx+OE2DCnV+U12 lVKgZat3LQ8YKcRYxjpCf6QjFQlnNLKrfALh6wX0HX0FaFFgF+GXlRYsZACOcR95ZI GWE8siM8d7u7xPg2qxDsktAAwONYtV/flV0qRa5qeob8ziShnD/IXh7jk+Tlb9Ub81 a7RL/aC3317VA== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Alexandre Ghiti , Andrew Morton , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , 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 v6 05/16] module: make module_memory_{alloc,free} more self-contained Date: Fri, 26 Apr 2024 11:28:43 +0300 Message-ID: <20240426082854.7355-6-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240426082854.7355-1-rppt@kernel.org> References: <20240426082854.7355-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; } -- 2.43.0