Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2039855ybv; Fri, 21 Feb 2020 07:49:20 -0800 (PST) X-Google-Smtp-Source: APXvYqzmZdJLN3COgEyPS2AJECrmvD8qssfK/kJReCL3xrvWh+BnNlNsuECXhn5wFsdT56Yw//gd X-Received: by 2002:a05:6830:95:: with SMTP id a21mr28876626oto.171.1582300160390; Fri, 21 Feb 2020 07:49:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582300160; cv=none; d=google.com; s=arc-20160816; b=S8Y+FGOOVJnS8aRLWhHgsGvQW3JAikUb5ZU6GSvcipgcyE99iE+5gti+iYM2Mivbyh WB7PELVsjZbgblJUmHxgQQSZQLq0rg8qi3w02YgzB82evNJ8s1Z90KFRas4440R64tWV MbA10Ft1Ra+gPLVawC/6F/ZoVVssGTRGdImCPrHAuWrQ7GwxErcK1YzGvmoEihAo75qW E+WjagH/gTq3sFISn5w8bFWq1ouonaJ7WWPuyCiLzzbjAoRwGOhk/MmFTIGlVRT6Fr46 wXiU8ZbK6dJqdF3idHazBFagOCKbHu108A5KBiUX3TS0r3FCvJYzBUOcSd2rYAjjr3AR K2mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zNOyXL2FHV1G3B3dMKi0NeHmPme3SyjuhzWOnvysfO4=; b=sVCdW4CmJqUD7xivFzxyylD3ChpUW5gsRjEer5JrmJLj4p3ToKAaBHLTz5nNYGm+fE kkwB5hrPETXopIRmGlHYU3hgzB7CMbX/WnN8+QaeiGks8b6GNTY0zBiwwFuJbektcC8h 3ws15lDDNKkTgiJKM7eSzuQ8L4OVXh6xOiFBjJfkcqPQtmX47zAISNwcZFziPjaDQd/q 6ullf0wuBHHHF5WB4P/VTS9flV/PiOa2pHerxchCNRv0QRl1PzG2HKsIMSFpN1q2FRRQ H136RDTyxXItWoiydi44Nl13Ne4d5FsocJgqPKGq6crnTIJYyEiRRgiB9IEcN7Rv0h9H kyDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b="e06/jlR3"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t11si860467oih.187.2020.02.21.07.49.08; Fri, 21 Feb 2020 07:49:20 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b="e06/jlR3"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729264AbgBUPsz (ORCPT + 99 others); Fri, 21 Feb 2020 10:48:55 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:37653 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729221AbgBUPsy (ORCPT ); Fri, 21 Feb 2020 10:48:54 -0500 Received: by mail-wm1-f67.google.com with SMTP id a6so2416923wme.2 for ; Fri, 21 Feb 2020 07:48:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zNOyXL2FHV1G3B3dMKi0NeHmPme3SyjuhzWOnvysfO4=; b=e06/jlR3Xwj0soL+EDf9/Gy1sjopUiSvXC+XtqUVPQxBvCHkODs3Zx4PnV9t3MH4JU l+OBb2kcjOFLo39kJ0XPZcUROuyKYAURuoBljL4yoq4DTSfGwrhZOry2Fm9KyKeZk1jE dCsO+2Qio9iv7eEJbwlEmwbFCY5jZyVL+oM10PXEtwFahGCIz1EH5sMbDhEgwpkozxaS AHH5p3ZWjEkqq21eO/yRjwBd5BUET6p0K3MwqtFRbrZN3TClWi9Fqj++Qscy0ZmaH8Z2 3jn5PC5zIBgfDsGIvmS87aC2FoBXXEGQGNKfNm+lJS7iwvWnC4yIuFcviyuBHhhdjevZ 6M4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zNOyXL2FHV1G3B3dMKi0NeHmPme3SyjuhzWOnvysfO4=; b=CRgHCJZXZwu2TzS3iAqqBQbWbj4TGFvbsrVDOGCUkRV1tER2NgecDlD65X91/1SDA5 qhacrH8lrQEU5/fYn6gmlkZqEh/dLROeLULgCo0LCUuOl16/ZATpBhgU/jOE2tjh0XDH 8VJUbU2y6SGYtaN5XX7bWf/a+Md1sv0jSRHsDrvwJFKT+mXaW/4AAo5RHrla5devjmGN w7NXkgXWlOvX6iFz457s0SIC9c61KfbMNit9pb0CtiTTCCxbMQNosxpr8t57AP0R8Nv8 Qt+WHzDqpjhF4AHkt3nprQ8RgpZAyFTpUKDo08tJd6GXLxFFZ24BhQ8q1tNEHXGZAImb 37Ow== X-Gm-Message-State: APjAAAWMuLWFZ+HlbcWZPvSxyNU7+rOpMk9/uoZE0veLBhamizKZ6Uc9 kQ0JKhoOkULqkpBP3Z7yxHDJ/w== X-Received: by 2002:a1c:dfd6:: with SMTP id w205mr4641601wmg.151.1582300131453; Fri, 21 Feb 2020 07:48:51 -0800 (PST) Received: from localhost.localdomain (lfbn-nic-1-65-232.w2-15.abo.wanadoo.fr. [2.15.156.232]) by smtp.gmail.com with ESMTPSA id h10sm4267947wml.18.2020.02.21.07.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Feb 2020 07:48:50 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Srinivas Kandagatla , Khouloud Touil , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v5 3/5] gpiolib: use kref in gpio_desc Date: Fri, 21 Feb 2020 16:48:35 +0100 Message-Id: <20200221154837.18845-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200221154837.18845-1-brgl@bgdev.pl> References: <20200221154837.18845-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski GPIO descriptors are freed by consumers using gpiod_put(). The name of this function suggests some reference counting is going on but it's not true. Use kref to actually introduce reference counting for gpio_desc objects. Add a corresponding gpiod_get() helper for increasing the reference count. This doesn't change anything for already existing (correct) drivers but allows us to keep track of GPIO descs used by multiple users. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 38 +++++++++++++++++++++++++++++++---- drivers/gpio/gpiolib.h | 1 + include/linux/gpio/consumer.h | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index da8ffd40aa97..a00e28ca8a49 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2798,6 +2798,8 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) goto done; } + kref_init(&desc->ref); + if (chip->request) { /* chip->request may sleep */ spin_unlock_irqrestore(&gpio_lock, flags); @@ -2941,6 +2943,13 @@ void gpiod_free(struct gpio_desc *desc) } } +static void gpiod_free_kref(struct kref *ref) +{ + struct gpio_desc *desc = container_of(ref, struct gpio_desc, ref); + + gpiod_free(desc); +} + /** * gpiochip_is_requested - return string iff signal was requested * @chip: controller managing the signal @@ -5075,18 +5084,39 @@ struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, EXPORT_SYMBOL_GPL(gpiod_get_array_optional); /** - * gpiod_put - dispose of a GPIO descriptor - * @desc: GPIO descriptor to dispose of + * gpiod_put - decrease the reference count of a GPIO descriptor + * @desc: GPIO descriptor to unref * * No descriptor can be used after gpiod_put() has been called on it. */ void gpiod_put(struct gpio_desc *desc) { - if (desc) - gpiod_free(desc); + VALIDATE_DESC_VOID(desc); + + kref_put(&desc->ref, gpiod_free_kref); } EXPORT_SYMBOL_GPL(gpiod_put); +/** + * gpiod_ref - increase the reference count of a GPIO descriptor + * @desc: GPIO descriptor to reference + * + * Returns the same gpio_desc after increasing the reference count. + */ +struct gpio_desc *gpiod_ref(struct gpio_desc *desc) +{ + VALIDATE_DESC_PTR(desc); + + if (!test_bit(FLAG_REQUESTED, &desc->flags)) { + pr_warn("gpiolib: unable to increase the reference count of unrequested GPIO descriptor\n"); + return desc; + } + + kref_get(&desc->ref); + return desc; +} +EXPORT_SYMBOL_GPL(gpiod_ref); + /** * gpiod_put_array - dispose of multiple GPIO descriptors * @descs: struct gpio_descs containing an array of descriptors diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 3e0aab2945d8..51a92c43dd55 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -119,6 +119,7 @@ struct gpio_desc { const char *label; /* Name of the GPIO */ const char *name; + struct kref ref; }; int gpiod_request(struct gpio_desc *desc, const char *label); diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h index bf2d017dd7b7..c7b5fb3d9d64 100644 --- a/include/linux/gpio/consumer.h +++ b/include/linux/gpio/consumer.h @@ -81,6 +81,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev, struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, const char *con_id, enum gpiod_flags flags); +struct gpio_desc *gpiod_ref(struct gpio_desc *desc); void gpiod_put(struct gpio_desc *desc); void gpiod_put_array(struct gpio_descs *descs); -- 2.25.0