From patchwork Fri Dec 15 15:52:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 179391 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9375259dys; Fri, 15 Dec 2023 07:53:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjIf8GwYvaBTzHJt+FRcmS2t3uatbPJN3kHpq/SsGh+m8KrXDeulT3T2g/uLkEAyqkuiRp X-Received: by 2002:a17:906:30d5:b0:a1c:d8c6:c21 with SMTP id b21-20020a17090630d500b00a1cd8c60c21mr12018228ejb.67.1702655629193; Fri, 15 Dec 2023 07:53:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702655629; cv=none; d=google.com; s=arc-20160816; b=p/8iqXDlYIGc2bkdcM/Xwh8IJ7ldhHIING7XjxAqhx7iBSS/+i13hg3EI6uUM+FNw5 6eUwx2smLW5LC0cmAhz+3S30NsNL1yzxCU0n9cjVbqX4jkPmwGRj2PGnAakzHK8ZdPDB Ztg5dM1WI4w4fxPmxn5jsqdyQg97ghJK5uUd8Vp4I/yBcw4YaOq3FpIDbQRUM2O0mMj0 hDwGvIwP1/szmFMsv9oxbfKd3O2tZKUm9NuiZdCGhMkEp7IviGI8ooOK8BtEDboAQb5a bIR78YcxOXXk99bMiWGYaVi/7wH75TTlYVTCWVNJFRIZY06COzFP3e1SJJeAyy/y5Pgz XiiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ap5YKACnlUbXS6eetDez0VUaGPWZc+QwWEfEjI6SZuQ=; fh=u8rLQsUjk/DQ68o09SM+wx8FxqDFcT3zeQdKot9Eyy4=; b=0Z5VpOAQ6a2T9K4IAjh9Y5oFngpyrEBCV6+T5h/ntbgbzPJfo5ZEdSPEBlyMfRYBI4 G/PHAskLxr/igEfUnqc1TzUh6K4+KClncY2NwWw7vmOp9gOR/g+RYYdC3BroeGZaOFTb Q5wpg1fTqGZYTDwnXqxft27pdXvLbMU4FtIYOm5Y1RDmuq5VOhJ1reSYOqmytiTeXpCf RDoU3FXnJyxu/t64+ICrwifIEToJd8tQzjhzIpl5h8ys1QKUAeBysjuiIS18Jl5d2v38 1O081m724OCvCaypoX+uckqw7v1z/qoGx4C3+6iV1Px7pSqfcvCHkBXNwmCV+7ZxEGGk v3lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=3B3hjoiy; spf=pass (google.com: domain of linux-kernel+bounces-1235-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1235-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id m10-20020a1709066d0a00b00a1a64319aa8si7580139ejr.510.2023.12.15.07.53.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 07:53:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1235-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=3B3hjoiy; spf=pass (google.com: domain of linux-kernel+bounces-1235-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1235-ouuuleilei=gmail.com@vger.kernel.org" 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 CAE2D1F23C3C for ; Fri, 15 Dec 2023 15:53:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 06BD83BB4E; Fri, 15 Dec 2023 15:53:12 +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="3B3hjoiy" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 DC4B4381BB for ; Fri, 15 Dec 2023 15:53:07 +0000 (UTC) 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-f51.google.com with SMTP id 5b1f17b1804b1-40c38e292c8so4611005e9.0 for ; Fri, 15 Dec 2023 07:53:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1702655586; x=1703260386; 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=ap5YKACnlUbXS6eetDez0VUaGPWZc+QwWEfEjI6SZuQ=; b=3B3hjoiyG4DVgpHA4+wXSmF6nRAJxitREHTvJCqkLiQTINL8x1wPMpVQq72pFMQsvp VbRU7hpIdOCog6/zXv2kV1fO5eZUZP46D0OonH/mxY9sk6X6QCm4EiaN7DbzQmmn3h1q ZuLuiH29Lc0cfDzGjIHxy1Ce+CKnr7ks8Q4uvAwUORpB4Md2RpEMbGVHrnQvXBRazXNr JsQ39BluYyrr/lC0L0XKCtbhDCN8r3MlOF9UfMGwFbj3mgJrqpEMXFjSImKRhsh7h2lN LR7Mx0cANYBtW35d9hMXqwGR8KDpsltNcxoEtrQDDm0ahPIOW64cPM3xCNwedkjChvd0 tITA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702655586; x=1703260386; 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=ap5YKACnlUbXS6eetDez0VUaGPWZc+QwWEfEjI6SZuQ=; b=fjxfkoNM1Qmar9FoOdjbxkMLhgSDeH/DS1rC434297pgIp+jifh5EP4GIdWpbz9f3w OQibpm57BGoEFz/zEW8T1CePXupZNTZdn8WWumyKyq2YUR5fzC0xMOL2k++CFfTud+7H 6I4vvUZHn7/wItdPpqHz+u0LgblZhfW27Gw+STOmAz2OUbZ9ym82rW+ZPOYqHWBT1B9M nB/t1nG/1tQsX1c+wgGOiKPrRfXaZAmiTMpLnDeVBhrtHMIj8i9gGMf+/E5/7sZHK0TM xseM25/HbiOqaLKNXCyRsSqO/6rfKWHeKqRZjYXv1qTFVJ/+AUSZnYJGQB0N4TM6Iq3y Vodw== X-Gm-Message-State: AOJu0YwSnJXt0FaWGABgMf2RqDRBoRvTSgT+9qQ30xGKfHmORm9Jtpfd G4JCYAPUXlFiiZ5uXm8FFI83Vw== X-Received: by 2002:a05:600c:16c9:b0:40c:3dd7:98d9 with SMTP id l9-20020a05600c16c900b0040c3dd798d9mr5620884wmn.21.1702655585948; Fri, 15 Dec 2023 07:53:05 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:8672:e2c5:37d9:3743]) by smtp.gmail.com with ESMTPSA id bg22-20020a05600c3c9600b0040c6ab53cd2sm3110484wmb.10.2023.12.15.07.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 07:53:05 -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 v3 1/2] gpiolib: rename static functions that are called with the lock taken Date: Fri, 15 Dec 2023 16:52:59 +0100 Message-Id: <20231215155300.21186-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215155300.21186-1-brgl@bgdev.pl> References: <20231215155300.21186-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785363828665453027 X-GMAIL-MSGID: 1785363828665453027 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 Reviewed-by: Linus Walleij --- 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 6efe44570333..5b744d1f31f6 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 15 15:53:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 179392 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9375342dys; Fri, 15 Dec 2023 07:53:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IGl9uT+SeaqvxjUz5jH4jNw6mz7H+JTm+z3Bp+u2WgLlt781g+gxUHCPuq2JqSIljmicEa3 X-Received: by 2002:a17:90a:5586:b0:28b:4322:16e7 with SMTP id c6-20020a17090a558600b0028b432216e7mr52325pji.82.1702655636980; Fri, 15 Dec 2023 07:53:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702655636; cv=none; d=google.com; s=arc-20160816; b=hDNGucYMkm8qzYuwmIFQV8dC37XCJBr1Cq9HgUPHpTsceR42isxVwZ/8rlve0j77zl 1QT0Ue2muFhdDCSNqaXeDbRDuIhxvUSrTgSEE2kXfCm2RLbSO6i6EhN/zdBKvHaDwjaH l5D1a5FY03nd8PZTOeG0hXUy7SiUAd74T7vujj3NKtOH+Ay327UGu76CBpFMRBv02eMs tueeSO9VOwghLQ6pc4k79SzvkukgUilOedP+h6aAdqABe4eR/uKWf9GVbBS+GkVQhuQR a1V8knF/I/pZSWI1ukY97gLYOgK9zD4rLdLtN1peJI9NP4YhIeQ+n/sbMhwKg7IMbXKL c+fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Drn2MP92GfxGHY+b7s7XFIx1YXY2oN3ER8r+Hc85sys=; fh=u8rLQsUjk/DQ68o09SM+wx8FxqDFcT3zeQdKot9Eyy4=; b=Knl/Xdm4I5UeoBTw9dQZwg7Vdi4F5dHXx7184Y46fon4hOfYBjE8sTVMgbwR4Ahu6S nD57lMXIGSruWHpWuwtmrtt5A547fY8Bb5hWdq7yeIgIUQ3SxF622Kr92bq5hCeAycff qInlH+7HjFFT2IsQFb2XK3PK3lQmoKr22kES2elyb141lyYTOgdrCGbs3e8D4vJUia7M Dmonc6ScK7agILMBnpGw4KWOVfrH5RJ46nSM83fF4pbzAcJj7ewaoCO1ENWqAprW/HHD F0JsEwzFR1JJ1EB7MzuTwb6YFldE9JSlgs2acLqQ0bXEDvjWI059DkzDcxsK9+S1/Z/U vTiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=sRCMROUq; spf=pass (google.com: domain of linux-kernel+bounces-1236-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1236-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id o22-20020a17090aac1600b0028659d5fb6bsi3057039pjq.143.2023.12.15.07.53.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 07:53:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1236-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=sRCMROUq; spf=pass (google.com: domain of linux-kernel+bounces-1236-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1236-ouuuleilei=gmail.com@vger.kernel.org" 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 B40FB2821EB for ; Fri, 15 Dec 2023 15:53:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5C1313C485; Fri, 15 Dec 2023 15:53:13 +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="sRCMROUq" X-Original-To: linux-kernel@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 DF5A739FC9 for ; Fri, 15 Dec 2023 15:53:08 +0000 (UTC) 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-40c6a71e88cso10099535e9.0 for ; Fri, 15 Dec 2023 07:53:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1702655587; x=1703260387; 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=Drn2MP92GfxGHY+b7s7XFIx1YXY2oN3ER8r+Hc85sys=; b=sRCMROUqehpEE+flhCIFj2KK5G0m07X5NOA9lFL41XdYZklFVKaaLNJaFyN5d7COID VIXiqA3M65IyF/mjzaQvEhaPwqKIsWc/FvFvSl/DbLCL7148xO10b7GM4hlD6RdK39Or l15B8QkaJj5qhVVx07VaI0y4UG6+y1nZUpt2zS5puHidxP6P/lk3Mi6/QeFBBaXrukvm H5yf0JAn3IoHAElREKB2wc7ljlfNq7dZ3oPzptwCZvHR4ifLo4/IGnRndhzevA4Z09fv v1I+fUMsIcPVEsRzOcvujI6GR4YKrUwCddrrm1aKB5wwAqaoezu+tb2dPsBrgAvXDump sw/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702655587; x=1703260387; 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=Drn2MP92GfxGHY+b7s7XFIx1YXY2oN3ER8r+Hc85sys=; b=XqGVSdCYTWEE5Baf2yVij1mt9MBLwZEvEud6xiGQ6qTDv04TkDGWgZlXnyLWHFBGAW g8J/K/XsFUfP2HcxPy7HVy08diJqtdTLjW48eSjTjqbXFge65nyuNepkHzV9bv/A8cM+ NuHO6UggYbN3cM7Q3L25f0wAcoOhXGcOd5z1WBvO3imhZBIZO5r7V1uRoLeigcKwKcGb EAviTywuPk67/3hDaxa/teDdJ4oXC0HOPXfUCA8I1OE1CSl3v2KPrmUIhbw3ADXfJI6Y m+aOuvtIkREXOJVzrEEepDArMIiMqoZEcHEsF2xWw4Y/GhDuL9CO7Kds2WKHZSt8/xRu rFhQ== X-Gm-Message-State: AOJu0YyhOuU5zbeXFQXp7h5PsuHAqALGRngmV51s7QeDd8J4Ry4mggBi rrK7AZAl22Fkogv+in92saeLxA== X-Received: by 2002:a05:600c:2a18:b0:40c:2671:2c52 with SMTP id w24-20020a05600c2a1800b0040c26712c52mr7158602wme.99.1702655587094; Fri, 15 Dec 2023 07:53:07 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:8672:e2c5:37d9:3743]) by smtp.gmail.com with ESMTPSA id bg22-20020a05600c3c9600b0040c6ab53cd2sm3110484wmb.10.2023.12.15.07.53.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 07:53:06 -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 v3 2/2] gpiolib: use a mutex to protect the list of GPIO devices Date: Fri, 15 Dec 2023 16:53:00 +0100 Message-Id: <20231215155300.21186-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215155300.21186-1-brgl@bgdev.pl> References: <20231215155300.21186-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785363837161238982 X-GMAIL-MSGID: 1785363837161238982 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 Reviewed-by: Andy Shevchenko Reviewed-by: Linus Walleij --- drivers/gpio/gpiolib-sysfs.c | 45 ++++++------ drivers/gpio/gpiolib-sysfs.h | 6 ++ drivers/gpio/gpiolib.c | 139 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 2 + 4 files changed, 91 insertions(+), 101 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 6f309a3b2d9a..ae4fc013b675 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -766,6 +766,25 @@ int gpiochip_sysfs_register(struct gpio_device *gdev) return 0; } +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; +} + void gpiochip_sysfs_unregister(struct gpio_device *gdev) { struct gpio_desc *desc; @@ -790,9 +809,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 +821,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 5b744d1f31f6..9508396c967e 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); +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); @@ -831,7 +823,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 +887,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 +1017,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 +1127,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)) @@ -4756,35 +4743,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..8142b5cbc395 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -15,6 +15,7 @@ #include /* for enum gpiod_flags */ #include #include +#include #include #include @@ -136,6 +137,7 @@ int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); extern spinlock_t gpio_lock; extern struct list_head gpio_devices; +extern struct mutex gpio_devices_lock; void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);