From patchwork Tue Dec 12 05:42:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 177100 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7528831vqy; Mon, 11 Dec 2023 21:43:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IEph2PxJdsiEyR0QKdyztLpZgZ8kbKyfaMmoCEo2AfbiwK468kE71A0xkMCHHE+uyOaHqJZ X-Received: by 2002:a17:902:e80c:b0:1d0:a791:7598 with SMTP id u12-20020a170902e80c00b001d0a7917598mr6260949plg.129.1702359817615; Mon, 11 Dec 2023 21:43:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702359817; cv=none; d=google.com; s=arc-20160816; b=cZpEJLAKKqRnHFYdXsK68OXP2rIqqY9fjec3KygIPLn3NumYJtVbCVcap8PLujExgu xbq66mj8ULNZ42c8vp32E0hgKgG2KS31Qu7N96okEzJJmSisVRFV4Ow92xybTLJfHKdD JGoyMEo2qJGpyPZ48wQxYe49Px1BNRPjo1WbJL6SMPSVdcf5eFJQZTLhHxsOrygkPpXv ptwEG41O433c1FOPb28CYSdpTTqKNNje7g3qUMNBIyFJe5hHcs8pTP5QjYeJPrBzB3/q nMoL5jLVstv7RZe+Quj46fbeQmz6Zs5S9oKoIAmBDVflKDqfM9AdND5T7+t1bxfkZAOp tMSg== 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=ZTiRVhNXg8I3Ixq+A5Iayl3CE87f/+Zaz2YhLRYre0A=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=fcsjlcJ4NBijVB5DaxqjOEjB57PayHHSab7KPdKcr+tGF2XsJcsLN/jg2IsLEZT6V3 oy3BkWx4BSKqJ45J9vUhUxMF+VLnb1jGqDGL2gJrGSpPU5m3TyflimvWv4d+Xnm0G3vi PtkJhCsL8kgi9XQ5XMB4fdWi5nUqPneBwyYDgC+4Sjln09RFEHbFXuYZUWHzghy2+q/a Xbp4zIaSdA+rqcfMssyHLBpHzXbwdIX3MNEVAlRXMSztK6Ds0rM+aHfQ/dusoWORYaYg 5r0Fyn11OJYhZxSALDsYZkYStETABKyBOPXSEkj8xdxZQJapRBPdHL6J9vEJwspCp+fC jE5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=St9G1UpO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id m3-20020a170902db0300b001d05efd818fsi7531647plx.58.2023.12.11.21.43.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=St9G1UpO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 897138022093; Mon, 11 Dec 2023 21:43:34 -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 S229841AbjLLFnZ (ORCPT + 99 others); Tue, 12 Dec 2023 00:43:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbjLLFnY (ORCPT ); Tue, 12 Dec 2023 00:43:24 -0500 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E774A1; Mon, 11 Dec 2023 21:43:30 -0800 (PST) Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-5c659db0ce2so4408335a12.0; Mon, 11 Dec 2023 21:43:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702359809; x=1702964609; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZTiRVhNXg8I3Ixq+A5Iayl3CE87f/+Zaz2YhLRYre0A=; b=St9G1UpO/pGbq2JYOqD5ih/hzMf8L2rFPsnqQyLZXtrfIMa/SoprtXloUHObV0uswJ 7WBWA9pPtozlEGiJxlMYYU1mfNX+IsmD8wA/tCJENObfHpdh3w9uCTJZUvg1M9iakAeq E7U/KJc930tFUsniok2f2Ij9EnXf47rTQqE204kbeevvkTfHdmjjtatF3Jah9zDMt+K6 1hMsXP7eM0zhpm3bCe7lqGWDZXa72D2ymdAMbR7GRz6Ax9ClM/NDzpRKVrErKWietIk5 PyETirld6VLfs6ya0c1Jw7gQPJlHPBfKxxPD8wv/g0HOB9wDenzqm+BlYJxXrDMNKs9o 6A+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702359809; x=1702964609; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZTiRVhNXg8I3Ixq+A5Iayl3CE87f/+Zaz2YhLRYre0A=; b=MY/M9weyw6H6+EIvGKXPisynNdJbdjX3ZzELN97JxcLxkpvjSEVopdnJxQ3gl5x+h1 FiIxeMxL12xb5fw7O03/XWjik90JRmArGwG4erjqIPGWimPj27bn/k6aVLrn6tOmuunc FUEU253+WEA+GpQrCMWy8moWD09S9mf68jey/nD3RhpFAYGMht2kj+OwGqf4g4fZeT9l 8wGP0k9FILioDJS8y3gqei6APFmpWsWUg4WW639/igRhfr0OClsSdM9JlBuxhDFRBTzk bxolLjkvXTRjNjt5dJFdT6sRRGDFsP204f/MCDGwySMZRGIAEfQatGh9OzZURiFoaoFc Pdrw== X-Gm-Message-State: AOJu0YzjBYjv8zzXw/5ZJRZK3tmV5k3fL4ANvA7NvCBlrTdm0V5US/5e rE0F7hodS68yDMB07Uhl5/GW0PIjMLo= X-Received: by 2002:a17:903:40c8:b0:1b8:90bd:d157 with SMTP id t8-20020a17090340c800b001b890bdd157mr7246429pld.26.1702359809356; Mon, 11 Dec 2023 21:43:29 -0800 (PST) Received: from rigel.home.arpa (194-223-186-106.tpgi.com.au. [194.223.186.106]) by smtp.gmail.com with ESMTPSA id u13-20020a170902e5cd00b001d0be32b0basm7591836plf.217.2023.12.11.21.43.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:29 -0800 (PST) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org Cc: Kent Gibson Subject: [PATCH 1/4] gpiolib: cdev: relocate debounce_period_us from struct gpio_desc Date: Tue, 12 Dec 2023 13:42:50 +0800 Message-Id: <20231212054253.50094-2-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231212054253.50094-1-warthog618@gmail.com> References: <20231212054253.50094-1-warthog618@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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]); Mon, 11 Dec 2023 21:43:34 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785053648144891545 X-GMAIL-MSGID: 1785053648144891545 Store the debounce period for a requested line locally, rather than in the debounce_period_us field in the gpiolib struct gpio_desc. Add a global tree of lines containing supplemental line information to make the debounce period available to be reported by the GPIO_V2_GET_LINEINFO_IOCTL and the line change notifier. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 167 +++++++++++++++++++++++++++++++----- 1 file changed, 146 insertions(+), 21 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 02ffda6c1e51..7999c1a72cfa 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -462,6 +463,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) /** * struct line - contains the state of a requested line * @desc: the GPIO descriptor for this line. + * @node: to store the object in supinfo if supplemental * @req: the corresponding line request * @irq: the interrupt triggered in response to events on this GPIO * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or @@ -473,6 +475,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) * @line_seqno: the seqno for the current edge event in the sequence of * events for this line. * @work: the worker that implements software debouncing + * @debounce_period_us: the debounce period in microseconds * @sw_debounced: flag indicating if the software debouncer is active * @level: the current debounced physical level of the line * @hdesc: the Hardware Timestamp Engine (HTE) descriptor @@ -482,6 +485,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) */ struct line { struct gpio_desc *desc; + struct rb_node node; /* * -- edge detector specific fields -- */ @@ -514,6 +518,15 @@ struct line { * -- debouncer specific fields -- */ struct delayed_work work; + /* + * debounce_period_us is accessed by debounce_irq_handler() and + * process_hw_ts() which are disabled when modified by + * debounce_setup(), edge_detector_setup() or edge_detector_stop() + * or can live with a stale version when updated by + * edge_detector_update(). + * The modifying functions are themselves mutually exclusive. + */ + unsigned int debounce_period_us; /* * sw_debounce is accessed by linereq_set_config(), which is the * only setter, and linereq_get_values(), which can live with a @@ -546,6 +559,22 @@ struct line { #endif /* CONFIG_HTE */ }; +/** + * struct supinfo - supplementary line info + * Used to populate gpio_v2_line_info with cdev specific fields not contained + * in the struct gpio_desc. + * A line is determined to contain supplemental information by + * line_is_supplemental(). + * @lock: lock covering @tree + * @tree: a rbtree of the struct lines containing the supplemental info + */ +struct supinfo { + spinlock_t lock; + struct rb_root tree; +}; + +static struct supinfo supinfo; + /** * struct linereq - contains the state of a userspace line request * @gdev: the GPIO device the line request pertains to @@ -575,6 +604,100 @@ struct linereq { struct line lines[] __counted_by(num_lines); }; +static void supinfo_init(void) +{ + supinfo.tree = RB_ROOT; + spin_lock_init(&supinfo.lock); +} + +static void supinfo_insert(struct line *line) +{ + struct rb_node **new = &(supinfo.tree.rb_node), *parent = NULL; + struct line *entry; + + spin_lock(&supinfo.lock); + while (*new) { + entry = container_of(*new, struct line, node); + + parent = *new; + if (line->desc < entry->desc) { + new = &((*new)->rb_left); + } else if (line->desc > entry->desc) { + new = &((*new)->rb_right); + } else { + pr_warn("%s: duplicate line inserted\n", __func__); + goto out_unlock; + } + } + + rb_link_node(&line->node, parent, new); + rb_insert_color(&line->node, &supinfo.tree); +out_unlock: + spin_unlock(&supinfo.lock); +} + +static void supinfo_erase(struct line *line) +{ + spin_lock(&supinfo.lock); + rb_erase(&line->node, &supinfo.tree); + spin_unlock(&supinfo.lock); +} + +static struct line *supinfo_find(struct gpio_desc *desc) +{ + struct rb_node *node = supinfo.tree.rb_node; + struct line *line; + + while (node) { + line = container_of(node, struct line, node); + if (desc < line->desc) + node = node->rb_left; + else if (desc > line->desc) + node = node->rb_right; + else + return line; + } + return NULL; +} + +static void supinfo_to_lineinfo(struct gpio_desc *desc, + struct gpio_v2_line_info *info) +{ + struct gpio_v2_line_attribute *attr; + struct line *line; + + spin_lock(&supinfo.lock); + line = supinfo_find(desc); + if (line) { + attr = &info->attrs[info->num_attrs]; + attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; + attr->debounce_period_us = READ_ONCE(line->debounce_period_us); + info->num_attrs++; + } + spin_unlock(&supinfo.lock); +} + +static inline bool line_is_supplemental(struct line *line) +{ + return READ_ONCE(line->debounce_period_us) != 0; +} + +static void line_set_debounce_period(struct line *line, + unsigned int debounce_period_us) +{ + bool was_suppl = line_is_supplemental(line); + + WRITE_ONCE(line->debounce_period_us, debounce_period_us); + + if (line_is_supplemental(line) == was_suppl) + return; + + if (was_suppl) + supinfo_erase(line); + else + supinfo_insert(line); +} + #define GPIO_V2_LINE_BIAS_FLAGS \ (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ @@ -723,7 +846,7 @@ static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) line->total_discard_seq++; line->last_seqno = ts->seq; mod_delayed_work(system_wq, &line->work, - usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); + usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); } else { if (unlikely(ts->seq < line->line_seqno)) return HTE_CB_HANDLED; @@ -864,7 +987,7 @@ static irqreturn_t debounce_irq_handler(int irq, void *p) struct line *line = p; mod_delayed_work(system_wq, &line->work, - usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); + usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); return IRQ_HANDLED; } @@ -946,7 +1069,7 @@ static int debounce_setup(struct line *line, unsigned int debounce_period_us) /* try hardware */ ret = gpiod_set_debounce(line->desc, debounce_period_us); if (!ret) { - WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); + line_set_debounce_period(line, debounce_period_us); return ret; } if (ret != -ENOTSUPP) @@ -1025,8 +1148,7 @@ static void edge_detector_stop(struct line *line) cancel_delayed_work_sync(&line->work); WRITE_ONCE(line->sw_debounced, 0); WRITE_ONCE(line->edflags, 0); - if (line->desc) - WRITE_ONCE(line->desc->debounce_period_us, 0); + line_set_debounce_period(line, 0); /* do not change line->level - see comment in debounced_value() */ } @@ -1051,7 +1173,7 @@ static int edge_detector_setup(struct line *line, ret = debounce_setup(line, debounce_period_us); if (ret) return ret; - WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); + line_set_debounce_period(line, debounce_period_us); } /* detection disabled or sw debouncer will provide edge detection */ @@ -1093,12 +1215,12 @@ static int edge_detector_update(struct line *line, gpio_v2_line_config_debounce_period(lc, line_idx); if ((active_edflags == edflags) && - (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) + (READ_ONCE(line->debounce_period_us) == debounce_period_us)) return 0; /* sw debounced and still will be...*/ if (debounce_period_us && READ_ONCE(line->sw_debounced)) { - WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); + line_set_debounce_period(line, debounce_period_us); return 0; } @@ -1573,6 +1695,7 @@ static ssize_t linereq_read(struct file *file, char __user *buf, static void linereq_free(struct linereq *lr) { + struct line *line; unsigned int i; if (lr->device_unregistered_nb.notifier_call) @@ -1580,9 +1703,12 @@ static void linereq_free(struct linereq *lr) &lr->device_unregistered_nb); for (i = 0; i < lr->num_lines; i++) { - if (lr->lines[i].desc) { - edge_detector_stop(&lr->lines[i]); - gpiod_free(lr->lines[i].desc); + line = &lr->lines[i]; + if (line->desc) { + edge_detector_stop(line); + if (line_is_supplemental(line)) + supinfo_erase(line); + gpiod_free(line->desc); } } kfifo_free(&lr->events); @@ -2274,8 +2400,6 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_chip *gc = desc->gdev->chip; bool ok_for_pinctrl; unsigned long flags; - u32 debounce_period_us; - unsigned int num_attrs = 0; memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); @@ -2341,14 +2465,6 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; - debounce_period_us = READ_ONCE(desc->debounce_period_us); - if (debounce_period_us) { - info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; - info->attrs[num_attrs].debounce_period_us = debounce_period_us; - num_attrs++; - } - info->num_attrs = num_attrs; - spin_unlock_irqrestore(&gpio_lock, flags); } @@ -2455,6 +2571,7 @@ static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, return -EBUSY; } gpio_desc_to_lineinfo(desc, &lineinfo); + supinfo_to_lineinfo(desc, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { if (watch) @@ -2545,6 +2662,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, chg.event_type = action; chg.timestamp_ns = ktime_get_ns(); gpio_desc_to_lineinfo(desc, &chg.info); + supinfo_to_lineinfo(desc, &chg.info); ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); if (ret) @@ -2812,3 +2930,10 @@ void gpiolib_cdev_unregister(struct gpio_device *gdev) cdev_device_del(&gdev->chrdev, &gdev->dev); blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); } + +static int __init gpiolib_cdev_init(void) +{ + supinfo_init(); + return 0; +} +postcore_initcall(gpiolib_cdev_init); From patchwork Tue Dec 12 05:42:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 177101 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7528876vqy; Mon, 11 Dec 2023 21:43:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IG245YMaarFMc6gqkLI6HmPMyShepHjp8gt2Z/lgtpu91kOBYGPk3gVylS0ljiThE6WU7eX X-Received: by 2002:a05:6a20:54a5:b0:18f:f151:916c with SMTP id i37-20020a056a2054a500b0018ff151916cmr6647044pzk.106.1702359829432; Mon, 11 Dec 2023 21:43:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702359829; cv=none; d=google.com; s=arc-20160816; b=mERSSBoCzxeFVZHGACXbsc2167bRw0MNIma7TkadsLoYp0v2DErBJEvlYQIJORmtAV 4mI132LB4JJIFtqsFlz5aBbgKr1UUmdFu+aw7mN32v8+bk4M5W6GSRta7e/3o+3JrUSI DRLm+CMYheF3QuvZbVSuUykknWBm+KL3t792NH771jZLKcJZ15ppLlsDq2g/11uHgUyb ZYHJbylsJJnpWcwUASgHf0VsNqFXYTgvLjWhgauK8kTffar5sZnoz53zGJXkgTwOpFBE 6NEHwCe0sDPWjTSaGJYGfY5kECRfYZ6W6Tal+eAp4n9RwRZPCyB4g8Yb8xd5r/iemQ2S WOIA== 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=QOksdqNs08pbi9TocVeWukoOqtj/lhUt0q7v2+r1tcg=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=doBX8GpYQATzUqZGVoGEt+IHKRdmSHcIYaMvxJ9aSwif/vKlWcg+eSp0dzoFLcT6el wG9DFcJkxvk/MPgjjX/8u0R/R46bMvlrAqpiYXHCVSIdGySh7FOkiPtYh4U8KHgmoMGZ zHL7wUOKTG2iWvkFaNf9fjA/RCoUOQLKDJ+J5gi1L6CJanoFdVozYDPJ7cvLTMyjHNjn KmDw/aEhqnQqMKzC1JxNvoksX8DAqyQtG005+sA9UBEHeauBscSXOU0lnmM2mAbT1Vbk 6NweeGFh07d/jzP/L0Z4VmFGc6G0e0Gg98GCtcyqVFrXAqGyJIw0qGSBvK/0aLbn03rW EvUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nMmNaRT3; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id mp5-20020a17090b190500b00286922ce5cdsi7350967pjb.108.2023.12.11.21.43.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:49 -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=@gmail.com header.s=20230601 header.b=nMmNaRT3; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 94E70802209B; Mon, 11 Dec 2023 21:43:46 -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 S229977AbjLLFnd (ORCPT + 99 others); Tue, 12 Dec 2023 00:43:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229843AbjLLFnb (ORCPT ); Tue, 12 Dec 2023 00:43:31 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 950E9D5; Mon, 11 Dec 2023 21:43:36 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d053c45897so46308535ad.2; Mon, 11 Dec 2023 21:43:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702359816; x=1702964616; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QOksdqNs08pbi9TocVeWukoOqtj/lhUt0q7v2+r1tcg=; b=nMmNaRT3rJ3FcPOSOicblhf2zId+qmtIivHsyrXo2VMPMOvul+y3yFiWxPPY6N6FU7 vZGVdzUWasvWx/sb/R0pC91TBh6zCKRiYTxA9sevEMxXjwjDQQT4b3tnuBdEQFSNUeKi 9FMVLGRH4sSg3FNKWWHK7SxMrgtm7KRwJXhk6WE4zMvMND2AVPhpXD3Qots1/AKwGk3G RQ7LATgK4nYbGnVWKH84lxm4i6dCuVCOn1l+s6UA/tL56dJCrPAn0HK2awxv7nWEE0Zr RYEdDGjKoHHjB8dkTqb9gvFRVvU18qaIDAN9kGC8l9u/1nBQ7v1deF0OvceES/iXKwyD OqLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702359816; x=1702964616; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QOksdqNs08pbi9TocVeWukoOqtj/lhUt0q7v2+r1tcg=; b=O0qhOv4yR2fQPr+5xA5waSj+CLb8hsGUNhPk0LFwuPSgaR8Tpj9KD7valfd/GEPr1y AKOxQ32wu8Xx4HdD6m7L5vpIIAy5ylD+eNB4paaY/MYwk06/+6XrxgmZsN2rbcNAo5jl KVgLaS8P9psBOr5TAkyds+ZHM0JlRfNKDGGHUYwsPdDZjanbxePVAH03WPKjXcMV2rHf Fq4NvEd6SnQWzXyVEJ53KTWQgyC9ZfWgIJ7wv42MJ1MAdDXx3Oil44CG8WCN8gR8r5D1 2UKCUVjfqSE9KnBmuydJWH/DpdSE8B4quqpDujk2C90keSuw+6/V/hEHnM/p85EgULk5 FXPw== X-Gm-Message-State: AOJu0Yxn4nekhIafS8vOGTafmG0y43Qm7I9Aykm1xW8qdl3NaCsdNZ/9 TMmExUr/LEvni+T2OTHEQWBWF17HjaU= X-Received: by 2002:a17:902:7ed0:b0:1d1:cc09:50ac with SMTP id p16-20020a1709027ed000b001d1cc0950acmr4107955plb.108.1702359815718; Mon, 11 Dec 2023 21:43:35 -0800 (PST) Received: from rigel.home.arpa (194-223-186-106.tpgi.com.au. [194.223.186.106]) by smtp.gmail.com with ESMTPSA id u13-20020a170902e5cd00b001d0be32b0basm7591836plf.217.2023.12.11.21.43.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:35 -0800 (PST) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org Cc: Kent Gibson Subject: [PATCH 2/4] gpiolib: remove debounce_period_us from struct gpio_desc Date: Tue, 12 Dec 2023 13:42:51 +0800 Message-Id: <20231212054253.50094-3-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231212054253.50094-1-warthog618@gmail.com> References: <20231212054253.50094-1-warthog618@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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]); Mon, 11 Dec 2023 21:43:46 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785053660547400609 X-GMAIL-MSGID: 1785053660547400609 cdev is the only user of the debounce_period_us field in struct gpio_desc, and it no longer uses it, so remove it. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib.c | 3 --- drivers/gpio/gpiolib.h | 5 ----- 2 files changed, 8 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 95d2a7b2ea3e..b1e81a561141 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2330,9 +2330,6 @@ static bool gpiod_free_commit(struct gpio_desc *desc) clear_bit(FLAG_IS_HOGGED, &desc->flags); #ifdef CONFIG_OF_DYNAMIC desc->hog = NULL; -#endif -#ifdef CONFIG_GPIO_CDEV - WRITE_ONCE(desc->debounce_period_us, 0); #endif ret = true; } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 3ccacf3c1288..a4a2520b5f31 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -147,7 +147,6 @@ void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); * @label: Name of the consumer * @name: Line name * @hog: Pointer to the device node that hogs this line (if any) - * @debounce_period_us: Debounce period in microseconds * * These are obtained using gpiod_get() and are preferable to the old * integer-based handles. @@ -185,10 +184,6 @@ struct gpio_desc { #ifdef CONFIG_OF_DYNAMIC struct device_node *hog; #endif -#ifdef CONFIG_GPIO_CDEV - /* debounce period in microseconds */ - unsigned int debounce_period_us; -#endif }; #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) From patchwork Tue Dec 12 05:42:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 177102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7529015vqy; Mon, 11 Dec 2023 21:44:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEkRGFcQ1sHAZALy2Wr/eLvyjMa4FjKOxZb4vv4j8KgLlmPq8ytPTCcA9ZZ1JVLcO35Pwja X-Received: by 2002:a05:6871:b0b:b0:1fb:75b:2fbb with SMTP id fq11-20020a0568710b0b00b001fb075b2fbbmr6929937oab.82.1702359859552; Mon, 11 Dec 2023 21:44:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702359859; cv=none; d=google.com; s=arc-20160816; b=BYgW05QR5r3+473/Jgf+Id1v/kIz46iMMuTeBinRO0HRTZ5OvLOv5vPf7jHTuTnkIe j5cAVffC64H2mN7ByKuPEB5jUB1RNyDFb4de2x3XOexUlQpJsHf8j9+BmaR3/e1BVkvf foD1guXasha+cCxJcdSL5F3L87/CMzo6UsVrUkChB9m82roX6D09P8wB1KlIce4zgYzo FHEgcL+dei5IHDOkfXrITPMr22HcjCBxk9bSGQ1uTB8rFprGV3eTC5mzFjmbrcy1abn4 39VcDtpIelsA+bJ7uceF3wYHO03yyVvMMDgYxoHUI/tBdV/hF08k1xjjaFwV/Bsaac2s fMkQ== 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=5FHTk9soipYpHLOiG237jcWWN9G9Yj9KlCtcjJBsP8g=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=Njvnemk9N2yjhcxHMkz2PzGnjpzZvQCNpbxAiPsHJlvqkzXup7B5Ck0rkHcRT+puUY CI7yvBd5lKWIJ5QHBTG0396prfT02WZvnGsSo8GYYnwA0b5Tsu+Um0QHMRmg171oWtA+ 4K5yZt2ClEV1HjUWGUD8wF3bQphTUgt2dv4zU29qR8A6YBNcnVkOs9BowWGv2ipRR5/6 6+qqL+7gGR54kz4VYpuq6erEc+kir2skCwJb47LcqBXvUah90HpYDVCq6gm1EHZQ9lQF JNaOhhjm8IJr1XYAVQf3hWG6yhka2oHcNEAxlkR+eRuEa4Hv169GBOPQy07/NcLMLVhC +pQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Li0QMl4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id p20-20020a63fe14000000b0059d48c43152si7347645pgh.40.2023.12.11.21.44.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:44:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Li0QMl4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 4339B809FA71; Mon, 11 Dec 2023 21:44:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbjLLFns (ORCPT + 99 others); Tue, 12 Dec 2023 00:43:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230236AbjLLFnm (ORCPT ); Tue, 12 Dec 2023 00:43:42 -0500 Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E97EFD; Mon, 11 Dec 2023 21:43:42 -0800 (PST) Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-5e1a2253045so7512287b3.2; Mon, 11 Dec 2023 21:43:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702359821; x=1702964621; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5FHTk9soipYpHLOiG237jcWWN9G9Yj9KlCtcjJBsP8g=; b=Li0QMl4KLsU2cRm+n1o770GuzudtsX8Cba2xfgUkxCt5CcDlhcZ9DT4dU+fBW2Mg/j GDVFFk8fF04Zek9LeQQMNW8POWiV5fvLD2PAVeYabEvGaubUmGM5uDDjv8JDrQB+sFo3 zDe8yesplmy3fxjQ7NP6mGHPq8DjfK+j5VGkBj55TtvCKqlzTBN4nIhUsNLvbX36ByJf T9N4VMvBDvB9LtGqbVWd7g8+AG2TuEIcEdqZ4OpcruEs4aA/Qg1D4YjoATbPMHvNfmyr xbMv56RWAxu7cx7RfDIskiyUWmIAPGdwHbrEhDAGConu2ETbl4LOdCko/Tl6DPgkA5D9 ZLVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702359821; x=1702964621; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5FHTk9soipYpHLOiG237jcWWN9G9Yj9KlCtcjJBsP8g=; b=PST0Qf+6K46ze+bkNpqBNlSWzSSKf74GSiUGgKYeiE64rXsupbl4os9+CDLHrFvUsP SDTj+oXZoed4/DnF5ediOyoZX+cLxNzxTBLCQmsCiya61XZ9zjlmhzsTcjbVXR7+eFUf sgiGjUwtNuzTUg1Q4ynW6XAD3hBeXrcOclXnBe1p9IdXXlVJVd3BxVHoX28W9GG9iDk/ 9W8GzWaVJm46oSR+IrciG0BOV+Nuna2DH9pCONQ8O7X+aGzR/9m3VXM2qE67j00MzEZY webyN3uIDc/acpxiFfCHqEHw52x/Fjtmq63hpJ5vbGEfc7OqaQAiFRtN5brmdQfmqQDl 0kvg== X-Gm-Message-State: AOJu0Yxpd2Xul/VL0w1qTcquiuGHcbLWtzm0G8P2ktk0tsSkT23Hgh2x 4VuH3kcQarOTi+BsjjH0VhmrhAlpTHM= X-Received: by 2002:a0d:eb4a:0:b0:5de:8c10:e5a9 with SMTP id u71-20020a0deb4a000000b005de8c10e5a9mr4446367ywe.49.1702359821337; Mon, 11 Dec 2023 21:43:41 -0800 (PST) Received: from rigel.home.arpa (194-223-186-106.tpgi.com.au. [194.223.186.106]) by smtp.gmail.com with ESMTPSA id u13-20020a170902e5cd00b001d0be32b0basm7591836plf.217.2023.12.11.21.43.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:41 -0800 (PST) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org Cc: Kent Gibson Subject: [PATCH 3/4] gpiolib: cdev: reduce locking in gpio_desc_to_lineinfo() Date: Tue, 12 Dec 2023 13:42:52 +0800 Message-Id: <20231212054253.50094-4-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231212054253.50094-1-warthog618@gmail.com> References: <20231212054253.50094-1-warthog618@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 fry.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 (fry.vger.email [0.0.0.0]); Mon, 11 Dec 2023 21:44:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785053692195919213 X-GMAIL-MSGID: 1785053692195919213 Reduce the time holding the gpio_lock by snapshotting the desc flags, rather than testing them individually while holding the lock. Accept that the calculation of the used field is inherently racy, and only check the availabilty of the line from pinctrl if other checks pass, so avoiding the check for lines that are otherwise in use. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 66 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 7999c1a72cfa..37f2c9acc770 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2398,22 +2398,12 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_v2_line_info *info) { struct gpio_chip *gc = desc->gdev->chip; - bool ok_for_pinctrl; - unsigned long flags; + unsigned long iflags, dflags; memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); - /* - * This function takes a mutex so we must check this before taking - * the spinlock. - * - * FIXME: find a non-racy way to retrieve this information. Maybe a - * lock common to both frameworks? - */ - ok_for_pinctrl = pinctrl_gpio_can_use_line(gc, info->offset); - - spin_lock_irqsave(&gpio_lock, flags); + spin_lock_irqsave(&gpio_lock, iflags); if (desc->name) strscpy(info->name, desc->name, sizeof(info->name)); @@ -2421,51 +2411,59 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (desc->label) strscpy(info->consumer, desc->label, sizeof(info->consumer)); + dflags = READ_ONCE(desc->flags); + + spin_unlock_irqrestore(&gpio_lock, iflags); + /* - * Userspace only need to know that the kernel is using this GPIO so - * it can't use it. + * Userspace only need know that the kernel is using this GPIO so it + * can't use it. + * The calculation of the used flag is slightly racy, as it may read + * desc, gc and pinctrl state without a lock covering all three at + * once. Worst case if the line is in transition and the calculation + * is incorrect then it looks to the user like they performed the read + * on the wrong side of the transition - but that can always happen. + * The definitive test that a line is available to userspace is to + * request it. */ - info->flags = 0; - if (test_bit(FLAG_REQUESTED, &desc->flags) || - test_bit(FLAG_IS_HOGGED, &desc->flags) || - test_bit(FLAG_USED_AS_IRQ, &desc->flags) || - test_bit(FLAG_EXPORT, &desc->flags) || - test_bit(FLAG_SYSFS, &desc->flags) || + if (test_bit(FLAG_REQUESTED, &dflags) || + test_bit(FLAG_IS_HOGGED, &dflags) || + test_bit(FLAG_USED_AS_IRQ, &dflags) || + test_bit(FLAG_EXPORT, &dflags) || + test_bit(FLAG_SYSFS, &dflags) || !gpiochip_line_is_valid(gc, info->offset) || - !ok_for_pinctrl) + !pinctrl_gpio_can_use_line(gc, info->offset)) info->flags |= GPIO_V2_LINE_FLAG_USED; - if (test_bit(FLAG_IS_OUT, &desc->flags)) + if (test_bit(FLAG_IS_OUT, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; else info->flags |= GPIO_V2_LINE_FLAG_INPUT; - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) + if (test_bit(FLAG_ACTIVE_LOW, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) + if (test_bit(FLAG_OPEN_DRAIN, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; - if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) + if (test_bit(FLAG_OPEN_SOURCE, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; - if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) + if (test_bit(FLAG_BIAS_DISABLE, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; - if (test_bit(FLAG_PULL_DOWN, &desc->flags)) + if (test_bit(FLAG_PULL_DOWN, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; - if (test_bit(FLAG_PULL_UP, &desc->flags)) + if (test_bit(FLAG_PULL_UP, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; - if (test_bit(FLAG_EDGE_RISING, &desc->flags)) + if (test_bit(FLAG_EDGE_RISING, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; - if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) + if (test_bit(FLAG_EDGE_FALLING, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; - if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) + if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; - else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) + else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; - - spin_unlock_irqrestore(&gpio_lock, flags); } struct gpio_chardev_data { From patchwork Tue Dec 12 05:42:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 177103 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7529061vqy; Mon, 11 Dec 2023 21:44:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IFo5LTCOiL8+0zBupUqCgkSGtOdtbofgUF5Tp2vMiv8Ekh9jXzMbGFzeKm+WeqgXxlDfj5G X-Received: by 2002:a92:c24a:0:b0:35d:59a2:2c7 with SMTP id k10-20020a92c24a000000b0035d59a202c7mr8081425ilo.103.1702359872049; Mon, 11 Dec 2023 21:44:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702359872; cv=none; d=google.com; s=arc-20160816; b=p5RmWVOQ8laCIyEP7vNDhjEq+LPd6xStx5Wae9TOOwkYpmf17utioMnYjcSnWH2awS wd1a/YnyFvff33tTk0+ITY9il6ObMdTLxHvf0ne+DseHwq2mdt/gc1sR/O94sClkIdze wuRBDV4a4LxNsVeHDGXYIL4bitYWcx7pgo9XKEzWcrZnG4b/uBOZUkb5pr8D+gr7CkvB bhvcQ8e8yTxdamIC8J/NyGBwlzg7m5xvBmVZDuY0TpkM1fqkYm7QeykBybuanRgWlgEJ gvClWMItXhu86t+UEWiJ/BpPMTOwTCxyI85xwWQ2Cd+enj9Wv4QiBwV31yIp0vMQXkFG zHnQ== 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=3KM4jX2NpfEf4zqjQE8TdyIYduegnir9m5FMRtl4TaI=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=a0XSdc/eEuqIiMEuol6oRF8JpgdmRDLHsE8X/Cm9+LH1ygFSaBtmoziRIKb+Gn9EcD eKaAnCV+dn9b1gFhVPaq/mQPLvm9QfEWIVWYrFw+o0WBjOS/okQk3Df5jUGxf+NaWsnY a5f9rcoC1TxY0pWt7pdEgfdyWBGuNDfi9lUN1HxcfhdYRKzYlnMbfD2AfqF35APRGqOe pc1+ZBPE8hOEk+MfCOidq5Da8kbflXnuOYhp6CAffXXfpnPm8TmP0V6JjFDmrmI77bxd tCGQ9y5Hev0CJcofOKf3buqoz7NNshMtymuzYVIa9Tgir4SzFw0aeQU0qfrFGCYn48ls M4Qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fkXbV5BA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id 8-20020a631748000000b00565dd108fd4si7011413pgx.115.2023.12.11.21.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:44:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fkXbV5BA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id B5EC280A0543; Mon, 11 Dec 2023 21:44:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234938AbjLLFoR (ORCPT + 99 others); Tue, 12 Dec 2023 00:44:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230186AbjLLFnz (ORCPT ); Tue, 12 Dec 2023 00:43:55 -0500 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8791DD5; Mon, 11 Dec 2023 21:43:49 -0800 (PST) Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-35d67599613so34683435ab.3; Mon, 11 Dec 2023 21:43:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702359827; x=1702964627; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3KM4jX2NpfEf4zqjQE8TdyIYduegnir9m5FMRtl4TaI=; b=fkXbV5BAVJd9RJt8sv+vCSDQo/Kuw91BeEQ0PqKxCgIpXnrbjZ4cIxtSH5zOWSd7Md WPn7rWKt9eCc8yNIXRojrlxylCuY6r0+dXz42OfnH777eXbkH3mF6hdOV/P1UJoabyem we7BTbfIk7015F0g4FgPqvYxxvYjJKchhJrxYWGUaCfRf+ZQ/GNiaQ9jE4hePbDgFaXZ TiBxswI7sy4VmyUlfXkEfn1iAhkMIIi4NdS2dUZa0tY1NNlylmp6NEAjP9l29avBMhjV 8GsM7ziEEBmYmyIiAM1k1aBBN9wIQxwwIHj9NkoDEM5OOlJURqCwfNsxEmyaO8UMXx3N KUEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702359827; x=1702964627; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3KM4jX2NpfEf4zqjQE8TdyIYduegnir9m5FMRtl4TaI=; b=P9fjVkpvt3Sl/uw2UScwSYh0XLI2rZjOMttAWFVDKflkARbKlRUGAQncvxkDifgz8s MsXu2BnLKRFgq5EIxBkDWUQ2psqpaCFYrYAyaIvc/ClgaflPZ7jA9RBSHZGPD4QYLFSO wuWQJZSl4QnVdgg1OOspPleZqA3Swz+JvlCHKiubu9buCpk8g9mt248uPiia+51QEK2l o1VHxLTTufTAQrahzOGjNmHXj1PGGbTXaCzPYDIwMKgmEBVl0M9Z3dKw/PL1NoAuUu+B HagPRfkY/tsZnKEMfSTn87SPaTEQYwiw/MWStaq8+tk5RhXP7AbIOyCD8KmM8LsHfwIR +DRQ== X-Gm-Message-State: AOJu0YwWJZzhXV+TZf8bPXDF47/pSJe6b3AzfYX6NmqPFetUXgJiNqet zIKMdtvQBgLT8xaHhTUWjdfb/xsULns= X-Received: by 2002:a92:6907:0:b0:35d:a73b:4d2f with SMTP id e7-20020a926907000000b0035da73b4d2fmr7261182ilc.117.1702359826947; Mon, 11 Dec 2023 21:43:46 -0800 (PST) Received: from rigel.home.arpa (194-223-186-106.tpgi.com.au. [194.223.186.106]) by smtp.gmail.com with ESMTPSA id u13-20020a170902e5cd00b001d0be32b0basm7591836plf.217.2023.12.11.21.43.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 21:43:46 -0800 (PST) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org Cc: Kent Gibson Subject: [PATCH 4/4] gpiolib: cdev: improve documentation of get/set values Date: Tue, 12 Dec 2023 13:42:53 +0800 Message-Id: <20231212054253.50094-5-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231212054253.50094-1-warthog618@gmail.com> References: <20231212054253.50094-1-warthog618@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 fry.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 (fry.vger.email [0.0.0.0]); Mon, 11 Dec 2023 21:44:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785053705217004509 X-GMAIL-MSGID: 1785053705217004509 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 --- 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 37f2c9acc770..6878da5056f9 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1394,9 +1394,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; } } @@ -1405,6 +1414,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; @@ -1425,6 +1435,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]); @@ -1450,14 +1461,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; } } @@ -1465,7 +1487,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;