From patchwork Tue Sep 5 18:52:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13374930 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 93F90CA0FFB for ; Tue, 5 Sep 2023 18:54:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=aUxh9zhvJBRTIhR0a97F3Fne8+7b188l8SN6UzzhJhw=; b=jenV71Uz+eAnFr /RHOCoLq9HXkEldk4TCKHxdSbGWVvFalcHRe6mG6UnO0oLMTp0CqrruYBSan0JDIME8RWE3ukZI5c b0uJBnvUoWIHeETPtlA3599milgQsyhxd86MkcK5cQlD8aCVdw7QceJusesA2LfwzcaSrCyj+QLhf 2WjSixtoMG6uS22hX/dKr67b7hSA2eAYSA39Sq9jTbfkca8sPY/rx6uK7pMpiO3ub8KQy4+XGKKoW qUbGBJlpP+rHC119kEe8clM9dOAyv1uTO0TndtTlyzfw+ZyKT1dMN7tsKIjim4/eoPH5xN1rhJr6C bBf+2AZ6CMfVD9k0bbNw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qdbBC-006YRS-1b; Tue, 05 Sep 2023 18:53:38 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qdbB8-006YOL-2A for linux-arm-kernel@lists.infradead.org; Tue, 05 Sep 2023 18:53:36 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-402c46c49f4so28185165e9.1 for ; Tue, 05 Sep 2023 11:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1693940006; x=1694544806; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Zm8uw+f5LvxPs16TrhkfvENCN1+QZz7s8rxXSoottC8=; b=WW1+ub5jmCds2UZilowIhVKQxuRcFfeqUSTyVs7XO1cl60hAORviHDPin0/diFwq3O sKExd/nLOPNB4Z4bvtwjWTJrcsYhb1vrKxB/hL7kw5Hlw6aKntGgfPZGZElAib1//KLb 6OnLr1TUQmqPRchnYlHE0bsV2RuGDgHIYqzflJn+KCDU/mT5gqhMzFJAEQCgPQavx/cY SIOwUt4mYzB2HB8TRMKmkii9dLoJQ8X3TK5LJNpvXtZc30odXS+wlRyUfHuYyLPzM1ij 3QOJSeRkC+rwMzJW5LdsZIZPeNtdAX5XUR0HLwJuuKivZ8dPikaceA0qwNYrH4Td/OSn qZ/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693940006; x=1694544806; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zm8uw+f5LvxPs16TrhkfvENCN1+QZz7s8rxXSoottC8=; b=Oq/ftfRSJYlct1/BHWtXXXKG7xlw5vMiPBSmh8sgemC06J+oG9xd813tfgJGEh6svK Mk/+pZrdb9QSYjWlOIM6rUr8M/K3JxK8aM/lfo/T8wzbKZoBh21wTr3D+qL7kZys7tbn ldPM1/2MVO+PilMfLdEjFtf5lo8/aWWW2M43QBXG5eWGC4uupR/pw6E+cekk1TVKHJn7 NRBnqdPOUve+reqFyuWvcgwJ7MLJCRK38fSjVSaJZmBErXwVM1S7xzJoZpFEp+kTfqIJ Xmp7oTdncqoOoHTDD3NGR5nOoPpWOjfmlTpkNZ7E8qXN2/K/q6fGdh0wV+cg3L+4BIS1 7BkQ== X-Gm-Message-State: AOJu0YxmvJ8RtXjUSqj4CcIU79hgj6xsZeGPwO4+FUpesJLGHd716fIE FeczDlL8dPOCMUU9fTLe4+3wCw== X-Google-Smtp-Source: AGHT+IFg7Vm2Cmqv15Sqv5calroUVFxizFJ5aDOZ0cNR6d+rt5FAt8Ucbl9e61HcahWOBMsw3jlYSQ== X-Received: by 2002:a7b:cb8a:0:b0:401:bf62:9456 with SMTP id m10-20020a7bcb8a000000b00401bf629456mr490236wmi.8.1693940006065; Tue, 05 Sep 2023 11:53:26 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:7a54:5dbc:6d09:48b7]) by smtp.gmail.com with ESMTPSA id 17-20020a05600c249100b003fbc30825fbsm17550010wms.39.2023.09.05.11.53.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 11:53:25 -0700 (PDT) From: Bartosz Golaszewski To: Aaro Koskinen , Janusz Krzysztofik , Tony Lindgren , Russell King , Mika Westerberg , Andy Shevchenko , Linus Walleij , Bartosz Golaszewski , Dipen Patel , Thierry Reding , Jonathan Hunter , Hans de Goede , Mark Gross Cc: linux-arm-kernel@lists.infradead.org, linux-omap@vger.kernel.org, linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-acpi@vger.kernel.org, timestamp@lists.linux.dev, linux-tegra@vger.kernel.org, platform-driver-x86@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 00/21] gpio: convert users to gpio_device_find() and remove gpiochip_find() Date: Tue, 5 Sep 2023 20:52:48 +0200 Message-Id: <20230905185309.131295-1-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230905_115334_712977_5B91CFC9 X-CRM114-Status: GOOD ( 18.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Bartosz Golaszewski The GPIO subsystem does not handle hot-unplug events very well. We have recently patched the user-space part of it so that at least a rouge user cannot crash the kernel but in-kernel users are still affected by a lot of issues: from incorrect locking or lack thereof to using structures that are private to GPIO drivers. Since almost all GPIO controllers can be unbound, not to mention that we have USB devices registering GPIO expanders as well as I2C-on-USB HID devices on which I2C GPIO expanders can live, various media gadgets etc., we really need to make GPIO hotplug/unplug friendly. Before we can even get to fixing the locking, we need to address a serious abuse of the GPIO driver API - accessing struct gpio_chip by anyone who isn't the driver owning this object. This structure is owned by the GPIO provider and its lifetime is tied to that of that provider. It is destroyed when the device is unregistered and this may happen at any moment. struct gpio_device is the opaque, reference counted interface to struct gpio_chip (which is the low-level implementation) and all access should pass through it. The end-goal is to make all gpio_device manipulators check the existence of gdev->chip and then lock it for the duration of any of the calls using SRCU. Before we can get there, we need to first provide a set of functions that will replace any gpio_chip functions and convert all in-kernel users. This series starts the process by replacing gpiochip_find() with gpio_device_find(). This is in line with other device_find type interfaces and returns a reference to the GPIO device that is guaranteed to remain valid until it is put. Note that this does not make everything correct just yet. Especially the GPIOLIB internal users release the reference returned by the lookup function after getting the descriptor of interest but before requesting it. This will eventually be addressed. This is not a regression either. First we add a bunch of new APIs that are needed to start replacing calls to gpiochip_find. We then use them first in external users and then locally in GPIOLIB core. Finally we remove gpiochip_find(). Bartosz Golaszewski (21): gpiolib: make gpio_device_get() and gpio_device_put() public gpiolib: provide gpio_device_find() gpiolib: provide gpio_device_find_by_label() gpiolib: provide gpio_device_get_desc() gpiolib: add support for scope-based management to gpio_device gpiolib: provide gpiod_to_device() gpiolib: provide gpio_device_get_base() gpio: acpi: provide acpi_gpio_device_free_interrupts() gpiolib: reluctantly provide gpio_device_get_chip() gpiolib: replace find_chip_by_name() with gpio_device_find_by_label() platform: x86: android-tablets: don't access GPIOLIB private members hte: allow building modules with COMPILE_TEST enabled hte: tegra194: improve the GPIO-related comment hte: tegra194: don't access struct gpio_chip arm: omap1: ams-delta: stop using gpiochip_find() gpio: of: correct notifier return codes gpio: of: replace gpiochip_find_* with gpio_device_find_* gpio: acpi: replace gpiochip_find() with gpio_device_find() gpio: swnode: replace gpiochip_find() with gpio_device_find_by_label() gpio: sysfs: drop the mention of gpiochip_find() from sysfs code gpiolib: remove gpiochip_find() arch/arm/mach-omap1/board-ams-delta.c | 36 ++-- drivers/gpio/gpiolib-acpi.c | 37 +++- drivers/gpio/gpiolib-of.c | 48 ++--- drivers/gpio/gpiolib-swnode.c | 29 ++- drivers/gpio/gpiolib-sysfs.c | 2 +- drivers/gpio/gpiolib.c | 203 +++++++++++++----- drivers/gpio/gpiolib.h | 10 - drivers/hte/Kconfig | 4 +- drivers/hte/hte-tegra194.c | 49 +++-- .../platform/x86/x86-android-tablets/core.c | 38 ++-- include/linux/gpio/driver.h | 30 ++- 11 files changed, 316 insertions(+), 170 deletions(-) Acked-by: Linus Walleij