Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp192264lqo; Tue, 7 May 2024 17:27:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhLNjIyzZx0+BXqy9UKWZ+gOkj0BW45xEc2C0Nh/GHs4cnD1Uo/+BfFXcVOB+vvQOCiXuOTGj0yUkWzvtjzq63ssuvGsFS2QM+LXgAGw== X-Google-Smtp-Source: AGHT+IH0F+np8LG03TVAxJhxCeNnbrLiHXjsV4QOERTWHwujL1MOp0xFXzcVpPUxCpbq5EGXmggh X-Received: by 2002:a05:6a21:7783:b0:1af:a72f:4317 with SMTP id adf61e73a8af0-1afc8d8ecb8mr1357052637.44.1715128039817; Tue, 07 May 2024 17:27:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715128039; cv=pass; d=google.com; s=arc-20160816; b=JDlLEaa7pRAZj/EilouCNzycdMs7hLCrpDprpwkr3V6amk6v3hbd7e80TVaEB/I9B0 Z9pZxWo7nFdfDefH8SsR7Lwzvfzq9iVUr4RLXVBtNBXCmanIAk4a0KgSTyvq7OXC/X7I cDXphGJpBv8jq24AUnWwaV6wMDhLykowagWZo9+14qIKBgc9vNWcB9VJI9uXozwx7WaY TVvNA74wcabOM0deQZ6B3gRCWYpl3nHZXuY5puaIG57ricSE77EJniC04X7ichgtd7ry HLjIavfM5K3tJl/XolNHMpNarkrB70Hm/jX6Z0IyItEk4+Oalp9NUG90+uJB2FSuDX5G fqCA== 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 :subject:cc:to:from:date:dkim-signature; bh=toxbKtlios/OXMsWkELSsG4wmGzSzCKNfORqN1i40yw=; fh=tCqh6BGddhDRP8zI1JN/zHK9jbevKh/Y3qnyfYlwr08=; b=UIX/oZLdvIjyDYLg9smWlvg1XfxTDvX8BSBxsB8u35atD4j3FcbQ1d42X1kBzPLLaU iwK6egP97ILmtFarfMFbN6Qk6WYISvyvOHbCfay4heeqzsJtXd9d5RmLHYCdWOFcDMHs ZJi5qnGOcOlK0XRCVVE9c2UhiffwGu97XtpqvJ2QvPB6ruamxo+jwvEFshXFzGI7VoXZ LJIeIqM02tSyFS2oyg1A0XL4eINvAOaDi63B6QMUYUHele2f6qBeiQbcA1UtteIX6KbZ jG72Xah9lhhHCTKBXSmdMIt0MiizxO40Jzq7DU9He62qOlPLoqGDeJsrFeeAIZv7A/7J GAsQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WH1d1Zrd; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172527-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i8-20020a17090a4b8800b002a5cea13d33si208894pjh.59.2024.05.07.17.27.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 17:27:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WH1d1Zrd; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172527-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 59B1DB26417 for ; Wed, 8 May 2024 00:06:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 034251332B8; Tue, 7 May 2024 23:41:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WH1d1Zrd" 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 C242B13329C; Tue, 7 May 2024 23:41:13 +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=1715125274; cv=none; b=OkB35gfkdzPhtxtDSZ4xgBJaX+HxhZRSo/vGhdQEEV5SQ3ksd4gKg110XV6oqNBBnWG8KIrg6i0gwC28u81zkEaeCKv2aG1aTHFRZ4SGQJc6xiwi22vHHkA0ro4iKNjdg8lGoLJOKgqC9G9mHqwnUFwN3gpTLAqnx0RSIXLU52w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715125274; c=relaxed/simple; bh=ODKOUMcSjW/vZC3ltI6tPmb1ZIxhx2e2qI8/N+kVOFM=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=t6CwqmleLwgGY8hf61vJqTDLDLKUtwOl7IzBSRD290G2mMXBMDMCx2LipMBcNV+MIlJJGXlHmZeFSZuCnSIP01uaA+ypqj6ovQ9E8BObn5JsrlczQyJPZMNFW+hJSgwjhYqdHmj6d7yuiJRfiFhF3lDHotmh5ouLRqgfEu8Km9Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WH1d1Zrd; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D7C2C2BBFC; Tue, 7 May 2024 23:41:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715125273; bh=ODKOUMcSjW/vZC3ltI6tPmb1ZIxhx2e2qI8/N+kVOFM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=WH1d1ZrdOijFn1cT49Jm2ZUSrhswkNac03IVHqq7Eohwb66eyC/upbIRg5OmSQrS5 590jVcfK2eY7mGIT+Mu2vfZ+2kgvT+bvvG+nO0mpjkaiB/hzuVoCqKdsgEfcB6GUI1 tZHVMdm8HMaoJWCDDdSEsUeSS2shrn9LwS64jApcEtp551mG5S0J2RXLfrbG59V5J/ HlHuUhoLZsEzbSOo52SetyHK+GisTyafXt2qQ3WQN6uI/FPqAL3Mvb4Ag0onvgsMRn PVoqChvTS3nyekeTg4nrB1W8PoOH/gLtrOXEqRETwcHpFeC7NS6eZgZtaW77r3KgQl SXWjnlSGSut3Q== Date: Wed, 8 May 2024 08:41:02 +0900 From: Masami Hiramatsu (Google) To: Mike Rapoport Cc: linux-kernel@vger.kernel.org, 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 , Nadav Amit , Palmer Dabbelt , Peter Zijlstra , Philippe =?UTF-8?B?TWF0aGlldS1EYXVkw6k=?= , 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: Re: [PATCH RESEND v8 05/16] module: make module_memory_{alloc,free} more self-contained Message-Id: <20240508084102.9e9b18a9b111d427e7cc9c94@kernel.org> In-Reply-To: <20240505160628.2323363-6-rppt@kernel.org> References: <20240505160628.2323363-1-rppt@kernel.org> <20240505160628.2323363-6-rppt@kernel.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Sun, 5 May 2024 19:06:17 +0300 Mike Rapoport wrote: > From: "Mike Rapoport (IBM)" > > Move the logic related to the memory allocation and freeing into > module_memory_alloc() and module_memory_free(). > Looks good to me. Reviewed-by: Masami Hiramatsu (Google) Thanks, > 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; > } > > -- > 2.43.0 > -- Masami Hiramatsu (Google)