From patchwork Fri Jul 14 09:17:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 120368 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp2380902vqm; Fri, 14 Jul 2023 02:34:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlHIxcvDE9yxVpBYo4d0AicesWQCtKfPeIhMySeWFsC8Loj4IGL/FUSGRHBRVrfPyjfgnt+c X-Received: by 2002:a17:902:ed8b:b0:1b3:e90b:93e1 with SMTP id e11-20020a170902ed8b00b001b3e90b93e1mr3512374plj.36.1689327279051; Fri, 14 Jul 2023 02:34:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689327279; cv=none; d=google.com; s=arc-20160816; b=uUXhwpxpF3J25QJuvo5QmHMGMJV9y8o1eS4nMha5l5SCn0m38BdXGo90dfMmnETs8G Oe9N/KVM+XpsM48XWCLAbOIWOAvDSaNboW0FdBY7T1NlsRfsNyq2UuLtipbt0leTRBoo cT1AkDBXjk1d+5vTzsgtgOTodz5+SJ6ZSzjrMRe9iDxkNDbfEMFhsnNtEgFL2cPKrXj4 7R/Atx/TvyYAUCxsAzKuUnRhbBtdhNnaApA58i/8uG9w0DVNui3uBWzuSSlgBvJ2IIV1 cMpqhl6DTEz5CqySvjGU2X4+dKMKEHKHAr/vD+oIX9gCmRTNC0ANu8tKqueJrYdxIUSH fdZA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UahGVgnG7TRruNhgk0MMUJHtSN/IcL1do/zRdXl4Ow4=; fh=Qdz9kbAdPAfvhj/swQYvX2BMbB6C1mb3M8cD7Q9tvzc=; b=0y+YDLE5604iXK/1YbHxi+V70LdMxe35nVrxcZjAweVzBV0fpJLwqj8LmmH1kp+FT6 hvcaCDeCL8kTxTD5IGj3BuWLqwGYapWMUJOy/GtjKqp4TcmufMhfkWVH8fTpmecfESkn suW59McUzQec2oh/EsoWOGG3O35sr5SfGCKWI34B15xdX7JRtv89qo0O2yYnkR3WR3U3 1BYToJF2Dm5BLoXmUFTuAsM3HDTNYUtWfBJY/0ho6Dq5aHnukrgY4rsECS8033AZ1Fcr ImmYcEJ6J7zy92P28y2Igvzb5qFaLuhXzcbaw7BGFppZD5gNtzMKwJ5EqJY/U1LFkJQ+ f9ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BhhCLTE1; 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=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n12-20020a1709026a8c00b001b80c35bc65si6606756plk.641.2023.07.14.02.34.25; Fri, 14 Jul 2023 02:34:39 -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=@intel.com header.s=Intel header.b=BhhCLTE1; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235413AbjGNJRv (ORCPT + 99 others); Fri, 14 Jul 2023 05:17:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234953AbjGNJRt (ORCPT ); Fri, 14 Jul 2023 05:17:49 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AA4B211C; Fri, 14 Jul 2023 02:17:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689326268; x=1720862268; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3dTgIUvDYhlnavXOxaZLL1p9dKAgNDGlalBZILcSqRs=; b=BhhCLTE1MFAnNbrfcS+QIW5/jHf9m0AXXqASs9f9rEIqw9QLVLJYhDaI NwsLQTbAv5CnqZSE8OXJTvCxmI/UlZ67orZOXJLWPmO/wqywZG8iA3mvV GL5yS4WGnIPrbI5/R0Kr9FqSor1TVHxJTUS8R9Co9VuWkvEy7poq04Mnu tw8Awp9aVpxqRGe1NBBB8aRq8UUlJomhtz/S3UL+UidGDsO5o2+qKzy1x ceCbb1lWtx2XAbf5AYXBhhMBzwTiO70hg7b8KQXQ04kie1l2VyGzZw8nw NLqjRzYhRJ+shTCOM/Z1TpxjZ/k/AkO17v/CJM9XB5JigajgiUvXpVEv4 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10770"; a="362897874" X-IronPort-AV: E=Sophos;i="6.01,204,1684825200"; d="scan'208";a="362897874" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jul 2023 02:17:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10770"; a="812357261" X-IronPort-AV: E=Sophos;i="6.01,204,1684825200"; d="scan'208";a="812357261" Received: from black.fi.intel.com ([10.237.72.28]) by FMSMGA003.fm.intel.com with ESMTP; 14 Jul 2023 02:17:44 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id AB94A3E3; Fri, 14 Jul 2023 12:17:50 +0300 (EEST) From: Andy Shevchenko To: Mark Brown , Andy Shevchenko , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sebastian Reichel Subject: [PATCH v4 4/4] spi: Use struct_size() helper Date: Fri, 14 Jul 2023 12:17:48 +0300 Message-Id: <20230714091748.89681-5-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230714091748.89681-1-andriy.shevchenko@linux.intel.com> References: <20230714091748.89681-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771388040513371597 X-GMAIL-MSGID: 1771388040513371597 The Documentation/process/deprecated.rst suggests to use flexible array members to provide a way to declare having a dynamically sized set of trailing elements in a structure.This makes code robust agains bunch of the issues described in the documentation, main of which is about the correctness of the sizeof() calculation for this data structure. Due to above, prefer struct_size() over open-coded versions. Signed-off-by: Andy Shevchenko --- include/linux/spi/spi.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 04daf61dfd3f..7f8b478fdeb3 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -1085,6 +1086,8 @@ struct spi_transfer { * @state: for use by whichever driver currently owns the message * @resources: for resource management when the SPI message is processed * @prepared: spi_prepare_message was called for the this message + * @t: for use with spi_message_alloc() when message and transfers have + * been allocated together * * A @spi_message is used to execute an atomic sequence of data transfers, * each represented by a struct spi_transfer. The sequence is "atomic" @@ -1139,6 +1142,9 @@ struct spi_message { /* List of spi_res resources when the SPI message is processed */ struct list_head resources; + + /* For embedding transfers into the memory of the message */ + struct spi_transfer t[]; }; static inline void spi_message_init_no_memset(struct spi_message *m) @@ -1199,16 +1205,13 @@ static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags { struct spi_message *m; - m = kzalloc(sizeof(struct spi_message) - + ntrans * sizeof(struct spi_transfer), - flags); + m = kzalloc(struct_size(m, t, ntrans), flags); if (m) { unsigned i; - struct spi_transfer *t = (struct spi_transfer *)(m + 1); spi_message_init_no_memset(m); - for (i = 0; i < ntrans; i++, t++) - spi_message_add_tail(t, m); + for (i = 0; i < ntrans; i++) + spi_message_add_tail(&m->t[i], m); } return m; }