From patchwork Sun Nov 26 08:08:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 169839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp2352692vqx; Sun, 26 Nov 2023 00:08:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqhXADgBi20AZMC37zqCXpGHgsCXVj7g8xB1l3NSXWDvAUWPMM3Ixx5PuOh51mA2eEad7T X-Received: by 2002:a05:6a00:2a09:b0:6cb:bc1a:dcff with SMTP id ce9-20020a056a002a0900b006cbbc1adcffmr8624295pfb.13.1700986137808; Sun, 26 Nov 2023 00:08:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700986137; cv=none; d=google.com; s=arc-20160816; b=o+QbzxyJ0Ph/MI/QRgwiutPeOVJm32fM1hgSdsQuQV0HXV6vljMhWduulLxeG+XMyg 0VK9s3xWFgtf3AOUk3BN388Rrrckf/tLk2tR3PfkIAJ3BGTmKN2ixYLEmbebXgvXi9mD 1Uw2XOCXDsmQuG00vQQ8lbRxN4naC5SDVuj9xzez07qy1iwCigDECZEKqLY63Uh7jCQJ UCG+YxnkPNHDrzosnhySRtg+NA9nbmNsc9XkOHj3wXjft2yZEMv4Uo0bHb+Lvq5dqw/u DtxCZ6rRFuVdudF/9ADsCvIfsLoODU8c8DmC9po/5Bc44W71v0/QwLhQnI/VYlbplZ/j YGrQ== 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=ihzn0KPFS4a8rEWK4q4gtFzvzq2XmR8bZ6Yyi85TMNU=; fh=Mp2RKKgsGTzxeuAJFV/mbH8ARysd2gZ6aHLbmovbMD4=; b=jm0nbGBjBCmc3WlnMYwoYAvdCef6PeGdwYI08bCT36W2y5WyObEOGRaVrctyQd98wN Ri5aEquPUINSAt3dsBHbS61od4sgjR+5tk+iQIFMpD2DWprI9hpfZIccJTuM+QCalNVs Jv+hsluSLmvcffwd98BBqCXBvMCrtjy7LwoiJjjifYJj/qJUJE3wSU0Bj2/PHwG44+im Ug/G2ilGEXvwiPVL3hjd/cbufKker117GdvhOANG5BCJ8ruN/S4quybCNBLehhr8Xv4O 8ZCfld1FfAqmsYqX1ydyu1scBbbZelroa0fnAd2Evmddoiay8ACmEfSmLtHw914NymRz WMow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=gWZrLjS0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id q3-20020a631f43000000b005b82f3dcec6si7063793pgm.269.2023.11.26.00.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 00:08:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=gWZrLjS0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id A4FE68050021; Sun, 26 Nov 2023 00:08:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229469AbjKZIIY (ORCPT + 99 others); Sun, 26 Nov 2023 03:08:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbjKZIIW (ORCPT ); Sun, 26 Nov 2023 03:08:22 -0500 Received: from smtp.smtpout.orange.fr (smtp-29.smtpout.orange.fr [80.12.242.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C361E9 for ; Sun, 26 Nov 2023 00:08:27 -0800 (PST) Received: from pop-os.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id 7ABkrYL9QVgeS7ABlr7RlM; Sun, 26 Nov 2023 09:08:25 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1700986105; bh=ihzn0KPFS4a8rEWK4q4gtFzvzq2XmR8bZ6Yyi85TMNU=; h=From:To:Cc:Subject:Date; b=gWZrLjS0flKsH0QmD1f7N4njbnOgf7UccPGQLRy3f9e3zlocR/IjqLZbtz7/K0wl2 hucwFmrV8bYzZDyRJMSVykswh7WBwyplVMjIW7dePApscUupy2JfElO1W44pWBsto+ 1UysEw8+ZcOuZlaTcFrNggcyWCx194nTLAnrBKD5rnKT0kAQDLOs2KUXhkS4wD5yuq Fc5WAgRwZvW9MRW+1D/SbdOm4WV43nWavlBWbDOo6fuYk+vWBwlcnupA+Y3YAJ3enm uoqQrDz8aJbNfREzIeB/Ad9YUwzAo8Pgpt9GejrE7r+XS2qAgCEAIQXczN4vGHb6IS mI0oSTRivjohw== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 26 Nov 2023 09:08:25 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET To: Peter Rosin , Kees Cook , "Gustavo A. R. Silva" Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-hardening@vger.kernel.org Subject: [PATCH 1/2] mux: Turn 'mux' into a flexible array in 'struct mux_chip' Date: Sun, 26 Nov 2023 09:08:11 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Sun, 26 Nov 2023 00:08:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783613240682243867 X-GMAIL-MSGID: 1783613240682243867 The 'mux' array stored in 'struct mux_chip' can be changed into a flexible array. This saves: - a pointer in the structure - an indirection when accessing the array - some pointer arithmetic when computing and storing the address in 'mux' It is also now possible to use __counted_by() and struct_size() for additional safety. The address for the 'priv' memory is computed with mux_chip_priv(). It should work as good with a flexible array. Signed-off-by: Christophe JAILLET --- The struct_size() goodies only work if sizeof_priv is 0. Adding an additional size_add() would make it safe in all cases but would make code less readable (IMHO). --- drivers/mux/core.c | 4 +--- include/linux/mux/driver.h | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/mux/core.c b/drivers/mux/core.c index 775816112932..80b2607b083b 100644 --- a/drivers/mux/core.c +++ b/drivers/mux/core.c @@ -98,13 +98,11 @@ struct mux_chip *mux_chip_alloc(struct device *dev, if (WARN_ON(!dev || !controllers)) return ERR_PTR(-EINVAL); - mux_chip = kzalloc(sizeof(*mux_chip) + - controllers * sizeof(*mux_chip->mux) + + mux_chip = kzalloc(struct_size(mux_chip, mux, controllers) + sizeof_priv, GFP_KERNEL); if (!mux_chip) return ERR_PTR(-ENOMEM); - mux_chip->mux = (struct mux_control *)(mux_chip + 1); mux_chip->dev.class = &mux_class; mux_chip->dev.type = &mux_type; mux_chip->dev.parent = dev; diff --git a/include/linux/mux/driver.h b/include/linux/mux/driver.h index 18824064f8c0..c29e9b7fb17b 100644 --- a/include/linux/mux/driver.h +++ b/include/linux/mux/driver.h @@ -56,18 +56,19 @@ struct mux_control { /** * struct mux_chip - Represents a chip holding mux controllers. * @controllers: Number of mux controllers handled by the chip. - * @mux: Array of mux controllers that are handled. * @dev: Device structure. * @id: Used to identify the device internally. * @ops: Mux controller operations. + * @mux: Array of mux controllers that are handled. */ struct mux_chip { unsigned int controllers; - struct mux_control *mux; struct device dev; int id; const struct mux_control_ops *ops; + + struct mux_control mux[] __counted_by(controllers); }; #define to_mux_chip(x) container_of((x), struct mux_chip, dev) From patchwork Sun Nov 26 08:08:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 169840 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp2352695vqx; Sun, 26 Nov 2023 00:08:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4pjUj+k6gpXPob+gZZXs2Y286CO+xpZ/72MtoWGJhh9fnT780sKdJqynWKuU/LfzQlvOo X-Received: by 2002:a17:902:f68e:b0:1bd:c7e2:462 with SMTP id l14-20020a170902f68e00b001bdc7e20462mr10123569plg.11.1700986138274; Sun, 26 Nov 2023 00:08:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700986138; cv=none; d=google.com; s=arc-20160816; b=m6T8oyHdcKRmexhUJUlPsSrJfilzwy2qPrsK2TOoIpnV/NVRJGJxut/IxCMPQlb3Ts uVbB+qo33xbKGA6orcaisJsMoWuPSgP812anNUmUQe1ThDk8snFH3p9aws5Aa8aBnTSD N6i6SFnNa/tFQ5YVPBFcLqG/WWU/q3ifAmwaTGZ9ICXKivObihD5vnOEkIqOtYLhuSBH tUsGI1n753KfNGsyptmN5YWtA+jc6DfMuOpwUAs51se2rtjvkOoCYlQKKkl0N8NEBRy8 MN8XiJpRkk2WyEym6xQthPmNTHGZ4BdcxVfkf6BtN43zPB2YHxCBefvrfLROBOISPUP3 X1uw== 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=lh71l96E6zVf9+1IM040Rgb2giV9A1CJHlxg8pa/sT4=; fh=bcQBqjYLegCslPpv2+sj/djB8RM3sYMYC5FxY/tMopc=; b=P/R8VvnEcPB/K+a04oZ8LmLRKlrnCU6JBSwWm/IyH+iierXtEAz61xTR239dQ0fdp8 bLU4spIzzN2M7NMcNEmYVLRO1AOT+fCdZG/Af0PpY7wU4Sn/cR5kL9S8FB8Qto1Xsi+A OLbi8FQhJmHoRto2RVt20sLFuAhMu9r7i5snqr/pQjUfU23IDF/EPoMctswRPwZuHaec qKDqXJnqcfWNtnZd9R1VWRNHW1qGkTVLaJhyc//8RiSDQS3UFBEskqphv/NWmxW7oWo9 rQlxOAR/2K/ehoVlljV/ihggOtth0cV8+k0EKf3WNA+CuezRORlUOFntO7Z9ET9Re2FS hMHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=pXlPEQDb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q12-20020a170902eb8c00b001c74d844254si7424965plg.101.2023.11.26.00.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 00:08:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=pXlPEQDb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id A05CE8089E5A; Sun, 26 Nov 2023 00:08:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229447AbjKZIIf (ORCPT + 99 others); Sun, 26 Nov 2023 03:08:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229539AbjKZIId (ORCPT ); Sun, 26 Nov 2023 03:08:33 -0500 Received: from smtp.smtpout.orange.fr (smtp-30.smtpout.orange.fr [80.12.242.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE77011B for ; Sun, 26 Nov 2023 00:08:39 -0800 (PST) Received: from pop-os.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id 7ABkrYL9QVgeS7ABtr7Ror; Sun, 26 Nov 2023 09:08:39 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1700986119; bh=lh71l96E6zVf9+1IM040Rgb2giV9A1CJHlxg8pa/sT4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pXlPEQDb8VmLZKUDWPEkg5SPAXTKe8Pw0JQXpfdEqlBz1yixrApVXYGyRNe8ZnpDF UoZuNFKpft6AVYekTzvYwyPX39x7c1y61/y+ep6xQ5LbArwjVD5iz2DWZwAJjoMch5 onCEt6jb+Y0+r1KtbnWYe3My637GRNU2tyrsctscZ+bZamd2Z38JEPEIkrITexykV/ 6cJVhdLRh4sGpMhIfAYu53utWZ7lCChICy7ceGXGdovEDugAzgAyiNzBvDH4mpOJ3Y H6AeOYumd5NIVpddrxPODXrTPtxJSnV2f+sSMOWruquMRuvaemiEW9Qj5tUhj00Dux 3Dz/L1NriyoEg== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 26 Nov 2023 09:08:39 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET To: Peter Rosin Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET Subject: [PATCH 2/2] mux: Slightly reorder 'struct mux_chip' Date: Sun, 26 Nov 2023 09:08:12 +0100 Message-Id: <90be77e27b96d47acc9f20416d8fdc01f9cb601b.1700986053.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 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 groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 26 Nov 2023 00:08:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783613241269148062 X-GMAIL-MSGID: 1783613241269148062 Based on pahole, 2 holes can be combined in the 'struct mux_chip'. This saves 8 bytes in the structure on my x86_64. Signed-off-by: Christophe JAILLET --- include/linux/mux/driver.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/mux/driver.h b/include/linux/mux/driver.h index c29e9b7fb17b..09d519af521b 100644 --- a/include/linux/mux/driver.h +++ b/include/linux/mux/driver.h @@ -56,15 +56,15 @@ struct mux_control { /** * struct mux_chip - Represents a chip holding mux controllers. * @controllers: Number of mux controllers handled by the chip. - * @dev: Device structure. * @id: Used to identify the device internally. + * @dev: Device structure. * @ops: Mux controller operations. * @mux: Array of mux controllers that are handled. */ struct mux_chip { unsigned int controllers; - struct device dev; int id; + struct device dev; const struct mux_control_ops *ops;