From patchwork Mon Feb 5 09:33:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1895182 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=jyOguWp+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.199.223; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2942-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [147.75.199.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1S202gzz23g7 for ; Mon, 5 Feb 2024 20:34:41 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1DDD41C220A9 for ; Mon, 5 Feb 2024 09:34:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC9FC12B98; Mon, 5 Feb 2024 09:34:30 +0000 (UTC) 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="jyOguWp+" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.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 786D112B6A for ; Mon, 5 Feb 2024 09:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125670; cv=none; b=iOuUi9t/kxn9C99N9/tjmPwznWHpBKxVOwItEchkEDY8xSegTZbcyu4P08rRy9xdRcgMk7jHvFFWNKFleLGBsM/ppD5GNXwvjoEQuSstxRTLVyfFgqnx3nq8KG0G5VWXlNF6w6oKw4G3PyhzA37PBVrfOZZD5tJ6SGtdz0ofmtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125670; c=relaxed/simple; bh=0Rfbf/BB2kuh3N9x1MELf3P0npd6SPlaMiWUc84gE7I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h9bEgfYjcDHHjy1ElJrGPkHm7bGBF2kipdXXy8leBhMIgX1mGVaST++Ae4+vV5/q96OV87Zaul6oQWlLBqCF+w969z+cYPwEvY5v0UW27bAE61P+xqLYzI3fx2ioVzoUpdtXY2OF1JhzLktaukqyipceFJbIYPzZFDzvPA8BxSs= 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=jyOguWp+; arc=none smtp.client-ip=209.85.167.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 Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-51032e62171so6366491e87.3 for ; Mon, 05 Feb 2024 01:34:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125666; x=1707730466; 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=gZRS1zyu26vQVi7j92zbmxVuaJZtVgYt1D5+4xPvFSc=; b=jyOguWp+kpH09gVJsrPO5KACqVmtQ3Ge82ffKzQ6D3X52lpjz6REcBUl5zFnwf+ZSF nwoP6FOvdi4E5tG0qWbhlMsgYvn0utd3R+qe/8ud9Jw90glrAAOVJ2gu2gfVFDieyb4i /gNWUInc16TgGmfoVTwkQ4KOc+3M+iMX4buxQJaAErsID9x7AezrxfHwsBB5Ip+Sjh1B 8ZRAtLsZcGvDRd9e6EkHpe6iBi7fOW0h1uU4QoZCwGdKPiwWwLb6BKT+t2lgsJtX65yC 1cEWOAunYNRlB24e0ObVNA23TVt3+j2cofTGMl/3Qsem7Qms1oFlgK+RTg0Ih9rHazDk nYYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125666; x=1707730466; 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=gZRS1zyu26vQVi7j92zbmxVuaJZtVgYt1D5+4xPvFSc=; b=THxw+bs+telx7x1w0x172nZ+/fjlLSPap6PTRJrbtIIMu4g76Gj5AvsZRQ8fKV6zXy iAXNHSEIdXQrDMOD+QjkGXv5uz7mPYKxCv7HTl6tpT+luMj0oghKcITxa/cp68vlfUNr JMmK8VK7/gmXxXf8dAKg57LGjYTSQlRNrolRpOkQ7YLhUDZZzSUqzTJgZbqgGKUhkEa4 cKe/uCLNHh+PMDp91gABZ3oj6/j/nSG5/rfsYAvyzn44ddjtqWK+vVSI3myU5d1Zpw60 mwa0EnyXfGAKntQvBC6g5Fe1bTjpadwfF4VIRekKHB0S5PlrSLEdXuL5LdfOetVcCNCn Hnlg== X-Gm-Message-State: AOJu0YwFbKsCwgWMw8fsHclvCAnQubrCQa/6jwlet6Eqoy1vcLs3MOPm VqYFSIjiOluedoQx19Uk+kWSfwPFSP653LKMfY/OnZz7cyGzydm8qc6bfsgNe8c= X-Google-Smtp-Source: AGHT+IEd6tunhVzZkyJVEKC6oz3MaVkv9hic/ul3q8nrd/ZO1wINV/ecIdZh/NoroatsW9bNRhx2CA== X-Received: by 2002:a05:6512:499:b0:511:4e85:5b2e with SMTP id v25-20020a056512049900b005114e855b2emr1712174lfq.24.1707125666288; Mon, 05 Feb 2024 01:34:26 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU+vE/hN5OHfThEldMWTXwjgwpzbjEIJXxJbsGGLwcQ3GS81/JAXe1wWuze1QVKDWESvHMDgYFpcnOzOZNAg64butxJVKCqHwfBpyCP6I5G8vsNwhcqT0ua8ubiGtu3ywLv3Mu2HqXx0E+RGoa631p+MMEmyRNvqWFD4FbDabeyjooQobe8SgiJZ1bTu9rZ9h2K8JS2dANB4zn8pQ3jOza6khb66H6bmKwh3TtAl86R6CWO41NlYVYW/uAJLQImEyD/+3xTpB1iynNXbvPyT8bvN2/6ck7wwfd2wBD3vH7UZMsy+lljQJipgJiXgBOJAE3xNO2XRyXEVS9pPRaXQ0Jwx65HMNiVUg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34:25 -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 v2 01/23] gpio: protect the list of GPIO devices with SRCU Date: Mon, 5 Feb 2024 10:33:56 +0100 Message-Id: <20240205093418.39755-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 | 220 ++++++++++++++++++++++------------------- 1 file changed, 116 insertions(+), 104 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d50a786f8176..a14eef93ead8 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; } @@ -748,7 +752,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, @@ -813,7 +820,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; @@ -878,49 +884,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); @@ -1011,9 +1015,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: @@ -1076,8 +1080,9 @@ 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); + scoped_guard(mutex, &gpio_devices_lock) + list_del_rcu(&gdev->list); + synchronize_srcu(&gpio_devices_srcu); /* * The gpiochip side puts its use of the device to rest here: @@ -1125,7 +1130,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)) @@ -4133,30 +4138,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); @@ -4727,35 +4741,33 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) { - unsigned long flags; struct gpio_device *gdev = NULL; loff_t index = *pos; s->private = ""; - spin_lock_irqsave(&gpio_lock, flags); - list_for_each_entry(gdev, &gpio_devices, list) - if (index-- == 0) { - spin_unlock_irqrestore(&gpio_lock, flags); + guard(srcu)(&gpio_devices_srcu); + + list_for_each_entry(gdev, &gpio_devices, list) { + 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; - 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); + scoped_guard(srcu, &gpio_devices_srcu) { + if (list_is_last(&gdev->list, &gpio_devices)) + ret = NULL; + else + ret = list_first_entry(&gdev->list, struct gpio_device, + list); + } s->private = "\n"; ++*pos; From patchwork Mon Feb 5 09:33: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: 1895184 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=Hve7TEUn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.48.161; helo=sy.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2944-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org [147.75.48.161]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1SK2bd0z23g7 for ; Mon, 5 Feb 2024 20:34:57 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id ECD8CB20A08 for ; Mon, 5 Feb 2024 09:34:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9177512E46; Mon, 5 Feb 2024 09:34:32 +0000 (UTC) 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="Hve7TEUn" X-Original-To: linux-gpio@vger.kernel.org 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 76B5512B88 for ; Mon, 5 Feb 2024 09:34:29 +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=1707125672; cv=none; b=g53Rqh1lMlyWe1awYRmaO0aX+/KQZSbitLeXsUcJWqU86r0K2e9iUjCHyJXwyutgqwbI5g2E0u3GwwIYOYRt7gBUc56VZ0UQa4iF9eZ5K9f8qJu/+399nPdmQPTy9CM5Bwc36kxWpso12a9FVNwBcPPBh2E0Anq291tLNspH4nk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125672; c=relaxed/simple; bh=w9OTZVARMR4Mdnl0o0yhVNJkoS2c5kOcFylZbMhF7Oo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lIEYB4l3EmHUIh6aiswplFHJDIUUkXtrZhQ75l9/3ElDO3e0DAvb2OIIQ9eLtaBJTt+SaGuYo8+NwbUr7l5CeATiJpulcZTovG0FxWTl+wymLpEnHU1tlxmXTnlyK9hQCbYdVh0V+IRSw7E7xmV7y+D6BANKD87MTgHr0UNiYRc= 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=Hve7TEUn; 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 Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40fccd090c2so18817325e9.0 for ; Mon, 05 Feb 2024 01:34:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125667; x=1707730467; 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=mCe01XgCpcinR2htLJWjY1RLrdRFN2p3/+OpFVj9aQM=; b=Hve7TEUn3MCqrdR7JXdEyuYo2XMGXSIvIlZ3buFYvmDsjzlT4dT5PgKTr7PmY2P2JH tp2bsA7Ih1g0u0B3M0V/hL20E6AFT5olkivyqRw7llPf71cuemaQuwwrfojcs8pEP6ow j2yZ2RGos/QQMckJsbbbB86EIVAXw7xBzeSDYcOx0KlIfWhqYX8BRKXDr9xbivhfM0dJ ZMhdOYCz3O/4zc5PtbSmSFhmNBCZzWc74X+RsEfP5FPFxHMbnRPZPSgyGoBI3Q1Rxp2g IJlJcr1YulUVDhCRMxOlTmYPyKPF0+/l4yESc74/EUbe+cZD/ip2eTAX/7j8o/pXMI1f Y+TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125667; x=1707730467; 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=mCe01XgCpcinR2htLJWjY1RLrdRFN2p3/+OpFVj9aQM=; b=NNIo4nwh65SLeCKHsWjblDP4J2L/AFx5eBd7GVZGG4+f71yT8tNm0WE7G2PYKmLKUL 2oM3En2/c4bibjYtyRNaeN+oQmK6cVUjhYyrW4viaF4yJWnqSmdd2hPgq3xYkBYWfpHi GzfsRlE4TgvizkcbdDCysMbptg0UPuaby1Gq+VkMOcqwcWDwNpgjsGWEY+W1AXNd/xHF l7HfUQxS7bkOUYPmLf9majWgTk31mXA5jmmVJSrs1+EL5q07p1W2HmqYgACryIU+q5Hi eLlOxDQtV2EvtEiMBHBaTMjmG4xjLFklnwVQ2I27Zf9LMlimsnBtG2XEkD44vnSy+/Qq X4bA== X-Gm-Message-State: AOJu0YyQbdgo5sOMsDCGzIj309Z2ErJdrxgoXl9iYd0CFe1Dsx/DgXeq /J8vROMKUoXJXhNegVXR0gBWv01ghKsatgpEMsgcwet42GZFS3BMPifnFvwsUJhwkKxEqBzMUXO w X-Google-Smtp-Source: AGHT+IEL3J9p5BXWGBPz7AI47HRJRAqQdPPdU85CEgl2ZpCxmqbm2bH4OibqUJ9q4vkgL1ejZSG1uQ== X-Received: by 2002:a05:600c:1c06:b0:40f:b630:a9cb with SMTP id j6-20020a05600c1c0600b0040fb630a9cbmr4101104wms.6.1707125667557; Mon, 05 Feb 2024 01:34:27 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXxt3LeBVGvnr1P5pPQb4AEa9+llY3je+7jtCjmRAYyLAZngAs/Xcd+Umcj1bz4tdnQ/lTJcXe/qcu7VCvfQ9Vf+884tRivPpIA5/efCz3hMMp4qqTn/Aw+ALXxQhvEmaXvseYnn6EAhRHucSmg3ltSb+J1T3hJ8p9SGAXuLLh2nfCQWDT8Z+ftk096wh/wtd3nxqYaavK4GtiRPIV/fNsXpVV07A/t7R/WD5NjN6u1CyxCnkSBcgKVp1Kg83drGbbDbypO9McRBjJPb4XfgZUUSZwEjXFdh3CYRQnQ4qy99/O0ajhg8D0fnHAOLaL0LU4ycgoKQdgyWyX/3+to3ggmSbJHr92LCw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 02/23] gpio: of: assign and read the hog pointer atomically Date: Mon, 5 Feb 2024 10:33:57 +0100 Message-Id: <20240205093418.39755-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 a14eef93ead8..4c18b693d06c 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2324,7 +2324,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 Mon Feb 5 09:33: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: 1895183 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=RVuSZXDU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=139.178.88.99; helo=sv.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2943-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [139.178.88.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1SC58FJz23g7 for ; Mon, 5 Feb 2024 20:34:51 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 6F9D1283316 for ; Mon, 5 Feb 2024 09:34:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1DBAE134A6; Mon, 5 Feb 2024 09:34:32 +0000 (UTC) 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="RVuSZXDU" X-Original-To: linux-gpio@vger.kernel.org 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 3D3C312B9C for ; Mon, 5 Feb 2024 09:34:30 +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=1707125671; cv=none; b=aBp+d5v+NLYjowmL81WVkWLO67rAhQgGrOyl91MuKaW1p8ERnfFmZtBrG+pJeXGy5o1ek2NkoupBkwsSAITCUWM6vLYyldyM4Q3BmuQ+535RVMnmniIV6I5+gRekFzNu1DmEnrDh1PM8tO9JYalJccVCcYX5juojZaA06wzzri4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125671; c=relaxed/simple; bh=wKoekw2eVVFcj9oxq6vzdf2ok15iXRtTtNq2jliv4lA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tolKB4g3D0l9nT64Eu4bdUTwkINqR4YZ9D9olvykeZMd1YPf6cdOf5LZV4uz9EPrAwq37jc1kJt4y45qr1/NJVeGLS3Ka6LSsTILn7/FrzjDnrRl9RrlPgqbm0a/3DuynJEzalwA9zZPjsQdhk9KdagSyta0+WziNpR/UxlMbRs= 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=RVuSZXDU; 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 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40fdd65a9bdso2704375e9.2 for ; Mon, 05 Feb 2024 01:34:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125668; x=1707730468; 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=RVuSZXDU1PPPz0u88nlb5x4+Ori5uy8kod2URqu0ndVD0AKG32/31hpR+Pvp/gc3b9 opVFI3FwfHGnVS2rk1TrPdYId4493uyRFx2V3pymUrV/9WPhPaTgeRPeFlp1GhDhFXif 6hfC5uL9+jo0HXqN6YntjPPC3fU166Ur4ddE2TTwZ13eUz+ivZ+r0FzO7f85bcLi51D0 xTPZ6qVdzEince491l5iGgp4Vt9uQrbXOwdYLCwqIKPaI5LykkTvG41rVmAwwFhyLkn7 WacRFnHgX3iPN8eMqF6ooKIkR3GjB4OrbakdAxYLBnvqhGEZJhkvcti+ZOGf8nOuTnsz mYTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125668; x=1707730468; 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=DfwyB95Qj5iez0v1JOaIBP1SXNalfOv+mTh4cktIwKrtSXxUujW0emGwVXv53aUKT5 fjVe/WFBUTf0jz4d84rF7a/v1QXlvhPs2ywRDPV7gc+y9dVyqTB4Lqo8AxbDHqu1446T xdnE+qqm3aNaCV0aJ4PnhVDsnL5611FiaDXpIEoWKW2D9FQ4AjI855LmE7mF4uFZNUIS zwUsHFcIALcRoxxqwzBf1gx5ir7ld84Mkgy45KQtxLV706boCHUCosAW+KUou1ULc5DW JCJXnvFBW8ushnjIL4qfv0ldAYLDX4bLqmZzrd+x+j5l0hawuOwEfOXF7tPBQydnzKoQ eOFA== X-Gm-Message-State: AOJu0Yyz3pRs7MwWWaG7Aod41iM8pWNrzOoiQzU3KzoIDRScTr7/TRJB on4UnqOXgg18cPoYHIXEGb+1GukQ2JukP/sl7oKl6oyeCn269hf6mk2mSOl8cfk= X-Google-Smtp-Source: AGHT+IGOzddUhMQZJ0PD2K9+76g0yiZag3OnprMbiqKxoQPCFOLRGilGiVvAOxPHQD+R4ETWI4CyHQ== X-Received: by 2002:a05:600c:a01:b0:40f:c1f9:c884 with SMTP id z1-20020a05600c0a0100b0040fc1f9c884mr4142575wmp.39.1707125668491; Mon, 05 Feb 2024 01:34:28 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUm83IhfTyQ7y6ls6Z3lT+Xx58RcKqxvlQep2ldxlrwxsmT6NcjogMnkVoMyYiXpWnpXWVIeKQtwOrmmrndaMAqI5KG9I60lg++7fcDO3ZPAQtwB/f2qKxHFMp0HkGXLedguNBPIg9CgET6gFQkgOEtAdQyYdY40lzTOYSeuOSfs/TkjOj2V0B1oloOCsHFU9z+cI8EgYAjCt1kBBVBqrpLZr8g/scnZo6DE9UR4/jE+/+Qvy50gfaRgAa57BMfsB5lFKBORni4uhRb7XTe46mfdX0t/Es7DCy64o5WBJFoIew/LJv4F2Y4AGKITDNSsozpyl6QecaDvfaJiMIFf8Yf7T81pzJ8bw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 03/23] gpio: remove unused logging helpers Date: Mon, 5 Feb 2024 10:33:58 +0100 Message-Id: <20240205093418.39755-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 Mon Feb 5 09:33: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: 1895185 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=lw5MOHFe; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:4601:e00::3; helo=am.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2945-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from am.mirrors.kernel.org (am.mirrors.kernel.org [IPv6:2604:1380:4601:e00::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Sm46z3z23gT for ; Mon, 5 Feb 2024 20:35:20 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 04C641F223BE for ; Mon, 5 Feb 2024 09:35:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2DC6213AF9; Mon, 5 Feb 2024 09:34:34 +0000 (UTC) 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="lw5MOHFe" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 28B74134AA for ; Mon, 5 Feb 2024 09:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125673; cv=none; b=LMEwSWtle6ZTC4eFjvZuz9F/Y/RWyJOg8SqexWYhwY6pQj+OBVtmNLYwrKJZyWwf3V6h9ZZvpn3MG5S7a4G9BX+DPd+6u6DtFypBZPiSbF1bELNg8q5TMVJE6tzx5aZYpxGD7nRVmwhQ6lM1TCDYOxREvHhNU7kgsOUKVoiuHIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125673; c=relaxed/simple; bh=rvDmiJvoOjsEAG1Ifqh4V4UhyPYg6qZuDJ20zQ/Khlg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A6sYhcNGU0Ri8FrA0UGXTKGnyjUyC+MuCL6Qt/KhTgZuFTqGVzVD5QV8okKyZ3xwru085TcqNONl7JSfYtOSgzd2uOSlX5oWSLvCL+0jEG2uCQeeXGDb3v5YRNaS3x0bymhR4KRn7+DejoUjjGGf8y6tCTygjhGU9yj/2KOwJrg= 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=lw5MOHFe; arc=none smtp.client-ip=209.85.221.46 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 Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-33b29b5ea96so929065f8f.0 for ; Mon, 05 Feb 2024 01:34:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125669; x=1707730469; 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=ggk/HS4V+gdB4Ghsw/F6Qu63h8wKplBC7+YEgEKvm/I=; b=lw5MOHFe8DfzKx2vttTyd4yHtivqVHI/wtoGHeHt5J7vTRC3sYrZzviPf1HrqARmgD xQSkKqKjjnm+3cP63K7x7DRBxapvqLVP0/OMavRCOHlz8guTZO+4YDENwr6jGzlSBHRk +7tfSmiIBOxxZtYeLnutc9Y2WndHGL2eFOsrHeCx92zBiL6g+uWTWX3xaMafAqMjm/k9 dbNZano0JMJRp5Ox+F7OkcSsgsXv7AYgCrxBn7Efa4hAU0I5pJpO+c8Db1tzDMG4wA44 q/+sqIMjObQyezTG8NMcQVnw4sOuSGTqkVi/z3ZfRSuakqgXORGU/qYfisPBNmv369j3 NH+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125669; x=1707730469; 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=ggk/HS4V+gdB4Ghsw/F6Qu63h8wKplBC7+YEgEKvm/I=; b=EvGHR7xFdRmLpD3phD+CJ3KzglIK52s3e5P0Xzd/ZIbZgc4fCJ9asN3cTWIOxwnSN9 9ZIX14Jqj7rhPNsbW+R2/lOOqHYbAbLNRvGJGiextAcGepu78M6/F5vkG9oeKLystOqf 1/xDlL8dSfgYHvLnyuKZQvP2YjffOuJTIjehaQvV8hNjDymzNAowvlsqtcl7yrk+VkjB qnjIGPUG1K088SSnWlHYYUqLT8gVsz2ONLOKDweIY/VWaZUfDfkLBWP6pa6mE7ROoLfN Zcb/wAaJApyLRVERsoJ3pKXrNkBEEWtlnr8JRffoiTSp3As1LFEMZ72SvqdNWuQPlMkl CSXg== X-Gm-Message-State: AOJu0YxVC1SDISQs98nc83iGIEw62OV6cL5rC8QFoO+jQ+tungKKSCNJ tzs9TxlXm4eydjKMV0W672oJz99F9SXyE53+vc7jPnHfamPcizXQAayQ3LNPE0Y= X-Google-Smtp-Source: AGHT+IGmV+7oO3tTihglftOvAY9ys2JC0HqwERsmzJ5U7qwgZaAkLeRUyf/il5ao0F9eBGp+iUhtKA== X-Received: by 2002:adf:db44:0:b0:337:d84c:1d6 with SMTP id f4-20020adfdb44000000b00337d84c01d6mr3723241wrj.18.1707125669370; Mon, 05 Feb 2024 01:34:29 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUwWtApFLfLTngT2lkUY8xlP8ZuNiah9r0wTDnJOYT7AjyUHSkQKKGQ/LAQz4/cydeGnZShFBn1MrvvPVo/PkEc/nACNX+IyRUnM6M6nnXnwZ5cLfLN9WuOwBvoZIQ8jVjIRsfP4LjMT9bEHv5eGfFCyvaqi2HLcKfSKh30/U8qIZGz652uPwvvV5NnrCwmS86MRCJlRPMWEzxmid9j6frzRqQglH9W+JAnkZ62dFIMHeHpTYmsJitoD+KMtC0WfZ3L52/QzmDYeIpiX6dbbdG5xCY/HxtiuaEoOi3IGQTnUW4FWAEQKJ9N3pVajdFRfKjOEuBNYk5MEiZqc1Qju124+XnPndyvkQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 04/23] gpio: provide and use gpiod_get_label() Date: Mon, 5 Feb 2024 10:33:59 +0100 Message-Id: <20240205093418.39755-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 4c18b693d06c..cfd311ec5dda 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; @@ -2385,7 +2390,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); @@ -4726,7 +4731,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 Mon Feb 5 09:34: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: 1895187 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=EvMogXAG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2947-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1T56N2vz23gT for ; Mon, 5 Feb 2024 20:35:37 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 056FF1C2098B for ; Mon, 5 Feb 2024 09:35:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E00E14266; Mon, 5 Feb 2024 09:34:35 +0000 (UTC) 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="EvMogXAG" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.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 3AE5D134D7 for ; Mon, 5 Feb 2024 09:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125675; cv=none; b=tzmIJb2BCOienVaCYUPrTQUsoFE1A41pBdfiTD3pLrY4LC1jVHC3K/d7O6JFlfnUC7emMrTlNkuNqR6b3D5NOlBf2qIvQcgPVoHHhcjr/MF0y07pDSBdsL6azAUd1ZRpDXZqHmvvoZgbBVWllC1ls9w9ZPTqdVUN8klItw8TcrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125675; c=relaxed/simple; bh=L91BBpikBkHqwcQ57UiFh0/5/PsxnAo8v9+gu/rUrOM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oQyAHjufamgHptzQmdSFDp39y6PQ1i63IGJiNg7wYoFHBuPWrMdwpQhK2ccdBTMZ1g2nZ78jIJJi8u9yA9pPLSsItmPjSp8pUo3hJCyxt+Grykf2Uv5pnN8cXrcPFqB312KaZbd3Yx1tgGKmi5eoHPIwMH01phyw38nbgobSXZs= 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=EvMogXAG; arc=none smtp.client-ip=209.85.167.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 Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-511234430a4so7268581e87.3 for ; Mon, 05 Feb 2024 01:34:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125670; x=1707730470; 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=RSEnfVuvu8Q/tNHp1neFQSbtxw3POuiVtvIa5az8diw=; b=EvMogXAGlsh8KWLG0umTfCX7vtaABlzwJdWaKMsuxtbJoYfEfi1kL8vzQxMo9Li1nl JHEnXPvxJgm2c5W3FrtYOHuvi97i5zs2r9VyhhIBgB/KUfwIBznnq2TRy+SW9sIa7I7X 4O7qkHbIjkja9QZ4/f7gCvE5/ZEw8LTuS/oZShaSqRWe2B/7xDUqYNTGCfOEfE3gPs26 EXTpBv2CKYdJZsrupiz1oakh8SQ/9/carta/uD37Om4SNhdrj5O/N8bszHRw7qsnG/MM 5nK6Sw7lBhWtXPpvAN8xJVyNdhbdw6UCS6fI3Mry+n+WgBGS8Ke2wLNgxhjHRXHeJeCS an1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125670; x=1707730470; 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=RSEnfVuvu8Q/tNHp1neFQSbtxw3POuiVtvIa5az8diw=; b=BDcDA5JB0aRYmyePKQucNR52G7SEBDgs9AAKk/X5PS8V9xaK4LqKld1M4JR1rosaWN 9eSx7S/GtGPGAYNeUArahPlkQIHSunBDvAHmdpIQBLYZnsaCiTHDBRssQEBzp2Vc1Kvz ZmSJWg4pBdsxA4yrvaYyPG0f8wcdaLLiGc1ORb8KFtvZVbEdR8TUmdz43lSZ+7P8sL5X /2XrtbWmSRULB4e5q0ku24SK7E00jgL18vyH6m0DDMTQTNbQn7tbUlipJXmXeiKv8G+D K9Olw8E4//w3MfJhc1OH/FU6JE2No6jUS8U99ibi9kw7/s8j2w7q7ER2/s/sxm7iHRep dCYw== X-Gm-Message-State: AOJu0YxvBqtBaGeB52PYjEsNLdSeY/x2w5k5tK+DW9jAoG2NHO6JWsXH no/Edyo/lw1eD3Z5GZU1mLtiigbwkle4pfcSpO0zcySIP4+CKNuvzzgVBF3XY2E= X-Google-Smtp-Source: AGHT+IEcPafTTkHgGkgn+DFvdWRnWTQwe2o6OEptnHsezn9spoJf9YLIqVdfi0Crff3zOVppi25yBQ== X-Received: by 2002:a19:f711:0:b0:511:51af:7548 with SMTP id z17-20020a19f711000000b0051151af7548mr1026924lfe.9.1707125670191; Mon, 05 Feb 2024 01:34:30 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXobqsD3Uis6hLjobTZp6XBfCqM+9f4VYj5owOVxKdXHlS2gCu1PCPgofYK23757bJ/XK2ElC26nPRA4h4YtVC/gdKQ4wq4XOjIOmbTYckFQlDJOqPc6AsLuLziM8+0Qyio6nOZmt8AyTrQuDJ+EJfop1t007ahMAjUqU+0A9qQ3eREHCs9wSMC3KjHXY0TEGrQlG9f6P3CakEMHWrkvp0gv9rO0NFnYOjqOkep+YgX9qCyiwo8Bxsf6PzudPDrsZ+MrNRJho9xwCRjRDokEx9AHlLZ42c+XoqwEem2lZAO+yd4LRCowmQWbAnvrsWp/7n1976/gdhfl9QqNOnLRGwgblsF8s3yYg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 05/23] gpio: don't set label from irq helpers Date: Mon, 5 Feb 2024 10:34:00 +0100 Message-Id: <20240205093418.39755-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 cfd311ec5dda..5c041d57077b 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) @@ -3593,14 +3600,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); @@ -3623,10 +3622,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 Mon Feb 5 09:34: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: 1895186 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=RGGg8q89; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:40f1:3f00::1; helo=sy.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2946-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org [IPv6:2604:1380:40f1:3f00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Sz1XkFz23gT for ; Mon, 5 Feb 2024 20:35:31 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A9C99B2213E for ; Mon, 5 Feb 2024 09:35:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E9DDA1400B; Mon, 5 Feb 2024 09:34:34 +0000 (UTC) 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="RGGg8q89" X-Original-To: linux-gpio@vger.kernel.org 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 C5E21134BE for ; Mon, 5 Feb 2024 09:34:32 +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=1707125674; cv=none; b=pevgOW8CIWvJ4I90LFn3SPXPHCmWU9Z6qmAVp16eU+WpZB3Qowwfl/PeZ8z8DPvWq9Dy5WTTfPekmC+AE95eVaWOdzWGu4gTsJxEd+hbjBPWOCnVAj5UqcqTO4MwFBzge3us23SjYZB6jtjAlP3EcWF5qt9z0xg3DHHEhmRV7KM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125674; c=relaxed/simple; bh=YfUs17R8IhDrlEqhEZxI5M9W373vGN2Q0/ixlOWEz9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aOqZRfyz0vX9FS1lQDYb3+ZlqfKJdSfg/xWRxCKVQiXEfsil/oCwCTOAMgea1B5Hum7IIp5UKVfGprY1VP4ZDG3iKBAZq40C5oJBm4zHEDAGPK6uNDfi0YycZkYAIOv+Z/4iNVA7ze6P5BXJTDy1FhTaJ5/yMqZWuYVznbxWBOE= 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=RGGg8q89; 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 Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-40fd446d520so12313065e9.3 for ; Mon, 05 Feb 2024 01:34:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125671; x=1707730471; 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=zpsQDT+5nWCXpBfKiz7J0RVwkAnu4buv5U2Z10kutak=; b=RGGg8q89OytlDemlxtnZ+lVkD8YTSn9byWWMiUr1VU2rShjXSvdsyt3r9hnTa4J+TV yl+ssFKmXnXokYmumyMaopS+8GmTd3BhBkasjxscjNFSQNVNQHUjNExUCxvmj3j4c9n4 5Dhzv1d6FbVcJGhXz+0Er1p3d2po4UFg9PV+jxV1AQKPxDT7AuU3QdpVvE/0ltuiLGo3 5KwpJLp5cKB3uDN1D84RoFGf21VSAhjYwu0/svu5pIFM+K4BM/onzzdtn5c1AJ+ldug3 zT67NdQNojnzYtvEVYcjagN9AlAqrKhTEwPx5Pu0J/EPNUtIVc/9n/XWRTaLZni76at7 2DnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125671; x=1707730471; 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=zpsQDT+5nWCXpBfKiz7J0RVwkAnu4buv5U2Z10kutak=; b=eZ5yNjL/qiVgJ6XP8Hh2zzhdHOFXm+mVXjh2yPsnk8cAx3YDoEPlIpWVGnBnIKG5BE 8yeXi3tX+iXGDtqr6KfnNJ58Aojbk4QkuTbqM7NAFIbR7jy++8Qq05mVfK+SWBYo4BVA b1Q7mzSdrr6PGBrRQVtojflGxDy83054w1Ac6uIX2Ylzxp8SSzaTUZL2QCop3M9i1fS8 a2Jlj1Uv5bQppvBX8ByR4rYW3cyRwe3TgG161UCi+0J0G8xKIEuDVBRgdv6svUQda6t1 zFeXbOnx/Y1A32Eoy3IfJ/1C5qtfKXPG7BNWrQkC8yYNL961a1pY3mcdDTFqz+lIWNoP ZFNg== X-Gm-Message-State: AOJu0Yx7H7/rcGrYA1bEgb8SGoEwtxZCcbXrIazCNy/stwVC6R5/6FyS HByl8v7Fyw4d2X/5HYk3siuvU1Ba/X9Q1Q05gyBi8/WFUF26RvCmkRknVkUKF1A= X-Google-Smtp-Source: AGHT+IFyKoc6eaHqWsITQ4+Ew7YxpHQC8SW/NhaAAtC3PI9fpag+n2maplobnK/D3irrszBWC8OO6g== X-Received: by 2002:a05:600c:4f91:b0:40f:b680:3e84 with SMTP id n17-20020a05600c4f9100b0040fb6803e84mr4720748wmq.2.1707125671145; Mon, 05 Feb 2024 01:34:31 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCV5ryJJgj3e8xjpQT65Nhl+E4gf4Nj+r4X4IFAL4AEZ/lGUwSZ0u+qFf/rPXMFox5a2kwQQv2on5DmzAoN3wu4NWApqIt10a2UOSYEtgwzO2EnTFT5lsh0vmX1j2WdXH+aTpbLYXOcM0DakgeJMcdQprv+MrtnPuP871RkNCO2+7vLEjXhAtZMnLeeIt0IxYYHmsdlwJegB06/90U9myiaXQIj7QROYhugqt62IyRIeaYcPnXEWA5HXmsw99ndaQhJ6afbJtYHIzsJhcucnfJba8IZ+7dYUIXo6ZNY1dZAcjGN1UyjBzUrCTXaD15G+AOEdCjqE2kMJNGpS6Qfp+8uVwaAc45ld6w== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 06/23] gpio: add SRCU infrastructure to struct gpio_desc Date: Mon, 5 Feb 2024 10:34:01 +0100 Message-Id: <20240205093418.39755-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 5c041d57077b..ea0c0158faaf 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); @@ -832,7 +836,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; @@ -965,6 +969,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(&desc->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)); @@ -976,7 +987,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); @@ -1015,6 +1026,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 Mon Feb 5 09:34: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: 1895188 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=ocrqLTly; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2948-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1TL1NY9z23gT for ; Mon, 5 Feb 2024 20:35:50 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id EC8AD287100 for ; Mon, 5 Feb 2024 09:35:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D3A412B84; Mon, 5 Feb 2024 09:34:36 +0000 (UTC) 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="ocrqLTly" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 EA56813AEE for ; Mon, 5 Feb 2024 09:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125675; cv=none; b=TDXS306Ic3xQuQJ+PDRg5vsKEtQoJ729WHJdsAnzJVfhEp/6QkrifDTDxrBYqebVL1koIBJq8/we292ErUN12EkPpjOe6vC8vGPt7jDFXcPDOQ+KvH8QShhWaYxgQcS6C5bt8v7NMOYjRGE4t3WjITEm9dH90jHluVlzBP71PFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125675; c=relaxed/simple; bh=Iu1ud2V7hAqRPw6qsJ4BEkBo20yGRwt6IVyG748uVng=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ql5yuhUqZDs1RppOskDnTE5UDbta8eX93HAE2jphG1EpRPVbXAfaoX0x/AuZ3OaLAQBCej+0p24/a2ibDTucWtZwTlShib9VlXJ4xpoM/Ai5Y5KPu41VNu+YC6Ku1m6jjrJxul5cDIUvGedKiDas5VXbLfZ3Srh5OIPETwgO3ks= 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=ocrqLTly; arc=none smtp.client-ip=209.85.128.46 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 Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-40fddfa471eso1550865e9.2 for ; Mon, 05 Feb 2024 01:34:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125672; x=1707730472; 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=SZKu4GUN8Mdrmtq+mCLkathjFvWJwZX/fdEUwbcMDKM=; b=ocrqLTlyHem20c2HZRIZRuPnZkX6vJXs3gzTELx+5TfKM2tkfIk72hkPB8VmNDYD4m baV8GCuD35Gkiec/lfL7mMWc+1gHV9o6XJB2BUcDemFK1I/KDuLrGatrhQbsN9qDEKbz +6OJwUibzIDSf+W8EHhcVbEEq0vlR3qEcSE6Le8OYZFQ3zJ95KT2o2eAM5hi5wZ0feVq w/74MwmtJ6Dv57u+5ckJoBbbENPPztRxJJoQieVglfd2NrK/Zl3k6w2KoglMfO79T3kn HoY+WBg2TovQRVpCpJfTCwp0q76QzLmritT8014t2Qxjy5E9S9rz0fPbR+WDsd6IJsl5 YiMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125672; x=1707730472; 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=SZKu4GUN8Mdrmtq+mCLkathjFvWJwZX/fdEUwbcMDKM=; b=QIzpWGGOKZ7rq3X4o6BdSvOE305jJ4TdxzL6Fn5WIAArLeKOnutREZLDvTpdHVcJMb UEI/KAFgZkzIQIuct4LvZuv/0cEvHpxfFYz4FlXiSQUcfZ5lVVEvPiq6dfRrBsNULlHj eP2r7Gwv0J3Q9YSqy3UsNhId/XfR1aN3zxXORZe7bly9Q0HIkshoS2Iiy1MlZhU/lph9 +NjvtjP2g0ZCTaWQd+NaFFZBmxWEjPuKw1H3peRnlUxI9b9cCKo3sneoTy3QBRlQUAL8 g6AR9WE4By0XmWlUFkdJa55Op74XFSrCf8umemumskk4+c0tMgfym1mYIs8pQ9iCkvS5 TgUA== X-Gm-Message-State: AOJu0YzrvEr+OPy7TBC5tmAporrKl+fjihpnu/hu5ea3UHtPoowab1Ya xRZqH3ZFiIytKvH2SerXXTLoYjNY+f1TzkjfYu7pA2POnZ0qES2vVHK5Lvs8o5w= X-Google-Smtp-Source: AGHT+IFDs25h1zo11JFYWw/lWaxEJX4khotrq6H9fGUlWGR98K+wisEz2DEBoU/sA+YMzz+RtVUYaQ== X-Received: by 2002:a05:600c:190f:b0:40f:ddc8:f801 with SMTP id j15-20020a05600c190f00b0040fddc8f801mr506301wmq.34.1707125672015; Mon, 05 Feb 2024 01:34:32 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVWF4OPWhnyYDlGIv9CoNgJBcAmGOGu4ZDCZ/I3ZjxRyQjJY6xv0cL40qoBgpJYceHqbjp7DG4VWbAQq7dcExpzxlkGQZkW04j6VWRnfm/UMGkzxEQ2b83rP+u8/z7qlIH9WkSxHHjqGkuPoPJk7sS2c6UWBtJGTctulhKyUGl4wG2PNk0InO3RiABpcHD+9T9+G/q/gA0uh+TZUfosdgkOxwKjf6JgCE7W4ENJMHlse6iRE6yY3nXpGO6Ey9bUZIKmOpaEmsxjwuqlePYcRWximBuS2hXvy5ToMcFcLjUIYINzPS/OETEA2sMm4dwPI7Pf11U0lo2YPZYt7LzmH8ZbuLoDGBb5tA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 07/23] gpio: protect the descriptor label with SRCU Date: Mon, 5 Feb 2024 10:34:02 +0100 Message-Id: <20240205093418.39755-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 ea0c0158faaf..f425e0264b7e 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; } /** @@ -2223,9 +2237,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; } @@ -2253,6 +2265,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: @@ -2337,8 +2356,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); @@ -2356,6 +2373,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; @@ -2403,6 +2421,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 @@ -3514,16 +3534,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); @@ -4733,6 +4745,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 Mon Feb 5 09:34: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: 1895189 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=RKjTqk2Z; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2949-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1TZ4l9Rz23gT for ; Mon, 5 Feb 2024 20:36:02 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C4A771C24572 for ; Mon, 5 Feb 2024 09:36:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0FA51156C2; Mon, 5 Feb 2024 09:34:37 +0000 (UTC) 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="RKjTqk2Z" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.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 DE83A14005 for ; Mon, 5 Feb 2024 09:34:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125676; cv=none; b=b29+n96nfnp/9PIZv2xJyLbwDqAGL1/DMoGRunUzSM0nJo1AceDiSsxllC0uSkECM1R63xONzmGV8ukSus7wZSZT0rdrv71dgp355ycGyPwy+lI+ckCiu9goDTBPso3GR+62mOMTLrylTDESupnfQdV1615eGoArmN0TYNy8bgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125676; c=relaxed/simple; bh=QndNeSWqDTX/zFjnwCo02F7D/CRpA6gYSyfRPlHOvEw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qFwS8M4eeaecconQa/I3KP85znfXzS3wgXbXjHZYYKE5m0+e9dcUtv7PEs6pSZtlAPlHAE4TuTh9kQ4nZA34ZwFHHJdgdB6pBJMkvy8ZMIv+O2arvd7UUJFSFgQqTrWVVHUorSqiPFBoYgBJ5E4jifwyy6+IWHrIoUBoqLGUQbs= 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=RKjTqk2Z; arc=none smtp.client-ip=209.85.167.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 Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-511234430a4so7268633e87.3 for ; Mon, 05 Feb 2024 01:34:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125673; x=1707730473; 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=GkfvkjtzEhDkQSOg4G+yVVF8O3dwJMh/iXMkfAAfTgY=; b=RKjTqk2ZylDq88zxeYhB89loVrt+e0Nihxz+aoTPRicgpNW/Nq/PAsowxtWAHfCAOL +3maT8cXr9s/COgOsAOn+n++p42AXHtfvk09WtU0CyVNvAS0TU8meeDfRp/4UGtmBkqw 0r+96EsJWBBZPloV646QtKRk3F/Oyi0s0on8sdiakvWUW4WKg6dIz+YcoUVHcDp/y5Ci aqYy4vdhLiF4YqO+RsLKC7/sIqlb6wuEHtW1bGnxJ0/NNPLqUEe/CcHITbN7JGsaDtwB jKDT6SVU4lcy5ukyeqD7kUOYrLBAEEOVM7MsUAG31QPBlPCKzyv1pjSsNTUxNL4USNCE CHtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125673; x=1707730473; 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=GkfvkjtzEhDkQSOg4G+yVVF8O3dwJMh/iXMkfAAfTgY=; b=VJQcN2uEOMejY4BJf0lDAtauG8vuA0RoSOE4hxgzVFq+Wb/Pt8mh6c1SFhXBWRCtpJ IiSXYWK10VRhLyJfbMtQ+VKhxguIyLZTEJSjDf9IcEdu0CLvNZ4GoaVheUpyDprcBi1+ +gd4P9stO26swAp7Z8ZglXmbY8LjATfTnpS4qXPSFlM5pEp7RwOvEb0vCgrsFW+8Ylu9 /oo3ENA/0R19ck49U3YO1j/i5fpJe11JSUd74gbW2KpxVjxN+xmQcGHiq4t7EYrzv10Q 4HULPCwbxci3lehvSLMyQTh9mxWlqQqLrAjPpClZYyo2VaglojXg1MAHNjhWiXkodb/S 6CAA== X-Gm-Message-State: AOJu0Yz8iXnCbme6KMfOYMu+EHvurXYed2HNvNlebW3rXo6vX6IRGEwF jNiBwIj8pR4OMdetJNI3gV87HA65NhLRwhVE3oSkbdymPk0xxiRnVPxwOe8zwrI= X-Google-Smtp-Source: AGHT+IEioOygbwzOiy8IYXaqffGjI1iQJ28ymii8IJ/QHZobuyxDN4NK6zExQjLK8wD8AOwGSbQsug== X-Received: by 2002:a19:7008:0:b0:511:4f1c:ef43 with SMTP id h8-20020a197008000000b005114f1cef43mr1520317lfc.10.1707125672969; Mon, 05 Feb 2024 01:34:32 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXJv09UAJMwHDheEwlhQmtq785dpHyI9qQU6Sgt9oU0/XH58nkLzfK4Bg+uwolkHslsZJtdQtN4SCOXjNBH6ed0woHAAYAU+ld5fGgnmcxc2E1Bjgsl2aMg5IKMOmz2uhvVUwZblNOp9bfso8qZFr7LGF3fppU6zTPTdPxdcFj976hV2v4tkrSccdQNR9ej7kqvq0CnDQUVVExd0LCXkqGZwpwUPNLKTPhaQ0XW1yRn1+KvpQYICzjMU2F76H1/xZgg85QgN010/3S8yovMH2xqkHgaMlCyFTD/4ZPPYbeYAYnW/rDr6h2AWHrbDZcq9qiXA4B2dRCn4R8zmJtwkP0Dl8q6P4D77Q== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 08/23] gpio: sysfs: use gpio_device_find() to iterate over existing devices Date: Mon, 5 Feb 2024 10:34:03 +0100 Message-Id: <20240205093418.39755-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 | 41 ++++++++++++++++-------------------- drivers/gpio/gpiolib.c | 2 +- drivers/gpio/gpiolib.h | 1 - 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 6bf5332136e5..3c3b8559cff5 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -790,11 +790,24 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev) } } +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 +819,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; + 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 f425e0264b7e..6cfb75ee739d 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 Mon Feb 5 09:34: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: 1895190 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=qEbT5HvP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2950-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Tt4yZwz23gT for ; Mon, 5 Feb 2024 20:36:18 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 67BCB287DF7 for ; Mon, 5 Feb 2024 09:36:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C71A12E51; Mon, 5 Feb 2024 09:34:38 +0000 (UTC) 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="qEbT5HvP" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.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 9F6ED14280 for ; Mon, 5 Feb 2024 09:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125678; cv=none; b=Id+bbav5XJVTO2o4UoIFnmCQClHldMwPZBRz3E/WI9KZbpAhAspMLG93mYDIoxHUXEjfrBkzA2VpSIF54s239px+f8fZvzsWG+SBMPBLlaES3rwGAhG8EyAmrOdEfUK7V8kKDv9UX1g3ftJhi1Pz62reTtFHB0OiqX9oDs+lDBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125678; c=relaxed/simple; bh=AZJTe9uH/HRjd3Eb1E9LViLgyCA0fSyifMWHq+z8I84=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=chLC4wlh/s1Mb4YZQkIFP8v5yDHCqJ0DHPfhFktBpAp2hje02KqkheMX+yaEgQvR3ebWgnH+p6dOBKSDzIs7rsUGp60hPILdXBZnqnGXo6pxp1jPaz0X5a5jxU7hSiQuKGqyxvyaDA+mdIvQXL+cWMjea28HRoW81IM4+r0v0zc= 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=qEbT5HvP; arc=none smtp.client-ip=209.85.128.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 Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-40fdcae97e7so3775495e9.1 for ; Mon, 05 Feb 2024 01:34:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125674; x=1707730474; 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=vmzSsuQlbDZ+t30agBye8l5Gy5EgwDATrqd1ITBls6s=; b=qEbT5HvPzWhg5xIcD7DCDzBGSPngY6uCPgEd+nYMGrdq0pKUXT9/pqW9i44SBawFWx 8eafhZuAjWOE6N9UX//xPvuBbSIpqFClWzp1X1iLf/lt5KURb/tQJ5UFlcjSxERMsjKe b+fxnMDbumy93CMPaaQYOl6yUwRPdF8I8RcIKzeKMdWXvEMzyBfDWgGkmHwjNCcf4zRa u8Vv3yftoPu9LQ+NcpNMXywN6dhook8J3RY44ycirvNfNx1f/JGpHokcToV90w1rLqKz MJLYYDYTey4w6hoHh8dgaBwgJj0JpKbpVfjeMrOmMe1Gg+r3jQzzbsUgMkH7D+aGzNl8 QcTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125674; x=1707730474; 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=vmzSsuQlbDZ+t30agBye8l5Gy5EgwDATrqd1ITBls6s=; b=oUbE5pjP7gl4BTvDM9iCP/f6/YlUQmtNAlCXsrG1TNKOk4OM7M48Yw5WimFRWAQ55I HATVm/yw0kUvT6DyUvyeX9iEMtC5ExQAVcZ5TmjYg0nlb52+JA1Fewcsj+LbVk8V+FOr TygXn4PCdM61qjMVtgKbKOyTDAgWmOYaFwukIIrGLyitogbztsGbW3DgFvPl/lJKTOY2 5aOk74h00uCuGabiXGi8aENG6KK//YXkO/xD05YkLJ4v1dhKZ9uqQgsDo9tTaHqSOLIw R0DeTeO8kMhep+MmlA7ed5qjibXzG8oIAf0G2DzHelNdBNfEFYUxNjdX5gqVwq3zi+9r heXw== X-Gm-Message-State: AOJu0YxyZ9p7JiB37VZ3eoxBAQgwlg42lTbme9b7w6QJ0oFpPIwJAZRh MuFB1huwmCe1kTD9U/eu7rojW/c12Iz3fjWU6eiLMurgRjbuvI+7rYIWtI3fhDw= X-Google-Smtp-Source: AGHT+IHDaj3kNqPSeHGzX7jhB1piE+yGHsR31u8qOcgsycwVXq6Gkc70ZJK4doI9mG88u18meGSCjg== X-Received: by 2002:a05:600c:19c6:b0:40f:b691:d3c1 with SMTP id u6-20020a05600c19c600b0040fb691d3c1mr3833847wmq.30.1707125673807; Mon, 05 Feb 2024 01:34:33 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWaFE4TMMOkTAIjFTWDd+DVJBGl1fKNmAfr2NjMOlktEWI9BDzUbkqEPClKAxZ/xbBeZOLOwD+/OMaZaQqiAxcC8/w8nobZZDEMwGISykisMLvPH2IwXWyp/asBCGeM2shxYLgPFHw2bu8yavrW7xGrgGd+MAaJsQb040xMAnnQqSZm9HLquiyFmm3J1Uskus9e6gWjs3L9kHuEqhggKQUr8D9lWGB70EaaTOGUxuxZl+vRC75dFsFbHDpRQf68Cj+ytznMV8H7U5zrXz1QLUWdjhiWEjmuMJbzpt/SfLujnsM3U/7y9ea5cZpHfP3Ghr38pfKPbVkKSA1Y1c+C8WjcSz9XRa14xA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 09/23] gpio: remove gpio_lock Date: Mon, 5 Feb 2024 10:34:04 +0100 Message-Id: <20240205093418.39755-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 3c3b8559cff5..1cc707685f87 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 6cfb75ee739d..5ed5b71590ab 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -76,12 +76,6 @@ static 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; } @@ -1088,7 +1081,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); @@ -1109,12 +1101,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, @@ -2221,62 +2211,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; } @@ -2346,35 +2317,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; } @@ -2416,22 +2384,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 Mon Feb 5 09:34: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: 1895191 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=eo6843DD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2951-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1V744kgz23gT for ; Mon, 5 Feb 2024 20:36:31 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AFB8F1C24886 for ; Mon, 5 Feb 2024 09:36:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 466061754B; Mon, 5 Feb 2024 09:34:39 +0000 (UTC) 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="eo6843DD" X-Original-To: linux-gpio@vger.kernel.org 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 E67B814ABA for ; Mon, 5 Feb 2024 09:34:36 +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=1707125679; cv=none; b=mFp7Zl+UsTsc9vVEvPS6NzqyvOYmRc6NZb8qSi/Pmzy9Cl1YEBF17ROVzruNPgaXy0o0lxIM81I7c2v0Xne3AOfOe7Sbu/EuSWMdGIC1bnIFQZ/qewjt2GSgpg1GrdIXORvbUIhJLH6rEMXy5fLJcp1YL36h6YOGv6gIVR4icJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125679; c=relaxed/simple; bh=rfKavv+WG6jGS5BHlahGWQusPjVqbbXZ7CB6SI+xjkE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aZpQeCiuzhNIydPusrN6C4vOFGyFrn4qhPYVEUs6KSp4/quCk4IAjjtWD+7Q7LaHFmJsGgE6I/Cyxc4/vtjyxkPcklw7vYV2daxKFGM1b6sdlBF3wCcVTky6kxZtcvXvmHmdDaGNkUw2u6wIUx24Al9sR0VSdIBZJAHdkvX7IMg= 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=eo6843DD; 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 Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-511344235c6so4714373e87.0 for ; Mon, 05 Feb 2024 01:34:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125675; x=1707730475; 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=oBVIUbEZhzPUY5wnbR8PSqxrCBFBG4RRWNGnrCutvhg=; b=eo6843DD9eFSeuKm9WWRqFLHKuNJsAsJCe1t5eP56+JJ5PPb0Uy35xAGsWJC47J78V ooKBZR48+Crkfcf8B5H0p9bmvcyvopaVnz/xZSJxjaRS7kWL+SwnlUxUDSJY6AbFAbTK DreAJ6YjZ6jU3kvpSdJ5RSE1G87roRZLMP/SdKjb0YHLiH4fn4wC2j9PFtuvfwdBTLa3 tN2C8mo/YzPr9ch9cyxyjTXVxNZeTnLmnu2UT7FXZ0hiY1v/KQFIfRyjBGn0knCWk3/k 3viNMxPwTRhIJwo5KlAhmwXfZjBTAkMB/PTNG85LG9DgDOuMgxM10k4ehNo7RKrQ1p/E 1upQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125675; x=1707730475; 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=oBVIUbEZhzPUY5wnbR8PSqxrCBFBG4RRWNGnrCutvhg=; b=o1VpBwLoef8bL9OhK9OQw/UGBiHrKl4GrpDr6aw2nFQgsRD2zhA3F0hBlZ79oQOs3E Wr196+82iYK+lc5yO+8JIQXunQEwxxCHt7PeyXZbbHfChxKAb9Zrt1MWYY3NimgoM+cE KiroOVaXwhQ7GdP7o9sga+SaISr78XjwSBjdN9VlaXCAGZhacbWFOF+6odPsNOhoWkIm QnkRdNjO3ikjcNOVSsy0sZsLy9QDhKdZ/mNgtMM3ViK2oZBO3WbpI7tjsBKEMZ2j0YLp HdlilhRoX1kESa7nNt5UWSMStrSR0tjAKQ545wVtsS4Cp8JYMGNx3A17GWdYqne/3EDc xO8g== X-Gm-Message-State: AOJu0YyTAO/gJ54obKs5bh4OqxoQhbggLc2/6cxNo78DypELlLkjToBs ySdqiNV+2D+cBy4iNR/6qG0zeuwz9eH+F8KwkCObnxAkV08ZkWGLJwgLekTus7w= X-Google-Smtp-Source: AGHT+IFf5V3o092bmqPbbvpWyHNdPE2eO7k80aGf9SuKa1MQgaO3K0nCTsSkjqLFZNeKshudZ6G6cQ== X-Received: by 2002:a05:6512:10c8:b0:50e:7410:d5b0 with SMTP id k8-20020a05651210c800b0050e7410d5b0mr6312960lfg.28.1707125674803; Mon, 05 Feb 2024 01:34:34 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU+vRrFS3A2LQom52oicbbrO1+QRNmZbSVSC2RqEnatt0iYkThW7J9MLI/MMTZItiNU4cz3RvUAZw2i+BnyZCheQOcyCcYrKBEa8LZGPtzgg/Wbvo/C/fo9aHnqMgD6aWctF6ZMr1nY7P7LAql/Z9HOAXR7qHTosr4yZLKD4rzhVqqsFyyrS9fNhgwstDS+qJVd6aaaAzK7zoN3jCZdzJRfHMJihL3AFoPtaXNULJtw5Ru1s8z6IDJDfwolEKW3s8fRKtgu9xPmBB/H5TQ3sGWvIkPe3/njUZsQevjoFlwMadyF1zAa8VW9+gzCEtVs2/zxSHsttQNI1q2RrRxH1QAI4Xe8GEtACw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 10/23] gpio: reinforce desc->flags handling Date: Mon, 5 Feb 2024 10:34:05 +0100 Message-Id: <20240205093418.39755-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 5ed5b71590ab..d49f84d692ce 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; } @@ -747,9 +750,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", @@ -2522,13 +2522,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; @@ -2694,24 +2697,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) @@ -2721,7 +2728,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; @@ -4418,21 +4425,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 Mon Feb 5 09:34: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: 1895192 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=d26DncOR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2952-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1VJ2q7lz23gT for ; Mon, 5 Feb 2024 20:36:40 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9487A1C2463B for ; Mon, 5 Feb 2024 09:36:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B8D6F168D2; Mon, 5 Feb 2024 09:34:39 +0000 (UTC) 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="d26DncOR" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.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 71222168B1 for ; Mon, 5 Feb 2024 09:34:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125679; cv=none; b=WeYWQJz3ifzAPEWV17Of5h99GsgTWHnTYHqlNxFEeVRzccqFKZ247IpZBesfMQ0M65gs98FQ9DI6X4OkSefmt1w88UM8pvG9uDXYFbd9wOlrcRCnbpA9UPpD7a5nt8vLXeTWX8UZEMo2xjbH4C58H+oR9Hr/woBfdI0NXkI8kbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125679; c=relaxed/simple; bh=ovwDn1Lb6tSdzCyIZYdd/rd1ePt6cjIadVa1Vg7f7aY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o2+VDxOclL3Bx8ekdcAMHmk6GqS8a8IzSvj4y/TNWAgeosXBb7wWYZcjK5k988SJ6rhzGpGtCHoeU5irtCEKLLB5CLGV/6FGXQXO46nETFwslBntqEnVRmfZdwzbOI9h9U1GR7qHcuSRXn2Uvx/zdiKAptOrmdGwQyOeqvphi8k= 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=d26DncOR; arc=none smtp.client-ip=209.85.128.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 Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-40fc6343bd2so25057965e9.1 for ; Mon, 05 Feb 2024 01:34:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125675; x=1707730475; 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=0ag8nRnrxnEMvnEvUPbNUe/ytaETypis2xHLc3wBVPY=; b=d26DncORw3t+yGIra0imTW+0tLdJRc1oa4ZSqyLFvsDHIc45NluPg1seG3pag1eKo0 VpJbppUYMFgjXnZLGiI8MV1uekgNHm+LRYzqO0MTJo8T5b5NFA1O4uvFz6SHHZfqTUAi +F+lZyi7PjhuoNyjSoOpcVbbvCp7jkv/C6QzAmn4YPRl8/UHOz+l6sVQPZGIj98QUIk8 bIFPXRKh/wMff0+MGX3AKQYrbTXkFNvfkyOSnMPd8yq1M51nOZL4n+HusCm8148bFwhf NIWHqPM3qeZU4sFb8OLbB2iFHxSsAYm8QRaiEQr+35+GRwzVJrodQhZTHxP+TtR9w8qe InFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125675; x=1707730475; 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=0ag8nRnrxnEMvnEvUPbNUe/ytaETypis2xHLc3wBVPY=; b=tt6jYb9VSz2GWPoNJn958nn57hg440waUbq1Qe0yPk2qMVdITaYaxu0FAGS5LfJyQZ 6sdqreL0zleA4rpPVOD/gher9MQFuPkFEBhHmxbtgtTYnV0SYryutWph5JTQ8DOXlM/4 vEtnuhQy9MipSQypWO/07B2Lee2pSABNEWhQciM4Qx1PCkVxddsybVCcwyLdH4m7cw7c zHCvlnMpYwMuGDlYatPaCxFtn+KM3vYPMT9r2g6S8/gkJpNEMCN948kqY6NHp97TlMXD cGcXlu+BdDbWVdNRxJ5Xdq+jcToR840Miq1MeBIxuy2IqZoI1cTVYMOsWmLxAv72/VvO jDrA== X-Gm-Message-State: AOJu0Yyi2X0UgotIecEJ2KFeaPDyCDfgbuxSIBh2EBzqRuBfsbMqdCYw Cli69KsZZv7s5K1uVWPgoXhLOj/XNvdmxdQpivjj9GyrHs8JVfSf44fJJCreUtOTseTdjw/HPKo V X-Google-Smtp-Source: AGHT+IHSJoTtocwyZLmVaK6jsmh0L0SfYF1jX0prYviYSXwbw76F10dFvCuDdYWWf2UvqRfu8/1Rnw== X-Received: by 2002:a05:600c:19cf:b0:40e:fbdd:238a with SMTP id u15-20020a05600c19cf00b0040efbdd238amr4414433wmq.37.1707125675774; Mon, 05 Feb 2024 01:34:35 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXdCX/l3wUqepSg30cg8LTd03aaPjbEVI0mjv9Mf5RrDr7lTxGAutl0RuHHzUC0iD87ZFAPr02TW26I7/M80456Z60mAJjmVXPYK0ONK9DaeFtjXvfb6kZdIxRLm+/+gt48fT1DVi7WjtUHYG08yT1JI0prbmnHCIIZvyuu/M9pCSsXQUGQop9gc2FEfmFCdvl9ZsdMZFDKGbVKlLWCoPt3Tj4tCEYyUlCMnOfAm4jaQe4NiifzVAP07t3/spZvpTBKDLRerfFXdLraeWAhrQapHsnzpmZbMnZVWjwevstDEUrRLJfYrSjyaJMb5HQtTw8cfNNg5SrBRie0YLJ+T5WLkQWIx/YHyg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 11/23] gpio: remove unneeded code from gpio_device_get_desc() Date: Mon, 5 Feb 2024 10:34:06 +0100 Message-Id: <20240205093418.39755-12-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 d49f84d692ce..f77f919dbad9 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 Mon Feb 5 09:34: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: 1895193 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=Wv111LTZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.199.223; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2953-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [147.75.199.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1VX74mpz23gT for ; Mon, 5 Feb 2024 20:36:52 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 449021C2474B for ; Mon, 5 Feb 2024 09:36:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 546E017591; Mon, 5 Feb 2024 09:34:40 +0000 (UTC) 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="Wv111LTZ" X-Original-To: linux-gpio@vger.kernel.org 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 588B2171A9 for ; Mon, 5 Feb 2024 09:34:38 +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=1707125680; cv=none; b=SxLrv+Hm/78DMj96bMYVvQ3Zxurn/nwcqBoacfD5V9R6E1q2hQwW038A92VJOb+oF65reCca0W/EH7HLfYpfwHvjMxlYRS6FMXt9ghOjvoTIfagvmut81qg6zkSz7dQHHup+ItCxrnuVp2aItZDQ6kUEjFQ4SZYBtT0Kcn1stpA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125680; c=relaxed/simple; bh=6pZf3MPfcP7U/qzAucrcxQHtn245jLEQDsd8yxAaDXY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vpkw3thWjpskifRHhya5uvFEuypXnjsH+H6EYBWSbi9wIQ59VkMbqvO4i+L4LKCphNaLSIJ5sD798uN90NCfLfWIxd5UAlfA4DEQA+oscis52zIAxqFhiUdEhzKhDNZu54ZBdZqtJtOsQRPmDAPg/qP1/Oc300F8f2foZ8KPlxQ= 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=Wv111LTZ; 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 Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40fdc500db5so3727085e9.1 for ; Mon, 05 Feb 2024 01:34:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125676; x=1707730476; 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=fPWICPIbUQqhQWXK/aPucJT3q02Uu3yz0IoZJm+ImZ4=; b=Wv111LTZxj8WUrUEuKtOGY7+yl5fqYo+8RKnHXmCJAphqtZbowkZHWQ/rB+aSqq29s QCLOYrGNPsnWDpdpEo7rVvKWPc4ROlrZVrdoQ1Ayo/JHorbX4YHau+YDZtv5aMcsAAu+ ofD4xkCP7G8qFUaKE+gJhrUBiDg8rH2lWcZbi3tVP16yn1pIRFeBdi7Zo9MelAxmb2Y9 rJSYX4pev8T5jtVycnl2HKPgoeIJzF6oe1t+xDTKpb31Jcf9xCyPcUclQTzxK1DTVvyM x0iSu+hKjYSmQj1DXh9fz7c7cfKoslZmumUjmhGU+6m7SM6T6T07YYovyEfaZ6vXd9tP CqSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125676; x=1707730476; 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=fPWICPIbUQqhQWXK/aPucJT3q02Uu3yz0IoZJm+ImZ4=; b=TJhynL8B77s+pOG01ZI+2xrWagOTsWB4sf/GK9/zWsDpL/A7xDhHDc0oS3+oTIoIQe 13r4PkAbWFId9xinB21ykYzC2SL3WAUnv9tayqGzudf7kVeGYlTcUVmjxM0UivKVnCI4 Z4/5uVtyZ5K7LVI2fekOd4Ni1MXSkAsvRu5fUk+5YNgCiSTgKIx+cr48nHlmDQpjCe5Y wrTwmYXsq1JFNLp0Wc/q8I0s2e3DcYOHHNvUvbDXQ4OElvRyiAaZHF3OdovhAkyPPqHZ eFqldAVXdo44yuAMKpHppkAyH1ySzS9zpd9i8Iwfl1i3vNLC35+JpEpDrvzxQeQRLtgz JGmg== X-Gm-Message-State: AOJu0YwBC3ICJ80ACJxdgljVViG4lIj85JAf6+xuxpCRGD1wpF7oLtsJ SHVVqnj9kzIGia7nwPWcnxgzks9oWI/VAaLg8mG+HcgQv9VY847qKopvmV0rrkk= X-Google-Smtp-Source: AGHT+IG21q8aYCNLml1cXS2JVIuYakcxkQAkcMiZABjqWfdpPL7byMFcdGrefOCQfYZl8ybztm6cMg== X-Received: by 2002:a05:600c:5191:b0:40e:60c3:c327 with SMTP id fa17-20020a05600c519100b0040e60c3c327mr3910240wmb.1.1707125676576; Mon, 05 Feb 2024 01:34:36 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUU/8yo/GZ+OF/G9T3pL+XmpQ6Byf0DyWVqn2QJEbCSns32/4tJB8SUprsV9AxBNmx3gG8oW13Wux0CylhKHd+TQbXiCadEdaXmUNbKIUZOOU7cZfDMXGjArmp/8bw58Pd9nBhV+X8b/WFw9qRiCgC92K8BBZH8t/7DHrvC3Q/K/QaCIlYwY6mzCXWT4peHngvkkVfXI6rzKLf65ONr91uKuWmOoMRre5LsmqAI3PxHVakQnNWGRYLjqe1y/y0QXfHK4YPAvk4fM9CdQIufi1Rhb62ZH5bEvd7LkDRDuLhx2VooOIwIq1qrvdaTKBghypZyJ9D9O2A9wYgS7OMYkp5fN+ml4DBJnA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 12/23] gpio: sysfs: extend the critical section for unregistering sysfs devices Date: Mon, 5 Feb 2024 10:34:07 +0100 Message-Id: <20240205093418.39755-13-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 1cc707685f87..5da316ea9cc7 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 Mon Feb 5 09:34: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: 1895194 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=orscmaR7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:40f1:3f00::1; helo=sy.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2954-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org [IPv6:2604:1380:40f1:3f00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Vs1RMyz23gT for ; Mon, 5 Feb 2024 20:37:09 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C249AB240FA for ; Mon, 5 Feb 2024 09:37:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58FD917744; Mon, 5 Feb 2024 09:34:41 +0000 (UTC) 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="orscmaR7" X-Original-To: linux-gpio@vger.kernel.org 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 2AD24134A9 for ; Mon, 5 Feb 2024 09:34:38 +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=1707125681; cv=none; b=qD2HSHAujOZLTKiKFKpj0P4irArTw1XXLHsvOs5P9x/bkBD/7Du8+J1OZT+g5VoQMCSKH2Kat51a3YTjjIIM+3LfK1L4bNs9YeMaHxdPNH7Q4DD62NY6/CPfIpPckgu/IwNuI4Cy1KSnAvFXpQIht0ByCwgSQEdjawqKz8W0aKg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125681; c=relaxed/simple; bh=dvNWzN3ht8/FdUM9oKycIS4wx6Ize3NJvGw5mcCj4kA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ISQk6DXinnk5N5VeNyZrFxRYUtTa1Urwv02apEXCroKvu5FEEcq0eHmxST/VyyjdeBhDn1VxekcqaMSfl19cYxi2FGGt2GUNZYMxVqRymrI0d5SIYjOv8fp+ny9Tq5VZGvhjBqf3Pg59InVxJeiEeNhHX/R7Eqa1cW6EUQc64dQ= 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=orscmaR7; 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 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40fdd65a9bdso2705335e9.2 for ; Mon, 05 Feb 2024 01:34:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125677; x=1707730477; 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=nkDPe1O0gAbIcKrknOKSxNlPidMI9YPWSkCZD1MG6Wc=; b=orscmaR7Efyb+6xjb8f5wnnU8y9W3058FcC+OP5Yj+a11/+LuPWhyzLLR3Xh4MLsvN 7IAe55dI4mmaz0I12mylsMDUOnOvMCDsT8kAHMZovp1g8wsTiq2+LBWfbd2uhjxHnd/e HgCOQAFh4N2yvw38CBjKKqAljikWUQ1e4Cw87V50YGlgUN6JJ/10rKqY35ACn6k3r7GQ S6iy9TW54oOehVrSblKPC3zWTCK0ByJVLhQ8SE8ISyAVEcQ9YDbFHqbe3B4gmr59MRkE SE+PlJdhN3hdpu6/I+nB5nIFkrWx3RsFBhb4YyfkLSVVPUEPAxoPJoa0+Uba3vJYsn13 ophA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125677; x=1707730477; 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=nkDPe1O0gAbIcKrknOKSxNlPidMI9YPWSkCZD1MG6Wc=; b=u2GIvwpKAcsouhMbxJOAoT8I/edAsMX0DyySkkQOS7e9biZcZ51Ji1a3W6PElZ2som 8rmRkN0vssCyRDBgsSzVbj9+s3mrQ74y6P3rIMbHpD5dgsx2mHg3ESIf+YC+Yicatt/B Qe+pCc/VpiHjBFXktyGiEbZZgtGjDF7hecbUTotODmUXO9mD93TN+lccM/Al68RoYpaD 8KlR1WB1QHv59mlBlhRGcDUQ1TvodgU4bShsWlNNhuhdKwWPbm/7VlxAYMoZ79RfUx8s vZSIEsIOn+Yd0QmoUGnoXP+MZeW1/Ei1wB0ZUz6kQVCi0C8VTf4yQq5ngqKa3S2Kt6iT +9Zw== X-Gm-Message-State: AOJu0YwHZnltQdIDHAO76iwJ8NnolVWFgVEsd80WhfzZs+ZLi+3faruk DPVvuOu+KTqAr2p87gZYASsRqk+iWjLXtltb3MCIYEPgjJNRY8L1RJkcLW95wVM= X-Google-Smtp-Source: AGHT+IGRtximsZuHOJ+4t7O1k20vhwzMFUlcMGGnBrLsaGExTNJF6XHpbFDep4ZG6gEfToYEqqNUog== X-Received: by 2002:a05:600c:154f:b0:40f:d253:38f1 with SMTP id f15-20020a05600c154f00b0040fd25338f1mr3921163wmg.26.1707125677471; Mon, 05 Feb 2024 01:34:37 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUtKZ4n8oU5koW9HFK4egEwXKSg2J4+0iyLOdhFrR5BFLFICgg2ZxVxx+HreUxjkIAcZ6Er128kBk0zLOFMm9V644WeSe7Em77IlosfjExrIlg1PVfe1r/QRTj2eojyqPxCu/1MB4dVaD5MBh+c5vQclpTdCLc1zlhVyNDUPB3IofmtBUzDqtC3CbIm7N3fiuHywSMglHKV6rWVC6TdXOEvv0Il52DsBqy2BQ9jT6wHy8U3UmPJkcLJFIAQhYXyOSCmrKkbchdWerYdX1CyBMx80AQu6oCx416aCbL2BYiYbg6F2VpvFNnGsDtJCmyZzKTSDQkgh0+tOl0DqhynPWNvXXYMx6Yz3Q== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 13/23] gpio: sysfs: pass the GPIO device - not chip - to sysfs callbacks Date: Mon, 5 Feb 2024 10:34:08 +0100 Message-Id: <20240205093418.39755-14-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 5da316ea9cc7..654a5bc53047 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 Mon Feb 5 09:34: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: 1895195 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=HU9Nb2/u; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.199.223; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2955-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [147.75.199.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1WG1nTVz23gT for ; Mon, 5 Feb 2024 20:37:30 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6A21B1C24982 for ; Mon, 5 Feb 2024 09:37:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7A96517C6D; Mon, 5 Feb 2024 09:34:42 +0000 (UTC) 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="HU9Nb2/u" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 613A61759F for ; Mon, 5 Feb 2024 09:34:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125682; cv=none; b=W/hFDxJ8YQ7h8heu8VMOIKxJ57sX4v60yYozUWt41qbruiVnHb9NrLQ1BNsL6Uk4fEI7M0jJ0jmTBZ+ajWNUvQiJeUAI7HejmXa42DIsm3ekiyke3ScuNz9WqOOXcuUXUkPcNuoSUqQyaT1tlJ2BIX4IvhQUC40CKsHCzDiDqqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125682; c=relaxed/simple; bh=yLFHAeDDaUuIFRqdSf+MG12ymiBBzwEskiNkeG6cKEk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l3ANzdS6MWxuQeHffxISzlSTynbva+blVEBHpa8Hl7fr6v7lGOYeaax2TlQDnNp1uPaJyq89Y6Ehhc/xelTtnRNOB+B98DxgBv062WZDjNRpmEvpgx33s9XPHauUKpUR+fkX2tSzabKErQuhO/pqTIKD8+e+lp2r4wNriuNYNTY= 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=HU9Nb2/u; arc=none smtp.client-ip=209.85.167.50 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 Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-511531f03f6so397001e87.0 for ; Mon, 05 Feb 2024 01:34:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125678; x=1707730478; 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=HU9Nb2/uyHGQwalTZDAF9Pk53iHHcGQmSFF3R+DwkIp3akQljNzZzkhAeZjoiAKnlw KWWSGf4BDMR7jSowS51HVJFA+K1SOTV6wxgSJhWpLfoM5gR0gZY+/C8upIopDm9MiTP3 dBkNJT0UErgrQe/IGr1zEovflTcNW8W0OS58pde+ctdqgDIu+PLeDsAtC5Qf225th4Jv ME3zodHI6gEO5jE0rKRO9dETanET90ep7/GvGvL1FvIUWZqwFsGFPKcA2q0FdGSWwFer ZI93Gi55Iukg5IUqlw6e+cw4w9Znm9lkjdOO18P3svoDY2rpy40mTWFUQgzBvNAXAiyl /9wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125678; x=1707730478; 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=VNtKPIewJyByJOOs/cMsB7cxpGIJ1I57ge2NSs3kxJWGNqrmlQDPE1WJPa02N3pM2L EzquIQsXRPHxzi+DAYPmeRTlFAZXQdP05Mmd5D0VZ8qaTkWsHsxYHndXMwU+2C0KsFjR Hycc0yavORG5sTypRXOzpglrA6Dq0eAoinsz1Ae0ntleAT91DDxULkUXGGaIL9kU88I7 grMLeXe61unzJEOTZxUW54+uNYoVGSiLAnNG2cbPYADZEgC1JNajn1ubs8+HX6Orllf3 WW+StvTgWkVsMWvrPfInJxqv/LqpVGc7O211MgBD7ExilJdY/f/KtiUMmjTVsU66mPBt E11Q== X-Gm-Message-State: AOJu0YwHyg5kAE61Dnc0Or+n2HqBL22tvyT/13ZOV+uc92Eg9RG3voua VpUTcg702xnKbtl/vK4LnUFMpqXxpoagctViYRcv6ffJPLM1lFU3RFmMrGdF8gw= X-Google-Smtp-Source: AGHT+IHJSPbTzeawrSm1nAoPiS4N3G71Ns86R6QPG9cspUSWjZVEyhsD2m1pxHBJmfCPkCAmQllpZg== X-Received: by 2002:a19:e058:0:b0:511:553a:a963 with SMTP id g24-20020a19e058000000b00511553aa963mr234884lfj.46.1707125678325; Mon, 05 Feb 2024 01:34:38 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXTHkt/qB5FXU75JA1jOYrtC+7TPevSXOu647eXrUYKxxCr1lss+Tz7tEhMBCZMheoh1VgOSfnyR48SNftUuoObspzJ4U6YMtYp8Xlb3zVzsG9ceD2B1fDwCNkhBNs90EAUoEYYyj37VzclnsG4bqiMp4TqH5FqU0KZSPiDxE3q0cc0AS5qC0/ngPCp9347dK/67/lxRHOpQ4o0GlswOOLW6i4C6JtMonyL3Hul7DOkyOAcLuUWL1/bO7YdHGkXgvG7Fdqz+uLjzXxdNxmp2ZoaR6KHVt9+ui/iE8+txviUdrNRZZ9Y7omDdq1voIY8Cx1KZt5G4MgWOEGv0+1CyYJOnwf1rha82Q== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 14/23] gpio: cdev: replace gpiochip_get_desc() with gpio_device_get_desc() Date: Mon, 5 Feb 2024 10:34:09 +0100 Message-Id: <20240205093418.39755-15-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 Mon Feb 5 09:34: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: 1895196 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=eRPX0c9M; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:4601:e00::3; helo=am.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2956-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from am.mirrors.kernel.org (am.mirrors.kernel.org [IPv6:2604:1380:4601:e00::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1WX6wF3z23gT for ; Mon, 5 Feb 2024 20:37:44 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 7F3641F236E7 for ; Mon, 5 Feb 2024 09:37:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2263F182AE; Mon, 5 Feb 2024 09:34:43 +0000 (UTC) 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="eRPX0c9M" X-Original-To: linux-gpio@vger.kernel.org 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 DA108175BC for ; Mon, 5 Feb 2024 09:34:40 +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=1707125682; cv=none; b=gKHvEAvWJE1OlW32FSscRZp49xP1cE58uxwrD1DdtAQskFdJp0yvYSraxhd14Qg1TLhDUgWchwYtz/HdGeOYWbLUvShl8Jw8MAi4G5jU8L81P6z71DjXBh+EhpcLPVmmrTFlWwF4g08lGpZ2uqz/ZV1Z1htt+qJYAMc0daAAIfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125682; c=relaxed/simple; bh=uoIIVRHu2uJkO9SDw8JuUKRoDUO7ImNaDoq5Rnmn41o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O0gwnn05Yr2Cd0VMi9u4dq4C6FnYG0GsNbf0SjzNfLUanKYwzLQKQFvsXsI9FV9NKTibbrGCNWx4GA5bF3YG3nW8ywAm2wlT0pb5ZdpsN3P9webGKuBTQJ4Mwn8spj8eGJ3ppnP6Seyq0I+fMf/17eVKJPBiex0qeK/1Oz1y40k= 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=eRPX0c9M; 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 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-40fdddbb8b1so1367885e9.0 for ; Mon, 05 Feb 2024 01:34:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125679; x=1707730479; 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=eRPX0c9MNcy97yyAx1T3uuLCXlNIgW9umvNP3JhanCUCB1ORc6hZmoE4j4VBXHBl61 QfTTY0QSEZk1sZo04yahG682FzVsHoYDCTUDYaLN+D5OD2vwPIiNhJZ9ehaAfr1mzyiS +9JeBPn4dsLYfjl3l1FeXi7MzJIhiAyLgg3WG87xJC+DIBDkbSobelhLhDVuqq6RMt2Y LTb8y2UJxm6U+fA2Kp2ZWHCGMUTZZvb5JV9PIIws4DQUYqsoczTHGRW+wse8JdUea2Rp r52zpDBL1zCO5O7rIRbEQIKeGjXVvZbBCc54eF+3VSrD/R+N4TErYzBd0GDNQt4nlec9 qDMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125679; x=1707730479; 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=R3oxjdugqNNDwwFLdrcQQRL9C0n2anbqny1wzRcfnz/Gz3S9kzQs03TsIkErXetfIf O/KMbzXnmhDZfsp7D+112I1AhwGboci+rwaVR5kHuSsWX0rozlcvq+xzwCY/H+bb3vFm Y1cwvV1QlAMHAJR16iKKqizma7q0XTVVHt4Kzlpt9UDUQi4xN3oyrSZd6MdzcYTAvJui luQIpWocqnxGXSbTA4cwr53+ELuVxEGWESaNc5OOCb1HsYDyFYTyX52qfdAfAmmlTZOU Xsk4tyqbiFCRNNYg0N5CRPEQa4mhkGE9FkqaTkTiSt203y17jybU8k3P5p1WEu8MCbxf fYhA== X-Gm-Message-State: AOJu0Yxs+jwMt8b8EZubAHtPAcDuHXE0rkN6phNq3HCqbYWlCk6Rut1v pxXXBST5J+6WtmtP8zEKmU5DVECZoo8vguJ1incwf+sg72rHlGURQsl/qM4rR+Q= X-Google-Smtp-Source: AGHT+IHKLS0m+gznRf7m4ewPz3qFdhpDGi5EcMyBvF51DPL4JncZsFuDNPywxKAWz7AcWuaFKKPy5Q== X-Received: by 2002:a05:600c:3b8f:b0:40f:d300:eaee with SMTP id n15-20020a05600c3b8f00b0040fd300eaeemr3235595wms.18.1707125679171; Mon, 05 Feb 2024 01:34:39 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXk7jaHjyRYsWkas0qWxQz/p3VQlAB7wKkG3+H6aFffc0ZmUGc1mt2/MfkUsz8Rf2/Q8fg+IP+u+KOSZFVjcbZd41sFlOvG/LFG7+IfiSfCYl136RixMsIEjs7hx1WuZjlzS0mdKqu/Skua9azLfhDNsYG3ELAcIYsMjJ2F1OOh4JkFDM8RdYuCCTLN9RErlSNBrAFviOMuYXWHMQntIGUGsdAPanN+Z7YECyg6jof8/C3OeiGS+O0I1WZaysma0zL4OWC6t5OOyOoWiCMKccl0fu2F//eExMctaZdRs7oZzE0+7UFn5sZzqFFS2xjmWPUpLUG0cuH459GqklTK/HP1n2aOpI9vjQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 15/23] gpio: cdev: don't access gdev->chip if it's not needed Date: Mon, 5 Feb 2024 10:34:10 +0100 Message-Id: <20240205093418.39755-16-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 Mon Feb 5 09:34: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: 1895197 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=dWw4p9kU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2957-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Wr5BZLz23gT for ; Mon, 5 Feb 2024 20:38:00 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id F40691C2468E for ; Mon, 5 Feb 2024 09:37:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD28B179BD; Mon, 5 Feb 2024 09:34:43 +0000 (UTC) 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="dWw4p9kU" X-Original-To: linux-gpio@vger.kernel.org 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 DD36E17BA6 for ; Mon, 5 Feb 2024 09:34:41 +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=1707125683; cv=none; b=gIsaWPDg0+ZXjsvX9Qxt5ujDyNJvDR29AzjS2VO7oXIcbITHoRrX3TsC4m5GXFtB0vXMomuMtfhPjlXlFHFXtoIA0axrxahc0W7R5UD1j2qNGneXcOfzwVKBVuAypzaLiKM+TDn7ify03WYeGtXDqzAwC3omuYAjvQWyqEPPNqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125683; c=relaxed/simple; bh=eyC1HYM9++8jEVAFNoIc4hkrxl4iukZ0gJFO/TcS1lM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dvK9b2CMupT8qLdyBPXl34Ganc+snDPnvdfdS1nmJobHs1QWHd5oHh6Q92dXN9MT9n3TkmqvATmUo5NUt6gxVrASlbPTbQSCwWISMlNRUxnK3E6VxQDaXt8Tk6q4cU5nM2nhkDsBO+qm9q59sOLibnZXYCStH+9RQsKe1Wj8+W4= 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=dWw4p9kU; 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 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-40fc654a56dso24711445e9.3 for ; Mon, 05 Feb 2024 01:34:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125680; x=1707730480; 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=4fizk7gLi6MsEYz29o/FD8jywGHpms/n5O2KFkgE/SM=; b=dWw4p9kUydg9enHFOM/CVcIeJ9JZKRZI8F4CXx9Tu+UmvJIkt+3HRAVZyjR20U9q82 vUY+biHeILC5MEA6AiszZI9UYZu4zPsUNX37UB2yOssBYZi6s1EOQ0JbnnhnsgUzOZYU HHkFQsDad0FggEoUsOskGFVTcFNDB8zXIetWAZouUustteGCSUFoy3Dg/h3H96TZU3kS 4KgZeJLPs89LtA8nfEesb9CyPBW+cCmxSm/iyrLed3IxnmWhb6bBbfFttpC++Rd1tQJI QISmSimtjyHRcEIr2IPM2QhzaKbSqvWUJJdk42O790doGBYpWDU/en87UQk/P1I3+k2n Efag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125680; x=1707730480; 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=4fizk7gLi6MsEYz29o/FD8jywGHpms/n5O2KFkgE/SM=; b=E52XmlBR6DnMK0BOoLlWAHziJ6PmX5MgFP1b61ZVKoDqepehppie5j02KahAQDJof3 NiSfKAJCvjM+P0jAun/azd7sqepImknMHp4GGd74nkHiFxvISR+eEAIPTJqGFYXNy93H xWa5ynR4MpuEkczXsCHL2Oz3p3/uKO4ff17/PjsIioW9Fz/sWjkAh5sKTFARSSqnZyYa WbN5x5fo5HHANu7V2qr+gqIY4mbD/bv1JSJmb+z+7oCZR6WV36hvDdbVm8ow+XI/4O1H 1l9n69lQp4BhCMqvBWEDY1jX2dSm2Ndag8vt9jjmoxXehnfBwQ6M1EYWOHfCgqfSUTFA UYzQ== X-Gm-Message-State: AOJu0YzNAiLdq5Il5JMlJ/MoTQOME9IpmuEreZqoL1okuJ7+jJ7sxL/R ReEbN6LWY/RWseqIPI0Bj7VZ756IiHu8ACaceRU49OVxR3YglK7Mjl16ji/rQL8= X-Google-Smtp-Source: AGHT+IHs8NsEz1j8efT31ngqMa1MhlmQPpIvYA/AUkpcjmbj0I9bIIh8/Kf/eWarc0dkyZLPNEzXWw== X-Received: by 2002:a05:600c:4e11:b0:40f:cb9d:bbea with SMTP id b17-20020a05600c4e1100b0040fcb9dbbeamr4038067wmq.0.1707125680144; Mon, 05 Feb 2024 01:34:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWBl3aOBWX59ah/FQPmhhvtdeqTvaVZ1j6HnJC4Kzm8kRil+MNUNO2s0OId3ecuwtYNrW+7WfwwrMoXstcfipbTh/MqkFaiCmePXRAX8x1QWvwgNHEPJC3JZvVnxdjP7xTaP45Dtc7SulEuda7FEDRL+b/VE8C9eUg9K7hXTSnJX864Q0/hMm2h5TQlII9ldj7hvZlDap/kw39/A7gkcU7LXRq3n9ZjtTWxkO9hIV81Q9hXw8lEm4XGJnKfQvvRmKdQGSD4LDpefXhYrcZLTCXtF8WxTHKhmg0/7J5FjozChL+1lhlvvBLaMSMpFAvGGSwpIs1+Q+r/LLHkwiOHKKVHNQcgjeN5oA== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 16/23] gpio: don't dereference gdev->chip in gpiochip_setup_dev() Date: Mon, 5 Feb 2024 10:34:11 +0100 Message-Id: <20240205093418.39755-17-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 f77f919dbad9..f8d53ebbf25b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -719,7 +719,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 Mon Feb 5 09:34: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: 1895198 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=PZyb4/wu; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2958-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1XK0vSSz23gT for ; Mon, 5 Feb 2024 20:38:25 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4E5701C24ECE for ; Mon, 5 Feb 2024 09:38:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13FE119473; Mon, 5 Feb 2024 09:34:45 +0000 (UTC) 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="PZyb4/wu" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 B893F1802B for ; Mon, 5 Feb 2024 09:34:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125684; cv=none; b=czZNT9Q+4LlWMAnLRQWqPyIY8Nv639Rhon8CUfeSpgiyi9CLPAmONYWI1XDfJ4+3Uc3Sb4d9Lc8JeW4+q7TrZ9guFbQnB0/ZQp5e+DadAHT2qL9e3LhVrjLcj6iXC3atp3QN8xvuhX7Z7ztWZVCJmam5kXB+BKXzm0GjQdrCf7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125684; c=relaxed/simple; bh=DEm4ivJPwvyJQ3+rwUCzWKrs05E/BVn3gRvUWmQAFN4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=g1X/phDb2CefR8o/aGgPkaD65pgWqi+jKO/9rs2KLtaCDVXO29XmhtO1yOTZz8/HCP5npccDEMXg/jKmveZw/wzW3QBtjonKrhrJA8ZybLrHraLxZVLRQiprLuUSTEkr1J5B+cc/WDfFnn94tjGY/D//ucHwxPBH72tpdfK8yzY= 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=PZyb4/wu; arc=none smtp.client-ip=209.85.128.46 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 Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-40fd72f7125so9368455e9.1 for ; Mon, 05 Feb 2024 01:34:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125681; x=1707730481; 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=VJfsBLVML9p9Iy9pV/o7OjQ6GeZUjbwVmk3zuZ2HyJg=; b=PZyb4/wuX4TCaUqA4Wksz+TjJoDLgA6L5b8sSBHxg97dbH9jJvwzBYcViteljdnNo6 x05CA8lPWScieQnZqqU4XU/GezrSTB/kjA3nwCS7mB+Lx/vS4Iheh/UCYjp5yMvFiHs8 hlskGtu64Jd78pUsQlKayd0Jucl9lwSzMEBPdBm3O8A6mlHcxI6SirOd6cM8q9YANbtC 0pfOtdFi5xMINaBetaDjhKy9/yBSt7F1xupntsv4CHPuyqNmI0/aSainfEi9+/eJ3T0V mN3T5GRBS5SI5970UTFl/lyd/RWSkbpUuPr34PKoQ9xt7Mqgdpqi70Gj70lvXlfhHF3H DoUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125681; x=1707730481; 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=VJfsBLVML9p9Iy9pV/o7OjQ6GeZUjbwVmk3zuZ2HyJg=; b=DCUAaFlhOA5Op517tgdBL3q06eW2f8ZOgaTwXqXNeF9OTA66LhPDX6/DAaMu89vnS9 N3s6bBjV6LqTS1jVYu/GFFpc2DH6zbZdJbHOs7XFUwADOA1ec8ADRq4zO94Pr+PVg5HB yBoR6AqKJZ4bqM+8mUkx+yxSXgxiAlw2qdg11RX6z2ZHBFWeRyeMo4t9NM6A7JuVVnkX s9OQuab1vQenTduwX7mEPJoL7D/CdGKDy92JfSGFixB98hB+MSVFDC/pVOZAl3Lxdb9d CYOAkyPXpxzsyLLFu5EjLYF5qWOH9AVxNWFomaYST+6YQtVB9zhgWXIpbFRAllHcGwcC JrBw== X-Gm-Message-State: AOJu0YyO5fPhHdNuL8nUa9+wDY9NZSzxvsrdhkqN/how5jIhxREmUfms de83OfDGEswEMHW4objO6JYV3yDp9OSGA+5uhCHqiYh7w8FNgLv8AS76un4rSB4= X-Google-Smtp-Source: AGHT+IFTk75p4T7YsB2qUfoM1Ugfl9KI9GBfXAbd4boWpGB3sTqz4xEuekxq7k9bDL7O579RyRi3fg== X-Received: by 2002:a05:600c:518e:b0:40f:ddda:4e19 with SMTP id fa14-20020a05600c518e00b0040fddda4e19mr506479wmb.9.1707125680992; Mon, 05 Feb 2024 01:34:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWjvm/tk38/dR++2+UOg7NwEVKczDnNOK22kyYjVAFTkO2DGlirfusYq123N8IU068HPfieFUIS/yUiCJFZTy9WOkfcVWCrbummQyXl9mA/PV7fIWijg1B9R3vFGjlEAtxZx5gZ3iQ/VWrexNNQWoZzoLJJaL+DHoYabAl+A9YY7DrudpTECMIi9ih+itfdWNQDKvFY92j7z3QCPeZWjBpij/e1nkZ9RRsYG+zVwIrR5lxfnbfcZhR4QbMBf1i3+sLXiP02vnoMs7JCZPRIjZ4Q2S/+G6GoXyf9F21akdE4JyhXh6cA/VMuU1ldTbkWVDY6+6zxuYu4ExuNGWAyrzxPPeSu8cyZfQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 17/23] gpio: reduce the functionality of validate_desc() Date: Mon, 5 Feb 2024 10:34:12 +0100 Message-Id: <20240205093418.39755-18-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 f8d53ebbf25b..7d897c807e95 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2251,19 +2251,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; } @@ -2339,12 +2332,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 Mon Feb 5 09:34: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: 1895199 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=yndcTLQ0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2959-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1XY1SkNz23gT for ; Mon, 5 Feb 2024 20:38:37 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 29EC91C24D01 for ; Mon, 5 Feb 2024 09:38:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 90F7B1A286; Mon, 5 Feb 2024 09:34:45 +0000 (UTC) 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="yndcTLQ0" X-Original-To: linux-gpio@vger.kernel.org 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 87D991863F for ; Mon, 5 Feb 2024 09:34:43 +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=1707125685; cv=none; b=hheS0kd0b6S59iuNEfVxnnA7agS7CIStTl1rJQEPr4V51k1z8RAo899HsWsCNt/shKWy9h77XgNHUbyV3Lb0qe1JWWy0eTwee8E7mZr4LFiFy5ogX89id6XJE4A93GTDOnsGjTjFbcN40kYEufA6sp93Y5breIG+MNrR7fcYIOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125685; c=relaxed/simple; bh=pEGFI4FzSuM9h9FPQ1IrcofNm0GzKPkH6oHp6XPXvQE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AawZvSYMZstW6AeXS5GB8f8HReo21XU3U7bao9GUGs8URXU7mx594xGiuNQ7MmPeqciq8ZsE0Ivp/03prEY/6nmw1hhlGBV4MIwQA4D6QBZuBCSjM9BXYkpwoqKgyZxFRGFIKArUAXzKOdI6PVufz2wbH1MUY8BL58AE7h5lYqg= 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=yndcTLQ0; 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 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40fc22f372cso31824025e9.1 for ; Mon, 05 Feb 2024 01:34:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125682; x=1707730482; 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=ooFRRocehP/GMHGdd997Hq+dPT1Zk15K+dtqhfO9BRA=; b=yndcTLQ0J+XMpWeWrTV69r+9Jv7+XpYXtiJ+SizmO2PPad0aPD8ZHtd+SanHbaquam fW+EhxZHfBFO9+guedt4pzkoiDJXzOdPlC6Jz1xKRV7MdX7XrYbcNWwwuRKqD3jbGZHn WN4mnGLU4Fc7XqGucb/QtVpnghKEQ5oTb46cvharxyDZaBQpt+maLDSsl8CTDTeVvfxQ YmIEAHFvKvsFvjJWbCwUJqGSS+Tc6WZ/vug0+IFOB/XAT5MNFUgKRURMEN8acDiTCdLB oOcifWTRzVANKBC78/KfXIo0ehGvrapQzeRrG0ACHu0B3qEW1o+GjsguR37W23D+OdHP T4aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125682; x=1707730482; 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=ooFRRocehP/GMHGdd997Hq+dPT1Zk15K+dtqhfO9BRA=; b=LmAbZ+fon9BUC9YNmOCxeR6c6XuHHqdnWsKaQe27Q7AXSYS23X6k1PFoNAwmi86M5B 5MlAuOVmHFi8dWX5GpdiNIz+xl/ILnxezSAj7jsD220PdNVJ7vqI6MW2lsNNlGvRoV6+ EP1BW2nDTNEW6L6aw1G7BXGZx6OoL44Y+BGEJrIqgUkeirGNL28dHYNcy7gvDZGztx+J 0CnbfFsEH//XqimXKv6ttufphhUgXl+ePrhyXCOt8lIvERoMdfUouwy1C+vSE4Z/lxoG 6qlGZifRdbKwQ6CKhvWKGOqWQCmNN150tEQDXK6wyPQSoVvq7ocwu7VdFk2G98xH6tKr R76g== X-Gm-Message-State: AOJu0YzvYE19b4ya5JJCwF3XikxY4DU9gz8I7EMxrBVI6QD8xQsK0KTG bB0BZj6ie0tBXaChJJPCDs2WvEklYjKl5IUwjIY0rDGe2MLMM6IHpN37ScCp7tw= X-Google-Smtp-Source: AGHT+IFhcLgkx8KHMiKDvuZqNmGC8eNgjwJXoKIcgWEf6Qq0l9U2BDTDNM+uWBEonFwKj2EUg30Mww== X-Received: by 2002:a05:600c:468a:b0:40f:d738:c213 with SMTP id p10-20020a05600c468a00b0040fd738c213mr2655782wmo.29.1707125681808; Mon, 05 Feb 2024 01:34:41 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVeChOtUDPQKIzjVR3gHFJwm13T5fUfp2EvjfPDa0WJAsGA4ulrpHbHNP3ClsXHPf9p/pjCSdJ9BDfYItHn2ag2YYMH9PdOge/KVOSeui5uWnZJwmduSIulnijzimN6uT1BuwnUCCMSQ/y2CoqN8mmCLI/u0lYAG07zbiYh3EDl/Nz8fOgfDfN89X8ZFGEaX/ykGrIPR2ZBNuQet0yxFq0v+ZJdcSitIRDGmnT2yY7htzxh7T+Yd3N1lFAgjziOo6pBuIkRaVDvaOsoiajQziNN5oSlXXaTn7uqCVCvDm3OXQY51irCEEPyXNBzaaKja7uiftMiFshUuLfqmzXsuoQB4/bd8f8TDg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 18/23] gpio: remove unnecessary checks from gpiod_to_chip() Date: Mon, 5 Feb 2024 10:34:13 +0100 Message-Id: <20240205093418.39755-19-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 7d897c807e95..95b480f01fca 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; } @@ -3499,7 +3499,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 Mon Feb 5 09:34: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: 1895200 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=zxXsC8LE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=139.178.88.99; helo=sv.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2960-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [139.178.88.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Xz53vgz23gT for ; Mon, 5 Feb 2024 20:38:59 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 256552825E5 for ; Mon, 5 Feb 2024 09:38:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC0AD1B7F0; Mon, 5 Feb 2024 09:34:46 +0000 (UTC) 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="zxXsC8LE" X-Original-To: linux-gpio@vger.kernel.org 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 8EF3C18EB1 for ; Mon, 5 Feb 2024 09:34:44 +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=1707125686; cv=none; b=qM98hk9L5uEwFKz3h8xsqnO5/zPbfsz7ZpKysu1hoY9aAE9nz9RQgrQ0OcN+J7JlU+i96gEquJaxVFe9HfbhagWjtbM+G3yQFw71X62Cd+PDEBwAuX/usqhmBSfnszZRBFqXRkyN30gkRZhJZnek3ZtxtcjqMRcO4pm5vVF8cxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125686; c=relaxed/simple; bh=Dy4mC9/ApSM4G2nhOEYNfhdshYRm02m6ARcUBbiiHZY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lcAhJKNjlDdxB6QtfLBpjCwWaMs32Z2ePPSc76rWc/+nazk8DMqgoCQmLiz6VZJg0RoazHPLy9pN5ghnqKh9uYP6ktcuQ3o+tUcWCUqbeansnuFsNhCgF90RojpqNX+k3R7RJsGg/Xdd8i77B4fAt1dWyyUeGTFjAJO2NXQ3Fsk= 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=zxXsC8LE; 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 Received: by mail-lf1-f52.google.com with SMTP id 2adb3069b0e04-511531f03f6so397087e87.0 for ; Mon, 05 Feb 2024 01:34:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125682; x=1707730482; 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=iw+aFC5ArC2ijhUY9JiIEGX3/cbiqFWJyl5FMkkWhDw=; b=zxXsC8LEaBbNRHIIUAdNOr5R+GRHSZC0RhzRwdim8ICgN9J3fHCuB1ajtMV5zJSBai gT9glb/Y7X8WX1LujUDAUJ2FFtWYR7TLFOa+tLOpMfXTvCadPIdMUOy0pMzQHU4iaM+L z3O8bP4k4ekD+SR4E3ciE/Dtj+RrKFd324bIYHaEC4sr4BGqHrqBUEVA6qL00uP2SdWr +3mSMCLkwMV8kDr3yQYKfvybTEL5lcMfhU6s6NalldQMXCh765g/lwNVvO9ixBroll8y HLz9XQIg20XwBzaBfMNT9MRJXP4Ko4XVUToD3KMSqMTsK3dgMq1bLVfaj6ab/41c9UBu MljQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125682; x=1707730482; 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=iw+aFC5ArC2ijhUY9JiIEGX3/cbiqFWJyl5FMkkWhDw=; b=gGf1/480ZhAQgi2opKk0DFC23QIfKcGylFsEUQy+ojOWY5YfMZmsg+wV1e2cloC9YM TJ4CCEePCTagnXvCChR4c/Qyimeskn7LcqsnBKkz2fX8nP+TRQo37/JHOZ8h74iATCD1 /yT53uc3DrZXiHoCN4cGaE+czdKO5PH1DVF7oC3KarUxDDy6uST9r0rsKJwyCpcjvprd va/v96pD24opvPzSpv4Y9bR6bR2DXhWYbZaOKNZXdyv0RzN4IIL+xPm/oD8IuR02exCq BKAnxKV0Z6Aj8B2NadeoVk8x8iHH11Qif9iJs0a4Fj1ZyIM9Jri0BcHaJeSkNmmSUkuI IxzQ== X-Gm-Message-State: AOJu0YykHlW73h+co0JC5TXPdoAvgDcBDf2b8quRt8grll+98IqAQR52 xqOd8jcaPx6iw3fcJZ6979Vd4poi1TMymBc4pzcPIr3O/jqRlDLr/5h+QB8OBAg= X-Google-Smtp-Source: AGHT+IH0kgu91seENjOkYZzprpiiHZskLJKVML41ZB5XP9T3vwqrG04okuAk1g3DYhSvhBfBk0+0mQ== X-Received: by 2002:a05:6512:239c:b0:511:3261:d112 with SMTP id c28-20020a056512239c00b005113261d112mr9043655lfv.9.1707125682699; Mon, 05 Feb 2024 01:34:42 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVTCt4J2ZTbN3EA1wpOUNy4BpzRJVF69tsMRM+N/P3JpQ6XRvjI01O1r4QlVcLcTm0q+KwQGQx6B4KEPf09BG7Lo9M2Hl3u/ZLoAxurWzye+beHodXs4+78XNQ750ntUeGTpazPnXyo0BaNz4nYT5rXDb4yWqu87OcwjG6peV5g4xB54Q4R4XinVQq3cMsbjTdfGnGS7zIx2S+MhiQU5BCXcV33ZOs23NjELsraFc3Xcmpfmr/64b63ai+/quKiyhShqF1xIiI8htQ8Qm7E6RPIaEqvw4/8REyKgkoEnqI4i6z3YWvUfvonjUNQBLzxsyXnVIrOOiVncdUGivf/aKm89A2i0Tg4Og== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 19/23] gpio: add the can_sleep flag to struct gpio_device Date: Mon, 5 Feb 2024 10:34:14 +0100 Message-Id: <20240205093418.39755-20-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 95b480f01fca..2f78fa9f8dbf 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -896,6 +896,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) { /* @@ -3066,7 +3067,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); @@ -3087,7 +3088,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) @@ -3360,7 +3361,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); @@ -3401,7 +3402,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); @@ -3465,7 +3466,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 Mon Feb 5 09:34: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: 1895201 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=EFOnRDkp; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:4601:e00::3; helo=am.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2961-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from am.mirrors.kernel.org (am.mirrors.kernel.org [IPv6:2604:1380:4601:e00::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1YL1pgwz23gT for ; Mon, 5 Feb 2024 20:39:18 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B88891F24DE4 for ; Mon, 5 Feb 2024 09:39:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1513D12E71; Mon, 5 Feb 2024 09:34:48 +0000 (UTC) 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="EFOnRDkp" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 878A219BA6 for ; Mon, 5 Feb 2024 09:34:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125687; cv=none; b=C3yZOGgV6ZWBg8fJ0DRwN8iOQodU9YbDcNsXk7TDvIL8bPYQidHLV+CuFuMlwXS6Z5Myab2wpbnb9bDKy1nODDDMNvhONxbjkxsrp36CpjLRdORgYagoaYpQXj0+J/+9AREyQaALMR4oRDd/dl2jItHx2IewI687CyhUPNAIwYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125687; c=relaxed/simple; bh=7mJd5R2FfiEUg4aI1+D/j4yKbEfikBS+gEjl/iwhNCA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DJ+qBSU2M0uZZxrO1XGYbgapscZKtds2BDTMhEU1QMyQcowLxrSbkHdAzy/g6K37UWRaOnufoLj0yrvyhmqL0Bb/ZyR4/oa26cKadxty9vAmjpgTrN5bSxVMowP7FyF4Zj4AOnBSNM+6vJxPzwKvDcflIjHHnlB4enShSbRdG7g= 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=EFOnRDkp; arc=none smtp.client-ip=209.85.167.46 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 Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-51147d0abd1so1370896e87.1 for ; Mon, 05 Feb 2024 01:34:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125683; x=1707730483; 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=EfCUtQSab/f+GEUcOlTEQIAjO6ADTWtbYYTnCznFDE8=; b=EFOnRDkp0AzyqNzWQHSJTgd0l2m0EZwSGebIvXeU0SQS/fsB0MV3tv1XH7eZbmeT1S 6G6WUJKUmjUwfNh5E6uyZVnpbsg8mSomtUUZNMfjD4CZtZmgFhmIy/ZZV04KjYxDEfrJ v/VKFv3wpZIUAhxyILlfiayI2Kj3/xSkBvQ2u976iThf7XEsgNyQL2H82hGW5RAAccui IwkcqBsBWDyU4Iwftqb77vvNB/qg7qF+Tud8OTunSamKhNKSZI+KKNeHBpgHNf0FDOwI SeTFxuLW9IMMjrHzEdZfZc+wVM3wQYm7iU57gDaNGhZ3KULvhN7WELTX9GJL4gUpInud 1VuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125683; x=1707730483; 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=EfCUtQSab/f+GEUcOlTEQIAjO6ADTWtbYYTnCznFDE8=; b=fzfUzLccHU/FtJOju8MGThukvKD2sXSiiejfeU5EvnZvT5I1Sv50lHeVnBk6cP7+Bu ozqD/8x5g1YlBjiajX8ExP5WT0D3piSkQedLdgLi9bMTsYOLu73fUKsOGPBQXn4cU/hW PSa8QPgmaunwC382mamY1pfMm2KpZPxf8XfX89YlrCjMCzBEGd+yXMNidnOTUWdeIcNZ zzDGPkT4uZvpymZhd/VfUJkDvce3Uga43xYD0Ysx42f+373nvE514YzVSjn5T/uSSKl7 EYhqWu0orOE0s6XzjgGsNiR/L3bNqu/Z22V9Wo6XkVf+SQPBWJmi7FP2K6K1g/fmkDYH Zzjg== X-Gm-Message-State: AOJu0YxoF8SXaW6oiRyDOQdC70lNZGSe/GQMb/8zu6nnfCl6t8Rce+ub DUmkgeER1HwB9uAyUrmmM+7F/y6RZj9Q7DWEwHiICJssSu7CFBgGmEhveRZ4+/A= X-Google-Smtp-Source: AGHT+IGbzkspZ2OxUbf5+9mtQYHG+0DglASi8UuDhWIORLHqIuJm4X1N/9W/AZO7TglbOMxo/nZ8Cw== X-Received: by 2002:a05:6512:3b27:b0:511:2e7c:768f with SMTP id f39-20020a0565123b2700b005112e7c768fmr9577478lfv.46.1707125683608; Mon, 05 Feb 2024 01:34:43 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU40ycQ2gHSQqNLzlj+wixA3sX46WZJeEInwacBub0YUBh0qHj392LloTrzZIyWuLx667WiMDnJRNeTuP623WmzduvSr04AQZqLrZCkoB3AX5UAloP49CHvarI5HWcXvoYbWXPj2xIYdLPIekKoHs411woSyIg0B5WWKqO2OrmkRScRLnjkDBKJYdBINEhy/jf8QFsFWsRw7RLcMPM2b99dUjtU3zLHxrC8OuRF1lSEREI5QLQnG3GJvSL6luR99Thwo11m/SbBCroaL+2LXs0eyNNwi4BH9alZYSA5kVsQ8zP7TEfm/gebww/JNVS7pkxOHYyrOWHqIjyfpqzrXsRKute8BGH/Eg== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 20/23] gpio: add SRCU infrastructure to struct gpio_device Date: Mon, 5 Feb 2024 10:34:15 +0100 Message-Id: <20240205093418.39755-21-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 2f78fa9f8dbf..c49d7a156bdd 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); } @@ -943,6 +944,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 @@ -950,15 +955,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) @@ -1038,6 +1043,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 Mon Feb 5 09:34: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: 1895204 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=DxWpusgM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.199.223; helo=ny.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2964-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [147.75.199.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Z96VDHz23gT for ; Mon, 5 Feb 2024 20:40:01 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1DBA41C252C9 for ; Mon, 5 Feb 2024 09:40:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CFA6B1BC4F; Mon, 5 Feb 2024 09:34:51 +0000 (UTC) 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="DxWpusgM" X-Original-To: linux-gpio@vger.kernel.org Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.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 AA87D18E1F for ; Mon, 5 Feb 2024 09:34:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125691; cv=none; b=tIxs1fGvGtCuoyGtCg/SaZtAP24usii2oGHFHapgj4m/pYq4Nbj739DgPn4J66ld3AbpFuf6Ijcf0zJ1/vrUW8X8w7qF+v00tJa3Q9/h/OJ/ViSHhU/mml3i2jb8bW19d3SLA7ykL2NgZVSnOCs70gwaBEoEjMvt6W3lpHAnELc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125691; c=relaxed/simple; bh=8ourVLEysJ5XmEvbiPJARNgkbTUOGYcVPstK66t5LBQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q6l8j2DhJED/TqNZXBHShaZTYigKv0OvDCFYlnMWGN3a9SGLfPLD9o/bUxZ4Jo2+gT8TUdkWjiGE3dcBiXL5KYAaEv+scIMgTNSDuILzMTYdicVmCTcBQAi2H4wc0Q8bUp0qQxEZIdxFOvt1pPVcjT+NcL/C5KpDAwxHV0moeMQ= 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=DxWpusgM; arc=none smtp.client-ip=209.85.167.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 Received: by mail-lf1-f48.google.com with SMTP id 2adb3069b0e04-5111c7d40deso6872091e87.1 for ; Mon, 05 Feb 2024 01:34:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125685; x=1707730485; 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=D36D0mH6CfPSIWqKS6UrwYoJeFTGfcveguFEIKk2pus=; b=DxWpusgMBRHNXvPBb61s2leMnR40F5GHGlAmkKI2/oxDn8mhBYtQHhzJUDjVO+aLll J9WS7gtNarWB1isx46LXgGnMN8rwYhnB1LVbmx7FDvyiNEP6kYGTvzP1Jswf74ASJ903 G7rwJvs1m2uJNiwZddoSSC5yH4PHJ69mKGP5BCLC18xHP3OjVzd5n4QVlshrwAqPrfNc KU2umEBpqCrLC5rYoBI6+ES0sdnu5cFDYW1ox+2j/Wbun+MvOZcxiApGdGqfPvnXVAd2 eTfQJGnZxs6WELc0GodqksdP3IrpqLPUHlN73MdizmADBgADOifY3ZNnKWcllcESrW/L cIuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125685; x=1707730485; 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=D36D0mH6CfPSIWqKS6UrwYoJeFTGfcveguFEIKk2pus=; b=GRqqeVvkBvM3S/toNu+pAy1gHzvl9tjGC7tawg3YND39o2kQ6YA3ZSo/NVJrqSEbtp x62GWGuWfRvaUCl/pCuHe+dtmH3ZfPK9jKLPpGwgefQSWzhFsOp2TRMyLqgAAASWVHiY W/A57IBUrYdYxUK7bGIRpDhyUFs9oTR2I2FRhtgN+5Toxl59OR3Jqwj+4Ki5xcQ7kGhz 8hr6XIO2eFgs+lXTNmsPaYjN5xEGUlY8QU4mF7KHpxkzkrshhWfCuQAtoyxN4E16I8m1 S0zOV+tL1cIYPYgBSw50S8q220Qs52eCTVbS8aY79QgCMqNHrj0mpcDRFsCwol7GnU32 bz2A== X-Gm-Message-State: AOJu0YyXrM/HX+DH1XVFsGQ5lYC3OK/c7VjkM983VV7tJzQFP4UvWzCy tPHCGYL0uTE2ohCfVtyXSI+oeLAJtZM/UTMCslSDraGTNGO+HJFkN2y0EzcOVHw= X-Google-Smtp-Source: AGHT+IF4QduckI8fvBjkqmVsLesc2FHREBtsNhmPVabnSPKRD2sH+NRSg/l2bUUUwRMa3fDTkWY+0A== X-Received: by 2002:ac2:4da6:0:b0:511:499d:5dda with SMTP id h6-20020ac24da6000000b00511499d5ddamr2866843lfe.10.1707125684600; Mon, 05 Feb 2024 01:34:44 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCV59de/kiZDl5eLc5DL1x76extKa0szhl8QjDr2dPl4XdXh0nR7UA0ibKw103x0ReR7/dBEYnfCTNCmU1ss/U6/G/5L2bCHZyirudomeyR2kaOt9MJ8184M/lbAANOIm7FJu2T/VNnKx5Cl0/Nt93IJwaspzG4pm7XCtWwwm7wSNGi9wcLpJ2AmwsBp2HRtMbxdru/dpjr9iJJDJGKRsbl2MoqRN8r2u8/BfQWVLTezH3VED+YzAbMAK8Z1ZcTuiG1nIKj4AQOjQuEdyqBMiKRyHSjvyI8YKCH69wYpsFVhHiou2pwPe9n21LfZ6mosP4fbLgbKFz4VPjmn+pxqEFVz/ivCzagBYw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 21/23] gpio: protect the pointer to gpio_chip in gpio_device with SRCU Date: Mon, 5 Feb 2024 10:34:16 +0100 Message-Id: <20240205093418.39755-22-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 | 90 +++++++++--- drivers/gpio/gpiolib.c | 257 +++++++++++++++++++++++------------ drivers/gpio/gpiolib.h | 22 ++- 4 files changed, 298 insertions(+), 134 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 654a5bc53047..15349f92d0ec 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); } @@ -401,27 +409,48 @@ static const struct attribute_group *gpio_groups[] = { static ssize_t base_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct gpio_device *gdev = dev_get_drvdata(dev); + struct gpio_device *gdev = dev_get_drvdata(dev); + struct gpio_chip *gc; - return sysfs_emit(buf, "%d\n", gdev->chip->base); + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) + return -ENODEV; + + return sysfs_emit(buf, "%d\n", gc->base); } static DEVICE_ATTR_RO(base); static ssize_t label_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct gpio_device *gdev = dev_get_drvdata(dev); + struct gpio_device *gdev = dev_get_drvdata(dev); + struct gpio_chip *gc; - return sysfs_emit(buf, "%s\n", gdev->chip->label ?: ""); + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) + return -ENODEV; + + return sysfs_emit(buf, "%s\n", gc->label ?: ""); } static DEVICE_ATTR_RO(label); static ssize_t ngpio_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct gpio_device *gdev = dev_get_drvdata(dev); + struct gpio_device *gdev = dev_get_drvdata(dev); + struct gpio_chip *gc; - return sysfs_emit(buf, "%u\n", gdev->chip->ngpio); + guard(srcu)(&gdev->srcu); + + gc = rcu_dereference(gdev->chip); + if (!gc) + return -ENODEV; + + return sysfs_emit(buf, "%u\n", gc->ngpio); } static DEVICE_ATTR_RO(ngpio); @@ -444,13 +473,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 +486,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 +595,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 +609,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 +640,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 +762,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 +775,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 +807,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 +819,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 c49d7a156bdd..89e1c98b0bda 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); + if (!desc) + /* Sane default is INPUT. */ + return 1; + + if (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; } @@ -847,7 +863,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, return -ENOMEM; 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); @@ -1087,7 +1103,8 @@ void gpiochip_remove(struct gpio_chip *gc) gpiochip_sysfs_unregister(gdev); gpiochip_free_hogs(gc); /* 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); @@ -1150,6 +1167,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. @@ -1160,8 +1178,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); } @@ -2208,10 +2231,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; @@ -2225,17 +2251,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 ? : "?"); @@ -2302,18 +2328,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); @@ -2470,11 +2496,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, @@ -2564,18 +2593,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__); @@ -2588,10 +2619,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__); @@ -2610,28 +2643,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__); @@ -2641,7 +2677,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) @@ -2757,17 +2793,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__); @@ -2786,17 +2825,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__); @@ -2815,12 +2857,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); @@ -2918,10 +2961,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); @@ -2947,6 +2999,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, @@ -2984,33 +3044,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; @@ -3025,9 +3088,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); @@ -3168,14 +3231,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); } @@ -3193,16 +3258,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) @@ -3213,11 +3280,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); } /* @@ -3278,33 +3346,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]; @@ -3340,10 +3411,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); @@ -3499,6 +3570,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; @@ -3510,7 +3582,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); @@ -4690,12 +4768,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)) { @@ -4761,9 +4847,12 @@ static void gpiolib_seq_stop(struct seq_file *s, void *v) static int gpiolib_seq_show(struct seq_file *s, void *v) { 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)", (char *)s->private, dev_name(&gdev->dev)); 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 Mon Feb 5 09:34: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: 1895202 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=Nuao0lpK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2962-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Yb1Xp0z23gT for ; Mon, 5 Feb 2024 20:39:31 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8C347289891 for ; Mon, 5 Feb 2024 09:39:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6090D1B95D; Mon, 5 Feb 2024 09:34:49 +0000 (UTC) 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="Nuao0lpK" X-Original-To: linux-gpio@vger.kernel.org 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 2EC311B80A for ; Mon, 5 Feb 2024 09:34:47 +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=1707125689; cv=none; b=nngdVD3dKMLSppMsrRt/jnH/eJcKOPd/1+W8mwEhur5nVq5PWDRH8FrlAdP5wHH0wx6ZC0PTvQqpRkkGeLfRU13l2WFgsNnLkVaHC/PCqZzDk4wuMnV5IaLp9eRmyozLynKTsvIC47S3kFflI+jtuHnPGf+IRMuz6eszwA5Plfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125689; c=relaxed/simple; bh=sR04Q3usSUpqWJ46O/KNDsk7BbO6EIW7XnGPi8eaia8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uH5Tx72b59nFBaDlQEh2G2ZrGeveeNZ4ka8qAd/sPCwx8Ki3Zk7xApG4zvJSkTkQkqWfFOwIj1u2OYS/Dq/17TYbdm9pRfU/1dRAo8sMjcfLD8nbDHd7B+5KJC2OwFtZNL511/75354FToDBJgB/EEJTydMMScFSGsRSBwuMAf0= 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=Nuao0lpK; 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 Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40fc654a56dso24712125e9.3 for ; Mon, 05 Feb 2024 01:34:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125685; x=1707730485; 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=ANtgl/tgq51oFHA95121eXS9npOxkHEx1ig3f0DQeJE=; b=Nuao0lpKZ1fMSAACPr4RNvbfevd32J4SGwdIRwBJvZErfMuINYZUm3KMud7VMxjZ9M IvhP8ORh6liEzvp5+PBSVsWvpXDyiaxQW/oSvjTvHHdzAMBa2TPVjQsuBeWgA9XvjsCg +3zYnlU685GCEZX40vRr+ls58jPM1tKT17t+HmmBwrupK5VsU87ozN+51Xh2+3IRL2Rm PEbC0WRvdRKt3f31TOo5AfYkb/WzzXFZ8wLcL5ho3m6uIfqkHHtsAfE7VA+y9R38EPei h1ZZP2CmifOx+W8RvL5dVmrO15eDF0KS1irQQiZTIF5HIqeScwFlC2IDXXhNFgiWuFKR iX+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125685; x=1707730485; 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=ANtgl/tgq51oFHA95121eXS9npOxkHEx1ig3f0DQeJE=; b=NznMyAkg9tejgliu8BIxC3+G8Tq/NlEIhRTVZTOOs1TBy9VG8cws/HJOG0JBVlXNjI QHmrNcUzKBeFJH74OiBksJ0MDJUh7W1nRI45SaYhqoNGcUVhJMuVUet+nNH3KF3vhGQv I9Lzz0omPH1NIsOcbEh7k+Y0maHUQAFZWB2yYiuX3I2yF+TpoUyQkQEdgWjvKUUu+BcV SWm/QwLbVuF1R4Wm+dKa2BXv3Kzq+piYsYs2bnIVwQ9+rlId8sN0mhwZHH92KYeHISsB SxHE0X5CRvQDOwyGS0VLdifJjhKGtdX++yxZVsllH4j+dGu4R1eVGFEov1lHppFaRyOv HQzQ== X-Gm-Message-State: AOJu0Yxelbss4dpaB1kvPj7RsVDBllwlAQYG4QgECj07ubeSeoRPh7le NIW9zHUAIcU7PeQcM+HZOJb62dUQAnRm9QTlaHwLJUyuFiPaq2M+fQkK4Rdd7k0= X-Google-Smtp-Source: AGHT+IEiAxBCDuSUfwYhDsmshalgLzlt7c17tLCl/VIHHdboWKtYEayQQdKgXmml+m/08rFU6F7suA== X-Received: by 2002:a05:600c:1d91:b0:40e:d425:85a with SMTP id p17-20020a05600c1d9100b0040ed425085amr4337540wms.17.1707125685449; Mon, 05 Feb 2024 01:34:45 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWCM1Y10GDWu31dId5ePrOKe9J3PDr0CUXAPIdd0EMoxggyYXZqtAeqpe8kczKAT1O7ZLJQ2/+nD5DNdEz0SMPvQaV+kr71Xyz/B5a6jEVPwSSpwN8TL2cBy17ayRp2ZSBsWdZGzgVsB/y9LHPSWgNqJewKOlaF1DIzeGCkTLwp2qQeMMOq8XhRj+XesAHax3BPVnb8KcWriACDGekYKcJWmhTaoIH3RKVIzF9flWTM8LJZMcvedkMlXeP7Q7A6RDKdBHVdeId08IbGNOr7uyJ4IzL46khiZsExPc8WpydB8ouM2rBXBYXoTWlziJhLwnncN1x8fn39kIVrfzD+w0gms7pRw+CLEQ== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 22/23] gpio: remove the RW semaphore from the GPIO device Date: Mon, 5 Feb 2024 10:34:17 +0100 Message-Id: <20240205093418.39755-23-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 89e1c98b0bda..c7b039398e0d 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -958,7 +958,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) @@ -1097,8 +1096,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); @@ -1136,7 +1133,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 Mon Feb 5 09:34: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: 1895203 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=lHv8qbOz; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-gpio+bounces-2963-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TT1Yt4f6Nz23gT for ; Mon, 5 Feb 2024 20:39:46 +1100 (AEDT) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 48AD8289B30 for ; Mon, 5 Feb 2024 09:39:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A60E91B812; Mon, 5 Feb 2024 09:34:50 +0000 (UTC) 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="lHv8qbOz" X-Original-To: linux-gpio@vger.kernel.org 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 2EEB01B944 for ; Mon, 5 Feb 2024 09:34:47 +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=1707125690; cv=none; b=JoUWhdi7qIXKSESM6GcLyFPGhySeyDZY/SzB7nLUz57GAPx9fVXNHagA8k7+MNAG4iY4+FSFxarSfjUHfDCrGS31NqwXDqzxh9facBC5ZDksExG9kIEWhyPu5frIQge4vXNGLVjZWc/6332pEspL+70lYfyj6PG2iW1d7rW1i20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707125690; c=relaxed/simple; bh=CiYSawPebEql0iEz39lQ3pUTtJzpFVAjWsd3kTouEKs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YyB9bDhbQBzHPPJk/OAjHqXM4R/8zQiqrZjn0Xg/d7gdtgBzJR/EExYV7W8CR558TddxsmJGyundhQnAabNSrMpgfRR5Eyks/o6OgItpMyhSg3CgTIkXjeyKZJfDBuBLjj6aPnT8wxlPkA6m6YJ7fe9eqxpIlBpBYQ/PbmxL7Jg= 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=lHv8qbOz; 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 Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-5112d5ab492so5538738e87.0 for ; Mon, 05 Feb 2024 01:34:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1707125686; x=1707730486; 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=IuD4jOVymyzS+IMUJo9eXlxxJz0pfH/DcmsAoXHdB48=; b=lHv8qbOzq2mTAPK7Kz9abzgZmMscc7YNlJf/fLpSSC3dCYHpTBuHtWRdKb3BkB2D7m rScQwt6Q+U1MPsQaLb7/XmBesEJy/c185vg42b9CWPdPys/n+UyRai1pMHAS1lLKvHYx Xoht/0vox05N192vamLBMsNICfFYJT/UpUSZY3aYWmrxpAFu0sg1h1Q0UEiJbhhAesz+ oyqdMQY371RSC2mg5nEaHFncFJ3bmHM6JRcAlGPUxaDpiKIcsHUa4hou/4wPnM/CxlAq yM9sN4dJft4ZUHRtyoRA9fglh7tBTIi7Ywaz6N+l8hQO6ZzRA2HM2qvM1VV43ZCifSIB qBsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707125686; x=1707730486; 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=IuD4jOVymyzS+IMUJo9eXlxxJz0pfH/DcmsAoXHdB48=; b=unAm+zYrRUVprOXXYlpI/aTGVI7//TGSBT9cZ1bCZpsjobx0/Br308k6sE1359bTa5 i4NJAQLseMpwW+P4FHSFoOJLZSB0k3dKDxq6cLvi4p4XL8rJKF2dg2kY8qHswQcf28HT h3gqG23cH97cqBsnHogB+XBB/e5MShYkY3YiP4T8MNKHws+6nTcNehBvLc+ARNo0a9v+ UKo51CgTAzpBCG0v0zf47zmANaIGJABX9nUrLAr1KmC7k1oVnaHn9EBpo/x9TL/mrIHx gvDgJv93f/jVfb+7Ko/HPZtlKwm4AgHjSZWCW9Zt7DJe5DE8aN3g4Dm0Cp6vakXGBLKl 223A== X-Gm-Message-State: AOJu0YzLqkYOWX7X+cEVZOhiuriV5PA9XF7y6IOkBZvoBNC8QrKbjvVO 5FGuuq912PUqB1VOpGykhz2Ioej1AeOdx5YHtRlWUHBD4pfoT8K5QI6n5TWTR1Q= X-Google-Smtp-Source: AGHT+IEPB2Je0SCogU+2gBiEiwjBglfb7KBSZJDiXKT4w6QPedNh07U5ZXtAspIFMe6Orcy3j89JkQ== X-Received: by 2002:a05:6512:220b:b0:511:4df9:1949 with SMTP id h11-20020a056512220b00b005114df91949mr2488688lfu.41.1707125686310; Mon, 05 Feb 2024 01:34:46 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWTnt8FacEOq6jarDsKcu1MJgylOwFHQHyWfITCiQqSjZiNyiEEjTGyq/NHvbkGJ3OF/qrhyzqtMkON24A487SywNaoeMRpos6ffiHwIOw1HDtvNu1tmdtUS9SZP4piE/w0mjapU5+tBc84QAizizuLfpQb53U1YBDSpAMVbRWEbZYr99rZSCsyrh0LGbWsKTN1oEOTuEe4fYLSuz29qg1BwcmZ6zv4xAXNq3ozMHNuj7Tw+bgNrvAa6At858h1RHQC9d1ymtd2hS0xMWg943JNQSS6jdHVJMB8vQm/xfRvMmZMyb3Lejqwb373HK7T99A3s3nlDyg7gNIxr5oVJcchmSTrWDWAiw== Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:d929:10db:5b5c:b49d]) by smtp.gmail.com with ESMTPSA id f15-20020a05600c154f00b0040fc771c864sm7980397wmg.14.2024.02.05.01.34.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:34: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 v2 23/23] gpio: mark unsafe gpio_chip manipulators as deprecated Date: Mon, 5 Feb 2024 10:34:18 +0100 Message-Id: <20240205093418.39755-24-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205093418.39755-1-brgl@bgdev.pl> References: <20240205093418.39755-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 c7b039398e0d..dc00eeed6253 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