From patchwork Fri Dec 15 02:38:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 179010 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8999241dys; Thu, 14 Dec 2023 18:43:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6tzvWWgE5ALk1EfswdeP7s5u9TOxACV8kaCXdiTvTolY8hrnl0FvaNpzseLyfCKnr5v/R X-Received: by 2002:a05:6a21:192:b0:190:1cf7:f80 with SMTP id le18-20020a056a21019200b001901cf70f80mr14244757pzb.93.1702608187141; Thu, 14 Dec 2023 18:43:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702608187; cv=none; d=google.com; s=arc-20160816; b=vlK+Gy7gwNbzvQ15nNdcnogK/eh96OQw670QOPkhO09Xd9d/PTh9ezdhlBpMLeyvu0 8OU/S75+d3qCoJiu7WgrQopNS/gb23LrA4CfURjtI9RmtS8iFoVJEkPcKzkvSouEF11b D7XoN8jvhSVUO5gw4ERxsLstQArxwYS5GTPxMpEcYl+Lt8k3e3f+/1mP30l8rniQYgw7 z1yelK1E+ccyLy/BrsCt6ZHSZOGog+Y6CyizLiBKMvtSQagUPtLghR/i3Nr5wT5Z/4J5 0PTTTWjl+wOvsRrVfJ5x15DRSVF8GBfqLRL0/dQfsXzy4UUDpXeyJ6gsQ9lZ8Hpi4VPc xUTA== ARC-Message-Signature: i=1; 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=47cjWhSgEF8kYG8UlE2EFWvUw8VZbJWXw60QIkQYmC0=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=aI5pIkG/5Ge/S0VR+ngsysdEvuTnZJjKu9HfNcdp1yRr9UpNhdQvOW9vqeAwlu1nFI 2sTUK+PaCa6fI9WoLK+uj+o1HCqtPPBVLBCNEGCt26GM9AbadwyU+qJOO6U7WW6+FtVp 6HRtq1JcjnJVezNU9FFQSPGZNz2MGIWVkLMCBJTdojipNjvFvNLJ5jmxnMyLKL+4LIqW WoaAMd2w8LFvV0P9SKEkQSDWsaQO3unY2xpsYMcBEKFsEvzFlRkadvJC0hFfoaz214LC 7yYVMGpJaQnCrcVvFDvvhk2k7T4HDO3uHJp27dO+4HriNuzQ804/x1gu1QXpOkxA5wsk hgaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VG0XTe9H; spf=pass (google.com: domain of linux-kernel+bounces-365-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-365-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id n13-20020a17090ac68d00b00286edd97032si1579123pjt.134.2023.12.14.18.43.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:43:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-365-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VG0XTe9H; spf=pass (google.com: domain of linux-kernel+bounces-365-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-365-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id F2850B21DF8 for ; Fri, 15 Dec 2023 02:43:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6BDE96AA4; Fri, 15 Dec 2023 02:38:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VG0XTe9H" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 098486139; Fri, 15 Dec 2023 02:38:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6ce7c1b07e1so155138b3a.2; Thu, 14 Dec 2023 18:38:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702607917; x=1703212717; 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=47cjWhSgEF8kYG8UlE2EFWvUw8VZbJWXw60QIkQYmC0=; b=VG0XTe9Hx3aBx/je6kYgKJlfGn3QK4FjyNDnq9j6Mg75UWTFoQzrZXgawv+bn1yhq5 4f2cPhHnUN0fro9y6ndnq7b4j724eBv6zvNjXVV4vy7cOs9o1w4zIMSRYGMqNzxYwo5N RQHt0FuyOCMdSEBHA4YRieP50rXY3C3mhturX5CodXke7oQ0Cw6v1P919gqiNU0RIyTU hSCvSVzFFhyYdPiZLWaWgnrogqdDs3BmRkmCfh2RBPA3jlh+ZG3h/kfFjxWRiN2eIfY/ LDIzgXE/3Dk4ZrT6S8zKNu5WxJ0d5Jv/OYlZX20q1+K7E5tzHNMFACy7Y2b0mvu6SuwY OyMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702607917; x=1703212717; 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=47cjWhSgEF8kYG8UlE2EFWvUw8VZbJWXw60QIkQYmC0=; b=VqSeNqMHFSUw+avJgkZvRsJ+uKmszeowrG6ogMHjiR5fAO9lAlkDNKdymeoCcaMPc3 ssYFqmKBeG8AAcdRi19aaZYW56cPYHG7qWujVn15CbPMqcoH8MThJ+GyK1GvcT2qZNLo SHWYgsYVvH62EMqVjanFMf+JT0/wSpugw+EbzyZl1thjV90qMoBwU4buWKOHGBXGqP6c AtDb0KS6ZyUBRcvBu8YRECP0jFvAY2QzOfjTpIbdUvXelSkfl1uKlmUOaCrkmjCeOEna jSGfa1AqXvdEEgZyC0vLyUd1tgJYh5rgzMQhuaIuqW/YsNyNl+pBAdHaJlErhQTe1sZW JfgQ== X-Gm-Message-State: AOJu0YwESOxj8okfRCSB77jIXr/04j8nk7DOW+46kmuC3/Ej52aAf755 JBrlf/lSkWNcBUjOdh5ic7iEQ2l7jDI= X-Received: by 2002:a05:6a20:8423:b0:190:93e:f224 with SMTP id c35-20020a056a20842300b00190093ef224mr14556562pzd.76.1702607917111; Thu, 14 Dec 2023 18:38:37 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id fk16-20020a056a003a9000b006d26920a11dsm1437987pfb.0.2023.12.14.18.38.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:38:36 -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 v3 1/5] gpiolib: cdev: relocate debounce_period_us from struct gpio_desc Date: Fri, 15 Dec 2023 10:38:01 +0800 Message-Id: <20231215023805.63289-2-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231215023805.63289-1-warthog618@gmail.com> References: <20231215023805.63289-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785314082351183398 X-GMAIL-MSGID: 1785314082351183398 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 | 152 ++++++++++++++++++++++++++++++------ 1 file changed, 130 insertions(+), 22 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 02ffda6c1e51..5ba900e5461a 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -461,6 +462,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) /** * struct line - contains the state of a requested line + * @node: to store the object in supinfo_tree if supplemental * @desc: the GPIO descriptor for this line. * @req: the corresponding line request * @irq: the interrupt triggered in response to events on this GPIO @@ -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 @@ -481,6 +484,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) * @last_seqno: the last sequence number before debounce period expires */ struct line { + struct rb_node node; struct gpio_desc *desc; /* * -- 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,17 @@ struct line { #endif /* CONFIG_HTE */ }; +/* + * a rbtree of the struct lines containing supplemental 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(). + */ +static struct rb_root supinfo_tree = RB_ROOT; +/* covers supinfo_tree */ +DEFINE_SPINLOCK(supinfo_lock); + /** * struct linereq - contains the state of a userspace line request * @gdev: the GPIO device the line request pertains to @@ -575,6 +599,94 @@ struct linereq { struct line lines[] __counted_by(num_lines); }; +static void supinfo_insert(struct line *line) +{ + struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL; + struct line *entry; + + guard(spinlock)(&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 { + /* this should never happen */ + WARN(1, "duplicate line inserted"); + return; + } + } + + rb_link_node(&line->node, parent, new); + rb_insert_color(&line->node, &supinfo_tree); +} + +static void supinfo_erase(struct line *line) +{ + guard(spinlock)(&supinfo_lock); + + rb_erase(&line->node, &supinfo_tree); +} + +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; + + guard(spinlock)(&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++; + } +} + +static inline bool line_is_supplemental(struct line *line) +{ + return READ_ONCE(line->debounce_period_us); +} + +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 +835,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 +976,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 +1058,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 +1137,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 +1162,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 +1204,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 +1684,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,10 +1692,14 @@ 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) + continue; + + edge_detector_stop(line); + if (line_is_supplemental(line)) + supinfo_erase(line); + gpiod_free(line->desc); } kfifo_free(&lr->events); kfree(lr->label); @@ -2274,8 +2390,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 +2455,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 +2561,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 +2652,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) From patchwork Fri Dec 15 02:38:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 179017 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9001937dys; Thu, 14 Dec 2023 18:51:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IGaYQt9jL/8HoPlwjTbtD6fhY/iGDFdQb/3c7NJZOVwmiwU1TSlNr0r98l80TDnJ554vgxc X-Received: by 2002:a05:6a20:e110:b0:190:917:4e3 with SMTP id kr16-20020a056a20e11000b00190091704e3mr16548821pzb.49.1702608706754; Thu, 14 Dec 2023 18:51:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702608706; cv=none; d=google.com; s=arc-20160816; b=UNOSuaNMi6uu9du9qRBBKuuovdgXnHE2389Ns0Z0XI0JvlUeIvhGB7JV/tIxwxGP4j tCCs9frUVJWtYGSC0AGoC7Vr+petPIINREQ5k5Tise8sV/6gHAltEREg/lMfpSY9Mzrb k4PKInbEXdM0nMf4xviKwAldWtdqFgAX/FspOUFIIWA0C7m/A4T9R2eXHE8w75+/Q8jX CeNxxCbiYn8gcNDUv8HmDS8cjAdin4v/t0IDRCu19YAjSOKXbgjxvS0iFzTegeBwXSYp 8Wav6I1aqalNUq4QKo5iY6uHBjAbtuxCv5bgyy/tVND/JRJE6YKFiqqIUUCLzvzVtfXo bfoQ== ARC-Message-Signature: i=1; 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=/gErARlFKGLkOKnr/eZMaU60+19wTX7Ibdpi8xmN6kM=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=fVfz4BBxNRsNBo6Ga438k3yu3x+DHlZRRGYphIk3jYHtrpj9ZXM4fGKaA2TW4M+Fvm 4TyvYUjaTFde/JCN+9l3GwmHoplVBoCtrui8fAVGr7VUoNtCUYdxV6GkDD2OgFhWZ9ey 3gxjvke+3fFkpzreBeNYC7uwSA674ZeD30H7EYZFaipllX9fCTdkdhpdBKDvibFwfGtX t9iUaQlzP2FlXAeeLK3l+1qKArHGzoUa/nXBZXqZv2k0SVU50P4reWtPZDCaKIunsMZo ONlO4m0sLQls/vzUZQJfjGJzWwXpoAfIbIzv+Y8miJipkIaxRHktdMnkq9bTieXNhaLj TZHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=CZ2f8aLx; spf=pass (google.com: domain of linux-kernel+bounces-366-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-366-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s24-20020aa78298000000b006cbd24bf0b7si6635113pfm.52.2023.12.14.18.51.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:51:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-366-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=@gmail.com header.s=20230601 header.b=CZ2f8aLx; spf=pass (google.com: domain of linux-kernel+bounces-366-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-366-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 C6DBF287479 for ; Fri, 15 Dec 2023 02:43:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 111A61A290; Fri, 15 Dec 2023 02:38:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CZ2f8aLx" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 157778BF1; Fri, 15 Dec 2023 02:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-5c6839373f8so113365a12.0; Thu, 14 Dec 2023 18:38:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702607923; x=1703212723; 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=/gErARlFKGLkOKnr/eZMaU60+19wTX7Ibdpi8xmN6kM=; b=CZ2f8aLxfc2/FeU6dsTWgSzXVdiKjLDVUFmYiPuBiSpEyBJrA8ih1uMhSuniXHlQ8/ Xh87/sk2R1TVFygwVy5YdH1NNPpmEzAEX4XbRc2JJJYQpfguyC45EEgMB9/ss7iKP3nZ BYRh+byLawwL21B6DrJierMCxAtAtXT7vE8/j1SySDiw6y6uj5j1QghzzAcXCNaP/KDa 5M5kQNUyihPAB9jW2WeIvaiXxO60UtgMdXecteRvpaRPafDDs34dX4llXBMB/RiWchD3 ZQ65Gu+1JFMrFlklylGEn/DEDUFa76ErlPefpa1aND5ui2suZ4w/zj3ZTZmJSKd/0tg1 D/ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702607923; x=1703212723; 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=/gErARlFKGLkOKnr/eZMaU60+19wTX7Ibdpi8xmN6kM=; b=S5dcCqkbxFcU5Xtup9D7cJT2nqHZ9Z4+6qdC9oSohDR6jlDhRz6DAmkXAVG5ENn8yj C9/LmRILuzWTucvxHtrermb1sur4KkQkU8svy2ynyet1h+B0AFzAl3Mmw8nz16n/O0uP YkxXFvNz6bhqes/T5pNOpnlpBYMUhSyybNk0GoU/Mkb+dJ5nT7PDx7yqqkafcBVUgyqy sU/Ri16y6RcYQejRrjKtYljY4SRHQRVoxqs1ERY290y2tmrgmfJTEv4Bd3HTaJ55sEcp JAPbGeN1MPDJ7OT39eGdwi/NHwOu85tPMe8SWIOe3YCNk9S0YNoqRfYAipqo/pVZA5Fq 3HoQ== X-Gm-Message-State: AOJu0Yx8s/W2zZ2yYyvkjQBe3OQO0tDcE/+0BDiFoBtWU9YLPozu0zdE ZSzaKu8i+Qk3nPWhDDIYZmp/oMF4Xv8= X-Received: by 2002:a05:6a20:3d89:b0:190:7b07:b7e4 with SMTP id s9-20020a056a203d8900b001907b07b7e4mr12917150pzi.10.1702607923142; Thu, 14 Dec 2023 18:38:43 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id fk16-20020a056a003a9000b006d26920a11dsm1437987pfb.0.2023.12.14.18.38.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:38:42 -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 v3 2/5] gpiolib: remove debounce_period_us from struct gpio_desc Date: Fri, 15 Dec 2023 10:38:02 +0800 Message-Id: <20231215023805.63289-3-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231215023805.63289-1-warthog618@gmail.com> References: <20231215023805.63289-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785314627491512588 X-GMAIL-MSGID: 1785314627491512588 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 4e190be75dc2..ca2216621619 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2344,9 +2344,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 Fri Dec 15 02:38:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 179011 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8999457dys; Thu, 14 Dec 2023 18:43:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJn9/5/j3dLo4DOpZmOwJMG1OLYOJBnO9VaFnA+LQXqKF7RzdYY46NeCPy6JV8bvE2Qc/M X-Received: by 2002:a05:6359:4123:b0:170:c1a7:254 with SMTP id kh35-20020a056359412300b00170c1a70254mr10182264rwc.2.1702608226207; Thu, 14 Dec 2023 18:43:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702608226; cv=none; d=google.com; s=arc-20160816; b=n3t8ICtIiz7PcFKh7sGzHku3QPFIysXzOZK4hniU0aYovgtAMqxAvxkw6QSpunIYUI NKomw7Gr5SKZNElOwLsutE6OFef++oldCmVxfNTb7WuirNt/lFV1FB9mEwvu+/BoxOvK qrH0G8U8U3EdOMqDtqrWzM+/om3Js1amWhrMKd3nNhzYxcYHsuDQTk59vmoyYgZ7CWWx It1guQM3sdgo13mhG/l7qe4HeaD86qHUkS1zxsEGXLcFa8o4n2wvQ3SAQ5GFk97Sc8jE RYaoDqBSUWV7+36+RKTFnDVXTUmShr5L3LzBQfOC06AoCjFawAeATAi1wv+aIloo7YVW np/w== ARC-Message-Signature: i=1; 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=CtO2jY+EJP8Yvc8VKZdfYbcqASBoJOxP1iWf7Ww080E=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=iefOKsNKckTMzen1eQ5SMHnK4GSQwM4a5c11hwABnrtrchZYiTF0y9B5Wi0zPF4zak 8UdNZVXlzNfThMLt5MzD4xARzN72fozoikX+d6ypIok2LjPK/CEF8fm+wnKr6m7oLSzB WTNBLzU0sqzeyjdctiF1fd7RFJK9FpY0OUaPXnwOZNDcHCxqgjuLljwGtSduEa5jywSG TNiMuDOQJrM6YX8Uy46VTYXd0sXe/vXtVXc1ecixsUcrbq3I6OVeG/u+aSbrLYjAJCl4 caHbEoTf+7PQ+mKh3pjVoseqIvhIs0lRuYKqKQWlSAZHUoz4TUPdqSPbEZwGa9kizInv m1Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ieCk6gG9; spf=pass (google.com: domain of linux-kernel+bounces-367-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-367-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id pg3-20020a17090b1e0300b0028ac5d6a4e4si5263498pjb.120.2023.12.14.18.43.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:43:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-367-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ieCk6gG9; spf=pass (google.com: domain of linux-kernel+bounces-367-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-367-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 6E8D3B20A39 for ; Fri, 15 Dec 2023 02:43:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44D8ED291; Fri, 15 Dec 2023 02:39:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ieCk6gG9" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CBC7D271; Fri, 15 Dec 2023 02:39:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3b9ef61b6b8so194947b6e.3; Thu, 14 Dec 2023 18:39:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702607964; x=1703212764; 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=CtO2jY+EJP8Yvc8VKZdfYbcqASBoJOxP1iWf7Ww080E=; b=ieCk6gG9OZ3zhBPcrphnGM4Zy31QEtol5Znhd8nXoBvILsf7aenHXe2gmoJX8B52Ha mlU9bLeeU499mQbATPYQtHDbtX0uyizU5J67KwAvOgOmSIQ4D8JeQLPxgiyDcgNKYHTn ichXppg1/BCx7dKJNrLTCsJK1YpEB9k4l3BDQnovo9OCKwlvBQuYeSmLKpLEBzSTNhQb y2ZKDbztadPCHk9W+bw5Dd5ivz3+OvfvMkhhOH4Qv8IaX1rhFYWMiqBi1D6JtJdI+58i Y/ijsgfqwByBRSBFmXfu8y+4dMqLLAN1SnG1cOf0RWE/NnZtOaA7XmPlu0kRBT8gSaai +GfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702607964; x=1703212764; 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=CtO2jY+EJP8Yvc8VKZdfYbcqASBoJOxP1iWf7Ww080E=; b=UuEnNACyOSfVsBN/oBcFiLz1FXOeZ/MM0J0v0/wIV4o3nqEbGd91ts0UDUF9DCXACa C5j/w+TA0oE3RNgPa+HJ1s2qCoJQvXXFPYflOMxrz1tZlmVl3v+Q19THtifap5oLsT5/ 7ETw0+obwpMnFVNNlgNbv/4NUoE+GKFNI4z76GP9f7HBKopwdv62haD8dM2xPdApckiv Wm3VgZH7MJOOAM5Bq3FaEO0fCcEXISJ+7yJNX4gzIu64tcwLju4en4oNDxnjajbDa8Hb UskjtRVrGMy8V8oPtbdr1dDA3PJGeQ46TV6IZ2MqJUG6HXTUNfwBXeBv2blamMGbCzQx 4FKA== X-Gm-Message-State: AOJu0YzndLHzrsSrj1Ew0Okia/OFACXTB8GXV7G4ab1qScDQxtO+M2rr O6DBRTst9z5iXMmQ/YjnohfhZq6lf3o= X-Received: by 2002:a05:6808:ecb:b0:3b9:d903:9fa9 with SMTP id q11-20020a0568080ecb00b003b9d9039fa9mr13502871oiv.105.1702607964084; Thu, 14 Dec 2023 18:39:24 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id fk16-20020a056a003a9000b006d26920a11dsm1437987pfb.0.2023.12.14.18.39.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:39:23 -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 v3 3/5] gpiolib: cdev: reduce locking in gpio_desc_to_lineinfo() Date: Fri, 15 Dec 2023 10:38:03 +0800 Message-Id: <20231215023805.63289-4-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231215023805.63289-1-warthog618@gmail.com> References: <20231215023805.63289-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785314123227676482 X-GMAIL-MSGID: 1785314123227676482 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 availability 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 | 74 ++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 5ba900e5461a..c140bcd63361 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2388,74 +2388,72 @@ 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 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); + scoped_guard(spinlock_irqsave, &gpio_lock) { + if (desc->name) + strscpy(info->name, desc->name, sizeof(info->name)); - spin_lock_irqsave(&gpio_lock, flags); + if (desc->label) + strscpy(info->consumer, desc->label, + sizeof(info->consumer)); - if (desc->name) - strscpy(info->name, desc->name, sizeof(info->name)); - - if (desc->label) - strscpy(info->consumer, desc->label, sizeof(info->consumer)); + dflags = READ_ONCE(desc->flags); + } /* - * 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 inconsistent then it looks to the user like they performed the + * read on the other 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 Fri Dec 15 02:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 179012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8999526dys; Thu, 14 Dec 2023 18:44:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IH9bf0OL5/yHiShZqeppVW48crHRi09eNs84Pc402iC+9Pki5oU0M5AExUzfvkFJ/3ShfJj X-Received: by 2002:a17:906:1db:b0:a19:a19b:55c6 with SMTP id 27-20020a17090601db00b00a19a19b55c6mr5470798ejj.86.1702608240798; Thu, 14 Dec 2023 18:44:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702608240; cv=none; d=google.com; s=arc-20160816; b=XIDBR1aMrO5mixfVMYcOy9WhfUWwTujy058gSQ+ELFWEBjwxtNb2jyCPiYmX7uvnTs TbuNQaPG9+hkyDm2WcHPVEqneIWO1uk4prxIldvK4wIYYrQ8TnKEhI1WU9clhzARVKiZ 7E80BemGD/FVPGrIQt+2AAHdi0rIm+lfvfI9e2bU2IZJXQIwj5B7ovX+tggNuW9Or+fT d2tG1oj7ouXv9O8LxikivEGBxdsDvOl/IzT9nFJWPECxkOihW7J5Q9VeyecFZt+ivn+H 3ZBQFbPid5qe7AW6Jnbu3Y7rJ6rtA0jS20NOE4XkUEvl6W63w8ZXWtonqVTc2i5NRnyw KC0w== ARC-Message-Signature: i=1; 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=Nqel2RWN3Kn4ajuEozWoNvrbGTL4J41kB/Wxn1TNEfI=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=AwglY18hVKyQEqLdVbtWCHidYMW8OCPJaofJwsLifVgKY+D/zxuEUNA0OwLbJHLTb8 /dvXM+DNKvwXPEpUZMTazWg4/mEHNhP9VLh1B7H0BlWuyK1t5xUlFRyO0Nq1lFVSoRgK 4AQW+Krr8yK4IkdnMgaoAFxioHPvdbAV6kCQMIlazaNrA7Uv9esX+nf7TkvfO/3Rb6YN 0alS0DNG2YADsA3Mzca0ec6PmvSbr5MS4NHOdi1WRQrBgJWz6fz0O7uNTfDenanlwipu mtf9lOrd/U9Tz1cFUN95VGa9NOL8mI66mPp4Wj5snhGHx+fNo5LxGJ+RuknkxSZ1gIss L8uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=i1BTw36N; spf=pass (google.com: domain of linux-kernel+bounces-368-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-368-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r6-20020a170906350600b00a1c9d73e569si7027241eja.472.2023.12.14.18.44.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:44:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-368-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=i1BTw36N; spf=pass (google.com: domain of linux-kernel+bounces-368-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-368-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 325C41F211E3 for ; Fri, 15 Dec 2023 02:44:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 70E36846C; Fri, 15 Dec 2023 02:39:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="i1BTw36N" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3073AC8FD; Fri, 15 Dec 2023 02:39:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3b9e7f4a0d7so201773b6e.1; Thu, 14 Dec 2023 18:39:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702607979; x=1703212779; 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=Nqel2RWN3Kn4ajuEozWoNvrbGTL4J41kB/Wxn1TNEfI=; b=i1BTw36N9iti7m1ZvH9wj6lDNfUzcZnOdEk8Ekl51L3hdOdZagD/YRapgGynwsnR3M xs9DHUJ7/r4slYVNLzfjDHyaX8zbKnEjWNTK3hM0GKat5UtXIqNrZjmUMcXdQfGY2LXT rAeHfwZWUtJhGc2peQLFA68JQfAm/hJb8ssXLGAZhLMBloTg6OhvmR17teYGJ4PrxAsZ WfuH7IXxMGnW8sws1Byh/235h2/yz7FGf2HSDtBu+b8dK0WhKb0/XoryrcWPi0q6EOTf Uo+Xy6jE/JCEQ8ZPS8EqSNvYL4UhYzADxeym5Hpta2CmY5kG1nxURF5Bt+5SrmsL1gCe S6yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702607979; x=1703212779; 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=Nqel2RWN3Kn4ajuEozWoNvrbGTL4J41kB/Wxn1TNEfI=; b=J/mIUxsSxQsOcgF1TGv6P+bdobJZCjAhk+n0eb2r7hn1pyG3zt3bOHFikw12gjQVk5 TJ+Fx49KCLyzn6nJdIZOmINDQKTBPXZGNWYpNmCcHQdIEgzqd4BzF5LAAr2bD0Y13wuu jrlckXMp1CYkwxMc0uMn9bZ3/S1WA9fupwUAEPVsv/Je87D3R7F6jvtAPjc3rpznYqcz TUusATMmitLukVCul5N/jG5oz0ffNQZix+bQIL4C25VL9NyNcBHegDmhhsRkrYeN3zXA EXQfBtYBdRcQwvuHxuJ9gqzbPHHW7XWVQhVRfOwYSgckD/uyjfKIuetdric8DRqIUAmE 04fw== X-Gm-Message-State: AOJu0YwJh6ETjQMuBk56sp8WfaY1hSKJ3vG5gP0F4vtyleVxlmQOhrU+ rxSPKX/AWtkl63nwCVjteJzBt5APFd4= X-Received: by 2002:a05:6808:320c:b0:3ba:54d:413 with SMTP id cb12-20020a056808320c00b003ba054d0413mr10950544oib.54.1702607979119; Thu, 14 Dec 2023 18:39:39 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id fk16-20020a056a003a9000b006d26920a11dsm1437987pfb.0.2023.12.14.18.39.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:39:38 -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 v3 4/5] gpiolib: cdev: fully adopt guard() and scoped_guard() Date: Fri, 15 Dec 2023 10:38:04 +0800 Message-Id: <20231215023805.63289-5-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231215023805.63289-1-warthog618@gmail.com> References: <20231215023805.63289-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785314138749757717 X-GMAIL-MSGID: 1785314138749757717 Use guard() or scoped_guard() for critical sections rather than discrete lock/unlock pairs. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 143 ++++++++++++++---------------------- 1 file changed, 57 insertions(+), 86 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index c140bcd63361..b956664f8649 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -737,13 +737,13 @@ static void linereq_put_event(struct linereq *lr, { bool overflow = false; - spin_lock(&lr->wait.lock); - if (kfifo_is_full(&lr->events)) { - overflow = true; - kfifo_skip(&lr->events); + scoped_guard(spinlock, &lr->wait.lock) { + if (kfifo_is_full(&lr->events)) { + overflow = true; + kfifo_skip(&lr->events); + } + kfifo_in(&lr->events, le, 1); } - kfifo_in(&lr->events, le, 1); - spin_unlock(&lr->wait.lock); if (!overflow) wake_up_poll(&lr->wait, EPOLLIN); else @@ -1476,18 +1476,13 @@ static long linereq_set_values_unlocked(struct linereq *lr, static long linereq_set_values(struct linereq *lr, void __user *ip) { struct gpio_v2_line_values lv; - int ret; if (copy_from_user(&lv, ip, sizeof(lv))) return -EFAULT; - mutex_lock(&lr->config_mutex); - - ret = linereq_set_values_unlocked(lr, &lv); - - mutex_unlock(&lr->config_mutex); + guard(mutex)(&lr->config_mutex); - return ret; + return linereq_set_values_unlocked(lr, &lv); } static long linereq_set_config_unlocked(struct linereq *lr, @@ -1545,13 +1540,9 @@ static long linereq_set_config(struct linereq *lr, void __user *ip) if (ret) return ret; - mutex_lock(&lr->config_mutex); - - ret = linereq_set_config_unlocked(lr, &lc); + guard(mutex)(&lr->config_mutex); - mutex_unlock(&lr->config_mutex); - - return ret; + return linereq_set_config_unlocked(lr, &lc); } static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, @@ -1633,28 +1624,22 @@ static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, return -EINVAL; do { - spin_lock(&lr->wait.lock); - if (kfifo_is_empty(&lr->events)) { - if (bytes_read) { - spin_unlock(&lr->wait.lock); - return bytes_read; + scoped_guard(spinlock, &lr->wait.lock) { + if (kfifo_is_empty(&lr->events)) { + if (bytes_read) + return bytes_read; + + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + ret = wait_event_interruptible_locked(lr->wait, + !kfifo_is_empty(&lr->events)); + if (ret) + return ret; } - if (file->f_flags & O_NONBLOCK) { - spin_unlock(&lr->wait.lock); - return -EAGAIN; - } - - ret = wait_event_interruptible_locked(lr->wait, - !kfifo_is_empty(&lr->events)); - if (ret) { - spin_unlock(&lr->wait.lock); - return ret; - } + ret = kfifo_out(&lr->events, &le, 1); } - - ret = kfifo_out(&lr->events, &le, 1); - spin_unlock(&lr->wait.lock); if (ret != 1) { /* * This should never happen - we were holding the @@ -2004,28 +1989,22 @@ static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, return -EINVAL; do { - spin_lock(&le->wait.lock); - if (kfifo_is_empty(&le->events)) { - if (bytes_read) { - spin_unlock(&le->wait.lock); - return bytes_read; - } - - if (file->f_flags & O_NONBLOCK) { - spin_unlock(&le->wait.lock); - return -EAGAIN; + scoped_guard(spinlock, &le->wait.lock) { + if (kfifo_is_empty(&le->events)) { + if (bytes_read) + return bytes_read; + + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + ret = wait_event_interruptible_locked(le->wait, + !kfifo_is_empty(&le->events)); + if (ret) + return ret; } - ret = wait_event_interruptible_locked(le->wait, - !kfifo_is_empty(&le->events)); - if (ret) { - spin_unlock(&le->wait.lock); - return ret; - } + ret = kfifo_out(&le->events, &ge, 1); } - - ret = kfifo_out(&le->events, &ge, 1); - spin_unlock(&le->wait.lock); if (ret != 1) { /* * This should never happen - we were holding the lock @@ -2719,38 +2698,30 @@ static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, #endif do { - spin_lock(&cdev->wait.lock); - if (kfifo_is_empty(&cdev->events)) { - if (bytes_read) { - spin_unlock(&cdev->wait.lock); - return bytes_read; - } - - if (file->f_flags & O_NONBLOCK) { - spin_unlock(&cdev->wait.lock); - return -EAGAIN; - } - - ret = wait_event_interruptible_locked(cdev->wait, - !kfifo_is_empty(&cdev->events)); - if (ret) { - spin_unlock(&cdev->wait.lock); - return ret; + scoped_guard(spinlock, &cdev->wait.lock) { + if (kfifo_is_empty(&cdev->events)) { + if (bytes_read) + return bytes_read; + + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + ret = wait_event_interruptible_locked(cdev->wait, + !kfifo_is_empty(&cdev->events)); + if (ret) + return ret; } - } #ifdef CONFIG_GPIO_CDEV_V1 - /* must be after kfifo check so watch_abi_version is set */ - if (atomic_read(&cdev->watch_abi_version) == 2) - event_size = sizeof(struct gpio_v2_line_info_changed); - else - event_size = sizeof(struct gpioline_info_changed); - if (count < event_size) { - spin_unlock(&cdev->wait.lock); - return -EINVAL; - } + /* must be after kfifo check so watch_abi_version is set */ + if (atomic_read(&cdev->watch_abi_version) == 2) + event_size = sizeof(struct gpio_v2_line_info_changed); + else + event_size = sizeof(struct gpioline_info_changed); + if (count < event_size) + return -EINVAL; #endif - ret = kfifo_out(&cdev->events, &event, 1); - spin_unlock(&cdev->wait.lock); + ret = kfifo_out(&cdev->events, &event, 1); + } if (ret != 1) { ret = -EIO; break; From patchwork Fri Dec 15 02:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 179018 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9002631dys; Thu, 14 Dec 2023 18:54:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IEaDC2wBBhU/oaR06AwjC/Zx4hm5bzVQ+Ke+idl7dPAP4Ht969k3LhF0SU4mQZsOGHLt+jz X-Received: by 2002:a17:902:e54f:b0:1d3:45b7:bc1c with SMTP id n15-20020a170902e54f00b001d345b7bc1cmr6323045plf.64.1702608848770; Thu, 14 Dec 2023 18:54:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702608848; cv=none; d=google.com; s=arc-20160816; b=pB1Ogk+i1gzPpnYScGsHfd+h+JyAIKon4HjmifLlQ1UvmIKnVWMd6abgSPA1t34o4z bqpcuTmPHRFkV4y3iyG1pDt8Ks8nMhBFWsyYifUoHX4kVC0qHPkRRfgKW3sU7zH1CSXU CQ8CoqTjywFH07c8GDWt4cIojL41H0c8inCLLHJq369D8WycEDqcOq7MW5eGkbXDHoy0 E3Rlibz/bOpp1YF2k/Ao/qyxzjXbUQGmjUeyzb13/36r6O4cRuk/J6WMi0a+YY8pF8d/ 1KYylubdbTrQRsaTlVWbdyDxjQZ6rTwU9qGPnpJXGNnl3xoJRYii4Gi9xAm9ywzeH+UN UNdA== ARC-Message-Signature: i=1; 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=cJew1Ut9h95zC9IuFrB6MxorQq1lAg1fBre3mDpboVk=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=h5lKZAK3eX7VmAEbT5oXYzwXJ2BlZerpipiqpe1FXxDPr9rDrWzllUmRzsgeuNbx3U yatX4hK1CGGQ3NmTJDMKJesCkL9soDpyRgwM2ehpY6JmtI3tsp/afQEX7rgUTd8u0Ytq zoY6UGHeN0GHQouEbwxrztjbXgp6yrpZ1/Q5QtUFpcaS7YxjPsPifZJPRVmj3xYu2qhh M0lKrrEUR1ycMzYe7fQA6qnrxWfZw8hsNS7GcH71FPb6EI/5Wkj70VQeWNXrXiPYMggj ywzrBczA8sah/Vxccc2v8mB7lqD8TiAxJ9roppguQHZKGub2qsSsk8PLUzXJ45AglmXk xfjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YKJomVo0; spf=pass (google.com: domain of linux-kernel+bounces-369-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-369-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q4-20020a63e944000000b005be03f0da68si12226583pgj.13.2023.12.14.18.54.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:54:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-369-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YKJomVo0; spf=pass (google.com: domain of linux-kernel+bounces-369-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-369-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 9A6E7287FF5 for ; Fri, 15 Dec 2023 02:44:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C4DE2DDDA; Fri, 15 Dec 2023 02:39:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YKJomVo0" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6B64EBC; Fri, 15 Dec 2023 02:39:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-5c66988c2eeso104494a12.1; Thu, 14 Dec 2023 18:39:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702607989; x=1703212789; 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=cJew1Ut9h95zC9IuFrB6MxorQq1lAg1fBre3mDpboVk=; b=YKJomVo0MbYyn+/pvpivd+zDy4KFTQ4wbSymKOjd2VcG4uYVYVMEnsuIxgniKWF7FH vMwKBRPnhClh69dqHGrR2L/qTcNBRTDSzLhpNQCDAZ1aZ5oNWwroqr8cDotC7X839Kdr 54AIqQBIlvJYoHLTVsl5STp2AR5nIbuY1XElP5Qu6vRE7LrY0UmDhMgV4s2ojYSrpO6R fw1rxGYS8unlPZKZsjuqsDtb9UEEz30yo6c9SA7UR2v5Out1bD6p/FbWpK78Xiur/BUP VZsBdVFnWTuDcGg+oYsbo+avedFBbA6YWNY0n1xHHxhKWlC73/SY8eeNdCDSKdvgzczY rR/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702607989; x=1703212789; 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=cJew1Ut9h95zC9IuFrB6MxorQq1lAg1fBre3mDpboVk=; b=oJ+VU1Ru2Ow1SadVwrjXl8hvq7m/elIJHji84y2HNhHUHCpaq9Ojg/J6WYcgWMlpJW bWwRS2bPQ3ncZ2vpdnvsJ6CTmjL5501XNOHbF/R0YWDiukqqbCaVhAHAWeIS5PnRjr3z jhg27ZxGPNS9qyyBiAk5NkH1LI10ZeabuxRp372iHMnhjxoxeQwLIAgzBeDYpSAmCKnR tNo9KJxTP1yX3Nd9xuq7fdsOxP6iJSho5z+Dh6HRWSVQ5ZnGHUMVQ3UTVm7Y7I1XVFZ8 eG/3IYM7G+LWGSxK4uMoLqpVMjETihjJrQfELH4x2ILfHdXW6Jvj6VQeHlxxh9aMqNW0 EyLQ== X-Gm-Message-State: AOJu0Yz8YBlda+4NExuet3IwxIO/Qopnw3451IRkNd85Q3OJCp4ZVR/5 Ef8ag54aH7RT9RxKw5U1Phl1KvfA1tw= X-Received: by 2002:a05:6a21:998e:b0:190:24f9:d579 with SMTP id ve14-20020a056a21998e00b0019024f9d579mr15682414pzb.1.1702607988870; Thu, 14 Dec 2023 18:39:48 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id fk16-20020a056a003a9000b006d26920a11dsm1437987pfb.0.2023.12.14.18.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 18:39:48 -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 v3 5/5] gpiolib: cdev: improve documentation of get/set values Date: Fri, 15 Dec 2023 10:38:05 +0800 Message-Id: <20231215023805.63289-6-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231215023805.63289-1-warthog618@gmail.com> References: <20231215023805.63289-1-warthog618@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785314775920468545 X-GMAIL-MSGID: 1785314775920468545 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 b956664f8649..37794de691f3 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1383,9 +1383,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; } } @@ -1394,6 +1403,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; @@ -1414,6 +1424,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]); @@ -1439,14 +1450,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; } } @@ -1454,7 +1476,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;