From nobody Wed Nov 27 14:25:40 2024 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 52DC41AF4E9 for ; Thu, 10 Oct 2024 09:10:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551443; cv=none; b=snMzdX9HB95UkcMwAZqpQKOU/9ztrFQex89XWJiZP4MW3mHu5ksLz/m214FrarIFz1b5oopDLPz8uut7zR037HgqONLmFh7pMuAgWSpYoEmmtR6kTA8ZflTWivfehT4TrWHR95jK5LMNKS1W1P3pRktvaDZsdbK5thKcH92LTkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551443; c=relaxed/simple; bh=aW/b/T1d54Yns4/Dn9IWAFr7kazZPhss/gdB8BCgwcc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NbG+BL35OuKdk6B4XHsqU6YfCbL9N+4QCr3WNT1Wp8kmIvcP5swX2p3boFevb9tTzmIw9dyF5gqANFHivbl0ROWmJiLr4pWwbigeWcmaLSVRquSi0rM530x779EuAz7pkR/7dPhZpl5SH2rmSc67qnMVW/AVdEzMsiQgcLPgg+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=kt3iZLm3; arc=none smtp.client-ip=209.85.128.47 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 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="kt3iZLm3" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-431137d12a5so6072265e9.1 for ; Thu, 10 Oct 2024 02:10:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551440; x=1729156240; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=KvoKEm861qnmPYL94kUVnrmcLydUdJpjADRRQZ6lwKU=; b=kt3iZLm3KuFGROtMehJlQW7OvGk47PgthNdLV/hfyLd6ckH4rKE9ijGgmsugHpmy0O 5MPsk0YptJVmMi+uv5aZHYCjcAn1gJwOPAYCnyn2XnGdXFNo68gx3izCHl5exnun+4/X 5E5p+TCb3R/+jkSLdVovVSytk8Vtp/HOUU/ZBwaKlVtsay57etuWGRY1moo1XFP60v5w AzctradVUKoc305FY//P/7YEDkVq/9sFjB0CkIM8c0O/VPEO+A0ByBRsoLBXVbtT0fZu wK3RNcnnZDBSUtjjmnCncrASLrQVXhOFJHR7uaMdX7zO/umB4J0LtnA0cvqyL2pc50sx aVQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551440; x=1729156240; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KvoKEm861qnmPYL94kUVnrmcLydUdJpjADRRQZ6lwKU=; b=eGPgGvpQx+7Lq+RnX73g19bDxITvvaeFfbtqcjt5IN21jm2qdBwOBVYqTr5PcOWyqp zY4552g084g95yr0uwWu200aOXhdDLwrl2mzPCsyK2AlVm6hZ++mafnnoTQ9M/AR3QQS 8+izCixXRyiMXY7vUKYytlgXY0PMyDeY6z96c3LLdIznKU7eVLhBOM/JNwnBjMRad6m0 h0kXkvD6LFXFzoqXf37fe6YIZhX6n5S2jicymlgnVd9bow8OsXMA1A26Qv79ucfUdW/J rANSy0ErSyXT5rDuUj5XSrKwEtmjIGhr4xefl03pzDUhxKGcPmAQ1PHrsxZ+YMbu/naY S9DA== X-Forwarded-Encrypted: i=1; AJvYcCVlgIvNOSxy7qQ80maYhv6OXuGsdcxWSeYrd4wtsnBtRrJ9QcpIFRW0G8rc1RA97XD/iz/ZtjqGZBL1T44=@vger.kernel.org X-Gm-Message-State: AOJu0YwJjvUGpyc9S7wjQnPqnoXGTUX3SQGclNmb1bKTiR1f0xjaVnkQ 79/zmHi2dhnkgWIF3nzdMbqiCcgtHV+1nFUpvxs6sT45PA5KhttO9/+n767mbIs= X-Google-Smtp-Source: AGHT+IFKSWpq+jqGFW9ejz23rt/KEV5YFr6YCkz/jzb4YhUhAucXzJ3D8WvGbchMf1Jov1MtKswThQ== X-Received: by 2002:a5d:5745:0:b0:37d:3742:a5a7 with SMTP id ffacd0b85a97d-37d3aa31aecmr3851200f8f.33.1728551439662; Thu, 10 Oct 2024 02:10:39 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:38 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:26 +0200 Subject: [PATCH v2 5/6] gpiolib: switch the line state notifier to atomic Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-5-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4156; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=q03bW6eyBOMt/E/qq9Du/B83t4iIgrEc2Jdh/e3KSKE=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oHqntgTP9D3GRBLflqkJjoa4tgqazn3xsg4 G/OwOJGX5CJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBwAKCRARpy6gFHHX cvPDD/9Ik0eOYYJZbp+zkQrkn9NONoXJK2PKerMqcD83cWWnZ7Y7Qrn9NJnLv2Y2WrK4H/u6fzG p/Csa+5KLQkxwKZAOjs9xqhswL7sOtc4t1ROjuncQADKNEHseWriisvGsdgZIEwSMHOyHYVonBd HBvoHur5hAAFjybhap4idMToqe2JysZYGdGSs74zhTxDCVgmhZfss62uChi7heOlrpy5cF/j3aN ICPlANAsajmm2iw0iFTvsl9mO2vcUqZ7n0edZehxzCVoksXW9zdKWSl8ht1gFUgJKtX8ToFH2Rc E8fURcreYxIYpfz6gech/st9Slg39/xTVLSkjqmpO3Q6DbBNBWYM6djSz120O+Vlp9acpTPZBQD kwmNEUypsaCzbBEZwX1v7P74mY9veus6Tk8Tj3+6z0QMgeqF60c8VkAMNnVJ6MWZEeONs+nd/Ml NI4zEkxGyHfrkGEC8tRBfemImPJCl9LKVxm5LzwQvF6iDIkfAuOxkgmwoNagJQTVaRFFoeaFn46 IPsPmx3vGy3rBk4+JqE7TVvr48FObFhHcPu5ujx1DfRJX5wDBWPg63J2UF6EQvBPBC3N8vUmyWY DbilOiqpuqqUYibh4xmm6mYEMCxtU5HR5BYYuFiGvVAv/1dbVwyxWJ/XqS75eRtp5FEYVSTHvQS O9oCwfKy46HK/Xg== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski With everything else ready, we can now switch to using the atomic notifier for line state events which will allow us to notify user-space about direction changes from atomic context. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 22 ++++++++++++++++------ drivers/gpio/gpiolib.c | 6 +++--- drivers/gpio/gpiolib.h | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 2677134b52cd..7eae0b17a1d6 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2673,6 +2673,16 @@ static int lineinfo_changed_notify(struct notifier_b= lock *nb, if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) return NOTIFY_DONE; =20 + /* + * This is called from atomic context (with a spinlock taken by the + * atomic notifier chain). Any sleeping calls must be done outside of + * this function in process context of the dedicated workqueue. + * + * Let's gather as much info as possible from the descriptor and + * postpone just the call to pinctrl_gpio_can_use_line() until the work + * is executed. + */ + ctx =3D kzalloc(sizeof(*ctx), GFP_ATOMIC); if (!ctx) { pr_err("Failed to allocate memory for line info notification\n"); @@ -2837,8 +2847,8 @@ static int gpio_chrdev_open(struct inode *inode, stru= ct file *file) cdev->gdev =3D gpio_device_get(gdev); =20 cdev->lineinfo_changed_nb.notifier_call =3D lineinfo_changed_notify; - ret =3D blocking_notifier_chain_register(&gdev->line_state_notifier, - &cdev->lineinfo_changed_nb); + ret =3D atomic_notifier_chain_register(&gdev->line_state_notifier, + &cdev->lineinfo_changed_nb); if (ret) goto out_free_bitmap; =20 @@ -2862,8 +2872,8 @@ static int gpio_chrdev_open(struct inode *inode, stru= ct file *file) blocking_notifier_chain_unregister(&gdev->device_notifier, &cdev->device_unregistered_nb); out_unregister_line_notifier: - blocking_notifier_chain_unregister(&gdev->line_state_notifier, - &cdev->lineinfo_changed_nb); + atomic_notifier_chain_unregister(&gdev->line_state_notifier, + &cdev->lineinfo_changed_nb); out_free_bitmap: gpio_device_put(gdev); bitmap_free(cdev->watched_lines); @@ -2887,8 +2897,8 @@ static int gpio_chrdev_release(struct inode *inode, s= truct file *file) =20 blocking_notifier_chain_unregister(&gdev->device_notifier, &cdev->device_unregistered_nb); - blocking_notifier_chain_unregister(&gdev->line_state_notifier, - &cdev->lineinfo_changed_nb); + atomic_notifier_chain_unregister(&gdev->line_state_notifier, + &cdev->lineinfo_changed_nb); bitmap_free(cdev->watched_lines); gpio_device_put(gdev); kfree(cdev); diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 839036b116a2..9b10f47832d5 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1026,7 +1026,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, = void *data, } } =20 - BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); + ATOMIC_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); =20 ret =3D init_srcu_struct(&gdev->srcu); @@ -4089,8 +4089,8 @@ EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); =20 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action) { - blocking_notifier_call_chain(&desc->gdev->line_state_notifier, - action, desc); + atomic_notifier_call_chain(&desc->gdev->line_state_notifier, + action, desc); } =20 /** diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index d24cd9e8b17c..2799157a1f6b 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -72,7 +72,7 @@ struct gpio_device { const char *label; void *data; struct list_head list; - struct blocking_notifier_head line_state_notifier; + struct atomic_notifier_head line_state_notifier; struct workqueue_struct *line_state_wq; struct blocking_notifier_head device_notifier; struct srcu_struct srcu; --=20 2.43.0