From patchwork Fri May 29 22:03:55 2020
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Pratyush Yadav
X-Patchwork-Id: 1301048
X-Patchwork-Delegate: sjg@chromium.org
Return-Path:
X-Original-To: incoming@patchwork.ozlabs.org
Delivered-To: patchwork-incoming@bilbo.ozlabs.org
Authentication-Results: ozlabs.org;
spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de
(client-ip=85.214.62.61; helo=phobos.denx.de;
envelope-from=u-boot-bounces@lists.denx.de; receiver=)
Authentication-Results: ozlabs.org;
dmarc=pass (p=quarantine dis=none) header.from=ti.com
Authentication-Results: ozlabs.org;
dkim=pass (1024-bit key;
unprotected) header.d=ti.com header.i=@ti.com header.a=rsa-sha256
header.s=ti-com-17Q1 header.b=oNcnikqF;
dkim-atps=neutral
Received: from phobos.denx.de (phobos.denx.de [85.214.62.61])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (4096 bits))
(No client certificate requested)
by ozlabs.org (Postfix) with ESMTPS id 49Ydrn5KL1z9sRN
for ; Sat, 30 May 2020 08:04:25 +1000 (AEST)
Received: from h2850616.stratoserver.net (localhost [IPv6:::1])
by phobos.denx.de (Postfix) with ESMTP id D68B981015;
Sat, 30 May 2020 00:04:16 +0200 (CEST)
Authentication-Results: phobos.denx.de;
dmarc=pass (p=quarantine dis=none) header.from=ti.com
Authentication-Results: phobos.denx.de;
spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de
Authentication-Results: phobos.denx.de;
dkim=pass (1024-bit key;
unprotected) header.d=ti.com header.i=@ti.com header.b="oNcnikqF";
dkim-atps=neutral
Received: by phobos.denx.de (Postfix, from userid 109)
id 2532980680; Sat, 30 May 2020 00:04:07 +0200 (CEST)
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de
X-Spam-Level:
X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,
DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED
autolearn=ham autolearn_force=no version=3.4.2
Received: from lelv0143.ext.ti.com (lelv0143.ext.ti.com [198.47.23.248])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by phobos.denx.de (Postfix) with ESMTPS id 547668067C
for ; Sat, 30 May 2020 00:04:02 +0200 (CEST)
Authentication-Results: phobos.denx.de;
dmarc=pass (p=quarantine dis=none) header.from=ti.com
Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=p.yadav@ti.com
Received: from fllv0035.itg.ti.com ([10.64.41.0])
by lelv0143.ext.ti.com (8.15.2/8.15.2) with ESMTP id 04TM41rU097641;
Fri, 29 May 2020 17:04:01 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com;
s=ti-com-17Q1; t=1590789841;
bh=XcmzSTkgjVsc+JZQh21NY+0hw3gdoAl7OJgLniIrKiI=;
h=From:To:CC:Subject:Date:In-Reply-To:References;
b=oNcnikqFLV9hzR2vUBAGDdUKU3OyBqJI59T3O5lt+f40tJz/N7tmBMiOuR0nuK8ek
eUgVffW5uOZaGJ09wP/mDWBynEkNdzh914MtovgkVGV57z9GE2P+JaH2qiTyajXPh6
uSFATEJHwnqVG710kVqIYRLuMPyueg49zkEiGMBs=
Received: from DLEE100.ent.ti.com (dlee100.ent.ti.com [157.170.170.30])
by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTP id 04TM41WT040632;
Fri, 29 May 2020 17:04:01 -0500
Received: from DLEE105.ent.ti.com (157.170.170.35) by DLEE100.ent.ti.com
(157.170.170.30) with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1979.3; Fri, 29
May 2020 17:04:00 -0500
Received: from fllv0040.itg.ti.com (10.64.41.20) by DLEE105.ent.ti.com
(157.170.170.35) with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1979.3 via
Frontend Transport; Fri, 29 May 2020 17:04:00 -0500
Received: from pratyush-OptiPlex-790.dhcp.ti.com (ileax41-snat.itg.ti.com
[10.172.224.153])
by fllv0040.itg.ti.com (8.15.2/8.15.2) with ESMTP id 04TM3uhS042574;
Fri, 29 May 2020 17:03:58 -0500
From: Pratyush Yadav
To: Simon Glass ,
CC: Pratyush Yadav , Vignesh Raghavendra ,
Sekhar Nori
Subject: [RFC PATCH 1/1] gpio: Handle NULL pointers gracefully
Date: Sat, 30 May 2020 03:33:55 +0530
Message-ID: <20200529220355.4396-2-p.yadav@ti.com>
X-Mailer: git-send-email 2.25.0
In-Reply-To: <20200529220355.4396-1-p.yadav@ti.com>
References: <20200529220355.4396-1-p.yadav@ti.com>
MIME-Version: 1.0
X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180
X-BeenThere: u-boot@lists.denx.de
X-Mailman-Version: 2.1.30rc1
Precedence: list
List-Id: U-Boot discussion
List-Unsubscribe: ,
List-Archive:
List-Post:
List-Help:
List-Subscribe: ,
Errors-To: u-boot-bounces@lists.denx.de
Sender: "U-Boot"
X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de
X-Virus-Status: Clean
Prepare the way for a managed GPIO API by handling NULL pointers without
crashing or failing. validate_desc() comes from Linux with the prints
removed to reduce code size.
Signed-off-by: Jean-Jacques Hiblot
Signed-off-by: Pratyush Yadav
---
drivers/gpio/Kconfig | 9 ++++
drivers/gpio/gpio-uclass.c | 86 ++++++++++++++++++++++++++++++++++----
include/asm-generic/gpio.h | 2 +-
3 files changed, 88 insertions(+), 9 deletions(-)
--
2.26.2
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index d87f6cc105..f8b6bcdf44 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -36,6 +36,15 @@ config TPL_DM_GPIO
particular GPIOs that they provide. The uclass interface
is defined in include/asm-generic/gpio.h.
+config GPIO_VALIDATE_DESC
+ bool "Check if GPIO descriptor is NULL and bail out if it is"
+ depends on DM_GPIO
+ default y
+ help
+ If a GPIO is optional, the GPIO descriptor is NULL. In that
+ case, calls should bail out instead of causing NULL pointer
+ access.
+
config GPIO_HOG
bool "Enable GPIO hog support"
depends on DM_GPIO
diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c
index 9eeab22eef..6b97d3aaff 100644
--- a/drivers/gpio/gpio-uclass.c
+++ b/drivers/gpio/gpio-uclass.c
@@ -20,6 +20,25 @@
DECLARE_GLOBAL_DATA_PTR;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+/*
+ * This descriptor validation needs to be inserted verbatim into each
+ * function taking a descriptor, so we need to use a preprocessor
+ * macro to avoid endless duplication. If the desc is NULL it is an
+ * optional GPIO and calls should just bail out.
+ */
+static inline int validate_desc(const struct gpio_desc *desc)
+{
+ if (!desc)
+ return 0;
+ if (IS_ERR(desc))
+ return PTR_ERR(desc);
+ if (!desc->dev)
+ return -EINVAL;
+ return 1;
+}
+#endif
+
/**
* gpio_desc_init() - Initialize the GPIO descriptor
*
@@ -303,11 +322,19 @@ int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
int dm_gpio_request(struct gpio_desc *desc, const char *label)
{
- struct udevice *dev = desc->dev;
+ struct udevice *dev;
struct gpio_dev_priv *uc_priv;
char *str;
int ret;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
+ dev = desc->dev;
+
uc_priv = dev_get_uclass_priv(dev);
if (uc_priv->name[desc->offset])
return -EBUSY;
@@ -434,6 +461,14 @@ static int check_reserved(const struct gpio_desc *desc, const char *func)
{
struct gpio_dev_priv *uc_priv;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ int ret;
+
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
if (!dm_gpio_is_valid(desc))
return -ENOENT;
@@ -510,6 +545,12 @@ int dm_gpio_get_value(const struct gpio_desc *desc)
{
int ret;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
ret = check_reserved(desc, "get_value");
if (ret)
return ret;
@@ -521,6 +562,12 @@ int dm_gpio_set_value(const struct gpio_desc *desc, int value)
{
int ret;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
ret = check_reserved(desc, "set_value");
if (ret)
return ret;
@@ -572,11 +619,21 @@ static int check_dir_flags(ulong flags)
static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
{
- struct udevice *dev = desc->dev;
- struct dm_gpio_ops *ops = gpio_get_ops(dev);
- struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+ struct udevice *dev;
+ struct dm_gpio_ops *ops;
+ struct gpio_dev_priv *uc_priv;
int ret = 0;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
+ dev = desc->dev;
+ ops = gpio_get_ops(dev);
+ uc_priv = dev_get_uclass_priv(dev);
+
ret = check_dir_flags(flags);
if (ret) {
dev_dbg(dev,
@@ -1043,6 +1100,14 @@ int gpio_get_list_count(struct udevice *dev, const char *list_name)
int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
{
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ int ret;
+
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
/* For now, we don't do any checking of dev */
return _dm_gpio_free(desc->dev, desc->offset);
}
@@ -1091,12 +1156,17 @@ static int gpio_renumber(struct udevice *removed_dev)
int gpio_get_number(const struct gpio_desc *desc)
{
- struct udevice *dev = desc->dev;
struct gpio_dev_priv *uc_priv;
- if (!dev)
- return -1;
- uc_priv = dev->uclass_priv;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+ int ret;
+
+ ret = validate_desc(desc);
+ if (ret <= 0)
+ return ret;
+#endif
+
+ uc_priv = dev_get_uclass_priv(desc->dev);
return uc_priv->gpio_base + desc->offset;
}
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
index e16c2f31d9..46007b1283 100644
--- a/include/asm-generic/gpio.h
+++ b/include/asm-generic/gpio.h
@@ -149,7 +149,7 @@ struct gpio_desc {
*/
static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
{
- return desc->dev != NULL;
+ return desc && desc->dev;
}
/**