Message ID | 03dd7de1cecdb7084814f2fab300c9bc716aff3e.1701632867.git.christophe.jaillet@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp2386793vqy; Sun, 3 Dec 2023 11:49:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IH2ECScxO8QriR1ladX4hV1Q3qq+Y4lcUy4ziGBZddxRgJynAOpxl4zACdxiill11ymyWXZ X-Received: by 2002:a05:6a21:6da3:b0:18c:1e48:366b with SMTP id wl35-20020a056a216da300b0018c1e48366bmr831192pzb.48.1701632967648; Sun, 03 Dec 2023 11:49:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701632967; cv=none; d=google.com; s=arc-20160816; b=FtUxjJvi849gwX3pV0TUpjBNqu5qXCZ5SKxhTOxqSrGcXb6enzNRte7o/8uQ9tP17G 7AwssuR/3G9pKZhQyODE3rMD6K9RG1zW2BcDCWaeDl69x7y9qbISG2SYZkuzur8be37m JuHPZ6UZnob2uJfmqSwfMlUvBKU9xr0O3eVLe4vR0yw0XvYU8C5bo6wXuCd8j80wsKGa Potn4JIVfLOqRfVSLqt0Y8Pce/C8bvg0hd+8Mxd7mAB+lXB6Fw/SqSYQMKBaBQ4iYJ67 aCiSRdxxl2xOKikxr9iEEZXJ3Q16gMSP2j83wuCOjWvPP1xqjOYS+e5U2dOFeJcMD2u4 b+Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=TtuieOqB3d5/4LucYB7b5rnfDifiCNhm6IjslDyNUYo=; fh=yIyoy0ChXqCAeZhgeOYs4BL63Xe7uyevqNGFGLU9CQo=; b=lRlGN7++zZ60fmECX3dNBmXOhf9rN3jZhWXn+bCfqjZQIGGe12umU8U12WyYPM+iB7 p8foBn0/z6OyJKSjDbL3+EhF05J2UxDNLVMs1rw0Mb0P/yBAs6D2bUCwXuxVo+DYgIiZ dvwRQCvUCuymrxClnAiebijfUkqS4N55a93vlH1sYKsMl8ajDj7kOv5xQ3S6b2TZX0dK 71vUuR0p5MSCZTEHxJz9gYHYE1AL4W42LMNKBe+nuRdasgDCb0RH1lY9GBQiJ6RaBVOP N9cn2ZhY60Em5itQUThGE4olxvceOHlAA4RsEaU7375XYByWjsT4kdqR4wHmS/OtfBbC h0Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=MS4EngJt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id a1-20020a170902ecc100b001c62139b164si6757927plh.38.2023.12.03.11.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Dec 2023 11:49:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=MS4EngJt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E87988087B4D; Sun, 3 Dec 2023 11:49:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229739AbjLCTs6 (ORCPT <rfc822;pwkd43@gmail.com> + 99 others); Sun, 3 Dec 2023 14:48:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234047AbjLCTsr (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 3 Dec 2023 14:48:47 -0500 Received: from smtp.smtpout.orange.fr (smtp-15.smtpout.orange.fr [80.12.242.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B18A0A0 for <linux-kernel@vger.kernel.org>; Sun, 3 Dec 2023 11:48:15 -0800 (PST) Received: from pop-os.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id 9sRor2SAvaCTP9sRprMITc; Sun, 03 Dec 2023 20:48:14 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1701632894; bh=TtuieOqB3d5/4LucYB7b5rnfDifiCNhm6IjslDyNUYo=; h=From:To:Cc:Subject:Date; b=MS4EngJtRAiKHXOhDy56XDRMvJvD38MihZwlUI9BM1bvLFwWgSWqc7ARwSYqGx4+M 9aUEU2XHOXW/zTqWH9JCKxmxNHJqJOi6GyXM8jZA6tdC9Cm0RpQRZC/vKOThwNyhkc Kk4pdzOxdqhd3Z16ftNUZtK0cgW9wgUHfqwoAFiG2A6AlCS/+r0dl0Bc4h1rKonw3P 83axw2HhH/MCu290ZmatMZIFnHJCOjU4SZEmumRe7klgCZ2mtKqQ1s4+9T34CmsWr7 54WEOyacF3tDZjfJ25dy/AyUsd4z/ETkLBuXJ6B3tHjWfObNnjv3GCNEWKLe5t5wmg 5uqzNG4j/Cthg== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 03 Dec 2023 20:48:14 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Song Liu <song@kernel.org>, Kees Cook <keescook@chromium.org>, "Gustavo A. R. Silva" <gustavoars@kernel.org> Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET <christophe.jaillet@wanadoo.fr>, linux-raid@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] md/md-multipath: Convert "struct mpconf" to flexible array Date: Sun, 3 Dec 2023 20:48:06 +0100 Message-Id: <03dd7de1cecdb7084814f2fab300c9bc716aff3e.1701632867.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sun, 03 Dec 2023 11:49:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784291490503682159 X-GMAIL-MSGID: 1784291490503682159 |
Series |
md/md-multipath: Convert "struct mpconf" to flexible array
|
|
Commit Message
Christophe JAILLET
Dec. 3, 2023, 7:48 p.m. UTC
The 'multipaths' field of 'struct mpconf' can be declared as a flexible
array.
The advantages are:
- 1 less indirection when accessing to the 'multipaths' array
- save 1 pointer in the structure
- improve memory usage
- give the opportunity to use __counted_by() for additional safety
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
On my x86_64 system, with configured with allmodconfig, I have:
Before the change:
=================
struct mpconf {
struct mddev * mddev; /* 0 8 */
struct multipath_info * multipaths; /* 8 8 */
int raid_disks; /* 16 4 */
/* XXX 4 bytes hole, try to pack */
spinlock_t device_lock; /* 24 72 */
/* --- cacheline 1 boundary (64 bytes) was 32 bytes ago --- */
struct list_head retry_list; /* 96 16 */
mempool_t pool; /* 112 200 */
/* size: 312, cachelines: 5, members: 6 */
/* sum members: 308, holes: 1, sum holes: 4 */
/* last cacheline: 56 bytes */
};
struct multipath_info {
struct md_rdev * rdev; /* 0 8 */
/* size: 8, cachelines: 1, members: 1 */
/* last cacheline: 8 bytes */
};
size drivers/md/md-multipath.o
text data bss dec hex filename
12863 1041 16 13920 3660 drivers/md/md-multipath.o
After the change:
================
struct mpconf {
struct mddev * mddev; /* 0 8 */
int raid_disks; /* 8 4 */
/* XXX 4 bytes hole, try to pack */
spinlock_t device_lock; /* 16 72 */
/* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */
struct list_head retry_list; /* 88 16 */
mempool_t pool; /* 104 200 */
/* --- cacheline 4 boundary (256 bytes) was 48 bytes ago --- */
struct multipath_info multipaths[]; /* 304 0 */
/* size: 304, cachelines: 5, members: 6 */
/* sum members: 300, holes: 1, sum holes: 4 */
/* last cacheline: 48 bytes */
};
struct multipath_info {
struct md_rdev * rdev; /* 0 8 */
/* size: 8, cachelines: 1, members: 1 */
/* last cacheline: 8 bytes */
};
size drivers/md/md-multipath.o
text data bss dec hex filename
12470 1041 16 13527 34d7 drivers/md/md-multipath.o
So:
- about 400 bytes of code are saved.
- because of the way memory allocation works, 'struct mpconf' really
uses 512 bytes of memory when allocated. So the "extra" memory that is
allocated (512-304 = 208) can be used to store up to 26 multipaths,
for free.
Finally, several places use pointer arithmetic to access the desired
structure, such as:
for (i = 0; i < conf->raid_disks; i++) {
tmp = conf->multipaths + i;
if (tmp->rdev)
Should this be rewritten as:
for (i = 0; i < conf->raid_disks; i++) {
if (tmpconf->multipaths[i]->rdev)
in order to have the compiler be able to check boundaries defined by
__counted_by()?
---
drivers/md/md-multipath.c | 12 +++---------
drivers/md/md-multipath.h | 3 ++-
2 files changed, 5 insertions(+), 10 deletions(-)
Comments
On Sun, Dec 03, 2023 at 08:48:06PM +0100, Christophe JAILLET wrote: > The 'multipaths' field of 'struct mpconf' can be declared as a flexible > array. > > The advantages are: > - 1 less indirection when accessing to the 'multipaths' array > - save 1 pointer in the structure > - improve memory usage > - give the opportunity to use __counted_by() for additional safety > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> This looks like a really nice conversion. I haven't run-tested this, but it reads correct to me. Reviewed-by: Kees Cook <keescook@chromium.org>
On Mon, Dec 4, 2023 at 2:20 PM Kees Cook <keescook@chromium.org> wrote: > > On Sun, Dec 03, 2023 at 08:48:06PM +0100, Christophe JAILLET wrote: > > The 'multipaths' field of 'struct mpconf' can be declared as a flexible > > array. > > > > The advantages are: > > - 1 less indirection when accessing to the 'multipaths' array > > - save 1 pointer in the structure > > - improve memory usage > > - give the opportunity to use __counted_by() for additional safety > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > This looks like a really nice conversion. I haven't run-tested this, but > it reads correct to me. Agreed this is a good optimization. However, since MD_MULTIPATH is already marked as deprecated. I don't think we should ship further changes to it. Thanks, Song
On Thu, Dec 07, 2023 at 09:33:17PM -0800, Song Liu wrote: > On Mon, Dec 4, 2023 at 2:20 PM Kees Cook <keescook@chromium.org> wrote: > > > > On Sun, Dec 03, 2023 at 08:48:06PM +0100, Christophe JAILLET wrote: > > > The 'multipaths' field of 'struct mpconf' can be declared as a flexible > > > array. > > > > > > The advantages are: > > > - 1 less indirection when accessing to the 'multipaths' array > > > - save 1 pointer in the structure > > > - improve memory usage > > > - give the opportunity to use __counted_by() for additional safety > > > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > > > This looks like a really nice conversion. I haven't run-tested this, but > > it reads correct to me. > > Agreed this is a good optimization. However, since MD_MULTIPATH is > already marked as deprecated. I don't think we should ship further > changes to it. Hm, that seems like a weird catch-22 to me. I would say we should continue to improve any code in the kernel that people spend time to work on, or we should remove that code entirely. Should MD_MULTIPATH be removed? How long has it been deprecated? (We just had an LTS release, so doing removal now is a good time...)
On Fri, Dec 8, 2023 at 9:27 AM Kees Cook <keescook@chromium.org> wrote: > > On Thu, Dec 07, 2023 at 09:33:17PM -0800, Song Liu wrote: > > On Mon, Dec 4, 2023 at 2:20 PM Kees Cook <keescook@chromium.org> wrote: > > > > > > On Sun, Dec 03, 2023 at 08:48:06PM +0100, Christophe JAILLET wrote: > > > > The 'multipaths' field of 'struct mpconf' can be declared as a flexible > > > > array. > > > > > > > > The advantages are: > > > > - 1 less indirection when accessing to the 'multipaths' array > > > > - save 1 pointer in the structure > > > > - improve memory usage > > > > - give the opportunity to use __counted_by() for additional safety > > > > > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > > > > > This looks like a really nice conversion. I haven't run-tested this, but > > > it reads correct to me. > > > > Agreed this is a good optimization. However, since MD_MULTIPATH is > > already marked as deprecated. I don't think we should ship further > > changes to it. > > Hm, that seems like a weird catch-22 to me. I would say we should > continue to improve any code in the kernel that people spend time to > work on, or we should remove that code entirely. Should MD_MULTIPATH be > removed? How long has it been deprecated? (We just had an LTS release, > so doing removal now is a good time...) We marked it as deprecated about 2.5 years ago. But to be honest, I currently don't have a plan to remove it. I guess I should start thinking about it. Thanks, Song
On Fri, Dec 08, 2023 at 10:11:10AM -0800, Song Liu wrote: > We marked it as deprecated about 2.5 years ago. But to be honest, > I currently don't have a plan to remove it. I guess I should start thinking > about it. Let's just kill it off ASAP. It never had a large user base and based by dm-multipath not long after it has been added. It also doesn't support any uniqueue hardware and has no on-disk format. If you want any blame deflected from you I'd be happy to send the patch to remove it.
Hi Christoph, On Mon, Dec 11, 2023 at 9:46 PM Christoph Hellwig <hch@infradead.org> wrote: > > On Fri, Dec 08, 2023 at 10:11:10AM -0800, Song Liu wrote: > > We marked it as deprecated about 2.5 years ago. But to be honest, > > I currently don't have a plan to remove it. I guess I should start thinking > > about it. > > Let's just kill it off ASAP. It never had a large user base and based > by dm-multipath not long after it has been added. It also doesn't > support any uniqueue hardware and has no on-disk format. Thanks for the suggestion. > If you want any blame deflected from you I'd be happy to send the patch > to remove it. Let me give it a try. I am kinda curious what gonna happen. :) Thanks, Song
diff --git a/drivers/md/md-multipath.c b/drivers/md/md-multipath.c index d22276870283..6a23065a65f7 100644 --- a/drivers/md/md-multipath.c +++ b/drivers/md/md-multipath.c @@ -357,16 +357,13 @@ static int multipath_run (struct mddev *mddev) * should be freed in multipath_free()] */ - conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL); + conf = kzalloc(struct_size(conf, multipaths, mddev->raid_disks), + GFP_KERNEL); mddev->private = conf; if (!conf) goto out; - conf->multipaths = kcalloc(mddev->raid_disks, - sizeof(struct multipath_info), - GFP_KERNEL); - if (!conf->multipaths) - goto out_free_conf; + conf->raid_disks = mddev->raid_disks; working_disks = 0; rdev_for_each(rdev, mddev) { @@ -384,7 +381,6 @@ static int multipath_run (struct mddev *mddev) working_disks++; } - conf->raid_disks = mddev->raid_disks; conf->mddev = mddev; spin_lock_init(&conf->device_lock); INIT_LIST_HEAD(&conf->retry_list); @@ -421,7 +417,6 @@ static int multipath_run (struct mddev *mddev) out_free_conf: mempool_exit(&conf->pool); - kfree(conf->multipaths); kfree(conf); mddev->private = NULL; out: @@ -433,7 +428,6 @@ static void multipath_free(struct mddev *mddev, void *priv) struct mpconf *conf = priv; mempool_exit(&conf->pool); - kfree(conf->multipaths); kfree(conf); } diff --git a/drivers/md/md-multipath.h b/drivers/md/md-multipath.h index b3099e5fc4d7..fb49e151ac94 100644 --- a/drivers/md/md-multipath.h +++ b/drivers/md/md-multipath.h @@ -8,12 +8,13 @@ struct multipath_info { struct mpconf { struct mddev *mddev; - struct multipath_info *multipaths; int raid_disks; spinlock_t device_lock; struct list_head retry_list; mempool_t pool; + + struct multipath_info multipaths[] __counted_by(raid_disks); }; /*