Message ID | 20240228204919.3680786-6-andriy.shevchenko@linux.intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-85695-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp100134dyb; Wed, 28 Feb 2024 16:51:09 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXtdv7VBbpx8s/ucdNnB6eUToWoxkW+OQPy8BZSm7pYIkNOSMqedDl+KOUtIjP1y2aZsMuiwD6CVOnEYTzK9acrOt4boA== X-Google-Smtp-Source: AGHT+IH5OzalICUfXLJKMDNKwgaLUFXxBpPsVJVmR8lvMeAZhemMx3TzseZSbR/yd8hhd7n/no59 X-Received: by 2002:a17:90a:8a0a:b0:299:5a55:ef3a with SMTP id w10-20020a17090a8a0a00b002995a55ef3amr884672pjn.4.1709167869575; Wed, 28 Feb 2024 16:51:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709167869; cv=pass; d=google.com; s=arc-20160816; b=Q9FHr6GP9JQi2neYy3T+R5Js7Qt+YpqVGgMf0ZiNo2xmkV4O94I3jvTNSQUe7/WIik BE0dbcEr2d/pw3W3gC+didMikHRwrI97VxM0C24ojLoGIuCIp3g3ht2VauTb2XRAocfu FXn0aDAvSKfUeTtjWPhxfQ0As6+BOK+5cw/sRjnSN2hNXHORbCo/4rBOjmSiCCBeEZSJ E/kT9DgDVQbvhwqVAGL6KVcfbLYaciuZtLERQNsJvb1dhXW/s+SaE1OEgZ5CynCKus9r i0ThM6UyJi0nHIHwFQQS11C10Z8wZOjxfHZ/Ll+MAR2DWkodll2NI94Vr517Yy8az64T tTRw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=9VI+tzy9q3a5cGA3aUoO+hv/XHY7hHOuGFLHfSDNihs=; fh=bBcueXoyYQ/4owtJ51rB6UqpyJY1f++Rn9/MV1CTR2g=; b=BFptpXU3oIDgOHkxJ5BPDd6paHP/7CiqTRrm97DsCPkVRys+UangU0NYt+3dnu1+0Y +WA6BgTNOo0Mv+3yyhNmqwN4EG88ti8HB6sWiV4U+zF0shJlsDjGHIN+N2WTIuDf7A3m JERxoCOSWqTKhygRR0J6S96K9pPXjoZtFkbdeCuDsqwmZNFmA5lgA109/in7F+0gS3EE PPFlbbuHH+L4lUOFEZdUj5b3MMlUiVWclyKxO0sPffXWR5H9PnU5TYo83tT64OqIA0yS +DDAcIBEuKeIoeR50ndbt3NucNrVxt28jpwUS6zMgWzkNORboiwbqQ1KDjAzJb+NzDlk e3Ng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lzvgQX7d; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-85695-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85695-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ot18-20020a17090b3b5200b0029b0f0e85acsi118021pjb.9.2024.02.28.16.51.09 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 16:51:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85695-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lzvgQX7d; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-85695-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85695-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B5A8A28D670 for <ouuuleilei@gmail.com>; Wed, 28 Feb 2024 20:52:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 636AF86262; Wed, 28 Feb 2024 20:49:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lzvgQX7d" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED8A17290F; Wed, 28 Feb 2024 20:49:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709153389; cv=none; b=rhP3ApDz/Ut0GxEWgtkJTe3fF/aZ992W+SYgqpgLgDDDg2AAs0dV27FiLS7DlKpFaUjnpaf6PSqDmyM2YlPEVhuEJWAZuXUNdmhfM2Q+pweWhy+qb3IW9OB+lE4sO2tebiU18hy1YQQtFXKWmDun4agWA48TmbOJ7D6D0Tx3MAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709153389; c=relaxed/simple; bh=R9j+Q0D/kA76h1U7UfPAGebwhkhuz8MuAm681KRIxuM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pwd96xwLoNVNWozv/agsIctPrz9W6rsbSGv9lgeu2evaJvuBJCNdhVjx9K+7KZvgtpz67g+JOujc97Tg88wfAVIN0lem4kaBj9sXYkLKFvurFzQRAxRSMsqG+yfEmX6vV0G/7hZFJsE1OuuzHixBfbe7VkJgWwvLcNi8XbXRegg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lzvgQX7d; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709153388; x=1740689388; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R9j+Q0D/kA76h1U7UfPAGebwhkhuz8MuAm681KRIxuM=; b=lzvgQX7dZu4f6YrJlOMYTKNoU18zl/LQvhBde5z6BP4SO+LBslH0Fvbf pL4rbYBwVqNPOZ2JlK3pIQIPJxStuQU8OxlKiZtJMooZUYnUQjHmnKS5R E5eMdUm8JMnfhDPGwZUpnJ6KX3UVosgLsfnJsvX20pCNneksEQiVIvrAi uhGqLQ+7q6H4zVBdJMDevYpIDSqYhIWIL6yj3QUWYFNSdRF5zR+oNyI9J 1f58ogcsphCl5RI/q1XoMm7KKUWLPTM1SyEG1/wg2o6+3ndrfm+tbcTDh Zbq5Ykk5mqZZSd8S+O5TSysmz/aFpsyWhBxMLubj59+ptIGc4EPeb1Qkp A==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="3428774" X-IronPort-AV: E=Sophos;i="6.06,191,1705392000"; d="scan'208";a="3428774" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 12:49:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="937034662" X-IronPort-AV: E=Sophos;i="6.06,191,1705392000"; d="scan'208";a="937034662" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 28 Feb 2024 12:49:41 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id 47C0A537; Wed, 28 Feb 2024 22:49:34 +0200 (EET) From: Andy Shevchenko <andriy.shevchenko@linux.intel.com> To: Vinod Koul <vkoul@kernel.org>, Linus Walleij <linus.walleij@linaro.org>, Jonathan Cameron <Jonathan.Cameron@huawei.com>, Andy Shevchenko <andriy.shevchenko@linux.intel.com>, Mark Brown <broonie@kernel.org>, Kees Cook <keescook@chromium.org>, linux-arm-kernel@lists.infradead.org, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-spi@vger.kernel.org, netdev@vger.kernel.org, linux-hardening@vger.kernel.org Cc: Jonathan Cameron <jic23@kernel.org>, Lars-Peter Clausen <lars@metafoo.de>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, "Gustavo A. R. Silva" <gustavoars@kernel.org>, Nuno Sa <nuno.sa@analog.com> Subject: [PATCH v4 5/8] iio: core: Use new helpers from overflow.h in iio_device_alloc() Date: Wed, 28 Feb 2024 22:41:35 +0200 Message-ID: <20240228204919.3680786-6-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1.gbec44491f096 In-Reply-To: <20240228204919.3680786-1-andriy.shevchenko@linux.intel.com> References: <20240228204919.3680786-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792192408391210382 X-GMAIL-MSGID: 1792192408391210382 |
Series |
iio: core: New macros and making use of them
|
|
Commit Message
Andy Shevchenko
Feb. 28, 2024, 8:41 p.m. UTC
We have two new helpers struct_size_with_data() and struct_data_pointer() that we can utilize in iio_device_alloc(). Do it so. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Nuno Sa <nuno.sa@analog.com> --- drivers/iio/industrialio-core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
Comments
On Wed, 2024-02-28 at 22:41 +0200, Andy Shevchenko wrote: > We have two new helpers struct_size_with_data() and struct_data_pointer() > that we can utilize in iio_device_alloc(). Do it so. > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > Reviewed-by: Nuno Sa <nuno.sa@analog.com> > --- > drivers/iio/industrialio-core.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c > index 1986b3386307..223013725e32 100644 > --- a/drivers/iio/industrialio-core.c > +++ b/drivers/iio/industrialio-core.c > @@ -1644,7 +1644,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, > int sizeof_priv) > size_t alloc_size; > > if (sizeof_priv) > - alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN) + > sizeof_priv; > + alloc_size = struct_size_with_data(iio_dev_opaque, > IIO_DMA_MINALIGN, sizeof_priv); > else > alloc_size = sizeof(struct iio_dev_opaque); > > @@ -1655,8 +1655,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, > int sizeof_priv) > indio_dev = &iio_dev_opaque->indio_dev; > > if (sizeof_priv) > - indio_dev->priv = (char *)iio_dev_opaque + > - ALIGN(sizeof(struct iio_dev_opaque), > IIO_DMA_MINALIGN); > + indio_dev->priv = struct_data_pointer(iio_dev_opaque, > IIO_DMA_MINALIGN); I'd +1 for implementing what Kees suggested in IIO. Only thing is (I think), we need to move struct iio_dev indioo_dev to the end of struct iio_dev_opaque. - Nuno Sá
On Thu, 29 Feb 2024 16:29:43 +0100 Nuno Sá <noname.nuno@gmail.com> wrote: > On Wed, 2024-02-28 at 22:41 +0200, Andy Shevchenko wrote: > > We have two new helpers struct_size_with_data() and struct_data_pointer() > > that we can utilize in iio_device_alloc(). Do it so. > > > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > > Reviewed-by: Nuno Sa <nuno.sa@analog.com> > > --- > > drivers/iio/industrialio-core.c | 5 ++--- > > 1 file changed, 2 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c > > index 1986b3386307..223013725e32 100644 > > --- a/drivers/iio/industrialio-core.c > > +++ b/drivers/iio/industrialio-core.c > > @@ -1644,7 +1644,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, > > int sizeof_priv) > > size_t alloc_size; > > > > if (sizeof_priv) > > - alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN) + > > sizeof_priv; > > + alloc_size = struct_size_with_data(iio_dev_opaque, > > IIO_DMA_MINALIGN, sizeof_priv); > > else > > alloc_size = sizeof(struct iio_dev_opaque); > > > > @@ -1655,8 +1655,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, > > int sizeof_priv) > > indio_dev = &iio_dev_opaque->indio_dev; > > > > if (sizeof_priv) > > - indio_dev->priv = (char *)iio_dev_opaque + > > - ALIGN(sizeof(struct iio_dev_opaque), > > IIO_DMA_MINALIGN); > > + indio_dev->priv = struct_data_pointer(iio_dev_opaque, > > IIO_DMA_MINALIGN); > > I'd +1 for implementing what Kees suggested in IIO. Only thing is (I think), we > need to move struct iio_dev indioo_dev to the end of struct iio_dev_opaque. That is going to be messy and without horrible hacks (I think) add more padding we don't need. At the moment the struct iio_dev and the struct iio_dev_opaque are aligned as at the start of the structure. The priv data is aligned by padding the larger struct iio_dev_opaque, so if you want the priv handle to be to data defined in struct iio_dev you would need to add additional padding so that struct iio_dev_opaque { stuff... // this next __aligned() is implicit anyway because of the rules for // a structure always being aligned to the alignment of it's max aligned // element. struct iio_dev __aligned (IIO_DMA_ALIGN) { stuff u8 priv[] __aligned(IIO_DMA_ALIGN); } } How about using what Kees suggests on the iio_dev_opaque (which think is cleaner anyway as that's what we are allocating) and keeping the magic pointer to priv in the struct iio_dev; The compiler looses some visibility for iio_priv() accesses but can it do much with those anyway? They always get cast to a struct driver_specific * and getting the original allocation wrong is not easy to do as we pass that struct size in. Note, for others not aware of what is going on here, the priv pointer in iio_dev is to allow efficient static inline iio_priv() calls without needing to either make a function call, or expose the internals of the opaque structure in which the iio_dev and the priv data are embedded. Standard pattern is: struct driver_specific *bob; struct iio_dev *indio_dev = dev_iio_device_alloc(dev, sizeof(*bob)); // which allocates the iio_dev_opaque, but returns the contained iio_dev bob = iio_priv(indio_dev); So struct iio_dev_opaque { struct iio_dev indio_dev { stuff.. void *priv; }; stuff.. int priv_count; u8 priv[] __aligned(IIO_DMA_ALIGN) __counted_by(priv_count); } with indio_dev->priv = iio_dev_opaque->dev? This cleanups up a few IIO core bits but no impact outside them. Nice to have those cleanups. Is there any way to have that internal iio_dev->priv pointer associated with a __counted_by even though it's pointing elsewhere than a local variable sized trailing element? struct iio_dev { stuff u32 count; void *priv __counted_by(count); } compiles with gcc but without digging further I have no idea if it does anything useful! Jonathan > > - Nuno Sá > > >
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 1986b3386307..223013725e32 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -1644,7 +1644,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) size_t alloc_size; if (sizeof_priv) - alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN) + sizeof_priv; + alloc_size = struct_size_with_data(iio_dev_opaque, IIO_DMA_MINALIGN, sizeof_priv); else alloc_size = sizeof(struct iio_dev_opaque); @@ -1655,8 +1655,7 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) indio_dev = &iio_dev_opaque->indio_dev; if (sizeof_priv) - indio_dev->priv = (char *)iio_dev_opaque + - ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN); + indio_dev->priv = struct_data_pointer(iio_dev_opaque, IIO_DMA_MINALIGN); else indio_dev->priv = NULL;