From: Kent Gibson <warthog618@gmail.com>
To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org,
brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [PATCH v2 5/5] gpiolib: cdev: improve documentation of get/set values
Date: Thu, 14 Dec 2023 17:58:14 +0800 [thread overview]
Message-ID: <20231214095814.132400-6-warthog618@gmail.com> (raw)
In-Reply-To: <20231214095814.132400-1-warthog618@gmail.com>
Add documentation of the algorithm used to perform scatter/gather
of the requested lines and values in linereq_get_values() and
linereq_set_values_unlocked() to improve maintainability.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
drivers/gpio/gpiolib-cdev.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index 73262305de0f..27cfed748b0a 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -1391,9 +1391,18 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)
if (copy_from_user(&lv, ip, sizeof(lv)))
return -EFAULT;
+ /*
+ * gpiod_get_array_value_complex() requires compacted desc and val
+ * arrays, rather than the sparse ones in lv.
+ * Calculation of num_get and construction of the desc array is
+ * optimized to avoid allocation for the desc array for the common
+ * num_get == 1 case.
+ */
+ /* scan requested lines to calculate the subset to get */
for (num_get = 0, i = 0; i < lr->num_lines; i++) {
if (lv.mask & BIT_ULL(i)) {
num_get++;
+ /* capture desc for the num_get == 1 case */
descs = &lr->lines[i].desc;
}
}
@@ -1402,6 +1411,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)
return -EINVAL;
if (num_get != 1) {
+ /* build compacted desc array */
descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
if (!descs)
return -ENOMEM;
@@ -1422,6 +1432,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)
lv.bits = 0;
for (didx = 0, i = 0; i < lr->num_lines; i++) {
+ /* unpack compacted vals for the response */
if (lv.mask & BIT_ULL(i)) {
if (lr->lines[i].sw_debounced)
val = debounced_value(&lr->lines[i]);
@@ -1447,14 +1458,25 @@ static long linereq_set_values_unlocked(struct linereq *lr,
unsigned int i, didx, num_set;
int ret;
+ /*
+ * gpiod_set_array_value_complex() requires compacted desc and val
+ * arrays, rather than the sparse ones in lv.
+ * Calculation of num_set and construction of the descs and vals arrays
+ * is optimized to minimize scanning the lv->mask, and to avoid
+ * allocation for the desc array for the common num_set == 1 case.
+ */
bitmap_zero(vals, GPIO_V2_LINES_MAX);
+ /* scan requested lines to determine the subset to be set */
for (num_set = 0, i = 0; i < lr->num_lines; i++) {
if (lv->mask & BIT_ULL(i)) {
+ /* setting inputs is not allowed */
if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
return -EPERM;
+ /* add to compacted values */
if (lv->bits & BIT_ULL(i))
__set_bit(num_set, vals);
num_set++;
+ /* capture desc for the num_set == 1 case */
descs = &lr->lines[i].desc;
}
}
@@ -1462,7 +1484,7 @@ static long linereq_set_values_unlocked(struct linereq *lr,
return -EINVAL;
if (num_set != 1) {
- /* build compacted desc array and values */
+ /* build compacted desc array */
descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
if (!descs)
return -ENOMEM;
--
2.39.2
next prev parent reply other threads:[~2023-12-14 9:59 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-14 9:58 [PATCH v2 0/5] gpiolib: cdev: relocate debounce_period_us Kent Gibson
2023-12-14 9:58 ` [PATCH v2 1/5] gpiolib: cdev: adopt scoped_guard() Kent Gibson
2023-12-14 11:50 ` Kent Gibson
2023-12-14 14:53 ` Andy Shevchenko
2023-12-14 14:53 ` Andy Shevchenko
2023-12-14 9:58 ` [PATCH v2 2/5] gpiolib: cdev: relocate debounce_period_us from struct gpio_desc Kent Gibson
2023-12-14 14:29 ` Bartosz Golaszewski
2023-12-14 14:45 ` Kent Gibson
2023-12-14 14:56 ` Bartosz Golaszewski
2023-12-14 15:08 ` Kent Gibson
2023-12-14 15:03 ` Andy Shevchenko
2023-12-14 15:09 ` Andy Shevchenko
2023-12-14 16:14 ` Kent Gibson
2023-12-14 16:41 ` Andy Shevchenko
2023-12-14 21:06 ` Bartosz Golaszewski
2023-12-15 1:04 ` Kent Gibson
2023-12-15 8:07 ` Bartosz Golaszewski
2023-12-15 8:15 ` Kent Gibson
2023-12-15 16:31 ` Andy Shevchenko
2023-12-14 9:58 ` [PATCH v2 3/5] gpiolib: remove " Kent Gibson
2023-12-14 9:58 ` [PATCH v2 4/5] gpiolib: cdev: reduce locking in gpio_desc_to_lineinfo() Kent Gibson
2023-12-14 15:10 ` Andy Shevchenko
2023-12-14 15:19 ` Kent Gibson
2023-12-14 15:27 ` Andy Shevchenko
2023-12-14 15:34 ` Kent Gibson
2023-12-14 15:46 ` Kent Gibson
2023-12-14 15:52 ` Andy Shevchenko
2023-12-14 15:53 ` Kent Gibson
2023-12-14 16:02 ` Andy Shevchenko
2023-12-14 15:50 ` Andy Shevchenko
2023-12-14 16:05 ` Kent Gibson
2023-12-14 9:58 ` Kent Gibson [this message]
2023-12-14 15:12 ` [PATCH v2 5/5] gpiolib: cdev: improve documentation of get/set values Andy Shevchenko
2023-12-14 15:23 ` Kent Gibson
2023-12-14 15:27 ` Andy Shevchenko
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=20231214095814.132400-6-warthog618@gmail.com \
--to=warthog618@gmail.com \
--cc=andy@kernel.org \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).