Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6627273rdb; Tue, 2 Jan 2024 08:00:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFU4kcZMQcfAKiKz+9ieLOHqDsefu54QxMuz3yt967lOD9DerlvlbiCY0xz6fQ+tXR9Fe1g X-Received: by 2002:a05:6214:b62:b0:67f:2107:f5a9 with SMTP id ey2-20020a0562140b6200b0067f2107f5a9mr22935509qvb.8.1704211225435; Tue, 02 Jan 2024 08:00:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704211225; cv=none; d=google.com; s=arc-20160816; b=yisQ1LBLBOqihqZdLXxLwZ4LSNYTvIMR7YBNYlSpsbuGh6tKi/apIBokQ7fQr+Nnzu Vb5BWLC/J2XL2eilgElOnJNLdOR7n0JYWmDozCSacZVJ7Jz+Mqcf/i5pvEP+QeSzTWJq sR5yGZIcmavgfFm2/FiU+lQ7vMr/kOrZodXAKEM7K1Tj/GdVjpsCIFCnf7TTaXXGwoBc EsMfZMBMexvOojzqvTd+AXhWaHmAlyFGBv8NlBXoM9v28blxaEe1DCGzecd7UfSEODnL R7pvCnEDtv2k7fklNFqGMfleJBNQLPEMk7DGZdRH2vSdPdyutVcRIvNsmMj8KhkoyQEh Vn7w== 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=g3wf/pQ/AJWgbz0DcSp/EQGOfmOL52HnCJ3+CUimtjU=; fh=kmduAntvCxYKWJxf0o5GnU6TlPpo49/gb3O/sJ4FX9Y=; b=qKhlypWMN8B6ksjJTZD1STD1Am+Yw1yUGTDJKYUnFqM/rg7ygJHVIDioo33FINeeoX pTetEkbbYJ7NrXf+GiEP1Ok8JKGlV/bN9d+CCCYFB7xxIEE0K99d4ZAhfWNuYh+R1Ger nEyLH6Exrh1PL/iAQW6eJmUghEZJCv0v9gTwzZoftQd9L7P+SfznVFmje/ssf+IdYgKL toGxbVQzjIvXfEwfjQeXQqXMBrf0EGQfWiD0MTM/gHX7AzO6/DJO+UqOBJ3INfs5asUh LJtnQ6v7z56Y2uNMTQs1FP3p13dW2WFWXEjF79tgLXxV0L+17VT+R4GjMZwKtRZO6JYA mCyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=PduPRUQs; spf=pass (google.com: domain of linux-kernel+bounces-14577-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14577-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id m7-20020a0cf187000000b0067f44854155si26806612qvl.92.2024.01.02.08.00.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 08:00:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14577-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=PduPRUQs; spf=pass (google.com: domain of linux-kernel+bounces-14577-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14577-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 23D7C1C22480 for ; Tue, 2 Jan 2024 16:00:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F39B14ABD; Tue, 2 Jan 2024 15:59:59 +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="PduPRUQs" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 EEC8814ABF for ; Tue, 2 Jan 2024 15:59:56 +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-f41.google.com with SMTP id 5b1f17b1804b1-40d88f9e602so14444325e9.3 for ; Tue, 02 Jan 2024 07:59:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1704211195; x=1704815995; 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=g3wf/pQ/AJWgbz0DcSp/EQGOfmOL52HnCJ3+CUimtjU=; b=PduPRUQsYm/KpfCIrEUCZUXYsQ7W+JTChiO0vxlA4tCt/TnMtN6KAmdNhvsJUnN1+9 xk6NawI3a+MvdzLGPMnsSw2Ri/H0/Zkk2YL6tte1jfjHmTyySxByGhzaZiTecbsKmjVk DHsTPO+WwUPvfXF8/AxcogXxL6p0FZGsdPO+kr+yKEw28WVDfDcmyhUgqgzwuZoq1Cvx x3yfPJGfsSSFvl1deH43Mj17jFTX+vZ2ljdRKl6vgifEzzmRhzCGeEz5v5YwyioaEboQ UlVfdZgS40bP1AqxgpXDJMIkCndRDH+LzRHzxRhQ7AaIFcZsFmSW68eUSs7Wsju8//Cq sD3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704211195; x=1704815995; 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=g3wf/pQ/AJWgbz0DcSp/EQGOfmOL52HnCJ3+CUimtjU=; b=L9XnvL7PJBh5k3yoMwDY/Xwul2Ok8Gyy5g7w2pcfp7FXHh7se5ZWJsA3F50RHe9Fxw FPM02cNteZYZItl79G9eZsTqW9GBHwzj6WaagfEW5YMqOrI5mdEFT31M7wbSs4OSJtFI 5NaW1F3hH/+fScNeytx2H8usiigpzS/K06Xxiby8glnPd++kfGc7+jHS/5OWsI/bt8U6 iUNrIdfGqZtaS993AK7v8JbiB09V31VEVodgPGgSv4IvM0dHq4v3JlBSQKXMggnVW2YV R81jhTvKk7fgBL7JYeyULVulrx63nT+J2u+kBmB04KUdNz4SU+Ajd/3gnh+xtF3XHFpo IcFw== X-Gm-Message-State: AOJu0YwSbFxY6zUdttEnIhMZTctSd2UKMXm7VxrexVuO9XhGCakhv6cz 7W+U/2ZP5S63PCMoK8DnHVPOtTtXvYTDnA== X-Received: by 2002:a05:600c:1ca9:b0:40d:5b0d:b105 with SMTP id k41-20020a05600c1ca900b0040d5b0db105mr6173715wms.117.1704211195108; Tue, 02 Jan 2024 07:59:55 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:6f51:fa47:f00a:a53c]) by smtp.gmail.com with ESMTPSA id u14-20020a05600c138e00b0040d62f97e3csm20556641wmf.10.2024.01.02.07.59.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 07:59:54 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 2/3] gpiolib: replace the GPIO device mutex with a read-write semaphore Date: Tue, 2 Jan 2024 16:59:48 +0100 Message-Id: <20240102155949.73434-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240102155949.73434-1-brgl@bgdev.pl> References: <20240102155949.73434-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Bartosz Golaszewski There are only two spots where we modify (add to or remove objects from) the GPIO device list. Readers should be able to access it concurrently. Replace the mutex with a read-write semaphore and adjust the locking operations accordingly. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-sysfs.c | 2 +- drivers/gpio/gpiolib.c | 18 +++++++++--------- drivers/gpio/gpiolib.h | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 2d29ae37d953..4dbf298bb5dd 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -773,7 +773,7 @@ int gpiochip_sysfs_register_all(void) struct gpio_device *gdev; int ret; - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->mockdev) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index e019c4243809..4c93cf73a826 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -85,7 +85,7 @@ static DEFINE_MUTEX(gpio_lookup_lock); static LIST_HEAD(gpio_lookup_list); LIST_HEAD(gpio_devices); -DEFINE_MUTEX(gpio_devices_lock); +DECLARE_RWSEM(gpio_devices_sem); static DEFINE_MUTEX(gpio_machine_hogs_mutex); static LIST_HEAD(gpio_machine_hogs); @@ -118,7 +118,7 @@ struct gpio_desc *gpio_to_desc(unsigned gpio) { struct gpio_device *gdev; - scoped_guard(mutex, &gpio_devices_lock) { + scoped_guard(rwsem_read, &gpio_devices_sem) { list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->base <= gpio && gdev->base + gdev->ngpio > gpio) @@ -402,7 +402,7 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name) if (!name) return NULL; - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { struct gpio_desc *desc; @@ -871,7 +871,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, gdev->ngpio = gc->ngpio; - scoped_guard(mutex, &gpio_devices_lock) { + scoped_guard(rwsem_write, &gpio_devices_sem) { /* * TODO: this allocates a Linux GPIO number base in the global * GPIO numberspace for this chip. In the long run we want to @@ -1001,7 +1001,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, goto err_print_message; } err_remove_from_list: - scoped_guard(mutex, &gpio_devices_lock) + scoped_guard(rwsem_write, &gpio_devices_sem) list_del(&gdev->list); err_free_label: kfree_const(gdev->label); @@ -1065,7 +1065,7 @@ void gpiochip_remove(struct gpio_chip *gc) dev_crit(&gdev->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); - scoped_guard(mutex, &gpio_devices_lock) + scoped_guard(rwsem_write, &gpio_devices_sem) list_del(&gdev->list); /* @@ -1114,7 +1114,7 @@ struct gpio_device *gpio_device_find(void *data, */ might_sleep(); - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { if (gdev->chip && match(gdev->chip, data)) @@ -4730,7 +4730,7 @@ static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) s->private = ""; - guard(mutex)(&gpio_devices_lock); + guard(rwsem_read)(&gpio_devices_sem); list_for_each_entry(gdev, &gpio_devices, list) { if (index-- == 0) @@ -4745,7 +4745,7 @@ static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) struct gpio_device *gdev = v; void *ret = NULL; - scoped_guard(mutex, &gpio_devices_lock) { + scoped_guard(rwsem_read, &gpio_devices_sem) { if (list_is_last(&gdev->list, &gpio_devices)) ret = NULL; else diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 0ce7451a6b24..97df54abf57a 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -137,7 +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; +extern struct rw_semaphore gpio_devices_sem; void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); -- 2.40.1