Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2446485rdh; Sun, 26 Nov 2023 06:48:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGdIMVNRKYni118p5DcYQXBflqQngnx5n39JNOgYx1YqF8xcPTsJREUxNRE0w+t33ZXGTOB X-Received: by 2002:a17:902:9044:b0:1cf:a3e1:21b1 with SMTP id w4-20020a170902904400b001cfa3e121b1mr6029258plz.66.1701010123012; Sun, 26 Nov 2023 06:48:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701010122; cv=none; d=google.com; s=arc-20160816; b=krpa+0/1/XnIE4Se1Tjb+XrJwUOMqCKaMqT44vQoItIBlkI7l+OFpSw8YHdOBc+vzG Krx+bdItVMC1fqZNmKHCEYMwYUKharJqBokywt2HaJH0VX6Uwf22COKnet5QCfnEQM7b vNGeKRhw8PHK5Bm1MBwgPj1MJrUpwpHHHs2M1XeNVk9xbUBfRI2RxzA5QcDw5VqeQfYz 8hzKgXyMgsAPXF89l97MJzr0Zi/HvOg0l9pqnN8dNn/ETeqLRiR7Bc2gHA15Kq10BA2b e63fQHYDlHRAEw7O589UQr6HnbvTbgwtaM3jk6uAxbChXq4P7sZCqGe9GBSCpdkLx/k4 cxyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=Hr/7m9hoMvRnDst3lNW04H2ADFNUoGwpJyvyjtjjCiw=; fh=K29BYA7yJ+GWvaA+piAjRQtfOVPRQMdQ7kbQovZBuLE=; b=NrlG6AJE0gu3Rky+7R5mFO9kLNh9Dx0nN5/dY20Dcvagbse9n9X7g4c8kpROsB565L Xk40hWDGlNQLmd5HVcNz1O8Vlrb2FdijQMPznqv/W0dkWIUYtGE/uCBbD0QZsYZXAPZD oet19o73QHogzpOlymW47uG/0Eq1UMI1BFbY6k14U9f+f6bV1nH9lfdn2lqvkm3LHKSh TfTZRySWLjubQ2KFtvjZBLba+jxOFFsJKNylyQeHaQ19BO8lA7+4uZfaGtdDtPPg6Z98 FkNehihhSVQHAQnMXDrcWiDAjMwZwAweyAd8BfXkG0A0aeBMmODu4LWQ7yiIkiseZITs T5yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@embeddedor.com header.s=default header.b="k/taYEh+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id iz13-20020a170902ef8d00b001c0c79b3869si7435200plb.578.2023.11.26.06.48.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 06:48:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=fail header.i=@embeddedor.com header.s=default header.b="k/taYEh+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0358E8065322; Sun, 26 Nov 2023 06:48:42 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229874AbjKZOsN (ORCPT + 99 others); Sun, 26 Nov 2023 09:48:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjKZOsM (ORCPT ); Sun, 26 Nov 2023 09:48:12 -0500 Received: from omta40.uswest2.a.cloudfilter.net (omta40.uswest2.a.cloudfilter.net [35.89.44.39]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CBEA92 for ; Sun, 26 Nov 2023 06:48:18 -0800 (PST) Received: from eig-obgw-6003a.ext.cloudfilter.net ([10.0.30.151]) by cmsmtp with ESMTPS id 7G5pr0WavL9Ag7GQkrUdsI; Sun, 26 Nov 2023 14:48:18 +0000 Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with ESMTPS id 7GQirWBHKAKSc7GQjrFf2C; Sun, 26 Nov 2023 14:48:17 +0000 X-Authority-Analysis: v=2.4 cv=N6w6qkxB c=1 sm=1 tr=0 ts=65635ab1 a=1YbLdUo/zbTtOZ3uB5T3HA==:117 a=yGeM7+xMb5a5VK1DGQx1ew==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10 a=BNY50KLci1gA:10 a=wYkD_t78qR0A:10 a=f0Whz4inhACu9W4x6ooA:9 a=QEXdDO2ut3YA:10 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Hr/7m9hoMvRnDst3lNW04H2ADFNUoGwpJyvyjtjjCiw=; b=k/taYEh+v8r6b8Ru5iAQrLIIaz KR9OtTQ6TTh4jK0kiz24EHOKxXiWKkARx/ESXAF3ehFRh3egvucQn3MoNe3TGrzJgTsOCe8N9HLf+ PS9jtbyJMzlXtryibQbpPwZVbMD1pLbt1CZt/H6TFx3NB2hhxI7oUVq/WLenXT8tcPz/lhDA+6sKI dX3S05+LaZ1ZI9qs4vELXGyoH7CiivCwLL2sCKrNwh3yFrybW80xz53X94HBCidiAQG8vkrZ+Qg8Q 1MA8pZNoEkncc7X2o5ePbIBGFckcsGaoe7McJ6MqseZMQrlBZisv0+MFS5Unqmxh0TPsBNDcfuK02 2Qy1d+Ng==; Received: from 187.184.157.122.cable.dyn.cableonline.com.mx ([187.184.157.122]:42256 helo=[192.168.0.9]) by gator4166.hostgator.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96.2) (envelope-from ) id 1r7GQi-002JlB-0q; Sun, 26 Nov 2023 08:48:16 -0600 Message-ID: Date: Sun, 26 Nov 2023 08:48:14 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] mux: Turn 'mux' into a flexible array in 'struct mux_chip' Content-Language: en-US To: Christophe JAILLET , Peter Rosin , Kees Cook , "Gustavo A. R. Silva" Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, linux-hardening@vger.kernel.org References: From: "Gustavo A. R. Silva" In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 187.184.157.122 X-Source-L: No X-Exim-ID: 1r7GQi-002JlB-0q X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 187.184.157.122.cable.dyn.cableonline.com.mx ([192.168.0.9]) [187.184.157.122]:42256 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 3 X-Org: HG=hgshared;ORG=hostgator; X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfGqbRGYa9LcMNxKfle6VGiv9VGRP7aHkDQToIjD9MZhe1AopHO0njz4ENCGueGkNiyVaAwPA75HjkFX8F5wSHpvWf+AfB02C+Dfu/x2517qxPIYJ7qC6 +JJw6TpQobNjgm+QVX+1hwHxcoLbzEwle4ebt92QwQmYFDJ4JufjULrU67QIG1INoN2k8qbck75hMpxy7womzuH9+xJPvFhw7PgCMYJwfG0VdGAxkyFSduTZ X-Spam-Status: No, score=1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 26 Nov 2023 06:48:42 -0800 (PST) On 11/26/23 02:08, Christophe JAILLET wrote: > 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). Just save struct_size() in another variable, and use size_add() like this: size_t size = struct_size(..); .. kzalloc(size_add(size, sizeof_priv), ...); -- Gustavo > --- > 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)