From patchwork Thu Feb 8 09:58:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771408 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 0CE536BB43 for ; Thu, 8 Feb 2024 09:59:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386371; cv=none; b=dObDKiA/JzZEZE8xthNw9WL8UdPkkANz2uOUrGqqLr5j8J5EXY6gyFT+e1TblIB3z3LGhNXwuTq/PMydl4ZgodP7NDo/cTA7JhG6cOKcwd07tnCBRFAhVfrYJxu7khjr4zHescBTNKTp7nXCR4Zxhd6gSQOFP6o2DQgXn8oyqD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386371; c=relaxed/simple; bh=r7k4ThFPIPCjUTp97jNbdGjayzkEWqIqnJHfC4InmdU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Fz7vfXutTsmWa2qIi9DmcQeTUu7o1Fvd1rGMlcB1p9dqrTPTLp11+re5SlEuaP8xNCEQK0H3afX7V/4z8YBTcZmJqayK+EoYyT0tLZpMRPx7RHy8EPLutp089C+q02rI+a62grfY5ycYnZu3XK55I7pm2Vsbj/lMNanJ72wFJQI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=YSkCSXKc; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="YSkCSXKc" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4102eae7e6aso6152895e9.3 for ; Thu, 08 Feb 2024 01:59:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386367; x=1707991167; 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=3h4gjaBErcezBWvS2amIavTmPxCKq+fozW11laYHipY=; b=YSkCSXKc79FNb+prYA2dZLxyjdXDLi9+S9tPmkthcnR81KHdAMy/C/c/ZNJhkZAY4y 7CXWrHrtWTxw5NagrpVt7g094KwWBNjGHM8IDEnhIX4OflEyeZugDEMnAgVQHR3JueC6 iy97aBq96tvomAywDYvyV54jSqWqT+uK1dt2jqoSU7BeCMa5HYKJ3vqM64XZbmwbJXZW Cqu6ADMTZsVW+ZYbdowrO0qH8cSK10YxC5Nb61aOPZI9ZrrpLH1BiYMMQQBR0cLIqcDS PxWgqxY2HyQyc/rZdO0rwDqDFVP1hWBoXiRkxFnhGWKlBDsc9CGWthl34mEDSZ9s4N2q zPXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386367; x=1707991167; 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=3h4gjaBErcezBWvS2amIavTmPxCKq+fozW11laYHipY=; b=OpPsvT13VobEiXzKdzmpGynKbijXwDcQFUbp5kIKkBANfqdRaSpeixPUmPYLJIXEYU u6JaUdK+dzzRYux4hlhWwZm2KPOH0oZHZkQmb6FaA4v+9UwFmgaDWAtHCF9o/CLE/VgX lrSeQWHs4mHYX7mHe9T6Ml/EKJ0FAnufwnDKNA2i4dBuY6FSnGzKbdO5R7ntwZEkKmas N8BFyrPbKWQ0AuJwF1Df361M+Pit123fwV64+gU45s4hdcK4ggI9b/z5y/4sDrss3g7j xSd7VGWkwA9bA7K59KKEzzFRposWIZsk4RTP+LfA6GZjPVENUnbtU9xwflebo4ZzwdXu GqVg== X-Gm-Message-State: AOJu0YzyBmm+uyess2qFSSgmHDuWclxoweJCTfIQxPaMTY8ipA5Payf2 uSjUgDaCI1gAHpMGrswuRcKXkngxXvtNQNYJmykbrWRJe+TCpFKEAZ5RWbXwlGc= X-Google-Smtp-Source: AGHT+IHJkJ94IQ7PWLqxB/B6uFdyiIvTAXiyEB/Yhwl5pcr97TP4U/XK5DBNNor916s9aiQ2pkHWJg== X-Received: by 2002:a5d:5f8d:0:b0:33b:28c0:7c98 with SMTP id dr13-20020a5d5f8d000000b0033b28c07c98mr6267038wrb.61.1707386366835; Thu, 08 Feb 2024 01:59:26 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWWcL0nDh5pbSrUAT13GEBj1In0xAgGJ/LT0gPXmbIp5+nHc6DOzF3gsxtx2+sJYnYMx1kR9z04buwvUg09lJfAFYVs/BJPPkEXqVfBeMVmb1IcqOmAQ3HtGVBobKSYD495B9+DwTu4yeZEN53imeGymPEcuo0tY/hQdxiZnCRI/21wzdLqYuQp9MKprjVm+d9Ru6OSvnQyzZsuCTRXIAd9b7OU2qh2rq55HaT9yweqOC+8gG5X3iG4RiGwKVeSnVLmmria1LwTR2BUY6pYNf+o14ZoKzvLf5led4KACkSRrnQzP9QG/0Zoy4jVUqy8uIcT/H4HBLg6/uuwMFH4NW45GRLO1x83Ww== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:26 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 01/24] gpio: protect the list of GPIO devices with SRCU Date: Thu, 8 Feb 2024 10:58:57 +0100 Message-Id: <20240208095920.8035-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We're working towards removing the "multi-function" GPIO spinlock that's implemented terribly wrong. We tried using an RW-semaphore to protect the list of GPIO devices but it turned out that we still have old code using legacy GPIO calls that need to translate the global GPIO number to the address of the associated descriptor and - to that end - traverse the list while holding the lock. If we change the spinlock to a sleeping lock then we'll end up with "scheduling while atomic" bugs. Let's allow lockless traversal of the list using SRCU and only use the mutex when modyfing the list. While at it: let's protect the period between when we start the lookup and when we finally request the descriptor (increasing the reference count of the GPIO device) with the SRCU read lock. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 251 ++++++++++++++++++++++------------------- 1 file changed, 137 insertions(+), 114 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 3cad49363a72..181faeeceb3f 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -14,12 +15,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -81,7 +84,12 @@ DEFINE_SPINLOCK(gpio_lock); static DEFINE_MUTEX(gpio_lookup_lock); static LIST_HEAD(gpio_lookup_list); + LIST_HEAD(gpio_devices); +/* Protects the GPIO device list against concurrent modifications. */ +static DEFINE_MUTEX(gpio_devices_lock); +/* Ensures coherence during read-only accesses to the list of GPIO devices. */ +DEFINE_STATIC_SRCU(gpio_devices_srcu); static DEFINE_MUTEX(gpio_machine_hogs_mutex); static LIST_HEAD(gpio_machine_hogs); @@ -113,20 +121,16 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label) struct gpio_desc *gpio_to_desc(unsigned gpio) { struct gpio_device *gdev; - unsigned long flags; - spin_lock_irqsave(&gpio_lock, flags); - - list_for_each_entry(gdev, &gpio_devices, list) { - if (gdev->base <= gpio && - gdev->base + gdev->ngpio > gpio) { - spin_unlock_irqrestore(&gpio_lock, flags); - return &gdev->descs[gpio - gdev->base]; + scoped_guard(srcu, &gpio_devices_srcu) { + list_for_each_entry_srcu(gdev, &gpio_devices, list, + srcu_read_lock_held(&gpio_devices_srcu)) { + if (gdev->base <= gpio && + gdev->base + gdev->ngpio > gpio) + return &gdev->descs[gpio - gdev->base]; } } - spin_unlock_irqrestore(&gpio_lock, flags); - if (!gpio_is_valid(gpio)) pr_warn("invalid GPIO %d\n", gpio); @@ -282,7 +286,8 @@ static int gpiochip_find_base_unlocked(int ngpio) struct gpio_device *gdev; int base = GPIO_DYNAMIC_BASE; - list_for_each_entry(gdev, &gpio_devices, list) { + list_for_each_entry_srcu(gdev, &gpio_devices, list, + lockdep_is_held(&gpio_devices_lock)) { /* found a free space? */ if (gdev->base >= base + ngpio) break; @@ -354,23 +359,25 @@ static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev) { struct gpio_device *prev, *next; + lockdep_assert_held(&gpio_devices_lock); + if (list_empty(&gpio_devices)) { /* initial entry in list */ - list_add_tail(&gdev->list, &gpio_devices); + list_add_tail_rcu(&gdev->list, &gpio_devices); return 0; } next = list_first_entry(&gpio_devices, struct gpio_device, list); if (gdev->base + gdev->ngpio <= next->base) { /* add before first entry */ - list_add(&gdev->list, &gpio_devices); + list_add_rcu(&gdev->list, &gpio_devices); return 0; } prev = list_last_entry(&gpio_devices, struct gpio_device, list); if (prev->base + prev->ngpio <= gdev->base) { /* add behind last entry */ - list_add_tail(&gdev->list, &gpio_devices); + list_add_tail_rcu(&gdev->list, &gpio_devices); return 0; } @@ -382,11 +389,13 @@ static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev) /* add between prev and next */ if (prev->base + prev->ngpio <= gdev->base && gdev->base + gdev->ngpio <= next->base) { - list_add(&gdev->list, &prev->list); + list_add_rcu(&gdev->list, &prev->list); return 0; } } + synchronize_srcu(&gpio_devices_srcu); + return -EBUSY; } @@ -399,26 +408,21 @@ static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev) static struct gpio_desc *gpio_name_to_desc(const char * const name) { struct gpio_device *gdev; - unsigned long flags; + struct gpio_desc *desc; if (!name) return NULL; - spin_lock_irqsave(&gpio_lock, flags); - - list_for_each_entry(gdev, &gpio_devices, list) { - struct gpio_desc *desc; + guard(srcu)(&gpio_devices_srcu); + list_for_each_entry_srcu(gdev, &gpio_devices, list, + srcu_read_lock_held(&gpio_devices_srcu)) { for_each_gpio_desc(gdev->chip, desc) { - if (desc->name && !strcmp(desc->name, name)) { - spin_unlock_irqrestore(&gpio_lock, flags); + if (desc->name && !strcmp(desc->name, name)) return desc; - } } } - spin_unlock_irqrestore(&gpio_lock, flags); - return NULL; } @@ -752,7 +756,10 @@ static void gpiochip_setup_devs(void) struct gpio_device *gdev; int ret; - list_for_each_entry(gdev, &gpio_devices, list) { + guard(srcu)(&gpio_devices_srcu); + + list_for_each_entry_srcu(gdev, &gpio_devices, list, + srcu_read_lock_held(&gpio_devices_srcu)) { ret = gpiochip_setup_dev(gdev); if (ret) dev_err(&gdev->dev, @@ -817,7 +824,6 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, struct lock_class_key *request_key) { struct gpio_device *gdev; - unsigned long flags; unsigned int i; int base = 0; int ret = 0; @@ -883,49 +889,47 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gdev->ngpio = gc->ngpio; - spin_lock_irqsave(&gpio_lock, flags); - - /* - * TODO: this allocates a Linux GPIO number base in the global - * GPIO numberspace for this chip. In the long run we want to - * get *rid* of this numberspace and use only descriptors, but - * it may be a pipe dream. It will not happen before we get rid - * of the sysfs interface anyways. - */ - base = gc->base; - if (base < 0) { - base = gpiochip_find_base_unlocked(gc->ngpio); + scoped_guard(mutex, &gpio_devices_lock) { + /* + * TODO: this allocates a Linux GPIO number base in the global + * GPIO numberspace for this chip. In the long run we want to + * get *rid* of this numberspace and use only descriptors, but + * it may be a pipe dream. It will not happen before we get rid + * of the sysfs interface anyways. + */ + base = gc->base; if (base < 0) { - spin_unlock_irqrestore(&gpio_lock, flags); - ret = base; - base = 0; + base = gpiochip_find_base_unlocked(gc->ngpio); + if (base < 0) { + ret = base; + base = 0; + goto err_free_label; + } + + /* + * TODO: it should not be necessary to reflect the + * assigned base outside of the GPIO subsystem. Go over + * drivers and see if anyone makes use of this, else + * drop this and assign a poison instead. + */ + gc->base = base; + } else { + dev_warn(&gdev->dev, + "Static allocation of GPIO base is deprecated, use dynamic allocation.\n"); + } + + gdev->base = base; + + ret = gpiodev_add_to_list_unlocked(gdev); + if (ret) { + chip_err(gc, "GPIO integer space overlap, cannot add chip\n"); goto err_free_label; } - /* - * TODO: it should not be necessary to reflect the assigned - * base outside of the GPIO subsystem. Go over drivers and - * see if anyone makes use of this, else drop this and assign - * a poison instead. - */ - gc->base = base; - } else { - dev_warn(&gdev->dev, - "Static allocation of GPIO base is deprecated, use dynamic allocation.\n"); - } - gdev->base = base; - - ret = gpiodev_add_to_list_unlocked(gdev); - if (ret) { - spin_unlock_irqrestore(&gpio_lock, flags); - chip_err(gc, "GPIO integer space overlap, cannot add chip\n"); - goto err_free_label; } for (i = 0; i < gc->ngpio; i++) gdev->descs[i].gdev = gdev; - spin_unlock_irqrestore(&gpio_lock, flags); - BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); init_rwsem(&gdev->sem); @@ -1016,9 +1020,9 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, goto err_print_message; } err_remove_from_list: - spin_lock_irqsave(&gpio_lock, flags); - list_del(&gdev->list); - spin_unlock_irqrestore(&gpio_lock, flags); + scoped_guard(mutex, &gpio_devices_lock) + list_del_rcu(&gdev->list); + synchronize_srcu(&gpio_devices_srcu); err_free_label: kfree_const(gdev->label); err_free_descs: @@ -1057,6 +1061,11 @@ void gpiochip_remove(struct gpio_chip *gc) /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ gpiochip_sysfs_unregister(gdev); gpiochip_free_hogs(gc); + + scoped_guard(mutex, &gpio_devices_lock) + list_del_rcu(&gdev->list); + synchronize_srcu(&gpio_devices_srcu); + /* Numb the device, cancelling all outstanding operations */ gdev->chip = NULL; gpiochip_irqchip_remove(gc); @@ -1081,9 +1090,6 @@ void gpiochip_remove(struct gpio_chip *gc) dev_crit(&gdev->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); - scoped_guard(spinlock_irqsave, &gpio_lock) - list_del(&gdev->list); - /* * The gpiochip side puts its use of the device to rest here: * if there are no userspace clients, the chardev and device will @@ -1130,7 +1136,7 @@ struct gpio_device *gpio_device_find(void *data, */ might_sleep(); - guard(spinlock_irqsave)(&gpio_lock); + guard(srcu)(&gpio_devices_srcu); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->chip && match(gdev->chip, data)) @@ -4138,30 +4144,39 @@ static struct gpio_desc *gpiod_find_and_request(struct device *consumer, bool platform_lookup_allowed) { unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; - struct gpio_desc *desc; - int ret; - - desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, &flags, &lookupflags); - if (gpiod_not_found(desc) && platform_lookup_allowed) { - /* - * Either we are not using DT or ACPI, or their lookup did not - * return a result. In that case, use platform lookup as a - * fallback. - */ - dev_dbg(consumer, "using lookup tables for GPIO lookup\n"); - desc = gpiod_find(consumer, con_id, idx, &lookupflags); - } - - if (IS_ERR(desc)) { - dev_dbg(consumer, "No GPIO consumer %s found\n", con_id); - return desc; - } - /* - * If a connection label was passed use that, else attempt to use - * the device name as label + * scoped_guard() is implemented as a for loop, meaning static + * analyzers will complain about these two not being initialized. */ - ret = gpiod_request(desc, label); + struct gpio_desc *desc = NULL; + int ret = 0; + + scoped_guard(srcu, &gpio_devices_srcu) { + desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, + &flags, &lookupflags); + if (gpiod_not_found(desc) && platform_lookup_allowed) { + /* + * Either we are not using DT or ACPI, or their lookup + * did not return a result. In that case, use platform + * lookup as a fallback. + */ + dev_dbg(consumer, + "using lookup tables for GPIO lookup\n"); + desc = gpiod_find(consumer, con_id, idx, &lookupflags); + } + + if (IS_ERR(desc)) { + dev_dbg(consumer, "No GPIO consumer %s found\n", + con_id); + return desc; + } + + /* + * If a connection label was passed use that, else attempt to use + * the device name as label + */ + ret = gpiod_request(desc, label); + } if (ret) { if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) return ERR_PTR(ret); @@ -4730,61 +4745,69 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) } } +struct gpiolib_seq_priv { + bool newline; + int idx; +}; + static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) { - unsigned long flags; - struct gpio_device *gdev = NULL; + struct gpiolib_seq_priv *priv; + struct gpio_device *gdev; loff_t index = *pos; - s->private = ""; + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) + return NULL; - spin_lock_irqsave(&gpio_lock, flags); - list_for_each_entry(gdev, &gpio_devices, list) - if (index-- == 0) { - spin_unlock_irqrestore(&gpio_lock, flags); + s->private = priv; + priv->idx = srcu_read_lock(&gpio_devices_srcu); + + list_for_each_entry_srcu(gdev, &gpio_devices, list, + srcu_read_lock_held(&gpio_devices_srcu)) { + if (index-- == 0) return gdev; - } - spin_unlock_irqrestore(&gpio_lock, flags); + } return NULL; } static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) { - unsigned long flags; - struct gpio_device *gdev = v; - void *ret = NULL; + struct gpiolib_seq_priv *priv = s->private; + struct gpio_device *gdev = v, *next; - spin_lock_irqsave(&gpio_lock, flags); - if (list_is_last(&gdev->list, &gpio_devices)) - ret = NULL; - else - ret = list_first_entry(&gdev->list, struct gpio_device, list); - spin_unlock_irqrestore(&gpio_lock, flags); - - s->private = "\n"; + next = list_entry_rcu(gdev->list.next, struct gpio_device, list); + gdev = &next->list == &gpio_devices ? NULL : next; + priv->newline = true; ++*pos; - return ret; + return gdev; } static void gpiolib_seq_stop(struct seq_file *s, void *v) { + struct gpiolib_seq_priv *priv = s->private; + + srcu_read_unlock(&gpio_devices_srcu, priv->idx); + kfree(priv); } static int gpiolib_seq_show(struct seq_file *s, void *v) { + struct gpiolib_seq_priv *priv = s->private; struct gpio_device *gdev = v; struct gpio_chip *gc = gdev->chip; struct device *parent; if (!gc) { - seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, + seq_printf(s, "%s%s: (dangling chip)", + priv->newline ? "\n" : "", dev_name(&gdev->dev)); return 0; } - seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, + seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "", dev_name(&gdev->dev), gdev->base, gdev->base + gdev->ngpio - 1); parent = gc->parent; From patchwork Thu Feb 8 09:58:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771082 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 7B5E96BB5B for ; Thu, 8 Feb 2024 09:59:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386372; cv=none; b=TtBDCH7m2NMoAJCE/I16EME9YSeJ03I9I6WBqp2dnHLLhvFK9KeQDWdKuFlk3ApFeGQ0YYBaS+XPNSw5UYikzov7o+qQ+TOPaQYmcFgld+2wnNd1nh5lxUegsWW1fzuPuom79fjOk5hlot57zJ1Cs93Jmyk20551wr9tWjOtSQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386372; c=relaxed/simple; bh=OSHkv+WDnIBMeHzKm0e8BUcjA4xFxonb/t9zZHvq2fg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jxUU04BMGpD1EzthAi48ig22xEtRVlBPr1VBicA8GgLNSeqv3KaCKvliTbUGVcL2Ve2Dqr1gbvGrpE/6Nj/bcY1T+suLDniPQkR1AyZ0hlaZ0q0zuMDRxBdfjkXzL/QxSs03m7AFSMobOtmiuJsGds7YeQSo0bJJSZCVLSnngyY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=HfPIuBBh; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="HfPIuBBh" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40efcb37373so14659105e9.2 for ; Thu, 08 Feb 2024 01:59:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386368; x=1707991168; 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=fMiVAuZ1WGGYPqmPuwOhICMBuiJ5VewzakZD6uTNbY4=; b=HfPIuBBh5fW4MMcz3mhnDTcewJ94WMKSEyXhptEbglKzdzqBWf0IidlLbOBuFwa2YV 1Hhno5roKqOKXkdkrb4EL/3hb3Wa3gzV9VyL8Bpvtb/B+m9AvmirMEb15hKkgGbdzOC9 GNU/QYP8O9mtXkbFGOdBTAuXTtCHtIfSoWFI+sWTZiO7PQETNTYABBUAailIpMMilYnV i8wu+JpaDxEBkLHq3st5x805aWd3DbEK0RvCrM54Wg6+pjKWxdy5v4KKlS3HTj46buhC sqJBtMpS2PfddA8ypf77gah7Dkp23PpnhDa8MuiUKcoA85g20qzsOaG3mXCjHCRBs/Xf tnTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386368; x=1707991168; 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=fMiVAuZ1WGGYPqmPuwOhICMBuiJ5VewzakZD6uTNbY4=; b=q1nUAomc+A7lCBKh+JCJXy2ppSwHwjvVm9V/BBjWLLmW0JtbfPVDMlmycNxRHJnRMk 22dEHUjHhRERIEyvh3JhjJJg6c3nt0gBh3uUAEJVg4QIqi0d0wjUJJC1SmOD48SV73Qo DK+kEqrDZ1HMUKYvHvFuQg8uglB09eNo1N/h04oLDODKeDuOwKhVqeJM44vDNLA94nuW sD5qdwHmGJk972m44i5e1QJIhn4TDAX4hbsgsIyLeRpRfZAZHsgwM4SPFWJiERlylMtf OGsQFnlyyrwkKCqh8D78zo+4ZxzhwxMK1ToYV8YQzG5w9JmpeYcXBNd4I+NUvb72mNAq krWA== X-Gm-Message-State: AOJu0YzQTffEh86wGeRfqGSLAiNwK+Y2O9fhRderO7CXJby0gGWCTj8d S9/PbcO+t82W5EIh1euUaDJZ/lPRfSlRgd871eOyFqASsdRGqcVKKKuVZYiRSlI= X-Google-Smtp-Source: AGHT+IFKY3O5mYGZBarKGAA2ip5fNYWH8Q4SS7g8JpsrDPFWfBuHNCDxsh7VgMqSJfkI2Hm43xSQ0w== X-Received: by 2002:a05:600c:4f8e:b0:40e:7d8f:6b1a with SMTP id n14-20020a05600c4f8e00b0040e7d8f6b1amr7404387wmq.5.1707386367840; Thu, 08 Feb 2024 01:59:27 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV+4ZbHZkGaSWduDwdD4tSjMgQfmytcicjQyWOVJMfsVhBbdS07EJ5Jyg+6bYHGsLCynjXlc6Ac5n4G8bVXj+HIqWY7LZ6OCRPfaQ5Mb56ZlrFahhu9pAA9TECXvH0x+oBgGyvLVFdB5ZbOZFcVKrSJtTJ0To2SHKD9t4X57syWmIaFcFTqF1mde/ARz/HpzVrTSogao5VnnkRigh4AP87VHn4MCt4MVTpXynk+q+Z0VNc92rbGfGtpM/Pp8i0FnQHKZiPtYw8jsCRuybT8tO5ExhKvblRdZ7TwyK+5o9VXpzez5A6NQcR87XED83w/UQ/jf2avcC20qydRBNqpMUE07UaPPjqf7A== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:27 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 02/24] gpio: of: assign and read the hog pointer atomically Date: Thu, 8 Feb 2024 10:58:58 +0100 Message-Id: <20240208095920.8035-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski The device nodes representing GPIO hogs cannot be deleted without unregistering the GPIO chip so there's no need to serialize their access. However we must ensure that users can get the right address so write and read it atomically. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-of.c | 4 ++-- drivers/gpio/gpiolib.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index 77509aa19900..bd6b8702c790 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c @@ -800,7 +800,7 @@ static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) return ret; #ifdef CONFIG_OF_DYNAMIC - desc->hog = hog; + WRITE_ONCE(desc->hog, hog); #endif } @@ -848,7 +848,7 @@ static void of_gpiochip_remove_hog(struct gpio_chip *chip, struct gpio_desc *desc; for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED) - if (desc->hog == hog) + if (READ_ONCE(desc->hog) == hog) gpiochip_free_own_desc(desc); } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 181faeeceb3f..8ca9aebfa81f 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2330,7 +2330,7 @@ static bool gpiod_free_commit(struct gpio_desc *desc) clear_bit(FLAG_EDGE_FALLING, &desc->flags); clear_bit(FLAG_IS_HOGGED, &desc->flags); #ifdef CONFIG_OF_DYNAMIC - desc->hog = NULL; + WRITE_ONCE(desc->hog, NULL); #endif ret = true; } From patchwork Thu Feb 8 09:58:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771407 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 843AA6D1C4 for ; Thu, 8 Feb 2024 09:59:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386373; cv=none; b=EWgEJI2bGniBBJirx3DZfapuOLZ62hIgLEJcfOwhom0rFg+XXmOP/LFnIw8c2PqXWCGaWNyzrXy7c4RdNq8KYoJVHYLIHIWSgXMWxlQMTkJrqEw4HHhUXTmrP3tRHSso/jRheouSr4QjXiFwE9t+mRUZhw7y8QYbhd+grdjo+Vk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386373; c=relaxed/simple; bh=wKoekw2eVVFcj9oxq6vzdf2ok15iXRtTtNq2jliv4lA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fUp4nmJ+PJsVBReHHKFAl7+qSXIoGFTGE3m7feoY7QOPjKO/SOEuPeLr9sXwRCRfl6IYBqMRR5WadCvQMLb2iOqMminMgwwWp4veYDFrvUBT0rjIT1+J19UnNIJnpA7lhiqq4nd2m5smNBI5T+9Zjdj+M7pIg5dPFR3d5oETVdo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=UMW0K4MX; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="UMW0K4MX" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40ef64d8955so14647025e9.3 for ; Thu, 08 Feb 2024 01:59:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386369; x=1707991169; 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=ZZDQVAK/m+EwHZp2HJIrHIBUSEbpzq9pOlgL2Kpkkjg=; b=UMW0K4MX76FF137+ghy+Pa5MKiqGwWp74uK9iA+NRtO7fimCnUetr2FOrl8E0KLZ53 5kYrjLW01V2m+K6ZYTpC0bU8kpvrN6+PWh3oBfB85Kjqr4ZbZ0NwnIRB8aHiLANsNi4c prQMbbltm6w/ngwq7YUgcMep9fs3yYCpMr6PT09V6ZbMlb6DCIE0Ywmk/aWtmN1N8Kkl jZwyBVRfDo0b9Dnt2eZTB/G188g3+Yh3LeFulRFgS3u6PXjQLVMCXh9t1f8EBY2i0emD Ctla7mGMN7HaxxGnc4/hUpjajE3hgDNFA9bcEj08rmSfUiFQT1gqJR5lSC1/MfMzmJm7 W1jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386369; x=1707991169; 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=ZZDQVAK/m+EwHZp2HJIrHIBUSEbpzq9pOlgL2Kpkkjg=; b=b6il4kAuu0hcBHPNiq53Rp15O5SB/cbaTH1NfqiYliA0o3pKiQECTXexyt1sB3yy7X Spw2CsJap5s1bxMDIeBzWX0EvQ3czS/2UU03D8LSXE0LTJJ1AyOnaznxDtchBTFOq4Kf BWZOwL8uHsnQWpN50rI6rkZI3D1JZtLk+CfoPnJV9wlWH8mc30A0zhrdPzCUqhdJQpgv 55NZU6GxoqDnZfT0+lvZa+pZzzjY2NfYYmmtqFa665vPBx1Iq8lr+6U4oPGqHDCZ7rHd 6ULsozEDXEEW7oOAnH3eybMqoYmBhQC7jAlu63EehaHa1EZH7q8mdsNrrdBvxkMsG+v7 ZHfw== X-Gm-Message-State: AOJu0Yw/P9P8wFEy5wkziZ2LqMKuVBxjmJE+9h6U3mXaBtGGJShmq5+V f609ssNRsBNfN01isHUJqUqxJGTt4c3Gh6j9EElARXw2o4SZQ00iXaBGkEo1Y9A= X-Google-Smtp-Source: AGHT+IHpYgYU7LpZVBEQhxULZvRynnzmGlJbp9aR0ZlD9CxSRz8o4AhactP27KD5wlwIMpxoEwxffQ== X-Received: by 2002:a05:600c:5204:b0:40f:bdf3:3426 with SMTP id fb4-20020a05600c520400b0040fbdf33426mr7424055wmb.31.1707386368785; Thu, 08 Feb 2024 01:59:28 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWxd02syAtgcCCVmPOmILiczOck7r4jrJz2J9HCO/q4RHO91hHtAxO4GoOgh8MZ9LDjmrWHSOxtZnsjTvHBKOfBGXFszXpmwEz1jBAorJTRtmXCOaqd4mviRKWe9HoPPxeGuMGuWviDb3I64HE+rb8Brk+eO9lm9h6i/lLQYERHPeYfNnqOXJJr+a/S1dW+ZIocRiWid3Z1h7YOSGFSJMAnY/QSxDu6KY0Ikxrj/GoaRxWLZVjBFYbvGMTIBi55vtMpZD2PfvPg4nEvmHj4TtIK1fKTQ5oZlRAFm3nBXCsEcmLOYg6cdQ6eAILgGjPCLSDGdqtdyIuCkI/4twKEY732giHiZRCx0w== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:28 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 03/24] gpio: remove unused logging helpers Date: Thu, 8 Feb 2024 10:58:59 +0100 Message-Id: <20240208095920.8035-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski The general rule of the kernel is to not provide symbols that have no users upstream. Let's remove logging helpers that are not used anywhere. This will save us work later when we'll be modifying them to use the upcoming SRCU infrastructure. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.h | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index a4a2520b5f31..c3ae5bfa3f2e 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -219,31 +219,18 @@ static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) /* With descriptor prefix */ -#define gpiod_emerg(desc, fmt, ...) \ - pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ - ##__VA_ARGS__) -#define gpiod_crit(desc, fmt, ...) \ - pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ - ##__VA_ARGS__) #define gpiod_err(desc, fmt, ...) \ pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ ##__VA_ARGS__) #define gpiod_warn(desc, fmt, ...) \ pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ ##__VA_ARGS__) -#define gpiod_info(desc, fmt, ...) \ - pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ - ##__VA_ARGS__) #define gpiod_dbg(desc, fmt, ...) \ pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ ##__VA_ARGS__) /* With chip prefix */ -#define chip_emerg(gc, fmt, ...) \ - dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) -#define chip_crit(gc, fmt, ...) \ - dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) #define chip_err(gc, fmt, ...) \ dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) #define chip_warn(gc, fmt, ...) \ From patchwork Thu Feb 8 09:59:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771081 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 B917D6D1C7 for ; Thu, 8 Feb 2024 09:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386373; cv=none; b=Xyipb/NCoq7GWMJHg3Pse8R6a4jtKlOqDzGqO4Vn6B7NkkBAXFg+aDt8DdbvL0+zA0ktXug5l1fxERDhhvktNt0G69l9qaiNtOJJ0HLdZHyohtvgwp3JLIK/S+8cFOI98tk3NEUEmwAqSVmo10KbbEYzRGPcNVFwNk6bOt/BhAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386373; c=relaxed/simple; bh=6KkqrXGs7aycNx1Pt1Ey9nkvS1TWh574Lfn+nakYYbE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=krnZZUiw/nc5I9kSKDe3sBPlGIOBQCYq+nNtW5sdS0RsD6R7TLf1dbJ7LpdC8MF6D9oPb00QOPyjJBfa6AqD4cKAvDRMVsL1Z+KYnJ1rAlLd3tgGRErLDAnmChosMSGA7EQmGkHmwXANNuz8+aSBoh1eEWW8kXI/A00yJUr2Cj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=fIyq1VYF; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="fIyq1VYF" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4102f273c46so6001155e9.2 for ; Thu, 08 Feb 2024 01:59:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386370; x=1707991170; 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=1boCyVDUOl0s5Ne2S4ZzfbfT4tyxZK7BzbtLRid5RA4=; b=fIyq1VYFJI7cvyPQPdMO5MJQHDqa362juBQa7cxrESd76PXRx+EpV1uN3MLYXbZK36 4fLWeC7BkV2p6B5ir8BLT6N3TK1sAFXkZ9hFbG0UfwL3hMHrcf6+904drlj1EGnr8mLQ EH9o28yJsQis3t1RhHiAh0rhakt77gb+PH/F4Iq1AAA+rWBIxxsoSH9lMs9efiJvwSXN 4GReGopslVIVfSMMnJKcBPpxqgW9+L9LkedIngC5oG9FK4ihgYFTrG5UbokeR1Kf17JO aNqK/VuH3NrPOYxcqWBJoVPlIFy8D57AkW61RKLHPxaEmM5k6exAdqn2miNg1lnrwAAH emMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386370; x=1707991170; 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=1boCyVDUOl0s5Ne2S4ZzfbfT4tyxZK7BzbtLRid5RA4=; b=FmRlyp5sxdaLcKMSDg0MQpKckzQoX+EK8KQIFLsV1HXGQo3LoEdjnjvQ+37vo9TZ3x mqIx0N7ih2KjlLgPXI0Y/hegC7EbwYvIHhRNMMTOyPDPPgM7OF2ipszB4H2sNbh0fYAu 7wntEwgTi6kHgTMKqQtp3k5UIKZePocgZC/tDgGS1Ec5FuoySR8abD/6ZSUeHL7fNWJq Wqx+k3U23DYCT8+RQOqYBdX8w+VFP3cvBP9WbRUHLmqky2u4xFIeTrd6VrQD/6LTL4W/ yA1EgaPBV86Lgfr5ngMmt8+m74XxmXTOelHIqHbeG7fF5Qt8wpB8AM78eDVeMNoic9Gw mbng== X-Gm-Message-State: AOJu0YyvXAbba2bnS5ap27GRN1we8P+zTI8pt7B8qk6f396lk9HousEM HodMKTWY8ulZ/ej4ZicRFWQCmw7NHtNZqwecfmrpQ2OahCG4N6yCQvNGzl8ABHo= X-Google-Smtp-Source: AGHT+IFFjVS3YwVCuCcafKppGcchURFZOMOOvCIbiPgHcDeN2vkcCaZ2bLRdkf/VgJ0z60f3HMJ+4A== X-Received: by 2002:a5d:6745:0:b0:33b:1b0b:934a with SMTP id l5-20020a5d6745000000b0033b1b0b934amr5045544wrw.47.1707386370056; Thu, 08 Feb 2024 01:59:30 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUqKubOYfamGk7FwY+dOGwErNuYFvV3vdl+FBXnWHX0baBexvPKKbQVUdSe2WMmItVrRP6ihIscsP+WxONbrCmBWQR1L/5mA7w0Ykus5eWVhaNxoEV4xCrlltppGb9FRS9lDiqRdKzvXqpoB7tYYyJOqAaTVCwPunYjimZhOUxdxxL/N8TjIyTLTqdm7X+jPMBp9C04LCH8QL+4wa5D8VSaT8Bx8+rK7Ewqc5JYKax/ghM/nWY8Ov1RCh+rBFji1AXRfyQWAeGxjj+jJJ8t+5Y9U1CX1JfYxv328w/4ieJq9wAH0SvDmhqlPJBz0CJTwqXKa1b4vrqzhPBq41HUMNg8PcD6OHiwuw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:29 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 04/24] gpio: provide and use gpiod_get_label() Date: Thu, 8 Feb 2024 10:59:00 +0100 Message-Id: <20240208095920.8035-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We will soon serialize access to the descriptor label using SRCU. The write-side of the protection will require calling synchronize_srcu() which must not be called from atomic context. We have two irq helpers: gpiochip_lock_as_irq() and gpiochip_unlock_as_irq() that set the label if the GPIO is not requested but is being used as interrupt. They are called with a spinlock held from the interrupt subsystem. They must not do it if we are to use SRCU so instead let's move the special corner case to a dedicated getter. First: let's implement and use the getter where it's applicable. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 4 ++-- drivers/gpio/gpiolib.c | 9 +++++++-- drivers/gpio/gpiolib.h | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 34d6712fa07c..2c0a0700762d 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2305,8 +2305,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (desc->name) strscpy(info->name, desc->name, sizeof(info->name)); - if (desc->label) - strscpy(info->consumer, desc->label, + if (gpiod_get_label(desc)) + strscpy(info->consumer, gpiod_get_label(desc), sizeof(info->consumer)); dflags = READ_ONCE(desc->flags); diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 8ca9aebfa81f..8331fdbb6faa 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -105,6 +105,11 @@ static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc); static bool gpiolib_initialized; +const char *gpiod_get_label(struct gpio_desc *desc) +{ + return desc->label; +} + static inline void desc_set_label(struct gpio_desc *d, const char *label) { d->label = label; @@ -2391,7 +2396,7 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset) * * Until this happens, this allocation needs to be atomic. */ - label = kstrdup(desc->label, GFP_ATOMIC); + label = kstrdup(gpiod_get_label(desc), GFP_ATOMIC); if (!label) return ERR_PTR(-ENOMEM); @@ -4732,7 +4737,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags); active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags); seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n", - gpio, desc->name ?: "", desc->label, + gpio, desc->name ?: "", gpiod_get_label(desc), is_out ? "out" : "in ", value >= 0 ? (value ? "hi" : "lo") : "? ", is_irq ? "IRQ " : "", diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index c3ae5bfa3f2e..1058f326fe2b 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -208,6 +208,7 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); int gpiod_hog(struct gpio_desc *desc, const char *name, unsigned long lflags, enum gpiod_flags dflags); int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev); +const char *gpiod_get_label(struct gpio_desc *desc); /* * Return the GPIO number of the passed descriptor relative to its chip From patchwork Thu Feb 8 09:59:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771406 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 C53806DD02 for ; Thu, 8 Feb 2024 09:59:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386374; cv=none; b=aJV3CdWatWvoPoPGJExLCEVszTaC74ZaZQfjeVa3n8jrt3BNOTZdjIKKPpwIhhlDkHTYBkrF2zXA1GtetTaFUssBe9y9iYccYbN1pZbHxoWVuOJdRS8AT8rZzKXhs+HDYOIp+tx74r9bXhxumds5enJoWY8nC5reg8+W4FStBak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386374; c=relaxed/simple; bh=Kuqae0gdXHk5aC8U+Pwv7M6H8BEVB/GtYBC+v8Te/14=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lTUbkh23M5TCHeJMO4ZH2dwOSVFY8nQ71nqOOTqa0O/ikyZH6DVvm+z6UwQN/JENiMfxNrgTC1CzLxNmshf4y7lmTz9PEEExFRg/NrIy2WJ0HB2G8O82NF3B0+o7J8D7jYa2d66JNxZAmfokfkJVeuYcCEKfGjxtWYZdtMBjwHY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=srym3g2n; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="srym3g2n" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-3394bec856fso408833f8f.0 for ; Thu, 08 Feb 2024 01:59:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386371; x=1707991171; 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=e6r04RSjl4/daqm6lsDxW2iCDk2nYZSqX3jgNVAZeU4=; b=srym3g2n3+ZR/mbPSksrQLBWxf8uVAJOAQu0HxGQQvoJxFUoQEJ+7ty+6nP2s1bup5 rWA+YITSfpgHbYidrLmMQ9RS4luXy/kDGvEinbGEsyUZUMBBZsgK3iNK0MFgNSiSjJle TTnkxtBD2yLFkH2ngeN0lqXh6vwbOjAp1+F9ZF/wqliEkAkYtwf3YZV694mNa2G/0CU2 81aVPJ37VTAkBNQBN0B6IydtLZnlyS/4J4jAA16ekF4YZIy079WDFFZUEbXbh2m/z968 FMEB7ZiWKjY0NWhyVrZccemefuwpoPrfDisVOCYLLxQxpx8ojP+Njf8WYYaUD2D5WfOh nITw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386371; x=1707991171; 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=e6r04RSjl4/daqm6lsDxW2iCDk2nYZSqX3jgNVAZeU4=; b=waHqahos/3kObubB63gCEJs9wJkTNoDxUzLl67Wbf0TjBfAHHDvslfCpq4d/X2WIBa rMcLGTC5AtR0rfWTOkjuR7T079Yy2Ps7T1eXQZrAw3t2q7RAKVNI5OAUlemVch1iqYCN t66GzQHBdek0oRrp6LhiFxxx+Yt+TCqziG7qqXjeGuK1z6FibCuUyNZdxS+MYHwjhYTM 5cLrBvGld5s8kst9Uc+IjFOULrrOdkT+2OWO9m67H5jc+2u2t/0noa6qLNoE1HI6TK8H w/QavR/4CVFhZgx6zibO5WLUSCHPXEzlaqqpxGba3TRs4+lci8J5OCOXjhqDM3h3lvOl LZVg== X-Gm-Message-State: AOJu0Yw+/piBo6HX51+8ZPbAhU8H9SbhEa084EB958bba6tRafa6xQcA GhoNmoGMUI5NTY3ypL+2lAKtgWsmmf5P2+brUtmPSStkWKO5yrQFE13xnASq67o= X-Google-Smtp-Source: AGHT+IFOtHWICKtvyNPMeJ786BFtSWR9xtskxC0gNMqrrtAhNX4cwqS9VXDPoUv5Td1EP0YiZoPuog== X-Received: by 2002:adf:ab1c:0:b0:33b:5815:d51d with SMTP id q28-20020adfab1c000000b0033b5815d51dmr1158047wrc.18.1707386371124; Thu, 08 Feb 2024 01:59:31 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXSULCHDgvyBzEkqouxdHRn0SRlEPfz7l9jMxWvFWI4GSK/eogNZG1gnGUVLx0/9ut57wLF0exIXM7ze4ZWkrBNkKAMxyU1HZ/pAUYzaUo7T2kLXEpuQeJBeCy1oTn/R5Rg0/20zHk/RrCk7jtHSO2aGzzr5/ZRPDnNU9SevVQvk8G3hmJFMaP22iENxUhfCCoy9UaKS7cvK50tZSwG2iZkOgVIMDlZgvo7HjTytT5p0GE1BCZ1qhzWO2ZxJrLLIeJliMyak9r5PApZwJdkGbhwsPQHO7vd1oBIrfdN+GNiqg03YxvLMX/j0evthTdJsTawaKYgo1kDqG+5ouDpyXlwaxK9Auy3Wg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:30 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 05/24] gpio: don't set label from irq helpers Date: Thu, 8 Feb 2024 10:59:01 +0100 Message-Id: <20240208095920.8035-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We will soon serialize access to the descriptor label using SRCU. The write-side of the protection will require calling synchronize_srcu() which must not be called from atomic context. We have two irq helpers: gpiochip_lock_as_irq() and gpiochip_unlock_as_irq() that set the label if the GPIO is not requested but is being used as interrupt. They are called with a spinlock held from the interrupt subsystem. They must not do it if we are to use SRCU so instead let's move the special corner case to a dedicated getter. Don't actually set the label to "interrupt" in the above case but rather use the newly added gpiod_get_label() helper to hide the logic that atomically checks the descriptor flags and returns the address of a static "interrupt" string. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 8331fdbb6faa..fbfddc9860fd 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -107,7 +107,14 @@ static bool gpiolib_initialized; const char *gpiod_get_label(struct gpio_desc *desc) { - return desc->label; + unsigned long flags; + + flags = READ_ONCE(desc->flags); + if (test_bit(FLAG_USED_AS_IRQ, &flags) && + !test_bit(FLAG_REQUESTED, &flags)) + return "interrupt"; + + return test_bit(FLAG_REQUESTED, &flags) ? desc->label : NULL; } static inline void desc_set_label(struct gpio_desc *d, const char *label) @@ -3599,14 +3606,6 @@ int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset) set_bit(FLAG_USED_AS_IRQ, &desc->flags); set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); - /* - * If the consumer has not set up a label (such as when the - * IRQ is referenced from .to_irq()) we set up a label here - * so it is clear this is used as an interrupt. - */ - if (!desc->label) - desc_set_label(desc, "interrupt"); - return 0; } EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); @@ -3629,10 +3628,6 @@ void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset) clear_bit(FLAG_USED_AS_IRQ, &desc->flags); clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); - - /* If we only had this marking, erase it */ - if (desc->label && !strcmp(desc->label, "interrupt")) - desc_set_label(desc, NULL); } EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); From patchwork Thu Feb 8 09:59:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771080 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 13B5A6D1C7 for ; Thu, 8 Feb 2024 09:59:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386375; cv=none; b=kR6WVEq9qETko73Y22jxuLDNhH8e19YkdNNx1fbSkkOy1TBZBbbBUv9YErGxKqirbympISDH0uk3zuc38QBwGn0sN53kJfFaH0LgNHabXbzsUXTlt1Zp7zSsCeqni1ti0z2i7ddjm40qWgAP98HXpDexdNEX/0y/1+NPAi/U2SA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386375; c=relaxed/simple; bh=mlwjr0aacGOVjyJQQ9hj6Ih/Pg/kHJfwwqJwE2q8KBU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BpGAsXFtUuZCH+mX3af3xUK7xGzuUqMa6I8KzA9MgMaubrN+g/cMjGr2V365ySzxy6nJZI391FkEBYJqeNxkjbC1hQ5uWxoBoDze7bNVuyBDJAtDF+ZXGB3grr+QvC1B1gP4AXOOVGAX0qXIIzg7/i/x5ZzMepEy0aWBkqxH+wg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=tylBFRuz; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="tylBFRuz" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-40fd2f7ef55so3844535e9.0 for ; Thu, 08 Feb 2024 01:59:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386372; x=1707991172; 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=5qtGEKl9eZhP9SS39ifvqdCChUOXpZpW+8/W/M5yqYc=; b=tylBFRuzHSbUWsTv0e9E9FNZvWie+J0Srf1fJ3/RWJ2I8+Piz5q9QW2uMYDyn+gn4N 13hN6cfpwv0inlzzOKb+CTp0Yc2xkdJOcHxEgWblRleTtsB2WWYPIq/ViA8zQZI/D/E+ qOVutYFCnmz7PopXmLXscCfizKea6RkjIu7PI7SEk3Gi2j0O9Nta/6QAzsH/kUqAmckd 8sUuUlwdpIk5cSoYruLbRHzoaAgNCOmTcZSoUFS7TUSv46PZsMW3ipclOeVrv47cNk/z JpRHvJYvVzKMCbRNQKqGzIu3+DOsZmqkfXB/73MwYGHFtcTacoHT8G5kqT6AgN3iCOM4 VGAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386372; x=1707991172; 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=5qtGEKl9eZhP9SS39ifvqdCChUOXpZpW+8/W/M5yqYc=; b=c/eN58ey7IE6Q+F3l8sTm42QR8hihOBsxheQo04Tx8gGWyYr/vsEqWKfP6P6udrAco Ipz4v4by3hJz2M7OG9gbdgvvZFXX7ekeIALAVu6uavOJ7Gs+FJe1GWqVeK9+nKeEkFgM ATZF5gkaJOFkILT8a6dJWR6KfAVdYqkw1953ERcI+BUPGgKxOGnwNbfLyHOEn+k/hTs1 TYv63Yv9gT9S7nv/7GX+xwziDorFfjpmBX5DDOWPIAkMNtDPRCrw2hW9NcI/Cev+pJh5 SbMHu9YF5nWqofLaoMPLe0jAHtgj0YyxdRdX5FtXCqmXpW4ggfKyIbLLFIkiVcshApIa G70Q== X-Gm-Message-State: AOJu0Ywzn14Ed2Gr1N5nMldAFhkrv0HKYk7bNrFEzXpMYhRR+gTv+ETY XMXzw8TQVPIRtwmun5jqq7Gth25VHgJ1HqrPuAYX91vJEsrXr81GIdy/9DMPk3s= X-Google-Smtp-Source: AGHT+IF7xF1sC1zEruEl91ExQYdoLugNN6uIujhqm6wWGiVrarrH3BqhqlibKBwtT7ck2eBr9xIv7Q== X-Received: by 2002:a05:600c:519a:b0:410:1d09:10be with SMTP id fa26-20020a05600c519a00b004101d0910bemr1767956wmb.8.1707386372069; Thu, 08 Feb 2024 01:59:32 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCX3SpTRg+/wyeLk3YyvoNnZTfqs3AZl025KQLmo/ZdCl5S+qlFwDXk5yPhWn29Jks9DaHl2xGg2rCb1mAML2RCd12SPU5nFup02EGJJWEzzOCjKl43uLj4sGdTiI7bWCogDEl5AnUHYucjWeVsnTKbHjEixrlf5/BqZz5Nrc+o5NBpoWJVdX+QS3ogZQnqcW6r0xU1GI538oz+0B4JsDgz4JrHCAIy4RN2mryIk1mE8f4PJzLwORKTgcWdglqS9NJ7Z9DJaHJ+EqcSROwVAFNTfLE+tPZMKDonuuStdtggQYfIaIdl9CCzLdnHZs0xvv4oinXrrcGhGaNOHbK5fNOZQfyfwVJjVHw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:31 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 06/24] gpio: add SRCU infrastructure to struct gpio_desc Date: Thu, 8 Feb 2024 10:59:02 +0100 Message-Id: <20240208095920.8035-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Extend the GPIO descriptor with an SRCU structure in order to serialize the access to the label. Initialize and clean it up where applicable. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 18 ++++++++++++++++-- drivers/gpio/gpiolib.h | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index fbfddc9860fd..b26d39bc3389 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -672,6 +672,10 @@ EXPORT_SYMBOL_GPL(gpiochip_line_is_valid); static void gpiodev_release(struct device *dev) { struct gpio_device *gdev = to_gpio_device(dev); + unsigned int i; + + for (i = 0; i < gdev->ngpio; i++) + cleanup_srcu_struct(&gdev->descs[i].srcu); ida_free(&gpio_ida, gdev->id); kfree_const(gdev->label); @@ -836,7 +840,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, struct lock_class_key *request_key) { struct gpio_device *gdev; - unsigned int i; + unsigned int i, j; int base = 0; int ret = 0; @@ -970,6 +974,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, for (i = 0; i < gc->ngpio; i++) { struct gpio_desc *desc = &gdev->descs[i]; + ret = init_srcu_struct(&desc->srcu); + if (ret) { + for (j = 0; j < i; j++) + cleanup_srcu_struct(&gdev->descs[j].srcu); + goto err_remove_of_chip; + } + if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { assign_bit(FLAG_IS_OUT, &desc->flags, !gc->get_direction(gc, i)); @@ -981,7 +992,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, ret = gpiochip_add_pin_ranges(gc); if (ret) - goto err_remove_of_chip; + goto err_cleanup_desc_srcu; acpi_gpiochip_add(gc); @@ -1020,6 +1031,9 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gpiochip_irqchip_free_valid_mask(gc); err_remove_acpi_chip: acpi_gpiochip_remove(gc); +err_cleanup_desc_srcu: + for (i = 0; i < gdev->ngpio; i++) + cleanup_srcu_struct(&gdev->descs[i].srcu); err_remove_of_chip: gpiochip_free_hogs(gc); of_gpiochip_remove(gc); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 1058f326fe2b..6e14b629c48b 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -17,6 +17,7 @@ #include #include #include +#include #define GPIOCHIP_NAME "gpiochip" @@ -147,6 +148,7 @@ 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) + * @srcu: SRCU struct protecting the label pointer. * * These are obtained using gpiod_get() and are preferable to the old * integer-based handles. @@ -184,6 +186,7 @@ struct gpio_desc { #ifdef CONFIG_OF_DYNAMIC struct device_node *hog; #endif + struct srcu_struct srcu; }; #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) From patchwork Thu Feb 8 09:59:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771405 Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (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 019906A8A1 for ; Thu, 8 Feb 2024 09:59:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386377; cv=none; b=moc6NvM7j/h4Q9tXOATNuh35JD23WUal5wlymYecGIleIdDbqp+DvEGT2b6CmApAr4xHaFxbdAftbV7rnUIOWh6O0USI6/vpb/WaSff2sLYTh2OdJoEwxj1PdPh1c6HmCl1/WCaBMY1D7YnGTM8sg+bFUKI/cu/Nvq1Ol6j87Pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386377; c=relaxed/simple; bh=sbeEFCjaW/O4DVCAwoiNebLnpHebEBuzjUexVdu18I8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NmhF5KaeQb4qd5/eCQ48lkhT2RVvbTU9JTBqV7yE0WspejBGcsWqrtS+gnW+lCckjgvSljUybroJN6ITHIfvHQqzOceGdPYOSrOohXQsJ1CsF1E3A9Xe2quTFBcCL/CVk6bLmM6Z7WARITVP4lu3Lht+4xGuMP+VXtNr/LcfSKs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=DN/ALtsm; arc=none smtp.client-ip=209.85.167.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="DN/ALtsm" Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-5116588189aso2782293e87.1 for ; Thu, 08 Feb 2024 01:59:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386373; x=1707991173; 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=Mk8/t4lpC1U+h34P66Tm04NaklyP+2BT7bb9Fj0gLH4=; b=DN/ALtsmGpnfp6MnozgRlTw8+p7hlPsUi4FiB6yHlxY2fByVJWrm5TVu8KvvWbg1LV bm6CEQcvrgV0E+lfqUUJ17rUhB7xW83tC/BLrr56L4WeP2J6KL91Lg37mSxe/Zy7G4sd kMMjDjY89OhXOUL9AmbYs57LizbWebD+odxNvB5tL9wdkwYQsudEg5QqAm6gJ/eBmF2Z 7eQZbjwVuVaTrhbvBW6xjNc9HQysG0JmCK1ObRytP6u3uud1uanWy/5r9VG5DEsLILU1 ms0fpRAChQpwdb3ZH0xH/5MZ41WO8Qbcd3irTKOSEfLNk+K6t7rit2RIZR4j0pP6TQ+B TrJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386373; x=1707991173; 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=Mk8/t4lpC1U+h34P66Tm04NaklyP+2BT7bb9Fj0gLH4=; b=lU2lGFGuIrXwZIvXWhsJdsIdr0Yo30u8PMdQPKC7cLiROARuWc5ds0P7x4JiBbxHJ4 8iLnpXaGZVrztaU1EMUvuFh29KbRESb/KsBCWno7bCGf4NxnbOjOBOdXJ66gSLLM3kwB RqQTpTDyhnseRT6Zy0dfwH2wiY64VuzPS5vsJo95dD7IecXRPUGcovQKylj6fzMXwBCP 0Ymu3tCQH+M1z1zbQxWvP62MHafexAURoHee+Tq754xa+Gn+l+LCnv0bM9kaTULYRdG6 Qkp1z27PL+2XscRXBzxkurEtekl1PeqQ/ksEMRY+tdE6O7UpZlXB6rkqnew9t3LTrnAH C0GQ== X-Gm-Message-State: AOJu0YzCh4B+YH16OFnYscsCiDxTCpm+FJgpyR7pvfMgMvllRlzdoaVe yTgVSsnOKd+8EYvdOLVdb20jAWT1DVJOZvBPuA6P/n1HH2IG00+mnS6Cqw7ysg4= X-Google-Smtp-Source: AGHT+IF6fRp5/NHFrkShluMjplKtXhZ9HNS6Xtid9aBfV69DG+FErqtNw2vxAvZAgYE4y/y6AgOs3A== X-Received: by 2002:a05:6512:b88:b0:511:54ab:9bed with SMTP id b8-20020a0565120b8800b0051154ab9bedmr7080929lfv.2.1707386373063; Thu, 08 Feb 2024 01:59:33 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWRljNZ8szBqDwxNd0CM2I2xNoQaylNLB28T9chIG/h2gQxNAq5f9dxmB8Um+x1TV+v4cSc660TokB8uXinEGSxMY4cARwOIznlCFIaPIjgP0nqFpENQ37u6d3igm0Jf3YyaeDSULJV8e65QVb+j36yOXCK8HHtSaoCqRg7+Ws29s2X3vOBroBiJfsr304tlboYQwgygVomzQ6KX/2lDsrJnxKbWscjQtBZ8aW0aa00ciLLfp+PDjOsa7aIBN2i1E7CydvT3J4V+1TAGS6Ceoz0+iQpgAs9xyTjY6W32NDPFAneYYtZXp27E2W8pTCEXC99IYD1PzHX42XAC7gfHvsSZKk3OsV5zQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:32 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 07/24] gpio: protect the descriptor label with SRCU Date: Thu, 8 Feb 2024 10:59:03 +0100 Message-Id: <20240208095920.8035-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski In order to ensure that the label is not freed while it's being accessed, let's protect it with SRCU and synchronize it everytime it's changed. Let's modify desc_set_label() to manage the memory used for the label as it can only be freed once synchronize_srcu() returns. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 10 +++++--- drivers/gpio/gpiolib.c | 47 +++++++++++++++++++++++-------------- drivers/gpio/gpiolib.h | 34 +++++++++++++++++++-------- 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 2c0a0700762d..75f4912339a6 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2297,6 +2297,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, { struct gpio_chip *gc = desc->gdev->chip; unsigned long dflags; + const char *label; memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); @@ -2305,9 +2306,12 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (desc->name) strscpy(info->name, desc->name, sizeof(info->name)); - if (gpiod_get_label(desc)) - strscpy(info->consumer, gpiod_get_label(desc), - sizeof(info->consumer)); + scoped_guard(srcu, &desc->srcu) { + label = gpiod_get_label(desc); + if (label) + strscpy(info->consumer, label, + sizeof(info->consumer)); + } dflags = READ_ONCE(desc->flags); } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index b26d39bc3389..a87ab8f013c4 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -114,12 +114,26 @@ const char *gpiod_get_label(struct gpio_desc *desc) !test_bit(FLAG_REQUESTED, &flags)) return "interrupt"; - return test_bit(FLAG_REQUESTED, &flags) ? desc->label : NULL; + return test_bit(FLAG_REQUESTED, &flags) ? + rcu_dereference(desc->label) : NULL; } -static inline void desc_set_label(struct gpio_desc *d, const char *label) +static int desc_set_label(struct gpio_desc *desc, const char *label) { - d->label = label; + const char *new = NULL, *old; + + if (label) { + /* FIXME: make this GFP_KERNEL once the spinlock is out. */ + new = kstrdup_const(label, GFP_ATOMIC); + if (!new) + return -ENOMEM; + } + + old = rcu_replace_pointer(desc->label, new, 1); + synchronize_srcu(&desc->srcu); + kfree_const(old); + + return 0; } /** @@ -2229,9 +2243,7 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) * before IRQs are enabled, for non-sleeping (SOC) GPIOs. */ - if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { - desc_set_label(desc, label ? : "?"); - } else { + if (test_and_set_bit(FLAG_REQUESTED, &desc->flags)) { ret = -EBUSY; goto out_free_unlock; } @@ -2259,6 +2271,13 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) spin_lock_irqsave(&gpio_lock, flags); } spin_unlock_irqrestore(&gpio_lock, flags); + + ret = desc_set_label(desc, label ? : "?"); + if (ret) { + clear_bit(FLAG_REQUESTED, &desc->flags); + return ret; + } + return 0; out_free_unlock: @@ -2343,8 +2362,6 @@ static bool gpiod_free_commit(struct gpio_desc *desc) gc->free(gc, gpio_chip_hwgpio(desc)); spin_lock_irqsave(&gpio_lock, flags); } - kfree_const(desc->label); - desc_set_label(desc, NULL); clear_bit(FLAG_ACTIVE_LOW, &desc->flags); clear_bit(FLAG_REQUESTED, &desc->flags); clear_bit(FLAG_OPEN_DRAIN, &desc->flags); @@ -2362,6 +2379,7 @@ static bool gpiod_free_commit(struct gpio_desc *desc) } spin_unlock_irqrestore(&gpio_lock, flags); + desc_set_label(desc, NULL); gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED); return ret; @@ -2409,6 +2427,8 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset) if (!test_bit(FLAG_REQUESTED, &desc->flags)) return NULL; + guard(srcu)(&desc->srcu); + /* * FIXME: Once we mark gpiod_direction_input/output() and * gpiod_get_direction() with might_sleep(), we'll be able to protect @@ -3520,16 +3540,8 @@ EXPORT_SYMBOL_GPL(gpiod_cansleep); int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) { VALIDATE_DESC(desc); - if (name) { - name = kstrdup_const(name, GFP_KERNEL); - if (!name) - return -ENOMEM; - } - kfree_const(desc->label); - desc_set_label(desc, name); - - return 0; + return desc_set_label(desc, name); } EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); @@ -4739,6 +4751,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) int value; for_each_gpio_desc(gc, desc) { + guard(srcu)(&desc->srcu); if (test_bit(FLAG_REQUESTED, &desc->flags)) { gpiod_get_direction(desc); is_out = test_bit(FLAG_IS_OUT, &desc->flags); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 6e14b629c48b..d2e73eea9e92 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -180,7 +180,7 @@ struct gpio_desc { #define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */ /* Connection label */ - const char *label; + const char __rcu *label; /* Name of the GPIO */ const char *name; #ifdef CONFIG_OF_DYNAMIC @@ -223,15 +223,29 @@ static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) /* With descriptor prefix */ -#define gpiod_err(desc, fmt, ...) \ - pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ - ##__VA_ARGS__) -#define gpiod_warn(desc, fmt, ...) \ - pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ - ##__VA_ARGS__) -#define gpiod_dbg(desc, fmt, ...) \ - pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ - ##__VA_ARGS__) +#define gpiod_err(desc, fmt, ...) \ +do { \ + scoped_guard(srcu, &desc->srcu) { \ + pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ + gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \ + } \ +} while (0) + +#define gpiod_warn(desc, fmt, ...) \ +do { \ + scoped_guard(srcu, &desc->srcu) { \ + pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ + gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \ + } \ +} while (0) + +#define gpiod_dbg(desc, fmt, ...) \ +do { \ + scoped_guard(srcu, &desc->srcu) { \ + pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ + gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \ + } \ +} while (0) /* With chip prefix */ From patchwork Thu Feb 8 09:59:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771079 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 D58516F080 for ; Thu, 8 Feb 2024 09:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386377; cv=none; b=ayt4dDUxx9P3fVIVBkH4C2NBP0RdsAIIbvPuZtIoWyBdl7GQD6Au4I1ya7Z2xKJS1zslEEa0gj6THFeK9JTf+1iZZ9oGErtRlV7DJXAkHMGm0QlCw5x6X+8CDnnHE05n4m1972iKh1kKqX/npR/zdUb9BIQp5VneD/11jUihpS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386377; c=relaxed/simple; bh=aksQxZZdyKnID/LS00ptyFtfb3SKVLVyZGehn3vT7g8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nz/zoJJaNIzMyF1RwNz08C/3ieGUTvdkXSmMbafAq5LKy/Nshw04IItE1Myy5b3HPJasFs6bSYFrqJdaBM+mwMYXAInHXjOtPItbhm4J5Hs8lU5wqG6TkTmJCZV2YWSz1D+Icue6Twudd1GI/oDbfCC89WzH7GikCwnhJ7nmdQM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=Z0PTXnye; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="Z0PTXnye" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4101d4c5772so4012635e9.0 for ; Thu, 08 Feb 2024 01:59:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386374; x=1707991174; 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=uWH2EaA/vWH0UVT8sf474qxL3ezIh0GZukTXWIvrUrM=; b=Z0PTXnyeWlKAzP3q4PLvK8C8gWoJhqTCMM9bawlwKFaxytVBryNfdPhqi6zRijmjZZ 7rY4Ra5FzOqe3YYyqFggVpJIshQXbhnguMQM3jUlykMLkg6KE2ZkiHSFPSFe8RkRqA1C l9ZczCX+amk25/WLGBFEiY2vo6sXFX+CxTb6mvjSAXe7PvQ22d4/Rw4T4csBqBJrr+l+ OMFtfux+5U3E8Umvu1egT3CHAqy6pLeQHgCoWimpG+rjFYXMMoGrCu4lU8i5Z43cFA31 pObzKM5aQDU5CcIVseYtRXoKJdjgoURaJf3YzrxVhMYbOnuiz5HVUTomdGDTXI3cYY1B m7tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386374; x=1707991174; 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=uWH2EaA/vWH0UVT8sf474qxL3ezIh0GZukTXWIvrUrM=; b=o+P8z4jqc/DNzrwASaiyxGTjcmQUh13ERc419K0oXlTDfvjZLnvZ6aKKH2cwMAdwOu y1KQuP3fh4YymxzZl/UYwbVnXCGpF6vZyRozIi3Mgo/Duwk3KVE1CwzaSoK4ZI+286s7 kPBbLMtRv7lIzAsZ6lnPTRrbufR/vLqcE1e1CaIuhJANBbGBzMGSJuKejwY/KE2Gsgkb Xk8LeXDiDfPFUpKtNGQDKnKtHsm0kyaRA3KpEYA1gk/E4ybpL1rzYwCY9/wutCU1m+51 d69IuROhDmzubwdhCReIPBJO8jidzhCGETjCxOBvXsfr0CDJxiCrxA0+Ss+Vd9xht+YF PDaA== X-Gm-Message-State: AOJu0YxelpJknLle7TJSFMdYBSdMB4giUGz8OV6IKFiIzxeCkBvhreYO NpUtPqd+bis9l840J/FsMIfLui4YXIxXqExw2N3jNOKlPN/P8ZnqQWNKqzcLp4U= X-Google-Smtp-Source: AGHT+IHSEx7dYDU6Nm8+6fF/5ptWjkqc7rf/FtoJps39u3rYAPUJYuFiAeqfbcA89AZ37q2l4wQJPg== X-Received: by 2002:a05:600c:4f0f:b0:40f:c5cd:374f with SMTP id l15-20020a05600c4f0f00b0040fc5cd374fmr1621789wmq.15.1707386374105; Thu, 08 Feb 2024 01:59:34 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXJejhuJw/mzF8+wEJoMlasnNl3EIKalgHU04nsbazGXhlYshC5tGYg2Oh+ZXT3o0ygxp8peg8kg00PW1uk2o+SKXx2a/pP8vyesNFt20ynEcWHrRzeyA4UYmYWKctxWI8aKpnsxhzSbRSzegsFRCH5KiNOQnJfapq7liqzFedcUjvOONi443PeEtWarzVwW+leCD23c8WGVlouyBcegj1r8JDlwc4eIdWae5JBjw+b5FMvrk3SAndxKy4cxCG2qinQiAlR0f5D46L/viEEfbos6VWNfPHaOeqA3PGjBO/C1us9nyClfx8y8+pwcATUsVda7Em8oyQ7AuPVGsyB41xiNvKd12HeiQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:33 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 08/24] gpio: sysfs: use gpio_device_find() to iterate over existing devices Date: Thu, 8 Feb 2024 10:59:04 +0100 Message-Id: <20240208095920.8035-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski With the list of GPIO devices now protected with SRCU we can use gpio_device_find() to traverse it from sysfs. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-sysfs.c | 46 ++++++++++++++++++------------------ drivers/gpio/gpiolib.c | 2 +- drivers/gpio/gpiolib.h | 1 - 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 6bf5332136e5..d6ff044ce887 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -790,11 +790,29 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev) } } +/* + * We're not really looking for a device - we just want to iterate over the + * list and call this callback for each GPIO device. This is why this function + * always returns 0. + */ +static int gpiofind_sysfs_register(struct gpio_chip *gc, void *data) +{ + struct gpio_device *gdev = gc->gpiodev; + int ret; + + if (gdev->mockdev) + return 0; + + ret = gpiochip_sysfs_register(gdev); + if (ret) + chip_err(gc, "failed to register the sysfs entry: %d\n", ret); + + return 0; +} + static int __init gpiolib_sysfs_init(void) { - int status; - unsigned long flags; - struct gpio_device *gdev; + int status; status = class_register(&gpio_class); if (status < 0) @@ -806,26 +824,8 @@ static int __init gpiolib_sysfs_init(void) * We run before arch_initcall() so chip->dev nodes can have * registered, and so arch_initcall() can always gpiod_export(). */ - spin_lock_irqsave(&gpio_lock, flags); - list_for_each_entry(gdev, &gpio_devices, list) { - if (gdev->mockdev) - continue; + (void)gpio_device_find(NULL, gpiofind_sysfs_register); - /* - * TODO we yield gpio_lock here because - * gpiochip_sysfs_register() acquires a mutex. This is unsafe - * and needs to be fixed. - * - * Also it would be nice to use gpio_device_find() here so we - * can keep gpio_chips local to gpiolib.c, but the yield of - * gpio_lock prevents us from doing this. - */ - spin_unlock_irqrestore(&gpio_lock, flags); - status = gpiochip_sysfs_register(gdev); - spin_lock_irqsave(&gpio_lock, flags); - } - spin_unlock_irqrestore(&gpio_lock, flags); - - return status; + return 0; } postcore_initcall(gpiolib_sysfs_init); diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index a87ab8f013c4..68543ab34fb6 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -85,7 +85,7 @@ DEFINE_SPINLOCK(gpio_lock); static DEFINE_MUTEX(gpio_lookup_lock); static LIST_HEAD(gpio_lookup_list); -LIST_HEAD(gpio_devices); +static LIST_HEAD(gpio_devices); /* Protects the GPIO device list against concurrent modifications. */ static DEFINE_MUTEX(gpio_devices_lock); /* Ensures coherence during read-only accesses to the list of GPIO devices. */ diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index d2e73eea9e92..2bf3f9e13ae4 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -136,7 +136,6 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep, int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); extern spinlock_t gpio_lock; -extern struct list_head gpio_devices; void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); From patchwork Thu Feb 8 09:59:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771404 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 ACAFC6F506 for ; Thu, 8 Feb 2024 09:59:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386378; cv=none; b=augoSQ6bwSmzjuaAP/zZsKYivi9z2+c0mBd2W6XU2OPnrxm6PlJxr21tHrqOcp7YEtLC+HGt/MOHsCtODyTlOJzYQfnBUQRLVQVUHXfV4sx6pQPeTW9xnnEANDJ5xJBvLkujVDnPiobXNIBLe2qWe2EHESW9Qqhdp7ZrAfUAI9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386378; c=relaxed/simple; bh=nqqbS2qohTq/5yvdbqYSBh0+77NQz8ZBBvynqMCXiJU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V2y4vqRoURca0x91ueG1CFvj2Sdeq781dclAfFi3TlwLihjkmAfXZ5Bl27okBHn/qLhAfL9dtWoW/cSfOTog+bHoDRqLjefg72WffXYzc66LQwF6nYMUlrE+hJeo7oF/MBna8Dxz/L9wRxwTkKhoVXHtogNJJcssvrhMQDG1yos= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=E5uVvmAv; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="E5uVvmAv" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-33b58ddefd3so285502f8f.0 for ; Thu, 08 Feb 2024 01:59:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386375; x=1707991175; 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=tw6Pf3/mNBAClq4IyQDOFbA2QhsVTUfszg40T5HZdL0=; b=E5uVvmAvitzHvkO9C3USCr3Cyi7jMj5LFWtCZWON5FRl38jy4tl82SIxWApvP/t452 rIHFN6U94uxS+LJhuoEwBPlttxtPtCjStZ5dyCxcnCW3zDnIvl5mAEFVb9EIsR3kE/wH 5R1/ewhfZ1gykCEgxSpN1jYfChDWOeJI5M7XNjRXNbS2DAqS41xVSu780ZnClG8hCaA0 VpS7259Gu6YAqvuEVnPGqLPldSAUJj1m/ax3rw3+12ZxFqZK+O/D6h6DMTw8JK+ZDkuo eoinlT1bbwb2xr9oDBBogofaArxwrL5mPsxUVHv/abnzdjihFv7d1QAS9wB0fHtPiZCx mK4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386375; x=1707991175; 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=tw6Pf3/mNBAClq4IyQDOFbA2QhsVTUfszg40T5HZdL0=; b=usjIFLeHzMpxqeWnHQjl96OIeUM4gRZm3jCmod5epU1h8Ra/VuBqeDSabyT9VciIDc IzsFnSUs/wnX0sQJLSTMiKANaQnslk2OJEpO6tRwKWQEDLysarUXzIeh7V5LC1Lh/gI1 9j4g5VpKI7vA8H3g5wuzmxw/Pbe8F94ia62rB4vHt+QW5iUCvrK2C8YafGGynoSWO2kV yUjMIlaJrv2I+XGLElkHlPDM9820jBAYdmNXqooNxPWZcCT2hzZW8C88rmFpSis2dna/ r36ITDM4LGKeZKsImwjtJmaqzPSkozNZy2D9bOAe6y6a3KWFZ8CT1ytFx8b6QoWS/FwR KE1w== X-Gm-Message-State: AOJu0YxxziaJEL6JFdXDYfp0OMQePF3pW7MIJ2BoiHu5dVC7R2dO5VtY 7jMucp8XF7cCSrArAGn8wc0iljkXAfkpaBzjSB532YHD5wvIFCYcBOVWxjDY6rQ= X-Google-Smtp-Source: AGHT+IF7aSliF1bS/hoW00jA8rqVHoDWdBy6QKI5zrq1nPbWER85XxobwzkHjUYJqd2O5MsKxbjYrQ== X-Received: by 2002:a5d:5889:0:b0:33b:4ec0:8159 with SMTP id n9-20020a5d5889000000b0033b4ec08159mr3704032wrf.33.1707386375106; Thu, 08 Feb 2024 01:59:35 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXpQA+i2qmzFmk802gcTHwt9lCaXpxug5opoef4pZAfxJxL2PrfaIZrPfJA2j4rES3gdsyGg0R7/vd3hzZ5UxcpsbioddUNDek8ObF1zOT7OJLGB858Fy58s5EzgxU1MVt6gsa2josB4aMEP6cxpMyMFUmsW+859/TkYuEAaXQ1zGCqYC38ZPNWU/diplYEBSaH7qtPazxMjGRnCJKsW7QbUHV9xVhQUqIsfGs91NW+d5BrMvmV+2utdJmsft9x1aEnzlv+GxfnSisBOo5lnes//tr3yiHuJYpkaofOY3KFzogxUx/TfZocgPLsd+id3+V3/LdGLz8Dq/aevvfZpNrSUNf0AP/i4A== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:34 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 09/24] gpio: remove gpio_lock Date: Thu, 8 Feb 2024 10:59:05 +0100 Message-Id: <20240208095920.8035-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski The "multi-function" gpio_lock is pretty much useless with how it's used in GPIOLIB currently. Because many GPIO API calls can be called from all contexts but may also call into sleeping driver callbacks, there are many places with utterly broken workarounds like yielding the lock to call a possibly sleeping function and then re-acquiring it again without taking into account that the protected state may have changed. It was also used to protect several unrelated things: like individual descriptors AND the GPIO device list. We now serialize access to these two with SRCU and so can finally remove the spinlock. There is of course the question of consistency of lockless access to GPIO descriptors. Because we only support exclusive access to GPIOs (officially anyway, I'm looking at you broken GPIOD_FLAGS_BIT_NONEXCLUSIVE bit...) and the API contract with providers does not guarantee serialization, it's enough to ensure we cannot accidentally dereference an invalid pointer and that the state we present to both users and providers remains consistent. To achieve that: read the flags field atomically except for a few special cases. Read their current value before executing callback code and use this value for any subsequent logic. Modifying the flags depends on the particular use-case and can differ. For instance: when requesting a GPIO, we need to set the REQUESTED bit immediately so that the next user trying to request the same line sees -EBUSY. While at it: the allocations that used GFP_ATOMIC until this point can now switch to GFP_KERNEL. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 18 +++--- drivers/gpio/gpiolib-sysfs.c | 17 ++---- drivers/gpio/gpiolib.c | 106 +++++++++++------------------------ drivers/gpio/gpiolib.h | 2 - 4 files changed, 46 insertions(+), 97 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 75f4912339a6..3588aaf90e45 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2302,18 +2302,16 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); - scoped_guard(spinlock_irqsave, &gpio_lock) { - if (desc->name) - strscpy(info->name, desc->name, sizeof(info->name)); + if (desc->name) + strscpy(info->name, desc->name, sizeof(info->name)); - scoped_guard(srcu, &desc->srcu) { - label = gpiod_get_label(desc); - if (label) - strscpy(info->consumer, label, - sizeof(info->consumer)); - } + dflags = READ_ONCE(desc->flags); - dflags = READ_ONCE(desc->flags); + scoped_guard(srcu, &desc->srcu) { + label = gpiod_get_label(desc); + if (label && test_bit(FLAG_REQUESTED, &dflags)) + strscpy(info->consumer, label, + sizeof(info->consumer)); } /* diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index d6ff044ce887..3fe740b7d84d 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -563,7 +563,6 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) struct gpio_device *gdev; struct gpiod_data *data; struct gpio_chip *chip; - unsigned long flags; struct device *dev; int status, offset; @@ -578,6 +577,9 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) return -EINVAL; } + if (!test_and_set_bit(FLAG_EXPORT, &desc->flags)) + return -EPERM; + gdev = desc->gdev; chip = gdev->chip; @@ -589,18 +591,11 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) goto err_unlock; } - spin_lock_irqsave(&gpio_lock, flags); - if (!test_bit(FLAG_REQUESTED, &desc->flags) || - test_bit(FLAG_EXPORT, &desc->flags)) { - spin_unlock_irqrestore(&gpio_lock, flags); - gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", - __func__, - test_bit(FLAG_REQUESTED, &desc->flags), - test_bit(FLAG_EXPORT, &desc->flags)); + if (!test_bit(FLAG_REQUESTED, &desc->flags)) { + gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__); status = -EPERM; goto err_unlock; } - spin_unlock_irqrestore(&gpio_lock, flags); data = kzalloc(sizeof(*data), GFP_KERNEL); if (!data) { @@ -628,7 +623,6 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) goto err_free_data; } - set_bit(FLAG_EXPORT, &desc->flags); mutex_unlock(&sysfs_lock); return 0; @@ -636,6 +630,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) kfree(data); err_unlock: mutex_unlock(&sysfs_lock); + clear_bit(FLAG_EXPORT, &desc->flags); gpiod_dbg(desc, "%s: status %d\n", __func__, status); return status; } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 68543ab34fb6..d0de492acd4c 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -76,12 +76,6 @@ static const struct bus_type gpio_bus_type = { */ #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT -/* gpio_lock prevents conflicts during gpio_desc[] table updates. - * While any GPIO is requested, its gpio_chip is not removable; - * each GPIO's "requested" flag serves as a lock and refcount. - */ -DEFINE_SPINLOCK(gpio_lock); - static DEFINE_MUTEX(gpio_lookup_lock); static LIST_HEAD(gpio_lookup_list); @@ -123,8 +117,7 @@ static int desc_set_label(struct gpio_desc *desc, const char *label) const char *new = NULL, *old; if (label) { - /* FIXME: make this GFP_KERNEL once the spinlock is out. */ - new = kstrdup_const(label, GFP_ATOMIC); + new = kstrdup_const(label, GFP_KERNEL); if (!new) return -ENOMEM; } @@ -1093,7 +1086,6 @@ EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); void gpiochip_remove(struct gpio_chip *gc) { struct gpio_device *gdev = gc->gpiodev; - unsigned long flags; unsigned int i; down_write(&gdev->sem); @@ -1119,12 +1111,10 @@ void gpiochip_remove(struct gpio_chip *gc) */ gpiochip_set_data(gc, NULL); - spin_lock_irqsave(&gpio_lock, flags); for (i = 0; i < gdev->ngpio; i++) { if (test_bit(FLAG_REQUESTED, &gdev->descs[i].flags)) break; } - spin_unlock_irqrestore(&gpio_lock, flags); if (i != gdev->ngpio) dev_crit(&gdev->dev, @@ -2227,62 +2217,43 @@ EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); static int gpiod_request_commit(struct gpio_desc *desc, const char *label) { struct gpio_chip *gc = desc->gdev->chip; - unsigned long flags; unsigned int offset; int ret; + if (test_and_set_bit(FLAG_REQUESTED, &desc->flags)) + return -EBUSY; + if (label) { label = kstrdup_const(label, GFP_KERNEL); if (!label) return -ENOMEM; } - spin_lock_irqsave(&gpio_lock, flags); - /* NOTE: gpio_request() can be called in early boot, * before IRQs are enabled, for non-sleeping (SOC) GPIOs. */ - if (test_and_set_bit(FLAG_REQUESTED, &desc->flags)) { - ret = -EBUSY; - goto out_free_unlock; - } - if (gc->request) { - /* gc->request may sleep */ - spin_unlock_irqrestore(&gpio_lock, flags); offset = gpio_chip_hwgpio(desc); if (gpiochip_line_is_valid(gc, offset)) ret = gc->request(gc, offset); else ret = -EINVAL; - spin_lock_irqsave(&gpio_lock, flags); + if (ret) + goto out_clear_bit; + } - if (ret) { - desc_set_label(desc, NULL); - clear_bit(FLAG_REQUESTED, &desc->flags); - goto out_free_unlock; - } - } - if (gc->get_direction) { - /* gc->get_direction may sleep */ - spin_unlock_irqrestore(&gpio_lock, flags); + if (gc->get_direction) gpiod_get_direction(desc); - spin_lock_irqsave(&gpio_lock, flags); - } - spin_unlock_irqrestore(&gpio_lock, flags); ret = desc_set_label(desc, label ? : "?"); - if (ret) { - clear_bit(FLAG_REQUESTED, &desc->flags); - return ret; - } + if (ret) + goto out_clear_bit; return 0; -out_free_unlock: - spin_unlock_irqrestore(&gpio_lock, flags); - kfree_const(label); +out_clear_bit: + clear_bit(FLAG_REQUESTED, &desc->flags); return ret; } @@ -2352,35 +2323,32 @@ static bool gpiod_free_commit(struct gpio_desc *desc) might_sleep(); - spin_lock_irqsave(&gpio_lock, flags); - gc = desc->gdev->chip; - if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { - if (gc->free) { - spin_unlock_irqrestore(&gpio_lock, flags); - might_sleep_if(gc->can_sleep); + flags = READ_ONCE(desc->flags); + + if (gc && test_bit(FLAG_REQUESTED, &flags)) { + if (gc->free) gc->free(gc, gpio_chip_hwgpio(desc)); - spin_lock_irqsave(&gpio_lock, flags); - } - clear_bit(FLAG_ACTIVE_LOW, &desc->flags); - clear_bit(FLAG_REQUESTED, &desc->flags); - clear_bit(FLAG_OPEN_DRAIN, &desc->flags); - clear_bit(FLAG_OPEN_SOURCE, &desc->flags); - clear_bit(FLAG_PULL_UP, &desc->flags); - clear_bit(FLAG_PULL_DOWN, &desc->flags); - clear_bit(FLAG_BIAS_DISABLE, &desc->flags); - clear_bit(FLAG_EDGE_RISING, &desc->flags); - clear_bit(FLAG_EDGE_FALLING, &desc->flags); - clear_bit(FLAG_IS_HOGGED, &desc->flags); + + clear_bit(FLAG_ACTIVE_LOW, &flags); + clear_bit(FLAG_REQUESTED, &flags); + clear_bit(FLAG_OPEN_DRAIN, &flags); + clear_bit(FLAG_OPEN_SOURCE, &flags); + clear_bit(FLAG_PULL_UP, &flags); + clear_bit(FLAG_PULL_DOWN, &flags); + clear_bit(FLAG_BIAS_DISABLE, &flags); + clear_bit(FLAG_EDGE_RISING, &flags); + clear_bit(FLAG_EDGE_FALLING, &flags); + clear_bit(FLAG_IS_HOGGED, &flags); #ifdef CONFIG_OF_DYNAMIC WRITE_ONCE(desc->hog, NULL); #endif ret = true; - } + desc_set_label(desc, NULL); + WRITE_ONCE(desc->flags, flags); - spin_unlock_irqrestore(&gpio_lock, flags); - desc_set_label(desc, NULL); - gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED); + gpiod_line_state_notify(desc, GPIOLINE_CHANGED_RELEASED); + } return ret; } @@ -2422,22 +2390,12 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset) if (IS_ERR(desc)) return NULL; - guard(spinlock_irqsave)(&gpio_lock); - if (!test_bit(FLAG_REQUESTED, &desc->flags)) return NULL; guard(srcu)(&desc->srcu); - /* - * FIXME: Once we mark gpiod_direction_input/output() and - * gpiod_get_direction() with might_sleep(), we'll be able to protect - * the GPIO descriptors with mutex (while value setting operations will - * become lockless). - * - * Until this happens, this allocation needs to be atomic. - */ - label = kstrdup(gpiod_get_label(desc), GFP_ATOMIC); + label = kstrdup(gpiod_get_label(desc), GFP_KERNEL); if (!label) return ERR_PTR(-ENOMEM); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 2bf3f9e13ae4..9b7afe87f1bd 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -135,8 +135,6 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep, int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); -extern spinlock_t gpio_lock; - void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); /** From patchwork Thu Feb 8 09:59:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771078 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 B96446F52A for ; Thu, 8 Feb 2024 09:59:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386379; cv=none; b=aSXcw4KHPPrHBBwfvC0eJDOkXAdnD6pkptOp+SiZTUIu/tVHLQaXN8ZMob3sJ4Oet4rBfSeVwlEdewMD5leaeZhi/11FVs49NtLqWZ+KP9O5og8yyIPSddDEYyIu/mGFwzs+wq1qy6K1mAuCKJh/IAKZ5gUI51UbA22YorRIJ/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386379; c=relaxed/simple; bh=zAtmOdNOGRBb31lyhhlsZytG0MEiuyGU1nnHlSqWLMU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vAwJ3mgpxX6viujFN/S6mQapg9kT+VXhc03Hl5MNYG8oD/3B53QXkp8r8Dig7iF0ztL2hYfq8Rb4ScnkaiQ2pyg3G8Ozul+qKmDj8fMUjx8ifsddqMaA0HKNzx4oV3vFnbJ+SHDB9eZkQac6ep3yOhO1CtqWhchnz1BFGzg+skg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=ahLjFakz; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="ahLjFakz" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40fd2f7ef55so3844915e9.0 for ; Thu, 08 Feb 2024 01:59:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386376; x=1707991176; 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=5Psr/S77Ef9/NFyemfTs4Z611NQxBnhaN12nhKTkN8I=; b=ahLjFakz228obu5b23OLx+/y4wgLYhtjsZaVH86HdXTyDIBoceweIepDUXU04DLwce mih9LqeY4W031x2I4249nZGnDtssRd0BwSfSQRzM0Apwtmc7wgjFEUE92Z0+Jj807FyF Tx3XlOt16rwi32YXJ+TBLnV/ax8ec1F/thcAi7dPIMeBz1ERmp4LNv7BCiEMAo1grHGQ e0QCuyFgIHTpaKpVN/qXp1uw+E/Ukz9EH8WQKXvHaU+hVkMQsFI+0uAXdpM0/rJcV5+v v2YVdnx4HE0GCXH2lhLOceK+CxLVXP1bnrxCKkqZTLnlAWFwzmYisxhaej3Z5kT7awuI VDTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386376; x=1707991176; 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=5Psr/S77Ef9/NFyemfTs4Z611NQxBnhaN12nhKTkN8I=; b=T/eMP4iCiFyQS80UnwgkBdSn3sYldrgme+H0cWnC2qba9WqRXPLHpi3DZGyffuQgaZ pauGybACt9Iyh/bsBDyVQZKBvkF7dlJiWM4E+yO36OLTkRF6W92owksokH33AHb3WXV+ QDQc07XisjytUKNEEroA6tvlJ9Q0Ufv4dPvAUCOK1D7qcZARpll1avEv9I2/kILK5jrC vD4sT05T1e3FnkOSw1luHZLoJ3BQrkCjK1LUy8cgjHEDSsp1Nzu7oj5qAgGrL24eKl5X J3iREsPoIDDKBTTqBN0H2gr+S6tC7GzHGR4pOMdoTiS6drmEiIQ+Dgxg+gSyIpFE2mqn FvMw== X-Gm-Message-State: AOJu0Yw0B6nocxgr/Ka0LG1G+NYN52wY8zRamuyvwiNe9P5Kwa/3xeXl mk6g8udvC92sAUOUnTCAUiqJbKAaEW7UkOKg9zEONHu8zsmYPwlXeBr+wLHJXw0= X-Google-Smtp-Source: AGHT+IGH7TXtO138rKdtwfptyU3y5eIAD1aH0S2JN39PJx44mURqBCg/VyAPzmgrymKi2Hy4yoWOGQ== X-Received: by 2002:a5d:59a1:0:b0:33b:4669:4ba with SMTP id p1-20020a5d59a1000000b0033b466904bamr2095371wrr.6.1707386376107; Thu, 08 Feb 2024 01:59:36 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWVzg/tGttYcpQQNBU4Wuz+y7v0z8uIClKBtLO/U+Tu/xGRtRGL5SN0jh09A1a9NhYNuAdj5+xSpjSKNhi8vi5bf5LnvvYA96D10zBExK2MLMZvlCg7mVlFxhVDfhD9ZxbHHgNf3U4O1RuktVlXigmJC7+Uz3i6SQL+8EQxSvFCUb7d2FwS8xDOdy+5zwcGJ4FTSpvpIlpwgR+WQwRHYQvkPDU2IK+yMj5hwJXFoKopa+2yYw97aRGF8K02zCDZasmXlA/FYMrEcdSi03DmEZ3ZfkVqFM45r2Zj+4Y+7y/5NQqCI9L6jTI3iU8OGkSP0D1Mgn1zWNeuBY7byAya3WZbe8CgvmBqDQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:35 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 10/24] gpio: reinforce desc->flags handling Date: Thu, 8 Feb 2024 10:59:06 +0100 Message-Id: <20240208095920.8035-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We now removed the gpio_lock spinlock and modified the places previously protected by it to handle desc->flags access in a consistent way. Let's improve other places that were previously unprotected by reading the flags field of gpio_desc once and using the stored value for logic consistency. If we need to modify the field, let's also write it back once with a consistent value resulting from the function's logic. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d0de492acd4c..5e098a7816bd 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -336,18 +336,20 @@ static int gpiochip_find_base_unlocked(int ngpio) int gpiod_get_direction(struct gpio_desc *desc) { struct gpio_chip *gc; + unsigned long flags; unsigned int offset; int ret; gc = gpiod_to_chip(desc); offset = gpio_chip_hwgpio(desc); + flags = READ_ONCE(desc->flags); /* * Open drain emulation using input mode may incorrectly report * input here, fix that up. */ - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && - test_bit(FLAG_IS_OUT, &desc->flags)) + if (test_bit(FLAG_OPEN_DRAIN, &flags) && + test_bit(FLAG_IS_OUT, &flags)) return 0; if (!gc->get_direction) @@ -361,7 +363,8 @@ int gpiod_get_direction(struct gpio_desc *desc) if (ret > 0) ret = 1; - assign_bit(FLAG_IS_OUT, &desc->flags, !ret); + assign_bit(FLAG_IS_OUT, &flags, !ret); + WRITE_ONCE(desc->flags, flags); return ret; } @@ -751,9 +754,6 @@ static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog) return; } - if (test_bit(FLAG_IS_HOGGED, &desc->flags)) - return; - rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); if (rv) gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n", @@ -2528,13 +2528,16 @@ static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode) static int gpio_set_bias(struct gpio_desc *desc) { enum pin_config_param bias; + unsigned long flags; unsigned int arg; - if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) + flags = READ_ONCE(desc->flags); + + if (test_bit(FLAG_BIAS_DISABLE, &flags)) bias = PIN_CONFIG_BIAS_DISABLE; - else if (test_bit(FLAG_PULL_UP, &desc->flags)) + else if (test_bit(FLAG_PULL_UP, &flags)) bias = PIN_CONFIG_BIAS_PULL_UP; - else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) + else if (test_bit(FLAG_PULL_DOWN, &flags)) bias = PIN_CONFIG_BIAS_PULL_DOWN; else return 0; @@ -2700,24 +2703,28 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); */ int gpiod_direction_output(struct gpio_desc *desc, int value) { + unsigned long flags; int ret; VALIDATE_DESC(desc); - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) + + flags = READ_ONCE(desc->flags); + + if (test_bit(FLAG_ACTIVE_LOW, &flags)) value = !value; else value = !!value; /* GPIOs used for enabled IRQs shall not be set as output */ - if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && - test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { + if (test_bit(FLAG_USED_AS_IRQ, &flags) && + test_bit(FLAG_IRQ_IS_ENABLED, &flags)) { gpiod_err(desc, "%s: tried to set a GPIO tied to an IRQ as output\n", __func__); return -EIO; } - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { + if (test_bit(FLAG_OPEN_DRAIN, &flags)) { /* First see if we can enable open drain in hardware */ ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN); if (!ret) @@ -2727,7 +2734,7 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) ret = gpiod_direction_input(desc); goto set_output_flag; } - } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { + } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) { ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE); if (!ret) goto set_output_value; @@ -4424,21 +4431,22 @@ int gpiod_hog(struct gpio_desc *desc, const char *name, int hwnum; int ret; + if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags)) + return 0; + gc = gpiod_to_chip(desc); hwnum = gpio_chip_hwgpio(desc); local_desc = gpiochip_request_own_desc(gc, hwnum, name, lflags, dflags); if (IS_ERR(local_desc)) { + clear_bit(FLAG_IS_HOGGED, &desc->flags); ret = PTR_ERR(local_desc); pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", name, gc->label, hwnum, ret); return ret; } - /* Mark GPIO as hogged so it can be identified and removed later */ - set_bit(FLAG_IS_HOGGED, &desc->flags); - gpiod_dbg(desc, "hogged as %s%s\n", (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? From patchwork Thu Feb 8 09:59:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771403 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 EBC8B6F50E for ; Thu, 8 Feb 2024 09:59:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386380; cv=none; b=aShquBNqWiRVhVwkyk4WRofInNBs5is+ExpJqCBDb7+D3vliMT8mhfE0qcJ1foM/nW/VKb7SsRW6+3zPM1+c3mhaRHOB0ULBA/m30GbPYoksmfmK7bs8aNaCsVWLtPO83Fd04n3xkvypUOdP7PrwnMqYHFHiTPMZQNHP5EjKN9s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386380; c=relaxed/simple; bh=44Khv6FYuGS4iTpA4mYtVBSUOUtv1cSVLwwGoaAVCzc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZMzFfbaJAIUIVjz7igbyrWLsglddLt07kqaRpcuSSwloLrNoHoOKisC40gI+Yk58KR73Xl2xtw0cXRpQ+qgjCflsRFFJXLeduE9Yl79Gw+VumOsSuuu7I0T2RmhPjjU40rO/aYky8cr2w+IRtJDLYgdgF3TgdTKsY/vK2u075Qc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=CeO8e5Gs; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="CeO8e5Gs" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-33b58ddefd3so285526f8f.0 for ; Thu, 08 Feb 2024 01:59:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386377; x=1707991177; 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=SDH9FjcEQjELl5YLDi/Mv6f4/sVoNw9s95JjQsJXfFU=; b=CeO8e5GstTxMGUOLdzSfOQiUUopuo7gqO519aKHVbA+HzDDtzMlm/SDx3YMpFe6xYT Fpn2Pwat2iAaaTYZH4lj/hZY8DE8+ZyA9wbqXNWi6VSr2cbT/Q+DDjEjhF7E3h7UEz+W FkvFTMZeB53BL8uQzokgQtLUauLB7DeehEhPAgDZJ4oczK5uOkjT5aNyJDRWNpZdRUP1 nBUExk5TDkylBXU53SuqGXUU5OolxtGhCzH3IBJabzu2qUy/ee32tsURcrdWgkk3SsF0 iD1XH5/5Sf5MKTGNv+tgEfeXAYQae5IJ8j8srfDOS0ftaVkmYUuQLv5lz/F+gMI4zX9H GgCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386377; x=1707991177; 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=SDH9FjcEQjELl5YLDi/Mv6f4/sVoNw9s95JjQsJXfFU=; b=UjhggGnDrinUUMLPFUnnQo2XIjet5UC1n0AHAozy1SO+re6UKrlQHuASRTjJWPEWuT kuuvFe6KJ3jvvxK1GLnXa/4YIKdDZmi6I/QaFuhpVIv9mmMQekgO9I89keABP178/Mnx ulS6BhAa9HT2FknNfZGGgrIkzWVXcZXseBm2oHgHiZQE3uxCiPhFbV6BD4t1UFeKeIhk iv+e1SQUoSVCSG/T5ZZZqs55m72OVAGjQMAaKB375O/VHxBbIYkg6YkgHJ4cNPMVaJPd U1CTjDaIdw2ByGfhXBFoU56firTUYXGGIpWi4fl6bi3jIUI70D3euGVbjDXhGKQbARZc EDhA== X-Gm-Message-State: AOJu0YzKmsm+C6Qt1a7i89EsfSh8WYYCfQJN2TwSXgHul0qs/e2fEJg0 xrsnaAh98oHvDAGCPbRHMuiD2BnBE1ILQihvVJcrSxrLFjbFVGCVlK4PWMAA+AM= X-Google-Smtp-Source: AGHT+IHJPGmnwEdCYIiqnvuMa3t/IOLS1maS1ioSsM1EpsF7be0KRjYGP2fIM9FXq373ezuSkLBcjg== X-Received: by 2002:a05:6000:1246:b0:33b:2fba:1eab with SMTP id j6-20020a056000124600b0033b2fba1eabmr4879005wrx.30.1707386377087; Thu, 08 Feb 2024 01:59:37 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXq7h4bUJb5LA5R2LqcyELd/oIeGfFD2bNctDA2TxzEqtFJBWk+P0wU6OZJVnPvgbai7uKrmYFQJSLqXEGY8CJYRCtr9JwUu5SJWW8dppJDNLwOycExWIPo9jzONi7Zug/weFuuzjkFJGXxeFOB8h0nP7E4GsTu0eaEjEEoryvy0aoN+PhrkweLHW5at77c49Tmqn72lILU4D0+K6zmllrGbfdn/kp3co4XPTL5C1JLG6B0z0ckw9R49y5rw3mM6O0CikB6dXtYKOHEB6dg6r/sibA9U4tL8ZHkrkUml969HJA5uGDVqudw+OWwxTDEtg5fmzcuWzSSY8xmyYJyc0fTlgo76fxepw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:36 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 11/24] gpio: remove unneeded code from gpio_device_get_desc() Date: Thu, 8 Feb 2024 10:59:07 +0100 Message-Id: <20240208095920.8035-12-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski The GPIO chip pointer is unused. Let's remove it. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 5e098a7816bd..939ae167244c 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -184,16 +184,6 @@ EXPORT_SYMBOL_GPL(gpiochip_get_desc); struct gpio_desc * gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum) { - struct gpio_chip *gc; - - /* - * FIXME: This will be locked once we protect gdev->chip everywhere - * with SRCU. - */ - gc = gdev->chip; - if (!gc) - return ERR_PTR(-ENODEV); - if (hwnum >= gdev->ngpio) return ERR_PTR(-EINVAL); From patchwork Thu Feb 8 09:59:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771077 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 DCDAA71B48 for ; Thu, 8 Feb 2024 09:59:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386381; cv=none; b=HokEi3A3JuzwYteg90U5zcyfnGMknNXHsU/dQoH5RAYVAxe8N+eSAUoFLTqnwgV+12OfFxTX6A2UoxPJdqxRW995SeVK0FxuKKZ73gODdxBRx9YND9L7ceLxlEOrnt4lPQ2NrEuLGJbwZWEAr75p3Anl4KFhOx3VZ7/dU72ogOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386381; c=relaxed/simple; bh=rpFcXuoa7XEKWGrXP4ohioPaux3+8YRMAEzyUwG0HNg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VOsOfk6+hD5GxYMW54dJf58qJxiY4xKfXBYVzz0H7ql5ma3YLVQjYkyNva7oeeLRkbtggfFZkmyIq7gdcvyPLZKFIYXtvhipqg1rOJX90UDpnm/Knoa0/39vP8F2aYIV/RgxvrytZKCRzMWXeRtnkStgXO8NUfXQlGhDuurwFz0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=WyJCJQpv; arc=none smtp.client-ip=209.85.221.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="WyJCJQpv" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-3394ca0c874so1215987f8f.2 for ; Thu, 08 Feb 2024 01:59:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386378; x=1707991178; 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=neaBFQruxVCvpnyb2xKS7yjOVX4paD5qlj8wJKLSUU4=; b=WyJCJQpveKpvfVnQ5i0/55XAJZn/fJedelS35oAVVgIuawGEpoaOlbBSVGKgV/CbIe Vw3tp6uRE0Qb7A0nBU+hiGGb8ScEzRcB9sdxckoufLQETzC/ylA1PS/Nm6aNYvJwNMHd yBuM4k0g6p0RHKkMZFnyw9uadXfHkgtL2YtnUYAsSSFQSffTh+qRLUhtr2CaV3NIHFYJ gBblmJHbEFyHlg4uwkn1xaxrjr4aRkuhIDuNiwL4pDRLdiQLdegel4g0FED0tuvhwtwf Xmj1uSolhprMurv8qBFrPUbXXlmEmvsFRMGCidRP3Z8so/yJSVhSSalWbDQ+XrfYQDcn X8Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386378; x=1707991178; 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=neaBFQruxVCvpnyb2xKS7yjOVX4paD5qlj8wJKLSUU4=; b=I2yk+DwiBqVRVXEcrGxo4xpLiqSfn94q7p6Eq5Q1qMMU+1Qz+xK9bQHJF954OGDY4+ sJm67IECgMHiZIUXlisR2y35C/OTUuAsyfY9u+j/GJMAzQ3RcjxaI4sRvdE+59nJgRMP QifnyjxXzMXaDFUxbFVZtGFAKGjvR64fI5ejSwvq0i7chSwWoDR6wfL3LBSwb1+bsWGj kHJ3rQoBeiWVvAdee38pL48ft2o81q4DD1M3H2PL/5mgBLC6vw3ChipyWrwTJVrX4RVg QMaE56FlDE1IVqLPxIOGdyqWld6ywTZAtpJdgtbgiSN/zKBZ6TI+pscQvE6JgusIpgHs rSwA== X-Gm-Message-State: AOJu0Yy8ivJP/NUSF5qboXJSYeCiAmMr/d6D88E5nL9c72NnUH8UDqRo QNvH6IPQqepPEKdy6Trb5aVlOA+W45S+dEhmVaDZA4ylZC5JSrW6QWoQQ6l7aMY= X-Google-Smtp-Source: AGHT+IG5jjUfQYqv8znpCpV5fwqXcxf1zcotF07Cq8PbCeS4wyDBbWS19Fu1H0O0M9nxqnNYUZPQ6w== X-Received: by 2002:a5d:6651:0:b0:33b:1a71:a8ef with SMTP id f17-20020a5d6651000000b0033b1a71a8efmr5180258wrw.26.1707386378172; Thu, 08 Feb 2024 01:59:38 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV4ST4VWVARTxh1vhyL+FYq91ffSkWIvf/1bkmhFgsy+vx6xsriKtI97qtYme/BPcerdpi/CirADryYQaFPFVCUhzKmZIgTbsYyJ4pOUDvix5hq2SXz2Cw/DzajqtVLr1ymDaSVkA5ckKgeiAv5Duzjps/gQgpx+TGMSf2qV7/Acx4O8RN0DimP947K085ktak2dW9XXjbzvlKilQnDp7z/TPd1kNQsyGHAI6dwuE/OuwVZ8r7stfYQqu+9abynimPqh2JjR2wxYEeVMp7BdNKVeEMrTYvgnmzqK6VVkNQml0n9QzlRTvGYt6+iNSsbTBYjgxerTdpM7pBPzdNkDgaBxDZ7ae9U4w== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:37 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 12/24] gpio: sysfs: extend the critical section for unregistering sysfs devices Date: Thu, 8 Feb 2024 10:59:08 +0100 Message-Id: <20240208095920.8035-13-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Checking the gdev->mockdev pointer for NULL must be part of the critical section. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-sysfs.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 3fe740b7d84d..fce4ef9754af 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -768,15 +769,15 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev) struct gpio_desc *desc; struct gpio_chip *chip = gdev->chip; - if (!gdev->mockdev) - return; + scoped_guard(mutex, &sysfs_lock) { + if (!gdev->mockdev) + return; - device_unregister(gdev->mockdev); + device_unregister(gdev->mockdev); - /* prevent further gpiod exports */ - mutex_lock(&sysfs_lock); - gdev->mockdev = NULL; - mutex_unlock(&sysfs_lock); + /* prevent further gpiod exports */ + gdev->mockdev = NULL; + } /* unregister gpiod class devices owned by sysfs */ for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) { From patchwork Thu Feb 8 09:59:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771402 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 D5AD673181 for ; Thu, 8 Feb 2024 09:59:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386382; cv=none; b=seUoXcToiS+bJZaYMROeUV8Rt8FjtasaKbes5iX4SB0sTbXanQaGGDmvbgnLqca9N9pPAcEI+TAMrlId+Ojb755lJnXFfUD4Myb2lN9N3YJk2MlW0rf0RL+jXRHgUE6bSjlY58B73bP3UG2yZMck7VIJhnBgoWLVUW9FbB9xhmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386382; c=relaxed/simple; bh=FDZO/TwcbnTddQUhjEAdRe7rViCju9Ibv+2BryqiENw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UP28Ux4FGTVwSa43NLgKEmx4xzsIf+cFfqtsZhFhy9fg12oujRZF6RU6iorKyxyi5676rfu7iiGTw3AWyPSSfr9ySWLquSuMZyamNYkRyxOD1imbu4TaVomOZ2oFLYRn0lkQxOS6ULbsR1XdmdCn8NwXT2ZFr9EUfROMtGcju00= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=pXldQt3l; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="pXldQt3l" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4101565d20bso13584705e9.2 for ; Thu, 08 Feb 2024 01:59:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386379; x=1707991179; 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=3dheTVBTayhCasfErTOxCrk5zmfbFuKKC4sgrDD6oeo=; b=pXldQt3l/WQBEm9FqzrI8g2cRltG0MVB0AC+8Y2D/sfA1GAeigRcgin//oN5MAk0dX dujG7hFgnLJBrBY4Wjj0e5YAaME3EsCOvrhOAYMPgubaAa+c2m0XKf6jU9gOb0pCjTTx 2pVvgFg0Z6KjeGzHMOpxf0DnSvhsPhVYb//jEYVQ8QN4cV5hPGPLPQsbMlsPni2JG2vU 1KN5vDNplwVuPlUIvAAlT38vYBVL4QrzCJ538vK9JDzmx/svzS6a/oDDLt8kVRkXO3fR 7u9SUIg15sgftvCJ6Mt39Yr5d9R6qtyn/jsoHT9RvQh0EIcusWx1EGLuT2QERd6RBfvL ODDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386379; x=1707991179; 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=3dheTVBTayhCasfErTOxCrk5zmfbFuKKC4sgrDD6oeo=; b=butaSZyQXzMBHFiH8l7VVfKNMHQx8to3hHE1M9J+F8091nYKS+0o8izg1J/WUhFTZD iqzz6DR1D/HRN9haPEz7SwzJ4XUubI3DV2sHPIeAX61W4HFcyG60wK+fWvcBa3j7/eLO LO1obbIsJ5FQGMmqAk3doflnOkZCp14gXoA2d4n/C2zajX6WH9Q29WrwBjsKSH3hQ0Ry h01AhcH9sa2OHZgTz+49hDS/+ShYKt7t+Ze8gccLaESbLfF7Gat/MvO0KU0CtF6fVKLq /AlxXs3N7IMp/ZQ3rumiJxMzfnbYSAzA5hmCvHCrTHVqnhFywIbEYZzWxmo/IVnCpPp4 tUOg== X-Gm-Message-State: AOJu0YyhQ5FXnxPY9ayMJaebw4HLz0D4rrPnNwcodQKJcyVa/8/cfNi6 lN44/IcP1Vyz03dM1TAT+bdH1rqvfm6dRk5NC/EWf7gSA6xO5TMRebV1ny5pQX4= X-Google-Smtp-Source: AGHT+IGvoCSeda2oqxGXWAWNFHn1kUFghSZ+a6X+tj167X2MWxY3zkdNsEwytkWknRL1v5hB13UjdA== X-Received: by 2002:a05:600c:580d:b0:40f:afd6:b266 with SMTP id jz13-20020a05600c580d00b0040fafd6b266mr6430692wmb.18.1707386379227; Thu, 08 Feb 2024 01:59:39 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXL5JPiAeZBUWUmjrQSoN1J7Ibl/MgYT7es1Lw5yZbX00z6XzS8ZdUh+8EgpuvfB2szke9h9jC06tqCMiLYheC0EOXDSMgRCpJR/TJ3XbcBlBgtIGyzIv0tlz4PLSVsP3Pyy0cXz6FvKGchuDIykfVVtE98nfV1aDCpfn6raaTpnDcB/f2lwNiW39qIj59mLzXkNr0tW/DZ0ajVX9fG5Iz5eL7AO3o97rmEtYZSTaTCf8vEurKqloCMFE5VneMkQogmeNx3p0itvtRTSWKeQ8uA86YRS1+b8SZYqqJpuzDnX6gyMOafSsyhi3tPF7CisK/peJikdddMm4BjbTWtILU4RDX32kmPiQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:38 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 13/24] gpio: sysfs: pass the GPIO device - not chip - to sysfs callbacks Date: Thu, 8 Feb 2024 10:59:09 +0100 Message-Id: <20240208095920.8035-14-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We're working towards protecting the chip pointer in struct gpio_device with SRCU. In order to use it in sysfs callbacks we must pass the pointer to the GPIO device that wraps the chip instead of the address of the chip itself as the user data. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-sysfs.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index fce4ef9754af..b46d50ac2452 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -401,27 +401,27 @@ static const struct attribute_group *gpio_groups[] = { static ssize_t base_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct gpio_chip *chip = dev_get_drvdata(dev); + const struct gpio_device *gdev = dev_get_drvdata(dev); - return sysfs_emit(buf, "%d\n", chip->base); + return sysfs_emit(buf, "%d\n", gdev->chip->base); } static DEVICE_ATTR_RO(base); static ssize_t label_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct gpio_chip *chip = dev_get_drvdata(dev); + const struct gpio_device *gdev = dev_get_drvdata(dev); - return sysfs_emit(buf, "%s\n", chip->label ?: ""); + return sysfs_emit(buf, "%s\n", gdev->chip->label ?: ""); } static DEVICE_ATTR_RO(label); static ssize_t ngpio_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct gpio_chip *chip = dev_get_drvdata(dev); + const struct gpio_device *gdev = dev_get_drvdata(dev); - return sysfs_emit(buf, "%u\n", chip->ngpio); + return sysfs_emit(buf, "%u\n", gdev->chip->ngpio); } static DEVICE_ATTR_RO(ngpio); @@ -751,7 +751,7 @@ int gpiochip_sysfs_register(struct gpio_device *gdev) parent = &gdev->dev; /* use chip->base for the ID; it's already known to be unique */ - dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), chip, + dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), gdev, gpiochip_groups, GPIOCHIP_NAME "%d", chip->base); if (IS_ERR(dev)) From patchwork Thu Feb 8 09:59:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771076 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 C669474E06 for ; Thu, 8 Feb 2024 09:59:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386383; cv=none; b=dB7nafn0qmjdEVkF+20sivr1dKdX9LSM2ahqQXlD7TlxIJmYbPsSlpJWcsLw/ZQ/FrozLGdQ62cAjdzPBL9tuHiodEHbVqDYlsyITClEJE0Enq3HiCZHda3EYq+OlDMGSPvTpxRtOOotEywutTHqNW4qDudf9Fgbzv7BXQSSE84= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386383; c=relaxed/simple; bh=yLFHAeDDaUuIFRqdSf+MG12ymiBBzwEskiNkeG6cKEk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=I08r98vGTtikXq+f4v7svjrcShUhWY1s7iem1fkfJDm/HGK0AkThtBUDhgb5ynCHV6P81n/OHrSD4XaNqWUj9FbwCxkZ4J543cb0Z8Uf0/bIcHlmox935cMwX7EWmKw2D/j7b8uQh9lnea/J1zKqYj4QIPWPPbFslxL4lKyQv/k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=1qtX6wqj; arc=none smtp.client-ip=209.85.221.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="1qtX6wqj" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-33b0e5d1e89so1338973f8f.0 for ; Thu, 08 Feb 2024 01:59:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386380; x=1707991180; 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=IsALcY7SVXjN//VI/pQguwi8KjWlDmDQg/LjiqJYZd0=; b=1qtX6wqj8/0p2hfysZ3jRLMZqZOS4HDed+2AdsWmkj5lHaoMuxRPZ2xTWkKvAqZH9F /kX83412zMylpC6URrLgRwWH3T8M6qN+55KhDGtEGafn3BVifZA/4fZ9hHqyv5dNsfAA BF9qJwGRahu04jBeofmWAub9/wEptX2Pb0GyjLsBo+fsaDVqJ/bBQwX8suifnz9qM0Z5 iCHNGpplhZEtSXIXVR+SiRGpuvHxx8gUA7tfWt+PtnzV7f5wNqcgOMIFp3SP0gx3KF3K /0OWsG34aTZkE5a9RrGCA2ZbYCqA7R+d9h0hRvx3y2JLp1UrRXjnkhDYScsSA71KuT8M vf0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386380; x=1707991180; 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=IsALcY7SVXjN//VI/pQguwi8KjWlDmDQg/LjiqJYZd0=; b=BahQwQpn73u56nWb7BBkMbi1xKdMPWags4wyO6OK2QQIJytNaTXf4hxhkG/V62+629 Pd1Phl0mRbjJVXGpwYkNwtkXT8KWhRdYqrLTiUw4jPdkXLw+kMVaeuLtyJiXCAVDWf/X KZi8A4zCbeHFZ1AJdxQPJLyRRqQA7Wr7/IXLoRnRuPvig1ufVXmLLTojz0MtIX0wjNju hg0FbgHfnEBaYSFgDT6jN3Wkq0K4e/c2gRCdQCEojXzF7aG7rLr6u69QW9+XTiarGItk M7O5Wg/reQrbzf8oiqJA+Mq8jM3n3x1PWPZXIUU2q8H6qtukD4kk6y0m5F7GfS4g8sX0 tCCA== X-Gm-Message-State: AOJu0YztX8YSUUImtb/WOemmGnwX1KXqnn7l5BJh5S9HIrY0zjnRE51f 65CM/l+KkFUMuaFabT34C7cbSqEhiOQ6Xo4sXLOSRK/fviNu3DphTfBi5Dsbp0oIFl1cLFAtv5y / X-Google-Smtp-Source: AGHT+IFrf5Mi2lbu0tAJ9MwJBdkg3fx6DDnSA74evr3YBdKkBJ30+7jCIZH5ZmX0DxE3/CQy1WwcGg== X-Received: by 2002:a5d:46cd:0:b0:33b:3770:730 with SMTP id g13-20020a5d46cd000000b0033b37700730mr4376990wrs.65.1707386380111; Thu, 08 Feb 2024 01:59:40 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWbyshcKUDQFIROX24EyWLxBStQmm0RK0cYD73MuCllakt7BZAuQG/6v+kj05bIzySdLlMqzEyzVXz1kjAG/Iugn+frgOb/ZrGoL4OuGkfBJfCMYN7xgAOAfnPW1psQWs7+6lJ3nN3DKlWiDpV1Hg7BJ3drork0Dyldb8n/ohbfBlY1BrceiLQpag4B72N7NdhAYPIQqMyhUmIuW59G8B6Rv8/ThlF3W+EjVX3QwWjfguU+XeRxkoDeQQhE10CYleJnas2CLJG20Pg6SglHGqw4G5Y9uv0pNPAh0NDa3EaqKZQOsKupHIXA9D6EvzLmHBtdXl0V7pGJwkEDBaAAQjmclPGAdR2oLA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:39 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 14/24] gpio: cdev: replace gpiochip_get_desc() with gpio_device_get_desc() Date: Thu, 8 Feb 2024 10:59:10 +0100 Message-Id: <20240208095920.8035-15-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski gpio_device_get_desc() is the safer alternative to gpiochip_get_desc(). As we don't really need to dereference the chip pointer to retrieve the descriptors in character device code, let's use it. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 3588aaf90e45..8e37e3befa08 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -332,7 +332,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) /* Request each GPIO */ for (i = 0; i < handlereq.lines; i++) { u32 offset = handlereq.lineoffsets[i]; - struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); + struct gpio_desc *desc = gpio_device_get_desc(gdev, offset); if (IS_ERR(desc)) { ret = PTR_ERR(desc); @@ -1739,7 +1739,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) /* Request each GPIO */ for (i = 0; i < ulr.num_lines; i++) { u32 offset = ulr.offsets[i]; - struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); + struct gpio_desc *desc = gpio_device_get_desc(gdev, offset); if (IS_ERR(desc)) { ret = PTR_ERR(desc); @@ -2123,7 +2123,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) lflags = eventreq.handleflags; eflags = eventreq.eventflags; - desc = gpiochip_get_desc(gdev->chip, offset); + desc = gpio_device_get_desc(gdev, offset); if (IS_ERR(desc)) return PTR_ERR(desc); @@ -2419,7 +2419,7 @@ static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, return -EFAULT; /* this doubles as a range check on line_offset */ - desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset); + desc = gpio_device_get_desc(cdev->gdev, lineinfo.line_offset); if (IS_ERR(desc)) return PTR_ERR(desc); @@ -2456,7 +2456,7 @@ static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) return -EINVAL; - desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); + desc = gpio_device_get_desc(cdev->gdev, lineinfo.offset); if (IS_ERR(desc)) return PTR_ERR(desc); From patchwork Thu Feb 8 09:59:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771401 Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 0CD7C76033 for ; Thu, 8 Feb 2024 09:59:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386384; cv=none; b=E+CGQYYnm6zNfjRc9Pi6d0sPbJyEunO8w/6rGp5SSoHYcrwjxFQqBlf2VMcNkLTjXyzQbERzr0XhkzhDiqr3i6NG4A72YNS5FS5/PpK5gHuEg7OPPIlPuTIaEg2CAC1IFftQQIm4Qx3yorlbeI1RXuJbBWUa0ms3G+OWpNlp3Mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386384; c=relaxed/simple; bh=uoIIVRHu2uJkO9SDw8JuUKRoDUO7ImNaDoq5Rnmn41o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=STy9pm+x/lQXhfzZsO5v/iwxhaMXXG1mkaFnVlp0qYtOUD6ilvcAqW7tirPoGk7xaM7tvuBnKq+rUEXPQtDGznP4gIWEd0WcGfqNISrkNGNXjco05t6P5Gg6QZpvoFi/zSCPzqxlYWFBPOMeDDOjSZCbKv32uE2QEV1Rjxp81GM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=VpKDgCwX; arc=none smtp.client-ip=209.85.167.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="VpKDgCwX" Received: by mail-lf1-f52.google.com with SMTP id 2adb3069b0e04-5116063585aso2022325e87.1 for ; Thu, 08 Feb 2024 01:59:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386381; x=1707991181; 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=k7MTTS1PPnO0o443gYifMTGiFsAoLhFBJ+RUlJK1fWQ=; b=VpKDgCwX+M4IUPcwf2nQjfRyJJcu1cazURmoDWk1mMnZHQ2rpjSoT3j/8MaeVLRzI6 wuuJGz//079pwkH+IaIIGmGkvXEFvuUTO3mgCEDo5WENUffd8Q1lB1Bfr6jLIlepgryE hpPkhHZipoRb+nC+7jhB2A4biHaJPoDm9ce2W9DwpE37Ci+bJDnfqDIkV9Czf+clY7ei hLGYVCbRJoj8moCqPp4zTJB436EUBRc97+9Kk3qGaM4sYz8+xx7wEJG5sRYsna9kI1Kg X2qDvzJPfmxfssmjsMGbDCofQR5DcRbOkhzSUP6aJJC1KOnOl7uJ8CcfjUUqsGSG/SUd 9Gsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386381; x=1707991181; 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=k7MTTS1PPnO0o443gYifMTGiFsAoLhFBJ+RUlJK1fWQ=; b=tpRKMncLngkm4rKdLU8xyjGVYKKnn5FK5IlEHn3arSU3cmaiqAKlzXJ4k7IQH1ozl7 YMGTrLySgI038p87iE3M0j+EnOJGNa2LQytOf7f9d6gG0aabKJWLQiY5Ogdt5oCefK8t FQzfh2vHbSy/Nd5Krvje7sPxoMgMskyspz5YrrNh6VEtQilGoFkvL8fz6z5hWTn8kmex MBg07HL81obFJ69k/FKUmsMFBKoG8PSgK/IRtJwPaIqZVIC4gbLtpq+w+Z9rx2XM2lyz 3Q0Qi8E3fm/TELbUG/35X7mJHo2kZHoMLzrRe41yREJnwgzYS4bhkhy0vBzUQ9KEs5Nu VE0Q== X-Gm-Message-State: AOJu0YwCOWBJTeu67gGmMTsl92O0vOX3ektJrYk7MlqrEaPw8WYKo9JK XdxTJxOTgAVFMEZvLjuFtJLXyHMbFhS+NS9wISNj0YgRgvghXOzmEpF6MADLDXw= X-Google-Smtp-Source: AGHT+IHNTKOLEHtsygIf2IrvhbWXAtgHLghUVOVCI/1G3Nr6yFdQZvPMiH4x1NBVHikMTpuFF9OBug== X-Received: by 2002:ac2:5ec4:0:b0:511:4f19:f45b with SMTP id d4-20020ac25ec4000000b005114f19f45bmr5806486lfq.41.1707386381267; Thu, 08 Feb 2024 01:59:41 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVfobfX7jRLlpRS5c8qTily6lghNl4/F63UQYOHOIcnviy1d/ZvQ/8OGvHJqPFMyPUow7ZFPnk8g+HphKSds9wHgBuzdhRM7MQjIs+njTpKknGNHf2oeBr3QOFySE717hxvdonMRtXJEAn2AK5sQSkm1b7iy9S2r8r+Wmb2AMrrLw+qZhEuhvWa9ztdg1y1vlK1TP2IH2NcRyaF7PNGeiQ+toPF/uFuc13tJxPSAh6+9o5XF6d3nqNSMDUawV4VDZFYNyGNWaZ3+nbEhDzJBLvCvUrA22nM27PNLb35j+E8m1neG9pEK539SYo+Fys3xAFh1YUxQ3D6ppt8gqG/HIWvmPM43pb3tQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:40 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 15/24] gpio: cdev: don't access gdev->chip if it's not needed Date: Thu, 8 Feb 2024 10:59:11 +0100 Message-Id: <20240208095920.8035-16-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski The variable holding the number of GPIO lines is duplicated in GPIO device so read it instead of unnecessarily dereferencing the chip pointer. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 8e37e3befa08..e993c6a7215a 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2701,7 +2701,7 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) if (!cdev) return -ENODEV; - cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); + cdev->watched_lines = bitmap_zalloc(gdev->ngpio, GFP_KERNEL); if (!cdev->watched_lines) goto out_free_cdev; From patchwork Thu Feb 8 09:59:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771075 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 093BC762CB for ; Thu, 8 Feb 2024 09:59:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386385; cv=none; b=CrzxdZXPuPrJkqk5dif8E4K6Z4j8mjWLPbP6eOIlxPQJetnE12jbmWGfUlua4/8unb9IGs8QHGcUseVDikaOkocM+DtVYWQjKfXJUWAdlfPQMI20VK71jifj3W8vdtI6N4E2y8z32Tq/yFH00GQYBxdjhwRTOCOM4HB5Qw/o1Eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386385; c=relaxed/simple; bh=4pcqVR2UKNjNb7TNBpVrhzZAcEAKto4VhymTx/E7Yb0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fDyPgF1Fq19JZ9j1HCoZpaQCGREMsv3hLe8LURPdEvkR8oJ5msljtxIGCxnQa7oZC5P/EBoDApEQXDLWdGX2jUDf6yjdChDGi4t577eveZDfr9VL18urZum8qq8AB/8UJRqTHYHNqwKO91aaFIPSUbnxaiq0OwOGleSMag1V6l4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=dHfD2vBf; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="dHfD2vBf" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-33b586e38f7so257338f8f.1 for ; Thu, 08 Feb 2024 01:59:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386382; x=1707991182; 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=hXQKcxTvr8ZEuMd3OtlDIObv0psz6oUY1P/Gu9NpG0o=; b=dHfD2vBfPwL8OXB08vMdVm9oMlWtHJafy8ZR0UMcyy9FJMavnjAtEsJAVpsX6/n6eN IwWbKqxsFsvDQUc0Ny8/bdY/7Qym+onbgyZ+LgEv2E8NBujyBowBawtiQR3KxTxDiH7u BnNoq+XV4xY61jVW0KDrQ3xHwi3XaeKInooaLzUiKY02QB6p7twIkFOnKmO/+IJlMn5D uD7LElW1anyBnV+yXQYV7Sz11OCAPPGjeeSWJijJjs8pBzDatD2mfbRHrbM1exZ70lCU e3xbvXJ/aMtFpZu/dO6zlaE6XkmoZB38VpGew8q0No3qYuqvSaWLBVQl94NSMuGZWFJ7 /kOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386382; x=1707991182; 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=hXQKcxTvr8ZEuMd3OtlDIObv0psz6oUY1P/Gu9NpG0o=; b=u9+CGKv2xs7mHvb3Eawwh5bY7hX8yQr6LNO6g3uhzCkz1KVBmzQKHAVFmuTjCHAgXj nDLHu0lsitwN/YgsIsGTmnwagGMsvY0I3TgFWsQCl+E/lB/aW34IEHbtjCLCdojhJOn0 ReJNM+ph/Ljy30Gbn9hXM8rKbvhKl7c7uxPTwPRqLBCVkouh4gvNKQ0EqYvSnhwuiGpd VMt2SSyTB73jQnDdb5gAGEURScHRDH6tnAqiwitOTucR0OsiSJeraEhmYFiBMBA25u1P 7dkEPNuiQbbuNNCVNqbN5TYaHlwYE8/F2bwR8aixtl42ZUQEsBfYfvWxT9czJPnA0wRJ Qlpw== X-Gm-Message-State: AOJu0YxYc85Oad5WT6Vh197WpaUK7paJCB66Muc211m6qYSjP41oXQHa rqkCd3r3DJT+MkC2Eburxs9gqxIfY3XoFEIYhZGl6ak5Cpb8q3Tv2ogP6vOCHug= X-Google-Smtp-Source: AGHT+IGWTHEe/i1Wmkb7L2L6RlK0ZbynRzx7eNQzssOSfrF6qS+pTlPhms6IddPzGCRIN3coN8yWNw== X-Received: by 2002:adf:e501:0:b0:33a:e828:c3e6 with SMTP id j1-20020adfe501000000b0033ae828c3e6mr5766414wrm.30.1707386382236; Thu, 08 Feb 2024 01:59:42 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUtUJ52Q8n40X2maKICeeuBN9bIYW5VfpWSOtWES22IXTX0RRT3Cr6oCiJM5XIWez0KAI/+Q2HqOntqRIGh/HIfWtLCOkIPmaE6f9MwbuifW+ceYfeF1IGKnI15eYwMt3n5CjN4zManxJXh8htl98wZrOfd+o+HVe2yrqx3z3aCZj977gEPuSeeo8IVOsa+OC7SkIdX+1MGfubv4JHFlug5E/6jxs3kJPdFjrk1NieAtttCChV2mYEwGzAPIUIOAPRz6ZEUcQIv/QuY8v/ty+G7irUWbqU8eotAqtM3GbVAPUs4tdWlopEeFA+Xua7m7BtZmwWeGxvmDkJ9la73vsArcTz5jZqlNw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:41 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 16/24] gpio: sysfs: don't access gdev->chip if it's not needed Date: Thu, 8 Feb 2024 10:59:12 +0100 Message-Id: <20240208095920.8035-17-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Don't dereference gdev->chip if the same information can be obtained from struct gpio_device. Suggested-by: Andy Shevchenko Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-sysfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index b46d50ac2452..2e3edf41e853 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -403,7 +403,7 @@ static ssize_t base_show(struct device *dev, { const struct gpio_device *gdev = dev_get_drvdata(dev); - return sysfs_emit(buf, "%d\n", gdev->chip->base); + return sysfs_emit(buf, "%d\n", gdev->base); } static DEVICE_ATTR_RO(base); @@ -412,7 +412,7 @@ static ssize_t label_show(struct device *dev, { const struct gpio_device *gdev = dev_get_drvdata(dev); - return sysfs_emit(buf, "%s\n", gdev->chip->label ?: ""); + return sysfs_emit(buf, "%s\n", gdev->label); } static DEVICE_ATTR_RO(label); @@ -421,7 +421,7 @@ static ssize_t ngpio_show(struct device *dev, { const struct gpio_device *gdev = dev_get_drvdata(dev); - return sysfs_emit(buf, "%u\n", gdev->chip->ngpio); + return sysfs_emit(buf, "%u\n", gdev->ngpio); } static DEVICE_ATTR_RO(ngpio); From patchwork Thu Feb 8 09:59:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771400 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 CBF30762ED for ; Thu, 8 Feb 2024 09:59:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386386; cv=none; b=TRbcS/6SFk8YuUa1PZzvFDtjjDay+vNX9RpkM1wa2LMoi0FztIa4kPpUZEljj2/Lkfw9L3GbsVeiOFBVmyzhaVIys4Ps30ObNimDj++68TU9qnjgcnl1ENdAgusDdIrkxdx6oHnMUtuB4rgPEsprL63aG60Vh8MGxSEg/aUDLAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386386; c=relaxed/simple; bh=+tN3iUBilTm+njqjZxTAfaV9+rTCJ+mPdFLpKDG4Gzs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HPG5feN0JIAV16g90v9MeUGWmHVoSVsSZIxTg6HepD68JO+h9KbeI336JokrKpoXQ6lgjefw69X7tOF3B/s8SASjryh6hq/PJnHIq1wkxImO6uOqjvQ++7QtcqX+uw2KYkQK0l2iYKKVCEPw0IcEp2TJiiDnMxbQQUKQupt8KGU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=iSUzhEMF; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="iSUzhEMF" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-337d05b8942so1198738f8f.3 for ; Thu, 08 Feb 2024 01:59:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386383; x=1707991183; 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=k2Hw6fcmS4ouu+tfkIaMvbg40PWFYNxx//+VN0hrkes=; b=iSUzhEMF+lLRq64cTfL55R5/TKFmWyyM/oe9d7lYqw7/6mMJ1hnqEddpjemv7R5l/s k6qB+an6SWre+wGjU0M7y3iFJW2WDOqUXfzwrliTjikA478w1Ib2lqRtMyRzXRz/vaeE gTbfvzhIRrPhBZcgITdVjIrp+6+8kVwMhvhbXF/D/rTFqZypw08dGQ0JOYJxdH2ZfX4W /VylmwbBoIdoZmhOLPnwR3qJbIYNHYVYqvhMTN61UO5SVY6B33N0QYsPkq5iI4av9+xq 7Ug29DxPMzBFdkl2MxdGRhK6JHJE2c/ddBBm2/xks6jJRi6SUBt/HbDBuA/ckUftiDiR rzsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386383; x=1707991183; 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=k2Hw6fcmS4ouu+tfkIaMvbg40PWFYNxx//+VN0hrkes=; b=ufHTfq8XqBTvJwuHc9TU2BxPdjNurxx/H9R5ZTVXocy6+tpfjDEmr8slq/CK1DmiSy /BKO+qv6aSGfDwkWANX0LoVIdhB5dax5ZclcpFCnPi1Umye6s4ullnOnf+qIFcsJij6T VGHDckuvWlsOOjVYiP9i3pMTpMty2YBuIafW1h9IpE167tB8S2wFpsS0C0/seicos9D/ +L+NiQQD7UzTqjoD78mbEdODTfCCu2MvcCzNwSI9PH/GWTMigSbFEb+RpHf9oENEgI38 HkKp4RFVdUxIhStHKy92+k8VwaFAxf26HV5+IT43/Bn5F7hNlT3LmZNUxa8g1SbLv0fE fKrA== X-Gm-Message-State: AOJu0Yy80miNf3VwUPpXxRQ/JHV+9YH6AvlN0C1JoOKyFOi2aWo+LX1K 58mBOoTVJV2anrRy1IJ7YHI8oAXY5e5InwUEhY02t0YC2Ubo7J+PioE9/n+JmsU= X-Google-Smtp-Source: AGHT+IHxH878oANm8oqm3qNTBZNgrE3JWNCMlIHJgpzSHPGM0IpvkFNHnUnvJvJzcQk8TXWW0xHhRA== X-Received: by 2002:a5d:638f:0:b0:33a:e77c:89d with SMTP id p15-20020a5d638f000000b0033ae77c089dmr5809877wru.59.1707386383143; Thu, 08 Feb 2024 01:59:43 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVrd10VfjCb+wIf2nuv8xBysAgfkYf0EClKNPFjUN8zFiY+AGI6auc9jkw6RNCjMrWpi80HULydspcubuu4ZVfyJvV6JxuPrUe5eb2qZmtVRMqnOxBFMrPWB3vB9vyVUiZzQ180TxTgxNLyGkepmS6+/XMfCOwdoXcr5J+CjsaDv1OdeRxSXtJF1OWJZxEZ1GvTbdbWP4BGx1OWEkYARt/tYDJZ7TvB/oiMPJGWBZmVJGUsBMBAHW+k+ZFu3z/C1NdCPR7zgBj4bmFHYGQYzKju8VA0Km3Rpc2zHtgVji4C8jB/ORbDhXe8QttyJF+EEN/C7LfCTIHzi7tmCrxycOup0tUTEApybw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:42 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 17/24] gpio: don't dereference gdev->chip in gpiochip_setup_dev() Date: Thu, 8 Feb 2024 10:59:13 +0100 Message-Id: <20240208095920.8035-18-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We don't need to dereference gdev->chip in gpiochip_setup_dev() as at the time it's called, the label in the associated struct gpio_device is already set. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 939ae167244c..6598960b29de 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -723,7 +723,7 @@ static int gpiochip_setup_dev(struct gpio_device *gdev) goto err_remove_device; dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, - gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); + gdev->base + gdev->ngpio - 1, gdev->label); return 0; From patchwork Thu Feb 8 09:59:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771074 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 D2758763EC for ; Thu, 8 Feb 2024 09:59:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386388; cv=none; b=htNHxNrKNAQbaDSxF8MDXXqNMyH45H2oQ3pcaeDTAhpxPf8/pI7ds+DUhIYuFlH7624ln36J2tk52phoebL02dNLlXk1ea3S++xxjIYarKbmAu6sXHSjL1/BjgevxZOVGN9hw1MIMRfOsr1JuO40OhCiHIm4CTCLuQWd9zFmghc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386388; c=relaxed/simple; bh=zyYRmke/1SCtbqMjlRvTGUpu/1RsBvW/3TjBpD2c1Lo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G4yA+IN2ODVDZBLaK3mLnsc+D93xYr1yBLHOlDtJGwDM8v23XzKRC92tGjHPg/UCSGsvIZBCE3xzroDmtDoKWqJVI/mGc4f1gLyqtCTkR4oDw1lNxdAqkSQfuQKeBgEIMj+csXHZVHfaTLSTIQzRL99/sxGn/w/PUDYQf9AkeoM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=LcvLjqIA; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="LcvLjqIA" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-40fdc63f4feso15128545e9.3 for ; Thu, 08 Feb 2024 01:59:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386384; x=1707991184; 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=RYIRlc5OkrwZx92ZtvrH7YghLqa9zERXf/+sLJWSs1w=; b=LcvLjqIARyhPHJeE+GFYZBV5UxQZzWZ0ij/9yf8Ngehd0w3F2g5D04+RrkJVTCqvBB RMB48xYbJWcgD4UK+JuFGYGWE4TjBiLX00L53ltF0fztELSUgrC4YN2cek7/GR1yxOjG f+kwbqUQA06VBoRHS3e5F1toEE2PwHyz9WoZWbNIVspTg+tT++K8y7Q0JDqsv3VecyMD a1UT4ARal0RrWdwmo/IAIA6peSyJtThQOxZ7xwlaMjVwev2Ry6J409fj3gRCWDz75lxn QhUBxzhlosS4Kg+K5nilCmrzcZtqIAErZmkOg2PqICPkmd36n3XAq3bcTtWfEBZg5Q9X gGnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386384; x=1707991184; 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=RYIRlc5OkrwZx92ZtvrH7YghLqa9zERXf/+sLJWSs1w=; b=ikc2R6eqv8YDURUna2hr+OrESb98zCpHNNPXGB0zBo1vA9N32AdMPAgMfqbTK97nJ1 JgEwYykg8YCDsciWiXT82QVZburRoW5bkk5BsAKZKzzu8ME3LXgHka9HEblDvDEPmQOP 9U69gwald/tHMp+VOQl+btIq+XVWL+LZhTVg4RluJQU/DgPnRqwGbkAceRkmJLYSS9uH gK7unnK+YNnxFgMu9NcJzSqZMV8TBG+poce/XYnGnljPfTVxrIEwBUIz7nP8eFoYuuK4 4j/12/ZbjurYSaKzH+pvOZyF4qT+AxrEah/4y86EI1cWvcaaCcl5kMKevBOlnp6GHjje 37iQ== X-Gm-Message-State: AOJu0YylRCe9SvsYPewRmhcuORSMvcZWoYRAu01p98Ge4n1iOvX7AV7G 1CnxFmwRw48KYZ6bbgozzNgqtvZgaCkrWH0hOnGWB+4emVpfwGKw3kgulemGVhw= X-Google-Smtp-Source: AGHT+IGmjI3oUCT6UGyEZLyHOIfrA6gY0gJbhE50NLpRBeSQx92Z/ddnY3cwTtLCzY6EZet+u3ke7g== X-Received: by 2002:a5d:5905:0:b0:33b:28df:c370 with SMTP id v5-20020a5d5905000000b0033b28dfc370mr5240161wrd.14.1707386384098; Thu, 08 Feb 2024 01:59:44 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCViQt0ORtUTwvgRsnwaxwfM9LU+1xPs52gzk/iVEXu3OHYomnxR4eYemcPqJP25T2cd28tqXed7sdF+8igZtyf/dHBDKXDm3jSYxaCQkQKqu1BfSc6k3hJvq+mDOwqfaX9jLa5S2pINY9+/dFo05YgavaF5YixHlIxhga+1Enjzw0YppwtRHXEX8B4Psmem6OjvYrTOaE2U2FiKmCWwXHwsh/oFxkTMsfS3YAJMCWvOBYjX8XDdc/taQNCOvcExbe5qj2Y1Jjrw0Nt1X4Xv7qvtvLNLshQkRchFXcYTpj+tDvQqiIZQ/jXnyKFbP9Dk1vkKtUzzOnGj+Hb81o1Cg+j9U+N/0pnI+A== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:43 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 18/24] gpio: reduce the functionality of validate_desc() Date: Thu, 8 Feb 2024 10:59:14 +0100 Message-Id: <20240208095920.8035-19-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Checking desc->gdev->chip for NULL without holding it in place with some serializing mechanism is pointless. Remove this check. Also don't check desc->gdev for NULL as it can never happen. We'll be protecting gdev->chip with SRCU soon but we will provide a dedicated, automatic class for that. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 6598960b29de..9be7ec470cc0 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2257,19 +2257,12 @@ static int validate_desc(const struct gpio_desc *desc, const char *func) { if (!desc) return 0; + if (IS_ERR(desc)) { pr_warn("%s: invalid GPIO (errorpointer)\n", func); return PTR_ERR(desc); } - if (!desc->gdev) { - pr_warn("%s: invalid GPIO (no device)\n", func); - return -EINVAL; - } - if (!desc->gdev->chip) { - dev_warn(&desc->gdev->dev, - "%s: backing chip is gone\n", func); - return 0; - } + return 1; } @@ -2345,12 +2338,7 @@ static bool gpiod_free_commit(struct gpio_desc *desc) void gpiod_free(struct gpio_desc *desc) { - /* - * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip - * may already be NULL but we still want to put the references. - */ - if (!desc) - return; + VALIDATE_DESC_VOID(desc); if (!gpiod_free_commit(desc)) WARN_ON(1); From patchwork Thu Feb 8 09:59:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771399 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 4ADA97640B for ; Thu, 8 Feb 2024 09:59:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386389; cv=none; b=pEczoxqqvDE2w3/43O6v65luOnVUrGYP3xX/CV8j3550ZaxILNV9oml0VZMxIFeWIDND4HH1ZXgZ96RJB6IR2h4S44oY+Y2PskOOwgXtl8D7XCuUpVhHvsPRnPBUFTN++TuzdtHC1IG3jQRG5s+ZeLJzPNRGr/2UkaOcVcbdrdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386389; c=relaxed/simple; bh=e2jA9sxoxDgk0Yo8yXFHQ5QmaZCOljQMxwcPvDfphck=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dcPb23QexBkBIoaB1Mt4md2hDMXZ4AjjUZD4ExjRFuQk6RlvjhXg13nmOyVNr+tRF8ceIjIlmeNkY/Nd8CnwAXnUbydck2XK+GG7PTzTzd7Gox19by+GAabHBN6PieOteHSPN2WMNttyEu7q+Cpke1cSqOg24JHNKe8awKvrqt8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=Oe2E0bfF; arc=none smtp.client-ip=209.85.167.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="Oe2E0bfF" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-5114c05806eso2417025e87.1 for ; Thu, 08 Feb 2024 01:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386385; x=1707991185; 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=gSvYY20bsG3YLQLtQqZAj4VIzY9NSOxs1N8eV9mNDzY=; b=Oe2E0bfFUX5flHJiOhsQBq0kz475M1E4K5M5ffAe+JKd8BsiRKbrOt/c7uW4B9BRFs 3lymI8EAoXec+3gMwYB1H8IM88fZkcBoVhVqIL1ouhHhyuXksL26ztZeFqndRwKn752B /c64LydeA4e+sj8kVyjb8ZxvOboGrwvIiz7vJ6LV/OFnx18v1gOPQkwPlMvml+mpDrvb TPnc6vb8WbM/9Nx/aeZjnPP+5gGL8TcTV6H4Lut70s5OsHlthKFcEjg3RD7uPACSP2si lhtqAZAszSFBWgfrpExJyBZBBeS4mmCP4wllcYtKYfnMlQeNwRhBxX1FAr0A87yFThSE RmNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386385; x=1707991185; 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=gSvYY20bsG3YLQLtQqZAj4VIzY9NSOxs1N8eV9mNDzY=; b=syVu+/InopHdPBwJ9ohDQG/nwfby5TNM2gCKOdXIHHAiLfOdqYUkbRbP44SzPdWeS/ 3MyYboKS9CIkftBOORpAAra498HjdXVqyM2KhJ3BiNJcVN9zpzjIq25sjGpS9+WDGT19 0/ODPDQiYcaRtYdl7o9QV+8ADsOtpPN8vyjb93n+RHS3a3JgPqVd95aN8GcoFZ89w0l7 GJ5bk5NOEfT4hbJKXEHCApMGDTcfW/i/pkH303shTbDKit00gxt5G4YyDMDM110z60zu zqaJKcsYTUXlBYhCcjQBy6IE0SoFvKpCzW8xNyLq9YrMqgTl5Al4mnkjU31EroOUiXUx qJGw== X-Gm-Message-State: AOJu0Yz25dt8PEch6E6Yvx3a5IM/ezQ5D11JWtjX/lhV1gCGDY6Gwr/Q C0pB7sk6QzQHatqwkwAkkScyJtms7NQvtZcjei+68nr8h/haEat7pGZZ3NhxMBM= X-Google-Smtp-Source: AGHT+IF8x35m80ZdaG3qrPqZ/C9cTaIubJF0gdb+qsxnQfZwIEe4xWD3obLvRa9ccOLIRwkXcRwTuw== X-Received: by 2002:ac2:5967:0:b0:511:69f8:c4be with SMTP id h7-20020ac25967000000b0051169f8c4bemr2183961lfp.22.1707386385122; Thu, 08 Feb 2024 01:59:45 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCU242FHHxTbD2IudtNlQqDRyZKNE5NLu3La2edstf4JAjElG4i5brk5j01vhvsOtxlzRaTNwry9DNuDAOBPIpyQV9GzycjZHwxrIABamxg4dLG4HEhzKeX1Fof137wrFKN09eCvjvtvrswlI4YZZ+/dR+6Y7yb3Wu/81ZDktnpoAvaLM1TdWnsHR4CCsQJnOqB3Ksd1IMNPHE1DtYxorgZSOZhiQagSQSh3prJFwIdpPw2dk5bRLlUaNxlUGZTnQ2D6L8fl3QXK4Un+H5zlldG9dxOUgx7t0NdszXb5YVGBRGnbpx/y90e6VdiEe+DNPNe7cnhNFVN7W6mjtjmeYL/i4eXulsfHTA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:44 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 19/24] gpio: remove unnecessary checks from gpiod_to_chip() Date: Thu, 8 Feb 2024 10:59:15 +0100 Message-Id: <20240208095920.8035-20-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We don't need to check the gdev pointer in struct gpio_desc - it's always assigned and never cleared. It's also pointless to check gdev->chip before we actually serialize access to it. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 9be7ec470cc0..140a44dec0be 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -214,7 +214,7 @@ EXPORT_SYMBOL_GPL(desc_to_gpio); */ struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) { - if (!desc || !desc->gdev) + if (!desc) return NULL; return desc->gdev->chip; } @@ -3505,7 +3505,7 @@ int gpiod_to_irq(const struct gpio_desc *desc) * requires this function to not return zero on an invalid descriptor * but rather a negative error number. */ - if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) + if (!desc || IS_ERR(desc)) return -EINVAL; gc = desc->gdev->chip; From patchwork Thu Feb 8 09:59:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771073 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 F1C8176416 for ; Thu, 8 Feb 2024 09:59:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386390; cv=none; b=jaSVlZYJY2uYRXX+ZiwzxHhSxVv4znjDzILnByYzjax3LSbeVsz5l3B8jSrmQMdCXgpv024+KkATM7w7NRtvyv8+HAgf6YSKl334jLSKmPR5lDK1QwLuk3+ZuyDviK75INW9ab9uCNn4xXb5Lh5MvmTAu9tmC/EFwf0bUigl1x0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386390; c=relaxed/simple; bh=ljjZy/qeWAChuW+CO/Y5jU0Jf2aUL7O/Whh7X+N9I7E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mJvHkDkByhmYFYE8r4jbuQ8t4hKTfRXrnZJ/Bv1OBsWk2jbsZbeKV8ZkiCTNOdHW+5aUScj2jCOkb6hfzw2yYlzOy9p8TFMzdvkXIJn60KRYdKOJqxD4Pxod8CGH84pSmeZXkYY5/KSnI3eCOK/KRLxFXIkk3uOhK9fzWeNXzrs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=M5mSXy1n; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="M5mSXy1n" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-33aeb088324so1126527f8f.2 for ; Thu, 08 Feb 2024 01:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386386; x=1707991186; 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=vIvNEBkPA+Jjg13j8QnlUqFTrBX7oVdTnxr73OrkEHQ=; b=M5mSXy1nCoEwNgnxZYBq3fDTGWqIuIG1FVyJlEw5r05PiohxeiAWTcvkgOHmYQr7aS dUoDvc3NCRhjEl8A9tvZ6johSAtfZJ2cZg+KxG89PEkbNZv15IZpT4vKe1/cXSvmBb4i fPeCUSe9RccDtnK7XuAqpO/9IkMSAH2Fzos59+uvKt95tO+eTSM0XrA2GYeuOP7lFkp7 MTH7IwlIGaA9IAscBeyCRPITAAUcr4t19UghL4VCSqWBGhAcBBo/I8dzrghi+/e54BjE ruhzTpUQFv0n3POKlPBQ6agaVMOywUSEDt1ze3QbfD7A2ONviafBtub75k8dMC4W2LCp Rx0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386386; x=1707991186; 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=vIvNEBkPA+Jjg13j8QnlUqFTrBX7oVdTnxr73OrkEHQ=; b=Ysba9RRxxvY8tDzCaKVHzn9FhaAy9q9UCBCoIxsVPVwteyEThShQjcFWxP9V2KB8C6 PLH2XibpKpYHoGQs9W2SC1//3ZYjPTNMraYzPX9qiBknUPM5JSHNPUyiBmOQsjBXhO+b EhcnNKsBzWEz8u995aMSp6/Abnc62jhWEE3p5Bj+5Hetqb7RDCXEqUuKzciikdUbE1gI lZQJMyDS57pozKHLEhJVXsmmHFmdy20wLhvOmEExt/l1SJZk/5ACdYSMW5f68VNof4f6 8GrbyW+uaGO+5prJ314ezgfxHQ1GbQTbgLjrLZWGJ4kJ6ftkZ0xD8CYpkyujTNp2dPjv /gLg== X-Gm-Message-State: AOJu0Yxy1ZtFU4e3xYhI4TheDOpm1Yyk1fX9jYVz9UuoHnJJzJjIgKGe H6G+m/Y5sqb6gEJkdKGZKdIQY1ZPSz8c9kkoCuF1fWRI/tTKFCgA0FKqr9ni3P4= X-Google-Smtp-Source: AGHT+IGvg9eLN/pGzCLxk/zi7uN/bHn9gY2+HRAG3E9ooY6h7syoSSP/0EobJQfiCCGWIJ3C36tmnA== X-Received: by 2002:adf:f44a:0:b0:33b:3c04:ddbd with SMTP id f10-20020adff44a000000b0033b3c04ddbdmr4917395wrp.21.1707386386086; Thu, 08 Feb 2024 01:59:46 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVQ/EQOWP5gB7wL9iMIPtnEWtWtOiCiPgt74dBoDZogxfj21dNZ8rHJUfAyzH7eWNeR5qi4HYuqRTz3ythjGeT3qoc8gq9HgKlota9F4tZC+nyZ61L3t0oxORzXEHXESHWLCA9BP4X8Rcc+nuDQVR3VbHNpRu/NaI09pphNLjMFzXqB9dXopCvY1qXKiAHbkvLZ0TVMeXcuOXVgdRWvpFf9bpqqU/PHCTlBnMBPJKat/EphpHopa9MeOORxp0mJelz9jw6gXhgypdHgTdko6tCCKlCRsT3N38xaCFhGwX4LTTjN5KcjjuzIFXVNliRbbSDTk/cZCF9njLXlt3Isr+5WSdGYXp+OEg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:45 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 20/24] gpio: add the can_sleep flag to struct gpio_device Date: Thu, 8 Feb 2024 10:59:16 +0100 Message-Id: <20240208095920.8035-21-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Duplicating the can_sleep value in GPIO device will allow us to not needlessly dereference the chip pointer in several places and reduce the number of SRCU read-only critical sections. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 11 ++++++----- drivers/gpio/gpiolib.h | 3 +++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 140a44dec0be..6b696087c7f5 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -901,6 +901,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, } gdev->ngpio = gc->ngpio; + gdev->can_sleep = gc->can_sleep; scoped_guard(mutex, &gpio_devices_lock) { /* @@ -3072,7 +3073,7 @@ int gpiod_get_raw_value(const struct gpio_desc *desc) { VALIDATE_DESC(desc); /* Should be using gpiod_get_raw_value_cansleep() */ - WARN_ON(desc->gdev->chip->can_sleep); + WARN_ON(desc->gdev->can_sleep); return gpiod_get_raw_value_commit(desc); } EXPORT_SYMBOL_GPL(gpiod_get_raw_value); @@ -3093,7 +3094,7 @@ int gpiod_get_value(const struct gpio_desc *desc) VALIDATE_DESC(desc); /* Should be using gpiod_get_value_cansleep() */ - WARN_ON(desc->gdev->chip->can_sleep); + WARN_ON(desc->gdev->can_sleep); value = gpiod_get_raw_value_commit(desc); if (value < 0) @@ -3366,7 +3367,7 @@ void gpiod_set_raw_value(struct gpio_desc *desc, int value) { VALIDATE_DESC_VOID(desc); /* Should be using gpiod_set_raw_value_cansleep() */ - WARN_ON(desc->gdev->chip->can_sleep); + WARN_ON(desc->gdev->can_sleep); gpiod_set_raw_value_commit(desc, value); } EXPORT_SYMBOL_GPL(gpiod_set_raw_value); @@ -3407,7 +3408,7 @@ void gpiod_set_value(struct gpio_desc *desc, int value) { VALIDATE_DESC_VOID(desc); /* Should be using gpiod_set_value_cansleep() */ - WARN_ON(desc->gdev->chip->can_sleep); + WARN_ON(desc->gdev->can_sleep); gpiod_set_value_nocheck(desc, value); } EXPORT_SYMBOL_GPL(gpiod_set_value); @@ -3471,7 +3472,7 @@ EXPORT_SYMBOL_GPL(gpiod_set_array_value); int gpiod_cansleep(const struct gpio_desc *desc) { VALIDATE_DESC(desc); - return desc->gdev->chip->can_sleep; + return desc->gdev->can_sleep; } EXPORT_SYMBOL_GPL(gpiod_cansleep); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 9b7afe87f1bd..43ff4931e2c3 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -34,6 +34,8 @@ * @descs: array of ngpio descriptors. * @ngpio: the number of GPIO lines on this GPIO device, equal to the size * of the @descs array. + * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep + * implying that they cannot be used from atomic context * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned * at device creation time. * @label: a descriptive name for the GPIO device, such as the part number @@ -64,6 +66,7 @@ struct gpio_device { struct gpio_desc *descs; int base; u16 ngpio; + bool can_sleep; const char *label; void *data; struct list_head list; From patchwork Thu Feb 8 09:59:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771398 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (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 3EC1A7691B for ; Thu, 8 Feb 2024 09:59:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386391; cv=none; b=oaC8Cl0PiQIBK/WGQOn+Fh+ejQsmil6joj+u6QNRKXuyzHdieH+DbPUYfPs4mtxN0Bsoe7m72dBYdb6CWR4xyE7PLDFJpHfbiXdhbSu1cmY/e31ZBAcEp4wmzNWaF2USqQEfdokoBnFZthFXm4vhrAA+Q1tbc2JfZJfJfz5OLlw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386391; c=relaxed/simple; bh=ix1NDNjlZCgQ+AyPAHUAD23avjxqKqI95b0uzY3jkfo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RvkBIlVwtpifQs+9+RwQPwcr3U5adRJ5mQzEAUbyEgtVREWDzF39d4ccvTJWjXHIEGHR/kIvImEd1fCaIcoLWIeuS6dpUwHidXqOMiYhTsGOYhbXQJQsQHTAlIByPOQnCtxQIb4FRp/389juZYhH10+3kATpaMVBQ7ghaw2J1CI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=tXSp842q; arc=none smtp.client-ip=209.85.167.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="tXSp842q" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-51171c9f4c0so149513e87.3 for ; Thu, 08 Feb 2024 01:59:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386387; x=1707991187; 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=miEDCzzmUK7ZFiY500smqwcP1l0x+JqtxelPsuadET4=; b=tXSp842qD9Pz+jkpwOWfJkue+qMBR/rEaClQUoLEuU5gJ71adSj9UUJq3TMANRIOxY fKNqag9BENGfbPM/dyjOsePEzKNlcDuyUw22Gx5kqpOtUrV9wNgxudi5Yb6xRM4/W+yJ +R9xKxeXTCNZeFKE+JqFSkz6xWxxo1YRYUsoOc4jgHJN9Tzh1ElVUo56TT1F00815tsi DEMS7+8PSLrphN2JOvjTQN8/5YxuKctFKODip42Aihu+a+Ro/FnylPtajk2LyU7AqOFI mStAK2FpT041kNQPGVK7Hdf38EztMcE88qgGPywnkZt3li9xjOWi0o13y1ucv5G9ocx9 BXuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386387; x=1707991187; 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=miEDCzzmUK7ZFiY500smqwcP1l0x+JqtxelPsuadET4=; b=JAS0vVw4wxwQjGkw38YIYBqJATtVUeN4F4wMRvpOnYByzN464Wjks/SjfPz4AdMmFb xsECeEL97EC/6JT9s0NS5w9/X55syCnQYkulCeaTyNWoBMSaR1bnxUFji6AC22z/oG0u iWuFFNYVEqxpatdTaPQF3bk45dJW9PfqoMAYqs/2bq5z0hrzWC+czoplKMOS4pW+0dpl XRNEI5DpzrBwuD9AfbNS/RfFWReSYvzxAUBsdJwt1X8WdPEUFTKRYuSgOw3km2BD8/hi KWgOT+1tR3ca5/9x2+wSX/kaj3J350IBF2rGfc0VNpxg9tfcOYnYNsh9gQ+hdigcgmGp 2SFg== X-Gm-Message-State: AOJu0YyCP6ZIDf572f0fGVjoXGDXe3NXqiISrgrg2ICq/buGMjQWk9oT si/Sw0h06TqPd6I3OKgF3EdRHeX3h12VAIHjyd8TTg2NHJJdwdK/pxLU0BTrL5k= X-Google-Smtp-Source: AGHT+IEdpn6LGQp8OBAFzoOjlSEX9Ftw8yJ2XbLP1JUnK7lkUl5Y5jb8ffeZO4cAFqabnXqCUjG7fg== X-Received: by 2002:a05:6512:684:b0:511:62f5:9068 with SMTP id t4-20020a056512068400b0051162f59068mr4182575lfe.11.1707386387052; Thu, 08 Feb 2024 01:59:47 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWlasTr89EWG4MCypFP9JeKiFH4l+rVCnhBP8Uo4QVe68AxuApbgrgW9oW3GoWVf+5jPSkyRN7ulizA1N4OppzGlNkotGDWwR/XZd+JjJn8gGtZjf2A1Zh4DLokunxBbft2LRdhcMHryl67aBjshmGtpSxhBKfCSAJ+j0811gHTMqqwakknQWjNl8e1nz6N2KBOo/v1uorgrarthg2vKVxIfektlpDMxESFKooB9I6HLCqV/XN0Z9ZxyQxBjfHQd18sOGnDOv0Ng8JdsoujCNfY5ujCfH95F6EFiU397n05myztc5c7Pl0akzZLoL9oFBBjWntMQHYQcZz9LJ8ykg6RmI3gDr9LjA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:46 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 21/24] gpio: add SRCU infrastructure to struct gpio_device Date: Thu, 8 Feb 2024 10:59:17 +0100 Message-Id: <20240208095920.8035-22-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Add the SRCU struct to GPIO device. It will be used to serialize access to the GPIO chip pointer. Initialize and clean it up where applicable. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 13 ++++++++++--- drivers/gpio/gpiolib.h | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 6b696087c7f5..9b1907f3e400 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -680,6 +680,7 @@ static void gpiodev_release(struct device *dev) ida_free(&gpio_ida, gdev->id); kfree_const(gdev->label); kfree(gdev->descs); + cleanup_srcu_struct(&gdev->srcu); kfree(gdev); } @@ -948,6 +949,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); init_rwsem(&gdev->sem); + ret = init_srcu_struct(&gdev->srcu); + if (ret) + goto err_remove_from_list; + #ifdef CONFIG_PINCTRL INIT_LIST_HEAD(&gdev->pin_ranges); #endif @@ -955,15 +960,15 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, if (gc->names) { ret = gpiochip_set_desc_names(gc); if (ret) - goto err_remove_from_list; + goto err_cleanup_gdev_srcu; } ret = gpiochip_set_names(gc); if (ret) - goto err_remove_from_list; + goto err_cleanup_gdev_srcu; ret = gpiochip_init_valid_mask(gc); if (ret) - goto err_remove_from_list; + goto err_cleanup_gdev_srcu; ret = of_gpiochip_add(gc); if (ret) @@ -1043,6 +1048,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gpio_device_put(gdev); goto err_print_message; } +err_cleanup_gdev_srcu: + cleanup_srcu_struct(&gdev->srcu); err_remove_from_list: scoped_guard(mutex, &gpio_devices_lock) list_del_rcu(&gdev->list); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 43ff4931e2c3..35d71e30c546 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -49,6 +49,7 @@ * @sem: protects the structure from a NULL-pointer dereference of @chip by * user-space operations when the device gets unregistered during * a hot-unplug event + * @srcu: protects the pointer to the underlying GPIO chip * @pin_ranges: range of pins served by the GPIO driver * * This state container holds most of the runtime variable data @@ -73,6 +74,7 @@ struct gpio_device { struct blocking_notifier_head line_state_notifier; struct blocking_notifier_head device_notifier; struct rw_semaphore sem; + struct srcu_struct srcu; #ifdef CONFIG_PINCTRL /* From patchwork Thu Feb 8 09:59:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771072 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 13CB477628 for ; Thu, 8 Feb 2024 09:59:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386393; cv=none; b=bwUlgL0xmTPXXJG3qh6T80SeKV+15DFTOfgUJhWvialzsiqhaaYtVrs+/emXSaX/8C6S7v4stXqOPsF1Vl1BDpCg2kh3bmOL/2mUGy5FMxQGFW6pSaezIkiTqhxXbAVwlf2C03WyPs/5P+G7lHIzbCuS25eIZePVip5ywrADOU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386393; c=relaxed/simple; bh=xS/4OdoDE5ZEccFWNqfOzxqqoxMbELyyd+PWeEwUUdQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qThge7c5SZq2jO/OHlklLmtJXJgeypyt80edjly9UkXy7Su4IUEXpbiaA41sX2PfBgKT1WiC1FezwqBcVWPN6UmCTxPqW0rtgLTBbsy8DGnm4ECEUa/ThHUkEZgSwiXadeUzIyvPuETY4FTqF7Blj6a2Dpxrq+SoJUG4aDdViUk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=qbWC0M9w; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="qbWC0M9w" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-33b2fba3176so970578f8f.0 for ; Thu, 08 Feb 2024 01:59:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386388; x=1707991188; 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=Evr1zkQTxWocQOGuDvfMfIJc+Cjuk/spUXcqLuZqEv8=; b=qbWC0M9wnaNcCjtJBEiYNlQIoTHAmhMd/oJpCn0CCPWyGDUWg9oqHh/10LyWtuHwL3 wJAAI6iqRXdT5flb5DCgieRK2WuZP79px4T/E7u1g32JJkqmbnaQu1A7Azz9CxyjmQxc ZYXkaqwIRLH86xUXLmJ4lRRGheL82T3tdprOWKde+aMP1pR8PMHhyeLAqxHOR+gKrSLm OpsVHmi7PtEEQANcUbJFbwbrZbPb70+zS8L26X7hlTfwz0NpiL62wkBL+Z34F5J/UYyO K0/iaa3teNWHT1b2z7yei8DhOEZ7aj++nD6hWCdEREZWP7ZX5OVW3WoqkJhjNH3DWHuy cPWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386388; x=1707991188; 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=Evr1zkQTxWocQOGuDvfMfIJc+Cjuk/spUXcqLuZqEv8=; b=fAC/Pzt9k2C3zXvqg+1OQy2md5zyojO6yho3uF4YIrNI24CdvsS//C0qwz0X/r9v5h ndCuE71Fne2XDpff5x0mH9bpYVedQYuGGK+WnKmwd5QyWAHWLP8MyJ7x1C6TGggZ/yMh LxlKKm2c2o/6YT4XtdtQR3EBHaYaHY8BcD1qy2YXtExQVLQibLVlIzumM0or0V08O0cB BuXQ6kCz1wA5k0YUtV3SaUvcbfweJdpnePn9YUva4xjasHygw194FMAL+lJJVsC+pxOE Qi/vnDoLPNk3cS9NxypH4udD03/oscJqi2RH5Unv6csJRt+cGXKlUb0oYEp+mAdHEeUM kQ0w== X-Gm-Message-State: AOJu0YyRyg/g9JLxjlucsA7ogKWO9Gb4FRCawRVY+dtm6ewRe0Vc/CXy lxj5/+VMvRamh1XP5A1wgq4ClA7/xx509DWjjujpXtt2QM9ZSlKiE8fOu2KDqVw= X-Google-Smtp-Source: AGHT+IE2kndSqOqMwW3qrnRsGs6HYNyjJsINkxFtiW+0AsGSr3MX2QzD2Lq5TFRVdkcKW0jQ2J8qow== X-Received: by 2002:a5d:4576:0:b0:33b:2da1:fdd8 with SMTP id a22-20020a5d4576000000b0033b2da1fdd8mr5802411wrc.20.1707386388148; Thu, 08 Feb 2024 01:59:48 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV5YZCZeou2O2fmQmzQhWIqludXa6ICqVy+70PVrX7xFtCnll7GL+LcC/K7Wc/znKxTCXwSVckFScPDm6z+YcNgze4o0eYRo8nDRriyNlAzEVwPJA5Ejp6NpITWTwJWdsen3dW4kOorbhUzBGvjR5QAMw9O6+ghufVHDKIohqSh/zD1eHytqW9Co/H4ifqrzEEuYgFHbt8YBQG+nGhK8F5LlkhCEDrR4kvj4wkqslPPz9U1ERq153Dpj7MZBq9KpOW41g3UCz/rjLYw3GAch37INPwNDvcAR43QRIB5Z8S8py3ngafXl6B9OFuCd2ehUsu4uDnIunryxMGanKJdSPPvpOmEjSARJQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:47 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 22/24] gpio: protect the pointer to gpio_chip in gpio_device with SRCU Date: Thu, 8 Feb 2024 10:59:18 +0100 Message-Id: <20240208095920.8035-23-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski Ensure we cannot crash if the GPIO device gets unregistered (and the chip pointer set to NULL) during any of the API calls. To that end: wait for all users of gdev->chip to exit their read-only SRCU critical sections in gpiochip_remove(). For brevity: add a guard class which can be instantiated at the top of every function requiring read-only access to the chip pointer and use it in all API calls taking a GPIO descriptor as argument. In places where we only deal with the GPIO device - use regular guard() helpers and rcu_dereference() for chip access. Do the same in API calls taking a const pointer to gpio_desc. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 63 +++++---- drivers/gpio/gpiolib-sysfs.c | 57 +++++--- drivers/gpio/gpiolib.c | 257 +++++++++++++++++++++++------------ drivers/gpio/gpiolib.h | 22 ++- 4 files changed, 271 insertions(+), 128 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index e993c6a7215a..ccdeed013f6b 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -205,9 +205,9 @@ static long linehandle_ioctl(struct file *file, unsigned int cmd, unsigned int i; int ret; - guard(rwsem_read)(&lh->gdev->sem); + guard(srcu)(&lh->gdev->srcu); - if (!lh->gdev->chip) + if (!rcu_dereference(lh->gdev->chip)) return -ENODEV; switch (cmd) { @@ -1520,9 +1520,9 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, struct linereq *lr = file->private_data; void __user *ip = (void __user *)arg; - guard(rwsem_read)(&lr->gdev->sem); + guard(srcu)(&lr->gdev->srcu); - if (!lr->gdev->chip) + if (!rcu_dereference(lr->gdev->chip)) return -ENODEV; switch (cmd) { @@ -1551,9 +1551,9 @@ static __poll_t linereq_poll(struct file *file, struct linereq *lr = file->private_data; __poll_t events = 0; - guard(rwsem_read)(&lr->gdev->sem); + guard(srcu)(&lr->gdev->srcu); - if (!lr->gdev->chip) + if (!rcu_dereference(lr->gdev->chip)) return EPOLLHUP | EPOLLERR; poll_wait(file, &lr->wait, wait); @@ -1573,9 +1573,9 @@ static ssize_t linereq_read(struct file *file, char __user *buf, ssize_t bytes_read = 0; int ret; - guard(rwsem_read)(&lr->gdev->sem); + guard(srcu)(&lr->gdev->srcu); - if (!lr->gdev->chip) + if (!rcu_dereference(lr->gdev->chip)) return -ENODEV; if (count < sizeof(le)) @@ -1874,9 +1874,9 @@ static __poll_t lineevent_poll(struct file *file, struct lineevent_state *le = file->private_data; __poll_t events = 0; - guard(rwsem_read)(&le->gdev->sem); + guard(srcu)(&le->gdev->srcu); - if (!le->gdev->chip) + if (!rcu_dereference(le->gdev->chip)) return EPOLLHUP | EPOLLERR; poll_wait(file, &le->wait, wait); @@ -1912,9 +1912,9 @@ static ssize_t lineevent_read(struct file *file, char __user *buf, ssize_t ge_size; int ret; - guard(rwsem_read)(&le->gdev->sem); + guard(srcu)(&le->gdev->srcu); - if (!le->gdev->chip) + if (!rcu_dereference(le->gdev->chip)) return -ENODEV; /* @@ -1995,9 +1995,9 @@ static long lineevent_ioctl(struct file *file, unsigned int cmd, void __user *ip = (void __user *)arg; struct gpiohandle_data ghd; - guard(rwsem_read)(&le->gdev->sem); + guard(srcu)(&le->gdev->srcu); - if (!le->gdev->chip) + if (!rcu_dereference(le->gdev->chip)) return -ENODEV; /* @@ -2295,10 +2295,13 @@ static void gpio_v2_line_info_changed_to_v1( static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_v2_line_info *info) { - struct gpio_chip *gc = desc->gdev->chip; unsigned long dflags; const char *label; + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return; + memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); @@ -2331,8 +2334,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, test_bit(FLAG_USED_AS_IRQ, &dflags) || test_bit(FLAG_EXPORT, &dflags) || test_bit(FLAG_SYSFS, &dflags) || - !gpiochip_line_is_valid(gc, info->offset) || - !pinctrl_gpio_can_use_line(gc, info->offset)) + !gpiochip_line_is_valid(guard.gc, info->offset) || + !pinctrl_gpio_can_use_line(guard.gc, info->offset)) info->flags |= GPIO_V2_LINE_FLAG_USED; if (test_bit(FLAG_IS_OUT, &dflags)) @@ -2505,10 +2508,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) struct gpio_device *gdev = cdev->gdev; void __user *ip = (void __user *)arg; - guard(rwsem_read)(&gdev->sem); + guard(srcu)(&gdev->srcu); /* We fail any subsequent ioctl():s when the chip is gone */ - if (!gdev->chip) + if (!rcu_dereference(gdev->chip)) return -ENODEV; /* Fill in the struct and pass to userspace */ @@ -2591,9 +2594,9 @@ static __poll_t lineinfo_watch_poll(struct file *file, struct gpio_chardev_data *cdev = file->private_data; __poll_t events = 0; - guard(rwsem_read)(&cdev->gdev->sem); + guard(srcu)(&cdev->gdev->srcu); - if (!cdev->gdev->chip) + if (!rcu_dereference(cdev->gdev->chip)) return EPOLLHUP | EPOLLERR; poll_wait(file, &cdev->wait, pollt); @@ -2614,9 +2617,9 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, int ret; size_t event_size; - guard(rwsem_read)(&cdev->gdev->sem); + guard(srcu)(&cdev->gdev->srcu); - if (!cdev->gdev->chip) + if (!rcu_dereference(cdev->gdev->chip)) return -ENODEV; #ifndef CONFIG_GPIO_CDEV_V1 @@ -2691,10 +2694,10 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) struct gpio_chardev_data *cdev; int ret = -ENOMEM; - guard(rwsem_read)(&gdev->sem); + guard(srcu)(&gdev->srcu); /* Fail on open if the backing gpiochip is gone */ - if (!gdev->chip) + if (!rcu_dereference(gdev->chip)) return -ENODEV; cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); @@ -2781,6 +2784,7 @@ static const struct file_operations gpio_fileops = { int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) { + struct gpio_chip *gc; int ret; cdev_init(&gdev->chrdev, &gpio_fileops); @@ -2791,8 +2795,13 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) if (ret) return ret; - chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", - MAJOR(devt), gdev->id); + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) + return -ENODEV; + + chip_dbg(gc, "added GPIO chardev (%d:%d)\n", MAJOR(devt), gdev->id); return 0; } diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 2e3edf41e853..9f7b5e20fc25 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -171,6 +171,10 @@ static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags) unsigned long irq_flags; int ret; + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + data->irq = gpiod_to_irq(desc); if (data->irq < 0) return -EIO; @@ -195,7 +199,7 @@ static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags) * Remove this redundant call (along with the corresponding * unlock) when those drivers have been fixed. */ - ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc)); + ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); if (ret < 0) goto err_put_kn; @@ -209,7 +213,7 @@ static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags) return 0; err_unlock: - gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc)); + gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); err_put_kn: sysfs_put(data->value_kn); @@ -225,9 +229,13 @@ static void gpio_sysfs_free_irq(struct device *dev) struct gpiod_data *data = dev_get_drvdata(dev); struct gpio_desc *desc = data->desc; + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return; + data->irq_flags = 0; free_irq(data->irq, data); - gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc)); + gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); sysfs_put(data->value_kn); } @@ -444,13 +452,12 @@ static ssize_t export_store(const struct class *class, const char *buf, size_t len) { struct gpio_desc *desc; - struct gpio_chip *gc; int status, offset; long gpio; status = kstrtol(buf, 0, &gpio); - if (status < 0) - goto done; + if (status) + return status; desc = gpio_to_desc(gpio); /* reject invalid GPIOs */ @@ -458,9 +465,13 @@ static ssize_t export_store(const struct class *class, pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); return -EINVAL; } - gc = desc->gdev->chip; + + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + offset = gpio_chip_hwgpio(desc); - if (!gpiochip_line_is_valid(gc, offset)) { + if (!gpiochip_line_is_valid(guard.gc, offset)) { pr_warn("%s: GPIO %ld masked\n", __func__, gpio); return -EINVAL; } @@ -563,7 +574,6 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) const char *ioname = NULL; struct gpio_device *gdev; struct gpiod_data *data; - struct gpio_chip *chip; struct device *dev; int status, offset; @@ -578,16 +588,19 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) return -EINVAL; } + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + if (!test_and_set_bit(FLAG_EXPORT, &desc->flags)) return -EPERM; gdev = desc->gdev; - chip = gdev->chip; mutex_lock(&sysfs_lock); /* check if chip is being removed */ - if (!chip || !gdev->mockdev) { + if (!gdev->mockdev) { status = -ENODEV; goto err_unlock; } @@ -606,14 +619,14 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) data->desc = desc; mutex_init(&data->mutex); - if (chip->direction_input && chip->direction_output) + if (guard.gc->direction_input && guard.gc->direction_output) data->direction_can_change = direction_may_change; else data->direction_can_change = false; offset = gpio_chip_hwgpio(desc); - if (chip->names && chip->names[offset]) - ioname = chip->names[offset]; + if (guard.gc->names && guard.gc->names[offset]) + ioname = guard.gc->names[offset]; dev = device_create_with_groups(&gpio_class, &gdev->dev, MKDEV(0, 0), data, gpio_groups, @@ -728,7 +741,7 @@ EXPORT_SYMBOL_GPL(gpiod_unexport); int gpiochip_sysfs_register(struct gpio_device *gdev) { - struct gpio_chip *chip = gdev->chip; + struct gpio_chip *chip; struct device *parent; struct device *dev; @@ -741,6 +754,12 @@ int gpiochip_sysfs_register(struct gpio_device *gdev) if (!class_is_registered(&gpio_class)) return 0; + guard(srcu)(&gdev->srcu); + + chip = rcu_dereference(gdev->chip); + if (!chip) + return -ENODEV; + /* * For sysfs backward compatibility we need to preserve this * preferred parenting to the gpio_chip parent field, if set. @@ -767,7 +786,7 @@ int gpiochip_sysfs_register(struct gpio_device *gdev) void gpiochip_sysfs_unregister(struct gpio_device *gdev) { struct gpio_desc *desc; - struct gpio_chip *chip = gdev->chip; + struct gpio_chip *chip; scoped_guard(mutex, &sysfs_lock) { if (!gdev->mockdev) @@ -779,6 +798,12 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev) gdev->mockdev = NULL; } + guard(srcu)(&gdev->srcu); + + chip = rcu_dereference(gdev->chip); + if (chip) + return; + /* unregister gpiod class devices owned by sysfs */ for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) { gpiod_unexport(desc); diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 9b1907f3e400..a14717a3e222 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -216,7 +216,7 @@ struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) { if (!desc) return NULL; - return desc->gdev->chip; + return rcu_dereference(desc->gdev->chip); } EXPORT_SYMBOL_GPL(gpiod_to_chip); @@ -285,7 +285,7 @@ EXPORT_SYMBOL(gpio_device_get_label); */ struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev) { - return gdev->chip; + return rcu_dereference(gdev->chip); } EXPORT_SYMBOL_GPL(gpio_device_get_chip); @@ -325,12 +325,21 @@ static int gpiochip_find_base_unlocked(int ngpio) */ int gpiod_get_direction(struct gpio_desc *desc) { - struct gpio_chip *gc; unsigned long flags; unsigned int offset; int ret; - gc = gpiod_to_chip(desc); + /* + * We cannot use VALIDATE_DESC() as we must not return 0 for a NULL + * descriptor like we usually do. + */ + if (!desc || IS_ERR(desc)) + return -EINVAL; + + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + offset = gpio_chip_hwgpio(desc); flags = READ_ONCE(desc->flags); @@ -342,10 +351,10 @@ int gpiod_get_direction(struct gpio_desc *desc) test_bit(FLAG_IS_OUT, &flags)) return 0; - if (!gc->get_direction) + if (!guard.gc->get_direction) return -ENOTSUPP; - ret = gc->get_direction(gc, offset); + ret = guard.gc->get_direction(guard.gc, offset); if (ret < 0) return ret; @@ -421,6 +430,7 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name) { struct gpio_device *gdev; struct gpio_desc *desc; + struct gpio_chip *gc; if (!name) return NULL; @@ -429,7 +439,13 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name) list_for_each_entry_srcu(gdev, &gpio_devices, list, srcu_read_lock_held(&gpio_devices_srcu)) { - for_each_gpio_desc(gdev->chip, desc) { + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) + continue; + + for_each_gpio_desc(gc, desc) { if (desc->name && !strcmp(desc->name, name)) return desc; } @@ -853,7 +869,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gdev->dev.type = &gpio_dev_type; gdev->dev.bus = &gpio_bus_type; gdev->dev.parent = gc->parent; - gdev->chip = gc; + rcu_assign_pointer(gdev->chip, gc); gc->gpiodev = gdev; gpiochip_set_data(gc, data); @@ -1097,7 +1113,8 @@ void gpiochip_remove(struct gpio_chip *gc) synchronize_srcu(&gpio_devices_srcu); /* Numb the device, cancelling all outstanding operations */ - gdev->chip = NULL; + rcu_assign_pointer(gdev->chip, NULL); + synchronize_srcu(&gdev->srcu); gpiochip_irqchip_remove(gc); acpi_gpiochip_remove(gc); of_gpiochip_remove(gc); @@ -1156,6 +1173,7 @@ struct gpio_device *gpio_device_find(void *data, void *data)) { struct gpio_device *gdev; + struct gpio_chip *gc; /* * Not yet but in the future the spinlock below will become a mutex. @@ -1166,8 +1184,13 @@ struct gpio_device *gpio_device_find(void *data, guard(srcu)(&gpio_devices_srcu); - list_for_each_entry(gdev, &gpio_devices, list) { - if (gdev->chip && match(gdev->chip, data)) + list_for_each_entry_srcu(gdev, &gpio_devices, list, + srcu_read_lock_held(&gpio_devices_srcu)) { + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + + if (gc && match(gc, data)) return gpio_device_get(gdev); } @@ -2214,10 +2237,13 @@ EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); */ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) { - struct gpio_chip *gc = desc->gdev->chip; unsigned int offset; int ret; + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + if (test_and_set_bit(FLAG_REQUESTED, &desc->flags)) return -EBUSY; @@ -2231,17 +2257,17 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) * before IRQs are enabled, for non-sleeping (SOC) GPIOs. */ - if (gc->request) { + if (guard.gc->request) { offset = gpio_chip_hwgpio(desc); - if (gpiochip_line_is_valid(gc, offset)) - ret = gc->request(gc, offset); + if (gpiochip_line_is_valid(guard.gc, offset)) + ret = guard.gc->request(guard.gc, offset); else ret = -EINVAL; if (ret) goto out_clear_bit; } - if (gc->get_direction) + if (guard.gc->get_direction) gpiod_get_direction(desc); ret = desc_set_label(desc, label ? : "?"); @@ -2308,18 +2334,18 @@ int gpiod_request(struct gpio_desc *desc, const char *label) static bool gpiod_free_commit(struct gpio_desc *desc) { - struct gpio_chip *gc; unsigned long flags; bool ret = false; might_sleep(); - gc = desc->gdev->chip; + CLASS(gpio_chip_guard, guard)(desc); + flags = READ_ONCE(desc->flags); - if (gc && test_bit(FLAG_REQUESTED, &flags)) { - if (gc->free) - gc->free(gc, gpio_chip_hwgpio(desc)); + if (guard.gc && test_bit(FLAG_REQUESTED, &flags)) { + if (guard.gc->free) + guard.gc->free(guard.gc, gpio_chip_hwgpio(desc)); clear_bit(FLAG_ACTIVE_LOW, &flags); clear_bit(FLAG_REQUESTED, &flags); @@ -2476,11 +2502,14 @@ static int gpio_set_config_with_argument(struct gpio_desc *desc, enum pin_config_param mode, u32 argument) { - struct gpio_chip *gc = desc->gdev->chip; unsigned long config; + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + config = pinconf_to_config_packed(mode, argument); - return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config); + return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); } static int gpio_set_config_with_argument_optional(struct gpio_desc *desc, @@ -2570,18 +2599,20 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce) */ int gpiod_direction_input(struct gpio_desc *desc) { - struct gpio_chip *gc; int ret = 0; VALIDATE_DESC(desc); - gc = desc->gdev->chip; + + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; /* * It is legal to have no .get() and .direction_input() specified if * the chip is output-only, but you can't specify .direction_input() * and not support the .get() operation, that doesn't make sense. */ - if (!gc->get && gc->direction_input) { + if (!guard.gc->get && guard.gc->direction_input) { gpiod_warn(desc, "%s: missing get() but have direction_input()\n", __func__); @@ -2594,10 +2625,12 @@ int gpiod_direction_input(struct gpio_desc *desc) * direction (if .get_direction() is supported) else we silently * assume we are in input mode after this. */ - if (gc->direction_input) { - ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); - } else if (gc->get_direction && - (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { + if (guard.gc->direction_input) { + ret = guard.gc->direction_input(guard.gc, + gpio_chip_hwgpio(desc)); + } else if (guard.gc->get_direction && + (guard.gc->get_direction(guard.gc, + gpio_chip_hwgpio(desc)) != 1)) { gpiod_warn(desc, "%s: missing direction_input() operation and line is output\n", __func__); @@ -2616,28 +2649,31 @@ EXPORT_SYMBOL_GPL(gpiod_direction_input); static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) { - struct gpio_chip *gc = desc->gdev->chip; - int val = !!value; - int ret = 0; + int val = !!value, ret = 0; + + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; /* * It's OK not to specify .direction_output() if the gpiochip is * output-only, but if there is then not even a .set() operation it * is pretty tricky to drive the output line. */ - if (!gc->set && !gc->direction_output) { + if (!guard.gc->set && !guard.gc->direction_output) { gpiod_warn(desc, "%s: missing set() and direction_output() operations\n", __func__); return -EIO; } - if (gc->direction_output) { - ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); + if (guard.gc->direction_output) { + ret = guard.gc->direction_output(guard.gc, + gpio_chip_hwgpio(desc), val); } else { /* Check that we are in output mode if we can */ - if (gc->get_direction && - gc->get_direction(gc, gpio_chip_hwgpio(desc))) { + if (guard.gc->get_direction && + guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) { gpiod_warn(desc, "%s: missing direction_output() operation\n", __func__); @@ -2647,7 +2683,7 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) * If we can't actively set the direction, we are some * output-only chip, so just drive the output as desired. */ - gc->set(gc, gpio_chip_hwgpio(desc), val); + guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), val); } if (!ret) @@ -2763,17 +2799,20 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output); int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags) { int ret = 0; - struct gpio_chip *gc; VALIDATE_DESC(desc); - gc = desc->gdev->chip; - if (!gc->en_hw_timestamp) { + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + + if (!guard.gc->en_hw_timestamp) { gpiod_warn(desc, "%s: hw ts not supported\n", __func__); return -ENOTSUPP; } - ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags); + ret = guard.gc->en_hw_timestamp(guard.gc, + gpio_chip_hwgpio(desc), flags); if (ret) gpiod_warn(desc, "%s: hw ts request failed\n", __func__); @@ -2792,17 +2831,20 @@ EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns); int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags) { int ret = 0; - struct gpio_chip *gc; VALIDATE_DESC(desc); - gc = desc->gdev->chip; - if (!gc->dis_hw_timestamp) { + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + + if (!guard.gc->dis_hw_timestamp) { gpiod_warn(desc, "%s: hw ts not supported\n", __func__); return -ENOTSUPP; } - ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags); + ret = guard.gc->dis_hw_timestamp(guard.gc, gpio_chip_hwgpio(desc), + flags); if (ret) gpiod_warn(desc, "%s: hw ts release failed\n", __func__); @@ -2821,12 +2863,13 @@ EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns); */ int gpiod_set_config(struct gpio_desc *desc, unsigned long config) { - struct gpio_chip *gc; - VALIDATE_DESC(desc); - gc = desc->gdev->chip; - return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config); + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + + return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); } EXPORT_SYMBOL_GPL(gpiod_set_config); @@ -2924,10 +2967,19 @@ static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *des static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) { + struct gpio_device *gdev; struct gpio_chip *gc; int value; - gc = desc->gdev->chip; + /* FIXME Unable to use gpio_chip_guard due to const desc. */ + gdev = desc->gdev; + + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) + return -ENODEV; + value = gpio_chip_get_value(gc, desc); value = value < 0 ? value : !!value; trace_gpio_value(desc_to_gpio(desc), 1, value); @@ -2953,6 +3005,14 @@ static int gpio_chip_get_multiple(struct gpio_chip *gc, return -EIO; } +/* The 'other' chip must be protected with its GPIO device's SRCU. */ +static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc) +{ + guard(srcu)(&gdev->srcu); + + return gc == rcu_dereference(gdev->chip); +} + int gpiod_get_array_value_complex(bool raw, bool can_sleep, unsigned int array_size, struct gpio_desc **desc_array, @@ -2990,33 +3050,36 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep, } while (i < array_size) { - struct gpio_chip *gc = desc_array[i]->gdev->chip; DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO); DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO); unsigned long *mask, *bits; int first, j; - if (likely(gc->ngpio <= FASTPATH_NGPIO)) { + CLASS(gpio_chip_guard, guard)(desc_array[i]); + if (!guard.gc) + return -ENODEV; + + if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { mask = fastpath_mask; bits = fastpath_bits; } else { gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC; - mask = bitmap_alloc(gc->ngpio, flags); + mask = bitmap_alloc(guard.gc->ngpio, flags); if (!mask) return -ENOMEM; - bits = bitmap_alloc(gc->ngpio, flags); + bits = bitmap_alloc(guard.gc->ngpio, flags); if (!bits) { bitmap_free(mask); return -ENOMEM; } } - bitmap_zero(mask, gc->ngpio); + bitmap_zero(mask, guard.gc->ngpio); if (!can_sleep) - WARN_ON(gc->can_sleep); + WARN_ON(guard.gc->can_sleep); /* collect all inputs belonging to the same chip */ first = i; @@ -3031,9 +3094,9 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep, i = find_next_zero_bit(array_info->get_mask, array_size, i); } while ((i < array_size) && - (desc_array[i]->gdev->chip == gc)); + gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); - ret = gpio_chip_get_multiple(gc, mask, bits); + ret = gpio_chip_get_multiple(guard.gc, mask, bits); if (ret) { if (mask != fastpath_mask) bitmap_free(mask); @@ -3174,14 +3237,16 @@ EXPORT_SYMBOL_GPL(gpiod_get_array_value); */ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) { - int ret = 0; - struct gpio_chip *gc = desc->gdev->chip; - int offset = gpio_chip_hwgpio(desc); + int ret = 0, offset = gpio_chip_hwgpio(desc); + + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return; if (value) { - ret = gc->direction_input(gc, offset); + ret = guard.gc->direction_input(guard.gc, offset); } else { - ret = gc->direction_output(gc, offset, 0); + ret = guard.gc->direction_output(guard.gc, offset, 0); if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } @@ -3199,16 +3264,18 @@ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) */ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) { - int ret = 0; - struct gpio_chip *gc = desc->gdev->chip; - int offset = gpio_chip_hwgpio(desc); + int ret = 0, offset = gpio_chip_hwgpio(desc); + + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return; if (value) { - ret = gc->direction_output(gc, offset, 1); + ret = guard.gc->direction_output(guard.gc, offset, 1); if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } else { - ret = gc->direction_input(gc, offset); + ret = guard.gc->direction_input(guard.gc, offset); } trace_gpio_direction(desc_to_gpio(desc), !value, ret); if (ret < 0) @@ -3219,11 +3286,12 @@ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) { - struct gpio_chip *gc; + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return; - gc = desc->gdev->chip; trace_gpio_value(desc_to_gpio(desc), 0, value); - gc->set(gc, gpio_chip_hwgpio(desc), value); + guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), value); } /* @@ -3284,33 +3352,36 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep, } while (i < array_size) { - struct gpio_chip *gc = desc_array[i]->gdev->chip; DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO); DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO); unsigned long *mask, *bits; int count = 0; - if (likely(gc->ngpio <= FASTPATH_NGPIO)) { + CLASS(gpio_chip_guard, guard)(desc_array[i]); + if (!guard.gc) + return -ENODEV; + + if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { mask = fastpath_mask; bits = fastpath_bits; } else { gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC; - mask = bitmap_alloc(gc->ngpio, flags); + mask = bitmap_alloc(guard.gc->ngpio, flags); if (!mask) return -ENOMEM; - bits = bitmap_alloc(gc->ngpio, flags); + bits = bitmap_alloc(guard.gc->ngpio, flags); if (!bits) { bitmap_free(mask); return -ENOMEM; } } - bitmap_zero(mask, gc->ngpio); + bitmap_zero(mask, guard.gc->ngpio); if (!can_sleep) - WARN_ON(gc->can_sleep); + WARN_ON(guard.gc->can_sleep); do { struct gpio_desc *desc = desc_array[i]; @@ -3346,10 +3417,10 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep, i = find_next_zero_bit(array_info->set_mask, array_size, i); } while ((i < array_size) && - (desc_array[i]->gdev->chip == gc)); + gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); /* push collected bits to outputs */ if (count != 0) - gpio_chip_set_multiple(gc, mask, bits); + gpio_chip_set_multiple(guard.gc, mask, bits); if (mask != fastpath_mask) bitmap_free(mask); @@ -3505,6 +3576,7 @@ EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); */ int gpiod_to_irq(const struct gpio_desc *desc) { + struct gpio_device *gdev; struct gpio_chip *gc; int offset; @@ -3516,7 +3588,13 @@ int gpiod_to_irq(const struct gpio_desc *desc) if (!desc || IS_ERR(desc)) return -EINVAL; - gc = desc->gdev->chip; + gdev = desc->gdev; + /* FIXME Cannot use gpio_chip_guard due to const desc. */ + guard(srcu)(&gdev->srcu); + gc = rcu_dereference(gdev->chip); + if (!gc) + return -ENODEV; + offset = gpio_chip_hwgpio(desc); if (gc->to_irq) { int retirq = gc->to_irq(gc, offset); @@ -4696,12 +4774,20 @@ core_initcall(gpiolib_dev_init); static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) { - struct gpio_chip *gc = gdev->chip; bool active_low, is_irq, is_out; unsigned int gpio = gdev->base; struct gpio_desc *desc; + struct gpio_chip *gc; int value; + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) { + seq_puts(s, "Underlying GPIO chip is gone\n"); + return; + } + for_each_gpio_desc(gc, desc) { guard(srcu)(&desc->srcu); if (test_bit(FLAG_REQUESTED, &desc->flags)) { @@ -4776,9 +4862,12 @@ static int gpiolib_seq_show(struct seq_file *s, void *v) { struct gpiolib_seq_priv *priv = s->private; struct gpio_device *gdev = v; - struct gpio_chip *gc = gdev->chip; + struct gpio_chip *gc; struct device *parent; + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); if (!gc) { seq_printf(s, "%s%s: (dangling chip)", priv->newline ? "\n" : "", diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 35d71e30c546..b3810f7d286a 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -63,7 +63,7 @@ struct gpio_device { int id; struct device *mockdev; struct module *owner; - struct gpio_chip *chip; + struct gpio_chip __rcu *chip; struct gpio_desc *descs; int base; u16 ngpio; @@ -193,6 +193,26 @@ struct gpio_desc { #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) +struct gpio_chip_guard { + struct gpio_device *gdev; + struct gpio_chip *gc; + int idx; +}; + +DEFINE_CLASS(gpio_chip_guard, + struct gpio_chip_guard, + srcu_read_unlock(&_T.gdev->srcu, _T.idx), + ({ + struct gpio_chip_guard _guard; + + _guard.gdev = desc->gdev; + _guard.idx = srcu_read_lock(&_guard.gdev->srcu); + _guard.gc = rcu_dereference(_guard.gdev->chip); + + _guard; + }), + struct gpio_desc *desc) + int gpiod_request(struct gpio_desc *desc, const char *label); void gpiod_free(struct gpio_desc *desc); From patchwork Thu Feb 8 09:59:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771397 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 6448073181 for ; Thu, 8 Feb 2024 09:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386393; cv=none; b=KoUoaCIFsTq5/6qLfHluCLKvsPgbWXaz6FeS6MH0adw5p5k1Lbms3NfwTD/QNYaX02j62aER8so9/nD97OJPomfkHKFjiPib4CxJB0K/aftnCMGVnZ0MXM+xDy7UnGZom9APtRruoiERSJ4G0kNUvyHV6o+Csdnz77F9HzokIpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386393; c=relaxed/simple; bh=KjuEOV2DTy6W3rRBoC+IRpehjM/uvenxd3UaC9zFfmY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c39YsIV3e2GHFR2BDxPKgI0Lu6CujHJLQ1E3UxdUN9+abxrV+8BD0E2v58dpt0NuKfrg1PCbpAz/mmZTMrsMvU8C7OQXluD4aYu/9nRpP2mMIwNXRphEv6SlLUYk8gxSVLxA1XdMliWKtNPoNT69rBQLVqHWuUmbmqz0uGpJb5U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=gB1MT/Ge; arc=none smtp.client-ip=209.85.167.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="gB1MT/Ge" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-511717cfa62so179870e87.3 for ; Thu, 08 Feb 2024 01:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386389; x=1707991189; 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=TyblqrzdktkrOu3Ks/tkxPwfRHlxiz4guYvygTG0Ma4=; b=gB1MT/GeiQabI6+7pb7AJk5rqr5su0AfjqN2cPVKAlFTiWh5a4rzl4l39UoSJ10uVM xBtfbKo30B7ZimPotL/v+pIiq1c44EWz3ORDGipHqgz42/c9TksilH5VF+8W0HO1LTwq 8e/8xLuzkntBzDJjPlSusPHv+19BFKQI/1FTS8Bp3oWjtMn1UUzYeIca4mLTzpz5wJT8 Jl+Iz2bcAieAKv7nJecXvKcEfi9xPrq0VfCPhzgJXC88mjO1CZjYuMEtP9g+htKRLMwX JIAxqLnO5GQDWoq0TwPx70VQBJvthytc1ohsNhsFbljPpa3uMGB3S44yoCorrfVJkvNI teUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386389; x=1707991189; 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=TyblqrzdktkrOu3Ks/tkxPwfRHlxiz4guYvygTG0Ma4=; b=m742jcSaXbmt/PfzGMiWPwV7fpE+7ywexS8yx4QVf0CCzoq81dkaWzJZiprbL4vN6o XJ6kS3YDLvaYtqpVBZx0dt9jSow7CwE4pGP9ZX96WisJpqDC203ecf4DPdYwwEM/Mqe4 ODyy3decajnIUq52oguaRP3CeHawEvYdM9WK3nK55nU1Bo6XCJuTAxF9m4M2fArQqqhx fQBHRE/CLAj9RiPb0y6dorsI1OyfXhbobprjinWuqzq6bkTTkH4agMliF3glvfjAEoVu 20KY1Dfc25EPAbuUrUItOx0VptWKReNNadBuZW4XdX8mXSu9n9OD1AAX1f+o2uxcsmWm dY9w== X-Gm-Message-State: AOJu0YzMQ4JRHrKOPEclUloEtAtN/RvAFWMLI64yw/BjNN0771JKU6A3 +4sT/pwQBXhtq/P82Kt0dYAIwd44JBDfByZN7zuxE1oNDiyF+s02r/RceiDVMAU= X-Google-Smtp-Source: AGHT+IE5JZEvxwuTlIj46DsBROoKNidH+KSkPjumTYTmU5HAW4otdlZg4czZ9z2qqNHjOm/5r+QY8Q== X-Received: by 2002:ac2:4116:0:b0:511:617a:3130 with SMTP id b22-20020ac24116000000b00511617a3130mr3899511lfi.35.1707386389457; Thu, 08 Feb 2024 01:59:49 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWsJjPanROTA8lPm9oZXRbP9FFnvouA8CoVl+/rKHJjV6K7cqHDtuwMWoZ8WJN5qNPdUiMtI7cphUZN4erNsfA8iOFd2JVopKNPJPy1hJ85KbG+TVvp51fRlMv0qfufl7o+krP55N39IKQaQtmXSXxPkjayigSZQjpag9vINi3JIV/4kpGbTxqO1cMLuM1en8W6EaUCfuvvcbx0xTonazyB+5W1Rp++ZqQckNqWAftT7zrbmib2Qknh2pgw0Dx5HPKG0t4HgPROsDefVKlzzDnmmuzwAVOiTQNxIQzzsMnmuVeSp+nKDzHxqhYt3ZzZxiGlm2jNrNQbXHJP4FJGXZwnIM0l9Asj9w== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:48 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 23/24] gpio: remove the RW semaphore from the GPIO device Date: Thu, 8 Feb 2024 10:59:19 +0100 Message-Id: <20240208095920.8035-24-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski With all accesses to gdev->chip being protected with SRCU, we can now remove the RW-semaphore specific to the character device which fullfilled the same role up to this point. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-cdev.c | 1 - drivers/gpio/gpiolib.c | 4 ---- drivers/gpio/gpiolib.h | 5 ----- 3 files changed, 10 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index ccdeed013f6b..9323b357df43 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index a14717a3e222..97829f0c8487 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -963,7 +963,6 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); - init_rwsem(&gdev->sem); ret = init_srcu_struct(&gdev->srcu); if (ret) @@ -1102,8 +1101,6 @@ void gpiochip_remove(struct gpio_chip *gc) struct gpio_device *gdev = gc->gpiodev; unsigned int i; - down_write(&gdev->sem); - /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ gpiochip_sysfs_unregister(gdev); gpiochip_free_hogs(gc); @@ -1142,7 +1139,6 @@ void gpiochip_remove(struct gpio_chip *gc) * gone. */ gcdev_unregister(gdev); - up_write(&gdev->sem); gpio_device_put(gdev); } EXPORT_SYMBOL_GPL(gpiochip_remove); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index b3810f7d286a..07443d26cbca 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #define GPIOCHIP_NAME "gpiochip" @@ -46,9 +45,6 @@ * requested, released or reconfigured * @device_notifier: used to notify character device wait queues about the GPIO * device being unregistered - * @sem: protects the structure from a NULL-pointer dereference of @chip by - * user-space operations when the device gets unregistered during - * a hot-unplug event * @srcu: protects the pointer to the underlying GPIO chip * @pin_ranges: range of pins served by the GPIO driver * @@ -73,7 +69,6 @@ struct gpio_device { struct list_head list; struct blocking_notifier_head line_state_notifier; struct blocking_notifier_head device_notifier; - struct rw_semaphore sem; struct srcu_struct srcu; #ifdef CONFIG_PINCTRL From patchwork Thu Feb 8 09:59:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 771071 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 24FB979927 for ; Thu, 8 Feb 2024 09:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386403; cv=none; b=G5CD7AwKuC4whBKhDjk7XhbS62SgMbp/Qi1M8kunc7RhPNWEb4zyX5SsuTVivh598CrSoe4HnGl21D+rPC5mbjY5k1b9Muk/4PyjjhKwHB0s9GBfrR1/E/EOq9Vvk+T8sXs9Zyn2crMiHpYE81M46YxRofbsQoOBdJK8PNqE3fg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707386403; c=relaxed/simple; bh=CtfJea0llJCBLvTJBTUKy6/8afj286rr8HrvBr31xBA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=I+qC6nkMzPBfYVat5OEIi6EiU9pbnK4+kTTVoeTbiGtevLw2uyJ6ANgDrAsfDGJzP7iumhJEzbC6o2rEoJAVh3mf1q6ruaxwPOTtu2UfvIWDqwX716ou6UjueH9N1UhVVb/5+W8O0vkTrDwwPui+lPuG9UroR3KpzhRHLTTjKv8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=lZyJUMJh; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="lZyJUMJh" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-3392b045e0aso1005404f8f.2 for ; Thu, 08 Feb 2024 01:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707386390; x=1707991190; 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=OQkXxgYTerhoxLqWK4A1mYLklWfm7Kp5H6fCKOtV4ow=; b=lZyJUMJhcFnOaL0i6TGu+B1up7B8tzncMZ/E0NKuoAY2P3obkhn9L4JdPrXwfXjG0i iIITJhIcpsNMeVE3wXB5MQQOMfPVprUHYsleuYHnTIcPHbKsSM2LsbUdrw2x04oAgMQo DZ27XBdKMQGF3Gq6aAoeRAm87FYEInaR4KzQt7NsiwEg8XqWYCl8mE5pw4gZ8KciCGn0 bYjNwY9NbPi4UbfGEQ4SBjEyoK7Xn1/8WjmWt60nIFcfqGBq4GvGU3TIYC1QDTgomMPd shsSKgx5rueSYTGF4foCW80h2GoxWoccYpEnwEv3gQ6X1Xs+LdkBubo63HSo0bJ15se5 anXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707386390; x=1707991190; 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=OQkXxgYTerhoxLqWK4A1mYLklWfm7Kp5H6fCKOtV4ow=; b=YD6Rr5yx+0S+In8NRi22XGB6gU+HyxJEfxzBrpa9UEpNFXk984lA/NH0xwe7kcpmmL BoUEJxSQVGIDT0gG7WGKEeweeaI1Hxm1sO0cB0y+DxA1r2iDHhV7Ij1jOcBWpQdi/o7l 4xwU7AQMgn8aZ+ZeMgQc1uetsYXfyiel1sXvyyx+cdPLJQSZMDx/GQRA7t/MkTUoyhyq iuxT2NEa0atT9d0hvxIAgXtyuZVLAcO+WISt9cFhcRxoTbefnJLBYXuJ02uRKKBXE4zj mtEfUAgKkYmaBh/wntuV5nLfgRZk0lUDJNuEpmp7CtT8KCegVKxl6ITAqiyErj0iLTdK VSpw== X-Gm-Message-State: AOJu0Ywdny4oFSNwYYCpfhtXEGGit06vdSJX9GoXqgcvppLUvUBzIhbd 2T1JgUzysY3Bn58f+1Ym85Ppmz8DEPq8r/DfBLc1yY8w+STH+E1Sb4jXoEyImT0= X-Google-Smtp-Source: AGHT+IGq3e298KVnbRyWWqb8ismpBx3ut4qdYui+oTCRp3zXsE+6izxgvQ0scMCLcwxnS3lYbIGtHQ== X-Received: by 2002:a5d:6da2:0:b0:33b:48ee:8a35 with SMTP id u2-20020a5d6da2000000b0033b48ee8a35mr6663999wrs.3.1707386390500; Thu, 08 Feb 2024 01:59:50 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXxrRfrkwx9pQNCPSexFg8gEuiisVSgdZDXYZemOfQu32vyLwfYaTiNTWin3thqJL+dyhNvivFtPGRwobRpYoMfOn/mOUrzvMwHWbE6NjArsDWPDj4OMDcqwqB9En/dv16KW1hPfl2a99JOlnRckxpb4NoPxIfoP70RZyn2foTURzqaYHLxASUq+yMbgrTnBtzAPmHYhmwoNe0Kw7gVS/VgP/KrVYH6qTiKaxO19iAA+Thbb9Mhojx3bDeJZx9iNgvhW2zWGv8ZCzw7JUcuFax3lDLrTk70sfGNQ6UPieT+NHJEDS0sGb240hUf5pzrDJec68rpd1DcsoAl5WTrL/k1lpWMaHhSmg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:44bd:e160:131:19a4]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0033af5086c2dsm3265963wrt.58.2024.02.08.01.59.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 01:59:50 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Kent Gibson , Alex Elder , Geert Uytterhoeven , "Paul E . McKenney" , Andy Shevchenko , Wolfram Sang Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 24/24] gpio: mark unsafe gpio_chip manipulators as deprecated Date: Thu, 8 Feb 2024 10:59:20 +0100 Message-Id: <20240208095920.8035-25-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl> References: <20240208095920.8035-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bartosz Golaszewski We still have some functions that return the address of the GPIO chip associated with the GPIO device. This is dangerous and the users should find a better solution. Let's add appropriate comments to the kernel docs. Signed-off-by: Bartosz Golaszewski Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 97829f0c8487..9f8cf064002d 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -211,6 +211,11 @@ EXPORT_SYMBOL_GPL(desc_to_gpio); /** * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs * @desc: descriptor to return the chip of + * + * *DEPRECATED* + * This function is unsafe and should not be used. Using the chip address + * without taking the SRCU read lock may result in dereferencing a dangling + * pointer. */ struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) { @@ -275,6 +280,7 @@ EXPORT_SYMBOL(gpio_device_get_label); * Returns: * Address of the GPIO chip backing this device. * + * *DEPRECATED* * Until we can get rid of all non-driver users of struct gpio_chip, we must * provide a way of retrieving the pointer to it from struct gpio_device. This * is *NOT* safe as the GPIO API is considered to be hot-unpluggable and the