public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Matteo Martelli <matteomartelli3@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>,
	 Lars-Peter Clausen <lars@metafoo.de>,
	Peter Rosin <peda@axentia.se>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Matteo Martelli <matteomartelli3@gmail.com>
Subject: [PATCH 0/2] iio: consumers: ensure read buffers for labels and ext_info are page aligned
Date: Mon, 02 Dec 2024 16:11:06 +0100	[thread overview]
Message-ID: <20241202-iio-kmalloc-align-v1-0-aa9568c03937@gmail.com> (raw)

This patch series is a follow up of [1], where I described an issue
related to the fact that devm_k*alloc() functions do not provide
alignment to the requested power of two size, leading to potential
errors when used together with sysfs_emit* helpers which expect
page-aligned buffers.

From that discussion, it became clear that this issue currently only
affects iio consumer drivers, as they can directly access providers
attribute formatted using sysfs_emit* helpers. For instance, the iio-mux
driver allocates a buffer with devm_kzalloc(PAGE_SIZE) to read provider
ext_info attributes, which could be handled via sysfs_emit* helpers.
This leads to an error in the provider ext_info read callback since the
allocated buffer is not page-aligned.

Summary:
- Patch 1: harden the consumers APIs to ensure read buffers are page
  aligned for attributes which could be formatted with sysfs_emit*
  helpers by the providers. Currently labels and ext_info attributes.

- Patch 2: fix iio-mux consumer by switching from devm_kzalloc to
  kzalloc for the ext_info buffer.

Tested with the iio-mux consumer driver alongside the pac1921 driver,
which provides an ext_info attribute (the shunt resistor in this case).
After applying patch-1, the error was detected during the iio-mux probe
rather than in the pac1921 ext_info read callback. After applying
patch-2, the error condition no longer occurred. Additionally, the extra
check in iio_read_channel_label() was tested with the iio_hwmon consumer
driver temporarily modified to allocate the buffer for retrieving
provider labels using devm_kzalloc(PAGE_SIZE) instead of
devm_get_free_pages(). The error was correctly detected during the
iio_hwmon probe when attempting to retrieve pac1921 channel labels.

[1]: https://lore.kernel.org/all/c486a1cf98a8b9ad093270543e8d2007@gmail.com

Signed-off-by: Matteo Martelli <matteomartelli3@gmail.com>
---
Matteo Martelli (2):
      iio: consumers: ensure read buffers for labels and ext_info are page aligned
      iio: iio-mux: kzalloc instead of devm_kzalloc to ensure page alignment

 drivers/iio/inkern.c              | 11 +++++
 drivers/iio/multiplexer/iio-mux.c | 84 +++++++++++++++++++++------------------
 include/linux/iio/consumer.h      |  4 +-
 3 files changed, 59 insertions(+), 40 deletions(-)
---
base-commit: 20fd1383cd616d61b2a79967da1221dc6cfb8430
change-id: 20241127-iio-kmalloc-align-ac7bfcfe5ec0

Best regards,
-- 
Matteo Martelli <matteomartelli3@gmail.com>


             reply	other threads:[~2024-12-02 15:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-02 15:11 Matteo Martelli [this message]
2024-12-02 15:11 ` [PATCH 1/2] iio: consumers: ensure read buffers for labels and ext_info are page aligned Matteo Martelli
2024-12-08 18:16   ` Jonathan Cameron
2024-12-02 15:11 ` [PATCH 2/2] iio: iio-mux: kzalloc instead of devm_kzalloc to ensure page alignment Matteo Martelli
2024-12-08 18:15   ` Jonathan Cameron
2024-12-09 10:39     ` Matteo Martelli
2024-12-11 18:17       ` Jonathan Cameron
2025-01-04 14:49         ` Jonathan Cameron
2025-01-12 16:24       ` Andy Shevchenko
2025-01-12 16:35         ` Jonathan Cameron
2024-12-17 16:14   ` David Lechner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241202-iio-kmalloc-align-v1-0-aa9568c03937@gmail.com \
    --to=matteomartelli3@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peda@axentia.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox