From patchwork Fri Dec 8 10:20:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 175725 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5360394vqy; Fri, 8 Dec 2023 02:20:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IE5BQIstZ0NcNbHMcTUYB4hHsvC0nvbXDh/H6jXQ55X8FMW+qThnb8kDSwHqsLbaGejND4O X-Received: by 2002:a17:903:1ca:b0:1d0:6ffd:6e5e with SMTP id e10-20020a17090301ca00b001d06ffd6e5emr3447393plh.86.1702030836791; Fri, 08 Dec 2023 02:20:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702030836; cv=none; d=google.com; s=arc-20160816; b=KC/gKjHP4MSByAivbb9+VEhDMhT8/wKmUIb2zBaNRnOfzGsnUG5+5m0LyOybz5WV+G W6iRy7x8DV6vOQLma3YJsvxr6sYxC0r2xdR8lAakMmQ3j5RoN8G/jn587L5biSpCxVnw gRPtEXqtmz2KVNymnat7uRPUOjSAyWiqa7nEYdF2Vv4c2x1q3mwS09lqid80vu7+O17J KbttNXvKh7jyMEkyu6NBeUCBAuqI28i645j264VLu18boCRj+WQ7biTkEqR3MChPnyrX MOSPQ5Y4j3u3DW+7zgAulDtuaN/oawzRV4VwqIYxEOMP1wixH6/m+70LsKywoVmHG+5O 6RZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xcBt1H06VnpfQ+udlk19+9Kb0QwElKrjTxgmIfyINVE=; fh=u8rLQsUjk/DQ68o09SM+wx8FxqDFcT3zeQdKot9Eyy4=; b=qn3FjX7iXoMg1Y0v9ErQGNXFQwN45Y0XHGwwD+44NUEi+axWkCgjnWTgPEOJ4pUHBS DuFP6aXPVYmFpW9eTY+ErYr5lR9LDYgDpNqp5MLzMlMAPXjl5i/FZ0Q+2JleOvcjwbat oAcugFfLRiPplY0IQDwehpz6A6rb94fCAIHXdG16fKaMGTUJw6q3/JjTmTCy2hjElBHc a5pPRJqby6/eNMv/j8KPEc4q8pZ+SP5gWOuIg2UazZg3AVIyLGnhTZGvJ81Ts/a9fJV8 pPyC2mLpb/A8cJiDAr2Yn2r39AXfRSO9aB24jl+C54QE/k8WSPNGbwKKa0+rSZegZj5H 8qdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b="EP/4/5Pz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id q16-20020a17090311d000b001d0750f2942si1421779plh.253.2023.12.08.02.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 02:20:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b="EP/4/5Pz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 84CC380978A6; Fri, 8 Dec 2023 02:20:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573481AbjLHKUY (ORCPT + 99 others); Fri, 8 Dec 2023 05:20:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573473AbjLHKUW (ORCPT ); Fri, 8 Dec 2023 05:20:22 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47952171F for ; Fri, 8 Dec 2023 02:20:28 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-332c46d5988so2004297f8f.1 for ; Fri, 08 Dec 2023 02:20:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1702030826; x=1702635626; 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=xcBt1H06VnpfQ+udlk19+9Kb0QwElKrjTxgmIfyINVE=; b=EP/4/5PzeliOihBSme+38ywRnGlLD66AtuLseXgTuycU+ECUhwT+OU7b+wfjw6JAEd XJvfgXcFymhoMF377pSpRmjGSZLPDPBwqBZ8K40Hx+EB8vH1ZGN579M03/yLtiXqJ2q7 1vs+86Z0TEDigQOYzWUXxl02ZN7nHxcU1fzzmfVv5+EniOLFMt7t93bdTzDOpHZV7ZK6 HN0y9dNxR2oHi+oDcxShw/k4c4EdDJ2ZxJADcwdHtg/+1z84KbqMsif+MQOlJV4ta4Xe pR1m7HEDJmxu4i3O7h308iqL9Vrp2yrJYj72MoeLZ0AHq0PlugDwrCJlEtJmEh5S7P5t jyTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702030826; x=1702635626; 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=xcBt1H06VnpfQ+udlk19+9Kb0QwElKrjTxgmIfyINVE=; b=K/DpH4K0dJhHTvwVA51glVt3iVj1o3Gd8TGxb/tUpVgw5AIBimmiGxCxgw7afIthuP Jxi4CMNRtxn4YMLps+o+FpRMPDuVLsRwj0PTuouzjqoZlahlu8AeRD62Q6XUcSPSfZ6u y/q/fpxBL3sNOIwZze8fHbuzERqstgWDiPHy8VKvoAIyMPvtv5Yrbwcqa2CvVxwFZOmX n5iVlgkafczywXUBMH6x6Pm167/ZcMCYE4JeN02OsKViPhhvwooJGUVomeuoQw37tSv3 oMupYBgGpGhhFIeye7DMDlwHql+5AYnMI04Swz4Yu2Up4Z8l1Gf/uyOijj5w/1q1ppOK QonQ== X-Gm-Message-State: AOJu0YyO9MBd4pTzz5cztG7dR2Euq4QaYdgazxYjOBEMBjYDMoNwpUZj PxN0SpN7Jb33Tm+BYbixjTVr8A== X-Received: by 2002:adf:ed06:0:b0:333:380f:2d2d with SMTP id a6-20020adfed06000000b00333380f2d2dmr2777351wro.92.1702030826702; Fri, 08 Dec 2023 02:20:26 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:b162:2510:4488:c0c3]) by smtp.gmail.com with ESMTPSA id n16-20020a5d4850000000b003334926fb81sm1678026wrs.90.2023.12.08.02.20.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 02:20:24 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 1/2] gpiolib: rename static functions that are called with the lock taken Date: Fri, 8 Dec 2023 11:20:19 +0100 Message-Id: <20231208102020.36390-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231208102020.36390-1-brgl@bgdev.pl> References: <20231208102020.36390-1-brgl@bgdev.pl> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 08 Dec 2023 02:20:35 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784708687032130558 X-GMAIL-MSGID: 1784708687032130558 From: Bartosz Golaszewski Rename two functions that read or modify the global GPIO device list but don't take the lock themselves (and need to be called with it already acquired). Use the _unlocked() suffix which seems to be used quite consistently across the kernel despite there also existing the _locked() suffix for the same purpose. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 4e190be75dc2..779f8b21bf05 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -290,7 +290,7 @@ struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev) EXPORT_SYMBOL_GPL(gpio_device_get_chip); /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ -static int gpiochip_find_base(int ngpio) +static int gpiochip_find_base_unlocked(int ngpio) { struct gpio_device *gdev; int base = GPIO_DYNAMIC_BASE; @@ -363,7 +363,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_direction); * Return -EBUSY if the new chip overlaps with some other chip's integer * space. */ -static int gpiodev_add_to_list(struct gpio_device *gdev) +static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev) { struct gpio_device *prev, *next; @@ -907,7 +907,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, */ base = gc->base; if (base < 0) { - base = gpiochip_find_base(gc->ngpio); + base = gpiochip_find_base_unlocked(gc->ngpio); if (base < 0) { spin_unlock_irqrestore(&gpio_lock, flags); ret = base; @@ -927,7 +927,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, } gdev->base = base; - ret = gpiodev_add_to_list(gdev); + 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"); From patchwork Fri Dec 8 10:20:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 175726 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5360418vqy; Fri, 8 Dec 2023 02:20:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFhIUeh46uU9DBAN7l5HLGktKkJlT/cbAEKKk3QTTFU3yM5ageQxmiIkXYV8gJU2+Tlxre+ X-Received: by 2002:a05:6a20:3d0b:b0:190:a4d:b5cf with SMTP id y11-20020a056a203d0b00b001900a4db5cfmr2039489pzi.48.1702030840129; Fri, 08 Dec 2023 02:20:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702030840; cv=none; d=google.com; s=arc-20160816; b=0ZZ0mXpHswOw9Csam2iY6PiKI5cV01Dwjz12RR3RMuzbiaxOsTQJ6pNesqmqIoQhfO gzm4QU0fPPAqB56rbGh6Od2t/ToylgFqlVldmzRY0/YD1zgEskI620tRQaS53WFuvjDX 0QSfKAa88pxken6XTtQMSGa39k3BN5dDY60QWFSMDQuDN51obvOrq/7kb6tSmMAFZ5UY YOAA1PNsNNJ3dzybl0oXM9MkYIbHUiV57Nceed8gH7ewWG8AF3pu/FbqfYFhjGT/Wn4t RYO712VHYPjRsk4QTpslbXnWDCLTRvSn8QmXxkuPpC46TFWc+A1FWgEp/BvoIslQjaLR 7PbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=G5z7BIkfWdq3iOxehOcCgzM2TU/UC34N0jjt3CjwoFs=; fh=u8rLQsUjk/DQ68o09SM+wx8FxqDFcT3zeQdKot9Eyy4=; b=eMbl4B8N4wlqkzxnOcB4CWY7dYm5HxtRy5qq2LWaNm1YZfq7e7J7L8kEq2n9lfIMVH BiZYdHbNX+alaZ5l+ROwUOgBWLleegPTJHwxchX1g5NvSyXO3hetJk8EWqh8fuCzq+ZH c2ir+cabS1zp0tZegJ5ZoA+h3gF7hXnnNvXqJnCL6kiCGIcikRpfXKGy1Ku3jJBtvKaQ QofzbC3/3CbPVi7s2K2Lnl2xmcV5RcgrYdNTnGa9R8osFKRl8osEP0mkEw614+oV0A6T Mq5D7bnyfl5DEMSIcK/t5x7J9V6d7oODZLbTcxyWsQ+d+l3a+DIUo82Us7mhw2gfAQEp 3qEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=SIq0iawX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id l3-20020a17090a72c300b0028868ea872esi1462852pjk.43.2023.12.08.02.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 02:20:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=SIq0iawX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 02DBC80DECE6; Fri, 8 Dec 2023 02:20:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573493AbjLHKU1 (ORCPT + 99 others); Fri, 8 Dec 2023 05:20:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573490AbjLHKU0 (ORCPT ); Fri, 8 Dec 2023 05:20:26 -0500 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 661D81706 for ; Fri, 8 Dec 2023 02:20:31 -0800 (PST) Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-50c02628291so1959730e87.0 for ; Fri, 08 Dec 2023 02:20:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1702030829; x=1702635629; 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=G5z7BIkfWdq3iOxehOcCgzM2TU/UC34N0jjt3CjwoFs=; b=SIq0iawXr7p+R+HOY6wJw3DixdQ/+hNqhMeQMc216jiuy/HMPJw2hqaIiTyViSrUg0 6RAWlj9zXiE1VgDHjdq+50xy8HVJpF0sWuycEYjrJA+ZBHwww0KqMvSjIxrKITrjyyb/ qdaa9uju+DjxQltUsbzBnxWJmJCVgELviPo/qr0lblq2xXkeTrIdi3x+jG+Cq2MFMcon piUB0L0Z/4X/Zpw1S4dpzh68tcC9oYDJErhtAT6539QVTJkGw/OfdJWnwLCMbPg8O6We HNm1PotKyG2QvKi9fToq8yO0UZG/y3e/PxnGnP2MlxGHckk0u320Tssz8xvtj1Z9VO+D EfZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702030829; x=1702635629; 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=G5z7BIkfWdq3iOxehOcCgzM2TU/UC34N0jjt3CjwoFs=; b=raDZgmoyKJb1CPnFqOkdBPV1OlmSJa8N4hAkytvzkH1wOjVw7hjqavaCsq1T/G2IIE vGbgWFKHKACRN1u3IN1CwstPBwqq+E7lsAonbux0T1NSm6oVe0C2RG/pggbNZCgTmPZh Jn0JqR4FJSU8K9sS7RzA5RZnx+Zc+3CAGbLTKR9is5DD5iWVL3xJqhdfymYywIxabzsm 63Lv+hQ8tgetoIK3XULWBgmC6+IA1lWIWaFm+UV+52bLW+E8bjpRE1ag95pN8W2A0USo jnonZe0gNhbeY1RStVgo9MCj78cRZD9zulnIM4VO7Bixz8ZJe3b1CJTzy3AFeQ7SdwnL H+kA== X-Gm-Message-State: AOJu0YwOdJoz/Ln0P8R/kX2L3O2pccxiZQ2mylfSxbCRxqZ2cJYqBNHm Xt1vTbvHnGfN4+pllSOV9OmCmQ== X-Received: by 2002:a05:6512:3e01:b0:50b:e77d:32e4 with SMTP id i1-20020a0565123e0100b0050be77d32e4mr2694162lfv.60.1702030829480; Fri, 08 Dec 2023 02:20:29 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:b162:2510:4488:c0c3]) by smtp.gmail.com with ESMTPSA id n16-20020a5d4850000000b003334926fb81sm1678026wrs.90.2023.12.08.02.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 02:20:27 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 2/2] gpiolib: use a mutex to protect the list of GPIO devices Date: Fri, 8 Dec 2023 11:20:20 +0100 Message-Id: <20231208102020.36390-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231208102020.36390-1-brgl@bgdev.pl> References: <20231208102020.36390-1-brgl@bgdev.pl> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 08 Dec 2023 02:20:39 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784708689937220874 X-GMAIL-MSGID: 1784708689937220874 From: Bartosz Golaszewski The global list of GPIO devices is never modified or accessed from atomic context so it's fine to protect it using a mutex. Add a new global lock dedicated to the gpio_devices list and use it whenever accessing or modifying it. While at it: fold the sysfs registering of existing devices into gpiolib.c and make gpio_devices static within its compilation unit. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-sysfs.c | 26 +----- drivers/gpio/gpiolib-sysfs.h | 6 ++ drivers/gpio/gpiolib.c | 162 ++++++++++++++++++----------------- drivers/gpio/gpiolib.h | 1 - 4 files changed, 92 insertions(+), 103 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 6f309a3b2d9a..c538568604e8 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -790,9 +790,7 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev) 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) @@ -804,26 +802,6 @@ 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; - - /* - * 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 gpiochip_sysfs_register_all(); } postcore_initcall(gpiolib_sysfs_init); diff --git a/drivers/gpio/gpiolib-sysfs.h b/drivers/gpio/gpiolib-sysfs.h index b794b396d6a5..ab157cec0b4b 100644 --- a/drivers/gpio/gpiolib-sysfs.h +++ b/drivers/gpio/gpiolib-sysfs.h @@ -8,6 +8,7 @@ struct gpio_device; #ifdef CONFIG_GPIO_SYSFS int gpiochip_sysfs_register(struct gpio_device *gdev); +int gpiochip_sysfs_register_all(void); void gpiochip_sysfs_unregister(struct gpio_device *gdev); #else @@ -17,6 +18,11 @@ static inline int gpiochip_sysfs_register(struct gpio_device *gdev) return 0; } +static inline int gpiochip_sysfs_register_all(void) +{ + return 0; +} + static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) { } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 779f8b21bf05..9aaf1d11fdd9 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -94,7 +96,9 @@ 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); +static DEFINE_MUTEX(gpio_devices_lock); static DEFINE_MUTEX(gpio_machine_hogs_mutex); static LIST_HEAD(gpio_machine_hogs); @@ -126,20 +130,15 @@ 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(mutex, &gpio_devices_lock) { + list_for_each_entry(gdev, &gpio_devices, list) { + 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); @@ -412,26 +411,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; if (!name) return NULL; - spin_lock_irqsave(&gpio_lock, flags); + guard(mutex)(&gpio_devices_lock); list_for_each_entry(gdev, &gpio_devices, list) { struct gpio_desc *desc; 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; } @@ -669,11 +663,9 @@ EXPORT_SYMBOL_GPL(gpiochip_line_is_valid); static void gpiodev_release(struct device *dev) { struct gpio_device *gdev = to_gpio_device(dev); - unsigned long flags; - spin_lock_irqsave(&gpio_lock, flags); - list_del(&gdev->list); - spin_unlock_irqrestore(&gpio_lock, flags); + scoped_guard(mutex, &gpio_devices_lock) + list_del(&gdev->list); ida_free(&gpio_ida, gdev->id); kfree_const(gdev->label); @@ -726,6 +718,27 @@ static int gpiochip_setup_dev(struct gpio_device *gdev) return ret; } +#if IS_ENABLED(CONFIG_GPIO_SYSFS) +int gpiochip_sysfs_register_all(void) +{ + struct gpio_device *gdev; + int ret; + + guard(mutex)(&gpio_devices_lock); + + list_for_each_entry(gdev, &gpio_devices, list) { + if (gdev->mockdev) + continue; + + ret = gpiochip_sysfs_register(gdev); + if (ret) + return ret; + } + + return 0; +} +#endif /* CONFIG_GPIO_SYSFS */ + static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog) { struct gpio_desc *desc; @@ -831,7 +844,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; @@ -896,48 +908,45 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gdev->ngpio = gc->ngpio; - spin_lock_irqsave(&gpio_lock, flags); + 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; - /* - * 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); 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"); + + for (i = 0; i < gc->ngpio; i++) + gdev->descs[i].gdev = gdev; } - 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); @@ -1029,9 +1038,8 @@ 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(&gdev->list); err_free_label: kfree_const(gdev->label); err_free_descs: @@ -1140,7 +1148,7 @@ struct gpio_device *gpio_device_find(void *data, */ might_sleep(); - guard(spinlock_irqsave)(&gpio_lock); + guard(mutex)(&gpio_devices_lock); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->chip && match(gdev->chip, data)) @@ -4748,35 +4756,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(mutex)(&gpio_devices_lock); + + 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(mutex, &gpio_devices_lock) { + 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; diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 3ccacf3c1288..9278796db079 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -135,7 +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; -extern struct list_head gpio_devices; void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);