Message ID | 20230405022702.753323-2-mcgrof@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp18457vqo; Tue, 4 Apr 2023 19:46:11 -0700 (PDT) X-Google-Smtp-Source: AKy350bFvZzkbTVm0QpxDzvBsrgIwnivk6YowwNtD/F1z2aUqkkij8VAKIWLSUrwYguztLObxJwo X-Received: by 2002:a17:903:2803:b0:1a2:7d:17f2 with SMTP id kp3-20020a170903280300b001a2007d17f2mr3889598plb.69.1680662771745; Tue, 04 Apr 2023 19:46:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680662771; cv=none; d=google.com; s=arc-20160816; b=oA/kO3/TB/rwEvtYIfCvYSYUvzJYPUz1tS/6Y2xQfnnXtzDujBW8pkQRjdXp20RnV6 nyDVbjX1hvg4borL4pB8huZsrGuCwXoN3Hoaz1d4Yx/WFNaTi9kqNS0zG1JII7alVaYw Ixn0BaYa10tHRVCNihRajDrpAhw4OSVRLHpk7o1W1J3FnUBWOiJ7aBXbQsiAAokSIg51 01XPYg4tEBWazqY6oOHmpyxnx1Bfp1YoAYyOCTM0ocr1BGwKm/c3apdlZG4cTjqcl+8I dwE9sf5oZbefn+fnWWC30Jf/80Ud02SYGHDKjdgtQMC4NkP4scUN5PhW3YVOdGoYpnNC Qkvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/zFMu+hCUyA4Z/2f84k8anH54tplY3/TfwT6JNMhL7g=; b=wiN9o8dloJbt6AXhn2coyCJpRXaSfpx/e7gUyoFx/kYyrzrGP3nUyhYT+yjJkS8rPu Brz5O+kMXScP0Faoy8Dg+sX4UzU33wPNBO19ob9mBFEim2NwTKFfunKNxpjtOR+mK1kR t3zaoRUAb7zABWzzxOCYUWQPG1gbl58D+8j0nRMYYRVEMsIygQlbdFX5Q2tsBujkhIFb LC1vqWpHSAfmoO9MDXUP7gCxd/IQpg6POIVKgFDAFSW9gU5oN/5/RYMeZIQGWOY4kvt0 9m5Wt0a+uTYi+H1N00cYteG/cIjlaOllR9ppDfvV+DZAdhHnIhnb04FD+1aIQmVpwXkf Aq+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=UjurHh14; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u17-20020a17090341d100b0019f24b0388csi11790554ple.486.2023.04.04.19.45.53; Tue, 04 Apr 2023 19:46:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=UjurHh14; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236761AbjDEC1X (ORCPT <rfc822;lkml4gm@gmail.com> + 99 others); Tue, 4 Apr 2023 22:27:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230455AbjDEC1Q (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 4 Apr 2023 22:27:16 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A668F2697; Tue, 4 Apr 2023 19:27:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=/zFMu+hCUyA4Z/2f84k8anH54tplY3/TfwT6JNMhL7g=; b=UjurHh14lS41LO3RhiYhdywUTf UEGL5ucwoWlBmR4Hh56vx44CPgIURTZMI+Sr8oPTudV3S1Fcxp4zufGjiC43e1ePkCMeBV+l4AuMR /+HFs3mc3aVXV+s66JFe+6Q3nYSF9IF1M+BCuEkQKFBiioYOWtcRJoNb48oOOKVL1vyQ/p9WNnNGT M1J7y9MPKOwQFj8/Ye23sAKx2zZsk+zHocAPyVlq99hFg8XUnbHmBDmqfPWdZIwMpN6ewNhd05eox KXhCOHRNjv2ytJo4Vf9IuwS/F8EH6fnRFtdknAq6LH8q7YYuzEuerM5aoWv6Vlxk+d6vKujoj8nKh vMShxgIA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1pjsrb-003A05-1W; Wed, 05 Apr 2023 02:27:07 +0000 From: Luis Chamberlain <mcgrof@kernel.org> To: david@redhat.com, patches@lists.linux.dev, linux-modules@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, pmladek@suse.com, petr.pavlu@suse.com, prarit@redhat.com, torvalds@linux-foundation.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: christophe.leroy@csgroup.eu, tglx@linutronix.de, peterz@infradead.org, song@kernel.org, rppt@kernel.org, dave@stgolabs.net, willy@infradead.org, vbabka@suse.cz, mhocko@suse.com, dave.hansen@linux.intel.com, colin.i.king@gmail.com, jim.cromie@gmail.com, catalin.marinas@arm.com, jbaron@akamai.com, rick.p.edgecombe@intel.com, mcgrof@kernel.org Subject: [PATCH v2 1/6] module: fix kmemleak annotations for non init ELF sections Date: Tue, 4 Apr 2023 19:26:57 -0700 Message-Id: <20230405022702.753323-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230405022702.753323-1-mcgrof@kernel.org> References: <20230405022702.753323-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: Luis Chamberlain <mcgrof@infradead.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762302647018282826?= X-GMAIL-MSGID: =?utf-8?q?1762302647018282826?= |
Series |
module: avoid userspace pressure on unwanted allocations
|
|
Commit Message
Luis Chamberlain
April 5, 2023, 2:26 a.m. UTC
Commit ac3b43283923 ("module: replace module_layout with module_memory")
reworked the way to handle memory allocations to make it clearer. But it
lost in translation how we handled kmemleak_ignore() or kmemleak_not_leak()
for different ELF sections.
Fix this and clarify the comments a bit more.
Fixes: ac3b43283923 ("module: replace module_layout with module_memory")
Reported-by: Jim Cromie <jim.cromie@gmail.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
kernel/module/main.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
Comments
On Tue, Apr 4, 2023 at 7:27 PM Luis Chamberlain <mcgrof@kernel.org> wrote: > > Commit ac3b43283923 ("module: replace module_layout with module_memory") > reworked the way to handle memory allocations to make it clearer. But it > lost in translation how we handled kmemleak_ignore() or kmemleak_not_leak() > for different ELF sections. > > Fix this and clarify the comments a bit more. > > Fixes: ac3b43283923 ("module: replace module_layout with module_memory") > Reported-by: Jim Cromie <jim.cromie@gmail.com> > Signed-off-by: Luis Chamberlain <mcgrof@kernel.org> Acked-by: Song Liu <song@kernel.org> Thanks for the fix! > --- > kernel/module/main.c | 20 ++++++++++++++++---- > 1 file changed, 16 insertions(+), 4 deletions(-) > > diff --git a/kernel/module/main.c b/kernel/module/main.c > index 5cc21083af04..d8bb23fa6989 100644 > --- a/kernel/module/main.c > +++ b/kernel/module/main.c > @@ -2233,11 +2233,23 @@ static int move_module(struct module *mod, struct load_info *info) > ptr = module_memory_alloc(mod->mem[type].size, type); > > /* > - * The pointer to this block is stored in the module structure > - * which is inside the block. Just mark it as not being a > - * leak. > + * 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 treat them slightly > + * differently and only grey them out -- they work as typical > + * memory allocations which *do* eventually get freed. > */ > - kmemleak_ignore(ptr); > + switch (type) { > + case MOD_INIT_TEXT: /* fallthrough */ > + case MOD_INIT_DATA: /* fallthrough */ > + case MOD_INIT_RODATA: /* fallthrough */ > + kmemleak_ignore(ptr); > + break; > + default: > + kmemleak_not_leak(ptr); > + } > if (!ptr) { > t = type; > goto out_enomem; > -- > 2.39.2 >
On Tue, Apr 04, 2023 at 07:26:57PM -0700, Luis Chamberlain wrote: > diff --git a/kernel/module/main.c b/kernel/module/main.c > index 5cc21083af04..d8bb23fa6989 100644 > --- a/kernel/module/main.c > +++ b/kernel/module/main.c > @@ -2233,11 +2233,23 @@ static int move_module(struct module *mod, struct load_info *info) > ptr = module_memory_alloc(mod->mem[type].size, type); > > /* > - * The pointer to this block is stored in the module structure > - * which is inside the block. Just mark it as not being a > - * leak. > + * 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 treat them slightly > + * differently and only grey them out -- they work as typical > + * memory allocations which *do* eventually get freed. > */ > - kmemleak_ignore(ptr); > + switch (type) { > + case MOD_INIT_TEXT: /* fallthrough */ > + case MOD_INIT_DATA: /* fallthrough */ > + case MOD_INIT_RODATA: /* fallthrough */ > + kmemleak_ignore(ptr); > + break; > + default: > + kmemleak_not_leak(ptr); > + } This works as well but if you want to keep it simple, just call kmemleak_not_leak() in all cases. When freeing the init sections, they would be removed from the kmemleak tracing anyway.
On Tue, Apr 11, 2023 at 04:17:35PM +0100, Catalin Marinas wrote: > On Tue, Apr 04, 2023 at 07:26:57PM -0700, Luis Chamberlain wrote: > > diff --git a/kernel/module/main.c b/kernel/module/main.c > > index 5cc21083af04..d8bb23fa6989 100644 > > --- a/kernel/module/main.c > > +++ b/kernel/module/main.c > > @@ -2233,11 +2233,23 @@ static int move_module(struct module *mod, struct load_info *info) > > ptr = module_memory_alloc(mod->mem[type].size, type); > > > > /* > > - * The pointer to this block is stored in the module structure > > - * which is inside the block. Just mark it as not being a > > - * leak. > > + * 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 treat them slightly > > + * differently and only grey them out -- they work as typical > > + * memory allocations which *do* eventually get freed. > > */ > > - kmemleak_ignore(ptr); > > + switch (type) { > > + case MOD_INIT_TEXT: /* fallthrough */ > > + case MOD_INIT_DATA: /* fallthrough */ > > + case MOD_INIT_RODATA: /* fallthrough */ > > + kmemleak_ignore(ptr); > > + break; > > + default: > > + kmemleak_not_leak(ptr); > > + } > > This works as well but if you want to keep it simple, just call > kmemleak_not_leak() in all cases. When freeing the init sections, they > would be removed from the kmemleak tracing anyway. It is up to you as you were the one who originally used different calls here, so I didn't want to change the old mechanism. Changing it to use kmemleak_not_leak() would be a functional change, do we loose anything for using kmemleak_not_leak() for all? Ie, why had you used a different set of calls when you first added this depending on the if its init or not? Is the value no longer there? Luis
diff --git a/kernel/module/main.c b/kernel/module/main.c index 5cc21083af04..d8bb23fa6989 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2233,11 +2233,23 @@ static int move_module(struct module *mod, struct load_info *info) ptr = module_memory_alloc(mod->mem[type].size, type); /* - * The pointer to this block is stored in the module structure - * which is inside the block. Just mark it as not being a - * leak. + * 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 treat them slightly + * differently and only grey them out -- they work as typical + * memory allocations which *do* eventually get freed. */ - kmemleak_ignore(ptr); + switch (type) { + case MOD_INIT_TEXT: /* fallthrough */ + case MOD_INIT_DATA: /* fallthrough */ + case MOD_INIT_RODATA: /* fallthrough */ + kmemleak_ignore(ptr); + break; + default: + kmemleak_not_leak(ptr); + } if (!ptr) { t = type; goto out_enomem;