From patchwork Mon May 22 21:21:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13251147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDE16C7EE29 for ; Mon, 22 May 2023 21:21:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232913AbjEVVVT (ORCPT ); Mon, 22 May 2023 17:21:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231439AbjEVVVS (ORCPT ); Mon, 22 May 2023 17:21:18 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBAD89D for ; Mon, 22 May 2023 14:21:17 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1afa6afcf4fso19394105ad.0 for ; Mon, 22 May 2023 14:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684790477; x=1687382477; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=EtWptAsa4eS8Kz5UJ2iVjBJtBMlQ0SUqdqNlkHKBLEs=; b=JIq7ayuhUCPY+jd41Z0soEOBn/LFyKyWmnktUDKUkFjbcodRiaN8GF9yRYnWncUysg JMis495bRgGRsJPACvfNdeWyPpeRvfE+6TCQlXMJSjY4oCQAKJ2NcKfPCjEzu8E/qn+d 4O2sTSDN8hdrY3dVMXBStxPpbdjTlpkZ95Wb4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684790477; x=1687382477; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EtWptAsa4eS8Kz5UJ2iVjBJtBMlQ0SUqdqNlkHKBLEs=; b=SlJp29rhKN+FjBjk9ugCjAxnD+A7TMJ98754N1+lXYE0bcbFFvK5K+Pdb/M9hFBXmG HqKo2qQP/xZbctsrgNZRdeKjB6YK0OqIiM8lkfHwocZIGjtK7j1OrB4QqUXKKCPA0MfW ZBTPSBxW5COEQvVS6MV9PMNJFYjXRPJ3OQ5m5Qki/PFK8fPfXBq7wnJkNoWrBaDTBvUK YApobsuRSTL0jU5ljfSLBWpp3IVyxlYtG0VF2S2jbM2n92NjHUqYc924rNRrEbkNKvP2 cJVS6N0JKRG7QMtUgqjBVGd3QvRGi7pD8v5xwd9rhOt9QOf9KPlXU03aWoBvadlH9y0V J69g== X-Gm-Message-State: AC+VfDyE24kvX9iHQRWVhDnPUogCkQP8mYXSTXHLZ1+aSae5gfSKNcQ2 7oNUjdif2NUix5+MvJkHJ77rQkCX/XTR4AkjgkI= X-Google-Smtp-Source: ACHHUZ4Qk7EITjWh+NVhaDiZRNwGFS+DHaJu7PM8BcgUFv03Lt3J0yK+zWPWlEVAJbnH7f3us6FDQQ== X-Received: by 2002:a17:903:48a:b0:1ae:52ea:5fa6 with SMTP id jj10-20020a170903048a00b001ae52ea5fa6mr11039976plb.8.1684790477220; Mon, 22 May 2023 14:21:17 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id o18-20020a170902d4d200b001ae44e2f425sm5239217plg.223.2023.05.22.14.21.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 14:21:16 -0700 (PDT) From: Kees Cook To: Song Liu Cc: Kees Cook , Christoph Hellwig , linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2] md/raid5: Convert stripe_head's "dev" to flexible array member Date: Mon, 22 May 2023 14:21:15 -0700 Message-Id: <20230522212114.gonna.589-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2398; h=from:subject:message-id; bh=fm/ezGihLT88LzMah4Fd0e38oNOXvo+hj+2PCvpzioM=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBka9zLVXP5GF5nusCTtCCpiPzdqxuZYP1lhGnV3/nP RaabgLaJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZGvcywAKCRCJcvTf3G3AJqLNEA CQhGxdsHJfIqJXnwaI/TKJqp1MlIRzOzuHQNQKFtG4lEGw33c0Z9FoyV3oXosgTp/gW+KVRR2Fp7Bn x4Ca7pXIy0HGuOLJlMhQJXKh3AfnPuibkkQVSpnvWqCjTcGz/mBhQOt+Q11QIAYdp+bL/n25kQV2Pm VuS3s0lLyhnltc6DArfq/ujY8mva8GUWZJ2i881mp2oL+BIgS7/4/HvvbcPj9tPQUXtKZv7UnVwFiK LhH/HXhio18g9cdQ9xgpsohuY0bjXL/Ij86RbwWRD3fhS6P7ndkUBXvFi3Fu4niWIWMKweZl6DdNnG 8nbNPhPd8QOCpREE2CfIW8irzwtgQo4wb6/xB1kaXjfnFb38T0HkDUP4lM4C5nOybf+CQiiEWJByHc Oo1q75O2wAAHLf7SG8kDsGKMyjXi4bKdFn8HW4qfaVITrNJIyayw8icR2WPeO+TAdMor4B+NDvJjtV 4YXgUxNr7U8KyTfeqSKfIP1/HJw20rOeFjeMwHSfHDtl5CMx6NzT9TceqDuw1TY/DnG7lPGP74L/JW 4he8eKVFBT/URlGL4CRj2P4gKD/FDj2Y6zVlR724kQKFHneEoSdRF0y40JyVXPD4Ixd+FvjxffTOYA K/6TvPUHFv6VKillKK3lpunJgPFx//uKrg35R9Fb1vkkMbmlz5+t0KQk4fOw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Precedence: bulk List-ID: X-Mailing-List: linux-raid@vger.kernel.org Replace old-style 1-element array of "dev" in struct stripe_head with modern C99 flexible array. In the future, we can additionally annotate it with the run-time size, found in the "disks" member. Cc: Christoph Hellwig Cc: linux-raid@vger.kernel.org Signed-off-by: Kees Cook Reviewed-by: Christoph Hellwig Acked-by: Song Liu --- It looks like this memory calculation: memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; ... was already buggy (i.e. it included the single "dev" bytes in the result). However, I'm not entirely sure if that is the right analysis, since "dev" is not related to struct bio nor PAGE_SIZE? v1: https://lore.kernel.org/all/20230517233313.never.130-kees@kernel.org/ v2: use new struct_size_t() helper from https://lore.kernel.org/lkml/20230522211810.never.421-kees@kernel.org/ --- drivers/md/raid5.c | 4 ++-- drivers/md/raid5.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index 4739ed891e75..64865f9dd3f5 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -2433,7 +2433,7 @@ static int grow_stripes(struct r5conf *conf, int num) conf->active_name = 0; sc = kmem_cache_create(conf->cache_name[conf->active_name], - sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), + struct_size_t(struct stripe_head, dev, devs), 0, 0, NULL); if (!sc) return 1; @@ -2559,7 +2559,7 @@ static int resize_stripes(struct r5conf *conf, int newsize) /* Step 1 */ sc = kmem_cache_create(conf->cache_name[1-conf->active_name], - sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), + struct_size_t(struct stripe_head, dev, newsize), 0, 0, NULL); if (!sc) return -ENOMEM; diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h index e873938a6125..6cfc74162b41 100644 --- a/drivers/md/raid5.h +++ b/drivers/md/raid5.h @@ -268,7 +268,7 @@ struct stripe_head { unsigned long flags; u32 log_checksum; unsigned short write_hint; - } dev[1]; /* allocated with extra space depending of RAID geometry */ + } dev[]; /* allocated with extra space depending of RAID geometry */ }; /* stripe_head_state - collects and tracks the dynamic state of a stripe_head