Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2119161lqt; Mon, 22 Apr 2024 01:50:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWhE3pdpROsDJVCBP6VxDEUgdwULxgXrMPF+pyQKkoivkmC+6w/c8S8DA1aeH+z/EjSUP0GADQGr4kBWuep/KBlPLS2THPn+JHX62WOAw== X-Google-Smtp-Source: AGHT+IEoBZjpzUoac2mB5i8rdbaBng14EJjd921Jd6CoXPeSHpc8gEBHqYE4mPurnHrmnNpkqqta X-Received: by 2002:a05:622a:1a28:b0:439:75a5:ae54 with SMTP id f40-20020a05622a1a2800b0043975a5ae54mr8273961qtb.11.1713775818906; Mon, 22 Apr 2024 01:50:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713775818; cv=pass; d=google.com; s=arc-20160816; b=oIq97EyTIfurjFLowT5lZzRjhStjh4SYknZnF/vPJ7A8bG/89FwcatLnP270DlksiU /Nnqqg8VuZcl4TGVNKN7aBfVDDvldPMTFtIi7sU2i4y7wJ6Nm6s7P/fuuJIcqEHe+z2h RCCVLGnUuyU0iW25QEszkdkWKwouZTQMsKRMFkcRB20D78jwj9UEfVGEJTZJVc09CpkL CMejQtmj2kuLw6lU82cOkPIEVLde04Rfqcatl5mFmJjPzeQQvy4Zh8TVB05uJnHety+q YgmE+foq0SnBaog723F0o6AQbphDprB/tfzHRP35G15BlxzqJNDU7jvbBgGyRx4wfYDX XpnA== 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=XP/vvXRpiwDYSHcu/54obFhzDcI5hFd+DXlb44w8JhjpEpzVTG+6Zt2zxHo7AggFg+ 6zjsY6Gu9aPzxpf5Ci524+2MiWq5OiNaWsCqWoOFf+kc5A8+Dbw4WGftYUm/49/edi/d 5aWD0v2iFHT6QexQEXkp7mQ+ivn1VzGmVsHwldf5LOq1pqbR2BmDMddEVCiW5VsV69Ko Pc5jixBkle91Yx4oRKIVuBfDzZMB1f8ZQdXlMJNxdjYWUnvZeMX/pDrNdoq+u3jZoVE6 z/khg0f56lj6WIpip7/GTSapB0UL37P97r5tiRCu+lVHEYlPL0Q5fqo0E+rGL+tdgSD/ Wvxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="AH9/m1aV"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-153014-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153014-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f34-20020a05622a1a2200b0043975b72e77si4651543qtb.300.2024.04.22.01.50.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 01:50:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-153014-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="AH9/m1aV"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-153014-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153014-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8B26A1C20E31 for ; Mon, 22 Apr 2024 08:50:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB2AA56B92; Mon, 22 Apr 2024 08:48:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AH9/m1aV" 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 752E55674C; Mon, 22 Apr 2024 08:48:23 +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=1713775703; cv=none; b=PWSgelwAM22jIVH1shqEb3rfM7dTgFBdXVboDkQSeQN09/ri4JIdeIHgN0FTfGEYnslZy4qRMuDd0WVaIIg5MvigXRAYZWl/pq2SB3j8WZeGN/qoAVke75ulG0LAY1RK2viUCauhEKYf1Ab48LMUXafvPtUCNQQytxrLFmwSxfY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713775703; c=relaxed/simple; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fCrnZHIufiHvLbG1BTqc+w09pTStFkJt1SkDQsmQOIK/eamINZrVRm7IBFIJyYSxRfVDl8frtha/4BjPXptjKoyemiQNDJBO0kExrAcZ72xWy69sSDXT7I1a8UCzi8mgxTUPmmG12M81FT8uiTENtFrI/Q29rt8ZvJuqoy0Ay0Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AH9/m1aV; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A503C4AF0A; Mon, 22 Apr 2024 08:48:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713775703; bh=et4MgsnYGm8cVKv9jXBAzJOVzMpXCbC7LVUAQwPu1L8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AH9/m1aVDHGuGvLI4j3W6Er2jkmhJNQCkQxO4QUroTUv8HHvPv9ax8HkYPpnVXo0H h6zGqluiK0/MkrlNMYCJHVcZLE6rqq7/wCQ+3sa/mcoqQEa5fVvQSIKcJ2QPIMyFRU Hj2jR+oLtqdyEiJ7d0ktan3WS/LOmv1DWCbjXCJuyPzrNeHPED/3/ahlFyipBYGtOA 7pMnab4rM0rp+ru673saM/mmkvwnmefJZIKJjtxcnCRw8Bx9/07TTrjYpttQvjrHu/ IFA1udw7VXCwZW0gWcjnWISQ8yMhFl9+3c7cfvwLABBa49RyVYZnSMb/uPlxLJ4/YR 41E6wQ3VoTr9g== 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 v5 05/15] module: make module_memory_{alloc,free} more self-contained Date: Mon, 22 Apr 2024 11:47:13 +0300 Message-ID: <20240422084717.3602332-5-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240422084717.3602332-1-rppt@kernel.org> References: <20240422084717.3602332-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