From nobody Wed Nov 27 00:43:16 2024 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 1BF741E6DEE for ; Tue, 15 Oct 2024 10:56:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989796; cv=none; b=kj63ONP2fbNk8xcjESyPTdQL39Lelzn7F0MMJOAtXC8qJCknvyPXb47VzcNG8giwtoklPPG5qlVb5P9HnVeAy34SDZ6r6a9AtR5f8KaRZZm61vLBZpA/MeuOLuZtV0nwK/ssLAR+tjZiy7JlwLqafnVcmA/EWr8I8hxE5Oz82UE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989796; c=relaxed/simple; bh=POHh8g+2CvyZoU0L+E1KmaYOw5grZB9aKoaGo7YEz6Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=i+0G9wb7V1OEViPQUnlf2tF2ZKwJA4GNgD30/K4oQ4FnHRF35mg1mBwU3KYmy/1ZrApfJcmZOU0vvLnGjhgXl3vERG8I1fwGTLxccytk7gF+4h7K52ogfOAUsqlAn6qwVApIYFFxXsyQPqyQmgf0sUsOe4pm/JFSGiTlme4r0Uw= 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=w9sXKcvb; arc=none smtp.client-ip=209.85.221.48 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="w9sXKcvb" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-37d462c91a9so3330456f8f.2 for ; Tue, 15 Oct 2024 03:56:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728989793; x=1729594593; 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=OB/wBkvSEUCyn6Pce3Sw/d9S+YTAmxmeVBF6YPbGUIs=; b=w9sXKcvbLnUe1a5tQ22wsZYvHb4Tw3cASFTgrym2nJ0UggtAwMDJl7JYrsrIFJxA32 9bDU++pW7fZ/9N3X6xYfI9cwxflLMhAU/BOPjBIimDlwutivGbWfpqMm6Eu+jNG0ZGEi cwieASLOIgo3qrk4QLZwxje2vgGCJWLlcmVvV2u3g3R4jNy0jcE8RVYf75f2EPUbkui9 +RZNWcN9q4WRG0NOX/lvEOR5skEL5comTMzqS+Rm0iwFeodFEFf3or+xaF68oIOHTU3D byZw6ymKQckv3E/2i0veXzgBj6KUGUCKU8WPO0Ju+viaIFXPoSiRK/Td7ldMvGnr8HBo yPlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728989793; x=1729594593; 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=OB/wBkvSEUCyn6Pce3Sw/d9S+YTAmxmeVBF6YPbGUIs=; b=qrRWA/mFZjEmXvtgA6y+ttc9c5HAaHLHdaSu12V9Oj93j93YT3wM3Hp9uCQC8kxSc8 9zYeIAjQNdjsNaNWX9WC26H70D9YjGJqEPhVzyFUrxR1spqPzfzyw+6ALlopC6/10uaO rF1uZ+W8TSGo2nEetXSUd6RpsiWh0AouuUlC2XRxkfbBA6LyDirshyx2YRbW0AFCfONu qPvKW4sDJixqwOP5CxBFr49YwNX5HAT3oLoQBXzHEFxWzeI7ZfWdypYuRAEyWcgTU6gJ jrNBpUYLPXh2/uJPDhWksrY4gyJYXnPGqyBE8iz3500hXZtATarI67iHAxY4LMXjTyn/ mdTQ== X-Forwarded-Encrypted: i=1; AJvYcCW8Dl9D3OpdOwUshmfaFFLP969hP1H509jczMThrXwHbRK0nqau195jqGW1XEkwCxLhv2JbhCm/ZOhJPjI=@vger.kernel.org X-Gm-Message-State: AOJu0YwxEoMNgeVbfsg20mbpvqDZvFyGcCp2OPM65JHXsruiJluF+5bd RzqCq7iRqbPO1pBorotTgn3aEbdj4lv3gu1nsMYGVfOFl3Kc8LP8yP+LX9SiLfzBQ5Z9bpD+eRX w X-Google-Smtp-Source: AGHT+IGPDhoxi4t0FwbzmOsIZ+cexOTtluzQbWzttetOTGpkuLst9zhZTEkHOz6D3ry6ydwqci61oA== X-Received: by 2002:adf:f288:0:b0:374:c4c2:fc23 with SMTP id ffacd0b85a97d-37d5529fabemr10540698f8f.56.1728989792803; Tue, 15 Oct 2024 03:56:32 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:d382:b11b:c441:d747]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fc403d3sm1254115f8f.101.2024.10.15.03.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 03:56:32 -0700 (PDT) From: Bartosz Golaszewski Date: Tue, 15 Oct 2024 12:56:13 +0200 Subject: [PATCH v3 1/6] gpiolib: notify user-space when a driver requests its own desc 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: <20241015-gpio-notify-in-kernel-events-v3-1-9edf05802271@linaro.org> References: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@linaro.org> In-Reply-To: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@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=782; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=zMsMSJX1Nl0+caHE+JZSEzX7KhbE4YG1tQGrQmi8r44=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnDkpeBfCLeK9n2OwNNmJVy5zXeg4cQdpyu/cED vlQToy6702JAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZw5KXgAKCRARpy6gFHHX crb9D/9Z4QrxjSsPaqs/c9ITfR89kkLc9Ke/IetJgq89alayXHBv62HDxc2ES/R8CD14LiZAeXu aYdLFTwt4qndPwq+Slj6jgdbUrCc7/YKeTRLYgQdPbgCDGC5KBW38BUZg3V/EQbkeHaY+6z1TL2 sszaWxReN0zuGFopufoQPH9CK6KNFVT8087Fs84p0jAGRAkxgX1AW68Oxw5gMDz86PTl30U0R0g YWc4BKkfgNyBnFN0CzIlUrLHfyo+hZRKTyIFE3UHG6YtHnoFxsz204NUShRT1dYkxnQu4SZd6P4 QDzjqzL1r1wYBn7tFERmJryQvX7Kut2vl3Oyc55H3ZM0QtsuZolwi0rhE2r589Wwj7tFCI3t9dn Jy7ZapZ1X/GdoVJa+QDFIR0SrwPXE/PmTs9ub/GmdzqR3VbrZAJ2sRGm+g/T38UOnDU85CGcBk8 Yuny6fVfxlz78r/GQaHcMRSJf7sOwOdvr5dTZFA2l05HmtujRQuUwR1ao4xh3/CNKpjAnYq3yj2 TCuK3HsSf+vf0VwJrAcYWCVHK8PpzQXu0X4GLAR1C5QiZ6IObqF7NF0meLcH0RFrSw+No2MRLPr FxvATwHmJMw8YXtikGAoeSuPh5u1vYJc5chUYH9WYwowKcOGLp8tAHFMsHi8bg0zsRzZPNRZpyo 4yYXGcxw5KL2OAA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski We notify user-space about lines being requested from user-space or by drivers calling gpiod_get() but not when drivers request their own lines so add the missing call to gpiod_line_state_notify(). Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 97346b746ef5..c09464f70f73 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2532,6 +2532,8 @@ struct gpio_desc *gpiochip_request_own_desc(struct gp= io_chip *gc, return ERR_PTR(ret); } =20 + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); + return desc; } EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); --=20 2.43.0 From nobody Wed Nov 27 00:43:16 2024 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 DCB971E765B for ; Tue, 15 Oct 2024 10:56:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989799; cv=none; b=Gg9xb9yiOU4c5Lc+pbhp6gQAXGVRjv6BGuTQ4ln3VUH+CW6P1EMVCqeTNphgcPnFJFKwKu1cmbynVRisnBLaSWaX30Yd0Q/1jwGe1JQVtS/nUN1MZeN4Db+XlVfB4jO0GYjKUjKUccs014zzAD/dUfs18YoAwsl5Z0eKd8Uf1p8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989799; c=relaxed/simple; bh=1FvGI52kfpezR3fRNk5gdU5X2NGN/Ce2PAUzH6Yx218=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eH064vSSL4G3n3MXavksiPUA/7aYfUGa9bdfvfTDTsazSMMSkTQz+ou3AnS/XTOrGUS4pWzVNH5nV1ZdtXyFCCzcos2SYiHz0q2htZWAMZeI6/1g58bIUJWCBny0/BRyJj12/9kj5e5hVdPMMJ07LJ6uqVF2xSJOprMKo/PHWf8= 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=Aef1CqdS; arc=none smtp.client-ip=209.85.128.48 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="Aef1CqdS" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4311bb9d4beso34997985e9.3 for ; Tue, 15 Oct 2024 03:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728989795; x=1729594595; 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=Io1/9kud22QAvhf6jzcXu5d+3TGY2BSmgSByZJNBKbU=; b=Aef1CqdSysm20ynvj7X6w47e0nPtHZwwcZ6595YqwD4TSpciRSJvbMOWE5rqGO/3me kZ4NcADrmOED2ohUJAnWM2fxhs/0nCuCf3YRPz3zP2wyoKlKxxMS2+CYYL+r7922g313 fRUkSUR9QDumXNYBPAeoE/YkwSYx8lo70YcmgofB/A7WN3j1gimLyLfCYDs9RIPjH7ar pmSbBEhnWLJdG1m4qGTmVXseR+ANt8baBIxFLuPXCNTFk1la7M4+MieezfDbSGdxh9qS yXJLFNTYMWsAwS/XKPjlTrPbeJrxtnSh0vMlZgGgmrObjabA6tbRAujk8cfA4xLFNg3C FWPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728989795; x=1729594595; 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=Io1/9kud22QAvhf6jzcXu5d+3TGY2BSmgSByZJNBKbU=; b=aJtyvnjPkOxRYEWFeYqMc3OIH40807lFbZ6UPjwowkTDpXEZo8Ugz18EnVQTRmMhLp tc3U+JanoSTFeOIWVvUgM2v1iuyoenVCgJp04CLAib0G+UcN46oWLmFq+g9MRLm2P8MH iE8UKaO0WFoy8lSsKMsjq+ljqPu80IM4RAI+Ut2TLp5DDyVj+RMi2Fu/mi+F8oaFDo4O SGjz/QUeShgmAPU091maPDMC3m8ePFuIhbrCSVRLPIpvXLQ+uHzPYOCoYRonBsQSXPFI RdLLVaWyql1xVQI41YV+0lYo5VlGWC/HiYp4OA9/VdBM/5wEzebR6aREZNePHvormCMQ f90g== X-Forwarded-Encrypted: i=1; AJvYcCVY1PYGYJqf3hGuCHNmC+XjJEcrfRS1jsayXykpI9FNtrz927zeTBQscUwOa1elTjk2JPHiccOpTKJoNek=@vger.kernel.org X-Gm-Message-State: AOJu0YyHbm52wxmeOyhdad5dd2bm6T9KicvnmHmpOXt9GFt6nx18Y2AE UDr/E6GLWuvwcupveUiRxxAVuPgtPMnhKyW8vWy70pHyFKAA3S2C46DoY2MGTy/Kl9GIznaZcKe i X-Google-Smtp-Source: AGHT+IE/KgsUcgkQEmhfXkFmH2Hx2cAjwfcojeoaOttZwqt4pV698ZjtBDq3aaLrmB8GcX6+TGtddA== X-Received: by 2002:adf:f70c:0:b0:37c:c842:a16e with SMTP id ffacd0b85a97d-37d5fe96419mr9151347f8f.5.1728989794246; Tue, 15 Oct 2024 03:56:34 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:d382:b11b:c441:d747]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fc403d3sm1254115f8f.101.2024.10.15.03.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 03:56:33 -0700 (PDT) From: Bartosz Golaszewski Date: Tue, 15 Oct 2024 12:56:14 +0200 Subject: [PATCH v3 2/6] gpio: cdev: prepare gpio_desc_to_lineinfo() for being called from 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: <20241015-gpio-notify-in-kernel-events-v3-2-9edf05802271@linaro.org> References: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@linaro.org> In-Reply-To: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@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=2678; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=wig1myTtW24p4sbtrRCA4A81OwCTFkQeSpoEcy0Eyvo=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnDkpeDtDXpfarJmYVsE4wOtqMq59PjI88NKdi7 fSqi/3aoLqJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZw5KXgAKCRARpy6gFHHX cgbCEACwvj7qcGHV+sVbRvRDrI0Zodz6pcCNqar5bxIA/t85KHc1Rs9f06seRHY1JaXmv4bTEoD Yz06lwZx/AbD6bBJIuT6GGWn8ntAmp/jJ8PLgi59qZ9v7VCCkKKHELtNnSHoH9jfvYpGRPpUtNp UepyZX3QfxkOumGHUD7QcUD4Fnrir7FsZObBGWSleSMbBDxVjUkUd9YM/aN+JLyuCFRgt+eUDBh R+48oyWC5nxVVExuaDS9qYKAHc5jwJ4bclNVv2+Ie4pCKs2sAa6NBH/kLD0rLJbFmMEbtQipOLx tK2C0ihc7dVGhX6j/a+BuY1G1v5aWzXQ/oNemL96Nh8Ce5heXCUI2AVQtgDfLVxZxFFvV0D71q1 MzVZBFhfwMgUanxXNIJuZ5fSOhiYmMbeWarjTgy62Ao/vyldVy+XwKxj62c3QzmYhan78mZz2ft 6q6eul1oKhH3RdpgnwwLeTQdJ5fjfGHmDqjyf/y9G6VqSVMIEq4ZePYcLIE0MMwrAf8Ba2eTSI/ CbwHh+Tr7UzODe9I/QuG7YvB68yuk5vZUSvkNo3Je3fjcQwjoWVJ2K3AXgzxgoxXkbg3gWWMEZp WOSw1/Bx/9hSycQCE7N2zVLyiLxNs31ogwYBRBoj/dP6GeZSNZw6K/RjFKbB/ugX56SYEQyeNR2 DG1+7srG7kPSO/Q== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski In order to prepare gpio_desc_to_lineinfo() to being called from atomic context, add a new argument - bool atomic - which, if set, indicates that no sleeping functions must be called (currently: only pinctrl_gpio_can_use_line()). Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index b0050250ac3a..b28751242835 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2361,7 +2361,7 @@ static void gpio_v2_line_info_changed_to_v1( #endif /* CONFIG_GPIO_CDEV_V1 */ =20 static void gpio_desc_to_lineinfo(struct gpio_desc *desc, - struct gpio_v2_line_info *info) + struct gpio_v2_line_info *info, bool atomic) { unsigned long dflags; const char *label; @@ -2402,9 +2402,12 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *= desc, test_bit(FLAG_USED_AS_IRQ, &dflags) || test_bit(FLAG_EXPORT, &dflags) || test_bit(FLAG_SYSFS, &dflags) || - !gpiochip_line_is_valid(guard.gc, info->offset) || - !pinctrl_gpio_can_use_line(guard.gc, info->offset)) + !gpiochip_line_is_valid(guard.gc, info->offset)) { info->flags |=3D GPIO_V2_LINE_FLAG_USED; + } else if (!atomic) { + if (!pinctrl_gpio_can_use_line(guard.gc, info->offset)) + info->flags |=3D GPIO_V2_LINE_FLAG_USED; + } =20 if (test_bit(FLAG_IS_OUT, &dflags)) info->flags |=3D GPIO_V2_LINE_FLAG_OUTPUT; @@ -2502,7 +2505,7 @@ static int lineinfo_get_v1(struct gpio_chardev_data *= cdev, void __user *ip, return -EBUSY; } =20 - gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_desc_to_lineinfo(desc, &lineinfo_v2, false); gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); =20 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { @@ -2539,7 +2542,7 @@ static int lineinfo_get(struct gpio_chardev_data *cde= v, void __user *ip, if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) return -EBUSY; } - gpio_desc_to_lineinfo(desc, &lineinfo); + gpio_desc_to_lineinfo(desc, &lineinfo, false); supinfo_to_lineinfo(desc, &lineinfo); =20 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { @@ -2632,7 +2635,7 @@ static int lineinfo_changed_notify(struct notifier_bl= ock *nb, memset(&chg, 0, sizeof(chg)); chg.event_type =3D action; chg.timestamp_ns =3D ktime_get_ns(); - gpio_desc_to_lineinfo(desc, &chg.info); + gpio_desc_to_lineinfo(desc, &chg.info, false); supinfo_to_lineinfo(desc, &chg.info); =20 ret =3D kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); --=20 2.43.0 From nobody Wed Nov 27 00:43:16 2024 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.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 43F681E7661 for ; Tue, 15 Oct 2024 10:56:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989798; cv=none; b=W8c3sDb1UyeoAwB5xAFRtyagG8Nts8JI16HhfyzukEZQFBiPn4ZngUKOQXQk/vLUYBDxxxsjL12ioChMcSzH4+2O7d1eKBgFfCv6YL63Zmmd4zHbWOspi1z4gvmvk2zr11xgnu1Wou3n8uK6FbCgjEu4sLW3Q93yCsQyM2+IoQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989798; c=relaxed/simple; bh=UNcQIfeI9Kctix5djTNGZtgwK9Bg+muQhyndnSvCRJU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jyMADr6xMvHd1OXKuQEhwAiUr2H+ekz0PVbim+LKSjViNzptyWct392KmjwaXSSJWqlPrCYjTHMDcZ3VzPvLhuZGfd5GQWhKkUaybKGWgUm+HOsExRRrgYTPUmUCjn2MgGJf102qgp06z9WO2lXhUTa57T6HVfuBkGaw482KSkQ= 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=DGNbxalC; arc=none smtp.client-ip=209.85.221.54 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="DGNbxalC" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d41894a32so3492174f8f.1 for ; Tue, 15 Oct 2024 03:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728989795; x=1729594595; 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=/WJlWZCGus+48LK34rnVtgH2/1CQrXAPJg0W6vmwbQk=; b=DGNbxalCkx51hn2c6oN2woq9L3eWrq8BaAnWjxC0NWmOlsQVkT1JcoK/K/8qf1CXpN ZfC9w1mjDDSOcA5r89O0mXXTFy4fDDwsEFRZg/1fBTGBBIKGP/+rLlP5Y676D/cnmSBe K4+xQOo1bopah5FoSYNIJXW0eh3EJazFTn7hBlOjnqJo9KUnsiLp9sYszShuNU0I09ss tSl4t2RghwqUx5Qn75Q8NZqiFjgurpDkMuiNNyvFPWnCGxGkZ8w3csdyJ3j5pWl7NL+W Jl2NvqS6+EoKRMYrTWBMPHbbkIWTZfLgPeJXyIdAGiMuljptr4rpUlEbWuKly1qAq6RM 0sDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728989795; x=1729594595; 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=/WJlWZCGus+48LK34rnVtgH2/1CQrXAPJg0W6vmwbQk=; b=YtoDMJAtuqt+zWAim8gkWHtU3rYBREz+SdpoNy98xyyoITK5o32oSoHxyB4n0ONd1D HTEq/gNjkz8vVcB7wwVAlQebJaJlA9r9NOPwdicqf8O0AwhmbCM9cCbvgtikXaSflpYm /0OK9PP2kNU+Na/HZq603GQmD87JpskvtOzvclA7/CrGkyc2CHHdQu5NBd9XxDV5DtB5 ruHa8Eb4gw9ZRjEH+pGHEPIInyoqbceG2gdhK8wRNtOIuJELifm5W/X0G/M8z9JB8dJy LdCCZbpyrTe+PAYxWVPY4r3rJYp2d8Q3WjGa3+l8+Om6M+bITQl6QMbvCWh9b71ycFD+ 69pQ== X-Forwarded-Encrypted: i=1; AJvYcCXEWgirN7CgElkr31ed+lpo3KLs9AYeyTaG8S7g1HLCukz5X2lp8Hq5NrOdxrQ6r+1PmRgGx4OBk3lCcCg=@vger.kernel.org X-Gm-Message-State: AOJu0YyQ6YSOO1rxnpWzaKalj/nQ1jZc3tzyD30jBHe17eDShtEvHXKc 0O3Or2R85zv+GxIhJqijhG7ksLMVzm5vsaGEpC+7LaCxPAdenMum/sIYgYldYKOAfkkK45xGT5F m X-Google-Smtp-Source: AGHT+IFvovF5mLQQmZxmCIDVkoFe0BZDeTv+THse/dtpG4Az72bgv1hAENLRayOhrJT1XMvhw3zObQ== X-Received: by 2002:a5d:6082:0:b0:374:c1ea:2d40 with SMTP id ffacd0b85a97d-37d86266403mr62456f8f.1.1728989795113; Tue, 15 Oct 2024 03:56:35 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:d382:b11b:c441:d747]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fc403d3sm1254115f8f.101.2024.10.15.03.56.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 03:56:34 -0700 (PDT) From: Bartosz Golaszewski Date: Tue, 15 Oct 2024 12:56:15 +0200 Subject: [PATCH v3 3/6] gpiolib: add a per-gpio_device line state notification workqueue 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: <20241015-gpio-notify-in-kernel-events-v3-3-9edf05802271@linaro.org> References: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@linaro.org> In-Reply-To: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@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=2506; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=mlz3Sg6ePkK9/QjJgP2Wdc6zjCPXTyUO0GrqAzTAxqg=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnDkpewwbPIah26/a8p3MigsOLSn2/5qYPfJcSC O48WJf9JjuJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZw5KXgAKCRARpy6gFHHX chl4EADLDr0mXxlXm1se7tuSg+VT7O3SjgeJGyhyhdgbph+/OhURZmgbI3vrmKwIsUUITsntQSM L/lVaKN/xxPKzlx1P3JXzRFiOSlWi6RyvM3LnK62/5geCPEMPFf+54JyDdsAwTlv6r+mKmsz6Tw gilBfdwphkO07/pRwH3V8DI6oM9mWJ7j+SkBfewdA0C1yN6Kt/mcqksmGvLFEhHsizQBRNmoeus j+E302BSi6yZj7/PWYiPgszZy54p+N7N2y15+vwQTLNTR4x1RO87ydrrArgCiCg6y5Y7kdtr+rK ylVZdwFJ9ymxyiSCjpN39JUg+5JxPFjAYG1K65GmYR/Y0v9Pf5wbjb3bjPiQ0/VUoAfNV9ETdR0 XN6AosfT/pP5peI4xZUzOhlu0PARqtZmYQmvsfMDh5lG4AwFFghRiADuIACKxsUmGotiDPw5ql0 GlarAckfOBopDt6StAnMFs6/iwSzGe2hYXalb6gh9t99f6lb2C0uA3U0xGjqkNTmSQ0M0UOk5Rm ovhe0ELKjpj7zLCmPb3T3tHijKm1vO9QVLyknUYBkYWeoP5ji+3FGq8tiMm9WCjHy7f0TThmjpP n1zpMzriuSkxcONSiMCQzW1BJZu1rKbJqRya8b1LCg/FPq1o2tk/nKfCA6ItZhl7VyMfB70PTti pw9YAF/YGMn/HrA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski In order to prepare the line state notification mechanism for working in atomic context as well, add a dedicated, high-priority, ordered workqueue to GPIO device which will be used to queue the events fron any context for them to be emitted always in process context. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 6 ++++++ drivers/gpio/gpiolib.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index b28751242835..5b28a558c9d0 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2868,6 +2868,11 @@ int gpiolib_cdev_register(struct gpio_device *gdev, = dev_t devt) gdev->chrdev.owner =3D THIS_MODULE; gdev->dev.devt =3D MKDEV(MAJOR(devt), gdev->id); =20 + gdev->line_state_wq =3D alloc_ordered_workqueue(dev_name(&gdev->dev), + WQ_HIGHPRI); + if (!gdev->line_state_wq) + return -ENOMEM; + ret =3D cdev_device_add(&gdev->chrdev, &gdev->dev); if (ret) return ret; @@ -2884,6 +2889,7 @@ int gpiolib_cdev_register(struct gpio_device *gdev, d= ev_t devt) =20 void gpiolib_cdev_unregister(struct gpio_device *gdev) { + destroy_workqueue(gdev->line_state_wq); cdev_device_del(&gdev->chrdev, &gdev->dev); blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 067197d61d57..d24cd9e8b17c 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -17,6 +17,7 @@ #include #include #include +#include =20 #define GPIOCHIP_NAME "gpiochip" =20 @@ -44,6 +45,8 @@ * @list: links gpio_device:s together for traversal * @line_state_notifier: used to notify subscribers about lines being * requested, released or reconfigured + * @line_state_wq: used to emit line state events from a separate thread in + * process context * @device_notifier: used to notify character device wait queues about the= GPIO * device being unregistered * @srcu: protects the pointer to the underlying GPIO chip @@ -70,6 +73,7 @@ struct gpio_device { void *data; struct list_head list; struct blocking_notifier_head line_state_notifier; + struct workqueue_struct *line_state_wq; struct blocking_notifier_head device_notifier; struct srcu_struct srcu; =20 --=20 2.43.0 From nobody Wed Nov 27 00:43:16 2024 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 BC55A1E6DCD for ; Tue, 15 Oct 2024 10:56:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989800; cv=none; b=WxCUZlVvEvhqj0SaNKmeSKyGJYKGL6D4shkWBzRA1D01nhBHr+iLeWEZABfnHcz2YPatoXwcpuAwjqXYRlpCcEqenqWR6COadNvfJgBp9IhNMX7fhMCbqO7Lr0yuPw+gXqY184QspGQrTo1Pp7M5FsRA1E14CRhYa6uCvX/4Lgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989800; c=relaxed/simple; bh=mHGeTXkSLz/quqKh+bgl/pARcGM54n5NF3tAxUstCXQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W4ihlvzZPbGLeIe8WANRY9X1QV0zm9oXGNXe3XsTpAR9vEKw+3bptI1rnSa1mTrGJVvXuCwT5GxV+mxyKKVm9faJja6BAM8b+DJaedGooDmvA6IndtOTeL8M9tfNfea0M45pc3gPe8wGEIeCBI3UjFIaznqPSdIoPARfOxCi/nQ= 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=18lyViOJ; arc=none smtp.client-ip=209.85.221.50 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="18lyViOJ" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-37d4612da0fso3757660f8f.0 for ; Tue, 15 Oct 2024 03:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728989796; x=1729594596; 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=bElfYV+JapcJ+9k6206jn81atKKk59YkmAHm0D9qLSU=; b=18lyViOJD2ZTvMcOe/Q4BabEBV2O2Vu2QrPF/42BmvVW4nh2yxF5txFtxByCBfvcgX DreXSYZRmSjt61BpT643vN9l4uBv2YC1g/o+QGoC9lG2y+Z3YLeROCUmZU21pudhgXB2 T//a0nwSLOCCdIVEUzQWJZAihOl5JzYORRAgCAtSI9nJyF1pUz+AB6VLMws0+cJ9GTZN 75u20kMyBvWcKJSuioZo9JRVT6OwuPE/iHoHWsak8z0ctWkKsW/Uy+12r4rnX20m8IQb QVH4iBXXIPSvL3MAt3Q5TXsVOmonH3K6eBCXNPnPxWGqe0130LqgW/kIJeqyNN4dWQtx 3UAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728989796; x=1729594596; 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=bElfYV+JapcJ+9k6206jn81atKKk59YkmAHm0D9qLSU=; b=UyMYJKWSs7wqo43u+GRhQJHDcU52PCbkaHdzBWRN2Ey4h8l6KtHCuexe7GSFEh6D/7 xaR3Pk1Bm+C6klkoSKb6hnms14osGO4x3/bgKd+JluXfa/rTihMNyYaC9qFta8NqpV16 BRnfWId0N1ryQzlaocLLLmkZwHbS8EwR9oL966LBa3/IWLvozbANmEU6E2vS+Mx4qf0/ MOsCLAMnPbnTVKTtrgzbzfU6yu9Baz3Iq7G+QiJyW4NpT4decTCAvXT3XyKeAOkw3dfT Hl2DqIWIGSFg0U++zWUi0AU0j+slQLcqjUoOOYhdYSAkG2m42yfS7UGyh5wQ6TzlB/YF GLBg== X-Forwarded-Encrypted: i=1; AJvYcCXE0g99aZ39+ybC/WHd3o6LtH+E6XL2JuiRLwihGKvKyOO3fKL9KXMiw4NzFuiHnP8m7i/ME75demTyb1k=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4Rz7VEwDY2yEYYdNrrABe6GiJwv2mrShHNF683X8r14f4l7/h GRtszSi5CBmAYlTDdVtn3UWHFM/9NAgNnNDFnDi9JgdgRBk6vRQBFy92Bg6HKiMAta36Ih3MnAO O X-Google-Smtp-Source: AGHT+IEIJ1qi8uK3FnfhqXu3LOYo+YKUT2SgWKr6f7lGkitGelO5x8dSZ/qNh/f1JGg+EOlbMdjOcw== X-Received: by 2002:adf:f5c6:0:b0:374:c157:a84a with SMTP id ffacd0b85a97d-37d551fc33amr9812385f8f.16.1728989796149; Tue, 15 Oct 2024 03:56:36 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:d382:b11b:c441:d747]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fc403d3sm1254115f8f.101.2024.10.15.03.56.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 03:56:35 -0700 (PDT) From: Bartosz Golaszewski Date: Tue, 15 Oct 2024 12:56:16 +0200 Subject: [PATCH v3 4/6] gpio: cdev: put emitting the line state events on a workqueue 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: <20241015-gpio-notify-in-kernel-events-v3-4-9edf05802271@linaro.org> References: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@linaro.org> In-Reply-To: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@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=4901; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=f8lvTlR7jwkkQHTiOBQ0YBYlycUUnx2/1dVqc+JGfAw=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnDkpe39urhIK2g2iZKIfiIdJYk0gGf71GbpYBz E/dfDumy3eJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZw5KXgAKCRARpy6gFHHX coaLEACZ+jXIKIcTgPJRx+YuzqYx+JNnJ1HTOu6bF/kUDBLmTOpkfmelTrIrY1gW5myTxvGtMdL 0zHa/PaRyWZD/RHQA0+iwuJrwBGssbG324Y2fdguJmxyaTHwT39yojGSNGw0FgHGU8a21xIG22u YzXhfMSSgr+e/wm+Jfv5rL1SUPcYpSWc64n+1nOwgJnnGfuvudYuKlVvf3bjdUkjLVB41DscKnt tQ9Rctdg7PPvsJKTYpMnEFkDl2dX+Q9RnK0HSa8vPFmvnsA42iGz5Rn2LWe46lG0PsIxFSTkXnd Su8CcPARCmITnLUh+qeZ7SLxgE7GAGUNkAmHy3XhmMKErfQ+tVr4W5CxmNU6KG6g1REaPTd4q+P NUsN1KvmQ/0gDoWDxxwZmnjGLHj3A/gI97j6TQF2E82fxh/yuApgL9SY3RW7QNDa2ABGhqF98oZ OR8JUcVOK1Y1I1U99wyUEujKGUOGMFZJWMnafYGUofcStMt6qy6itPh59mtNhhMb0imKlAa8/C3 Ls0oom4SoUtP4qsGL3ZUprZad/vFV6PUMXbaOWg949HWhLHgG+fMLxaWwLZIENTPFeOGrExGoCI k8I2hKBUu5KYY4u3QkSAZ62+I7URLv+PMBuWWUL7UJ/aHySM3+tcg2RRq4xtDQBjed6DOI4rF6Y H2fEX/OBPVvz1qg== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski In order to allow line state notifications to be emitted from atomic context (for instance: from gpiod_direction_input/output()), we must stop calling any sleeping functions in lineinfo_changed_notify(). To that end let's use the new workqueue. Let's atomically allocate small structures containing the required data and fill it with information immediately upon being notified about the change except for the pinctrl state which will be retrieved later from process context. We can pretty reliably do this as pin functions are typically set once per boot. Let's make sure to bump the reference count of GPIO device and the GPIO character device file descriptor to keep both alive until the event was queued. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 84 ++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 5b28a558c9d0..a10d6ea22415 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2450,6 +2450,7 @@ struct gpio_chardev_data { #ifdef CONFIG_GPIO_CDEV_V1 atomic_t watch_abi_version; #endif + struct file *fp; }; =20 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) @@ -2620,29 +2621,87 @@ static long gpio_ioctl_compat(struct file *file, un= signed int cmd, } #endif =20 +struct lineinfo_changed_ctx { + struct work_struct work; + struct gpio_v2_line_info_changed chg; + struct gpio_device *gdev; + struct gpio_chardev_data *cdev; +}; + +static void lineinfo_changed_func(struct work_struct *work) +{ + struct lineinfo_changed_ctx *ctx =3D + container_of(work, struct lineinfo_changed_ctx, work); + struct gpio_chip *gc; + int ret; + + if (!(ctx->chg.info.flags & GPIO_V2_LINE_FLAG_USED)) { + /* + * If nobody set the USED flag earlier, let's see with pinctrl + * now. We're doing this late because it's a sleeping function. + * Pin functions are in general much more static and while it's + * not 100% bullet-proof, it's good enough for most cases. + */ + scoped_guard(srcu, &ctx->gdev->srcu) { + gc =3D srcu_dereference(ctx->gdev->chip, &ctx->gdev->srcu); + if (gc && + !pinctrl_gpio_can_use_line(gc, ctx->chg.info.offset)) + ctx->chg.info.flags |=3D GPIO_V2_LINE_FLAG_USED; + } + } + + ret =3D kfifo_in_spinlocked(&ctx->cdev->events, &ctx->chg, 1, + &ctx->cdev->wait.lock); + if (ret) + wake_up_poll(&ctx->cdev->wait, EPOLLIN); + else + pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); + + gpio_device_put(ctx->gdev); + fput(ctx->cdev->fp); + kfree(ctx); +} + static int lineinfo_changed_notify(struct notifier_block *nb, unsigned long action, void *data) { struct gpio_chardev_data *cdev =3D container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); - struct gpio_v2_line_info_changed chg; + struct lineinfo_changed_ctx *ctx; struct gpio_desc *desc =3D data; - int ret; =20 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) return NOTIFY_DONE; =20 - memset(&chg, 0, sizeof(chg)); - chg.event_type =3D action; - chg.timestamp_ns =3D ktime_get_ns(); - gpio_desc_to_lineinfo(desc, &chg.info, false); - supinfo_to_lineinfo(desc, &chg.info); + /* + * If this is called from atomic context (for instance: 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. + */ =20 - ret =3D kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); - if (ret) - wake_up_poll(&cdev->wait, EPOLLIN); - else - pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); + ctx =3D kzalloc(sizeof(*ctx), GFP_ATOMIC); + if (!ctx) { + pr_err("Failed to allocate memory for line info notification\n"); + return NOTIFY_DONE; + } + + ctx->chg.event_type =3D action; + ctx->chg.timestamp_ns =3D ktime_get_ns(); + gpio_desc_to_lineinfo(desc, &ctx->chg.info, true); + supinfo_to_lineinfo(desc, &ctx->chg.info); + /* Keep the GPIO device alive until we emit the event. */ + ctx->gdev =3D gpio_device_get(desc->gdev); + ctx->cdev =3D cdev; + /* Keep the file descriptor alive too. */ + get_file(ctx->cdev->fp); + + INIT_WORK(&ctx->work, lineinfo_changed_func); + queue_work(ctx->gdev->line_state_wq, &ctx->work); =20 return NOTIFY_OK; } @@ -2802,6 +2861,7 @@ static int gpio_chrdev_open(struct inode *inode, stru= ct file *file) goto out_unregister_line_notifier; =20 file->private_data =3D cdev; + cdev->fp =3D file; =20 ret =3D nonseekable_open(inode, file); if (ret) --=20 2.43.0 From nobody Wed Nov 27 00:43:16 2024 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 01E101E4110 for ; Tue, 15 Oct 2024 10:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989802; cv=none; b=m3G6Yp/S6nQ6ob+nOAWdYcpO9jwxEiBREFE1aUOOJS1bUBF7yWEvT+a8cjmmkDSpNVO5dKF2sghQFGR0X87zq6fX4sP6xdIdrAZZCV1QHacznqUyFPn+OMKy8JlksyVjsDRJgPMkXT79m4gIx9hS98IDA7f8Mdbre/mBILuectM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989802; c=relaxed/simple; bh=QwLsQS7+YTa0mwb1vrwV8L24m/vFtrTi/0g+0KOyv2g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Zl9Z579Rha8Yt/f+U+D15sXmNAmct5GJ0RpUvTjPN9IeWKkXw0XbG1rja3GfF9xZP3trY0Z3iF7XWTzYPelrjQsG8wm0TEMpW1WLN+7mOOHjurJZH4mxSPKVfG/gAp0poz03+tXLk+YTjhnIMHwB4DxTVj85EUWFgSU2RgGvmI8= 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=U6e6gJfP; arc=none smtp.client-ip=209.85.128.42 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="U6e6gJfP" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-43111cff9d3so39161405e9.1 for ; Tue, 15 Oct 2024 03:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728989798; x=1729594598; 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=vr+76nULAqAGoFwQMgtDIWsEZhtzqWTjmTFSlRdLw5s=; b=U6e6gJfPnYHkUUsXTzNXSZ2Z5yC8NOyckMMoufPIi82DXAD56f+VTcizW40lvAErqf /L5MoOL1zUfdB+YHqy7h1/m4ReRnbJMgLK8DJ7QqAPpFAzG9dvL2wJlCwyNKN78Z+Oba SOE2cCQHxDFrGCVbMJQV/HdnMs/zpVfRGjguYLDp4ayCiZ1rRokQOUtOSA3KNtoinFNI 56VEyDPhIhvderNqLCdsh9iNiyUiKG2M5XvQxhkLSpY0voKiC1WtNLCA4D6tI8HbZ3J8 Oj6ivuXaNrxVGXmTNq5QELWarMczmDN7NBoQ5Vv6RVJdpF/LsqP6aQfD/khR+KKYhNmG cssQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728989798; x=1729594598; 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=vr+76nULAqAGoFwQMgtDIWsEZhtzqWTjmTFSlRdLw5s=; b=ef+4D4QSn6GL7kZ1HR5oAvDm+Hb5siyfwTvTpJZUOavOznflU3sBE6UAs3MXk4fnW4 ESCuqHN1sTJB/+xyl+fc13KWmhBDDsJzBcsRsluTkS0jS1gWIilWIA2L4+Ditv18bnW1 ENPnkee3p15wGt8F1RxO6Jsd+bGuBVwOYC0PhPKmnwSOd/9N2zRYR2WRYBXn4LabGpc2 jLHM6WhD77LuDrGHCbwZbCdMl5619h369sxu2wkDluUTiLzgKQnEWrKtl03hTTxUnsyV p7wi4wp8Buv3iEbJNyTD/gUnjinEJr0rWXx6CtO9P9+SQjicJOSAZsMsMHPw5U1SjnY0 wfeQ== X-Forwarded-Encrypted: i=1; AJvYcCWNNQev+DJRCUoKUWqpJAH2G2dZO1737sElDXtDT9/a2bC44eRnirNmY0gSUFzRjbC7J+7hR9sWbPPh/Ws=@vger.kernel.org X-Gm-Message-State: AOJu0YwS7PjH9WWq22ePsgguoC6p3RowTa/1rikWniIckCYa5i+2Hi/i uO3F0WR1LTiCvVdUAKg/hLpLYj0hzfkPzeWYaNw1e8aVeCTMhTeIWfOlhwLXeBcVQuz8H6NUQNQ 3 X-Google-Smtp-Source: AGHT+IG1zc8U83uSOzFrmbYgN2ib98c7nSSxX0OXroh1zj0tYW0kCZmpF+Q+MCRz16Jl9L5ftOfG7Q== X-Received: by 2002:a5d:688f:0:b0:37d:50e1:b3e1 with SMTP id ffacd0b85a97d-37d551b79c7mr10678598f8f.16.1728989796822; Tue, 15 Oct 2024 03:56:36 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:d382:b11b:c441:d747]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fc403d3sm1254115f8f.101.2024.10.15.03.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 03:56:36 -0700 (PDT) From: Bartosz Golaszewski Date: Tue, 15 Oct 2024 12:56:17 +0200 Subject: [PATCH v3 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: <20241015-gpio-notify-in-kernel-events-v3-5-9edf05802271@linaro.org> References: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@linaro.org> In-Reply-To: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@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=3446; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=Y2EyvkeErztbFdHhfdU6K77nhrwpBwdGEoPFbeXCRO4=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnDkpfUZHsriwZiPEi4+AtbBBPPnuAC4IFx9aqh ije0Wt6/+SJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZw5KXwAKCRARpy6gFHHX clJaEADNXX83lmyEuAlg/uVQXe0YjUABAvjrsojScMrEtoWdZSDyx3abkfblB33eq2VVnHtQiSg 6dffmdoYmT673m1HqbfH7A4LGS5Yu0KE0ePqUGJuOe19WLxfkDy6yODSOIHWHUx6snJwMfW+5fN 4BZDU1madoZqZM2TgEVzMx7q3VeCI0c7rdBe/IpIrbraCu6YFjcYjX97b4nPdnw6Y5EZfGRhRIE iyfl0vzceypA7tZp8k2MsiLOO6IhfxcvG0RZxxJQFmRPEI/0HTTjwCSe7cd7Iu6ZQp78g/snbuR C2FBMJ13NM0XyQlMApHY+v942NhXRZC6JLH92y6gIU4/GkqoGVxpRWxW6pZ4XB0b0xjXscKELQl ZVUrlZg8l583gH2o+DJ9MFU5Jgs7aB2IHyaqo147VqMcvRZhf9lMr8vM616FoKaI4Z/YNB2yQwh TYmMhsIIF6F1RGgF8rRRFBRk1L0rxfqXLQbYQKq4eT4Lwmhv/fRKU2YmaT3g/VpX6f93tkYEczX H/NXyII6Z22hrhUYieRuUpD3LwDt/cvdGYgM1J0FRdJze9N8NwAU/sydVLGzEcu8tozdtMfdX58 GhlRKALcQ+YmTtx9rSpPGb7VRsrvMRQILFCNDLLBOJy+jqB3CGprbL6dPI28vQn+GsbMTwNqTGm cCPjmtmWylBQLLw== 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 | 12 ++++++------ drivers/gpio/gpiolib.c | 6 +++--- drivers/gpio/gpiolib.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index a10d6ea22415..f8c69ef33888 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2848,8 +2848,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 @@ -2873,8 +2873,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); @@ -2898,8 +2898,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 c09464f70f73..fafa759ce743 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 From nobody Wed Nov 27 00:43:16 2024 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.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 80E351EBFF4 for ; Tue, 15 Oct 2024 10:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989802; cv=none; b=sC8SSFwX5C0TsBko/NR4UVnbpUjPY20ZPMbYtSrxEzZRCeNgdqMfb0ygQ1AsxlVpZHF8ukQDs3trAbEKIoxDhTwxkYMAOtoeLDca76PjliW8ve0z7fhmGWcEPXHuVz9/+Q9bTF0WU78XX0P3nCy2ESZ7f3UTr2C5GFWD3TYl/9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728989802; c=relaxed/simple; bh=3P6ed0aEY+FPZKTKm4aUbMLJLE88YJga0WdfcjryGRo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=taU6eQazPAMrabvq8ra/60cdNyvNEsoI2HebFagXssNsShNTWBEI4obRbHf7lmohetJsPalsjBMuRtAwWeFndYyNWcOkrd30UMuvj0rhQXAf17oTFNP254AAkI/Oj+9ugxSUgIL7ekssNv2aHVPuMU3pe05OTdqTuQ3ZBKMQJSg= 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=R4MDUNnZ; arc=none smtp.client-ip=209.85.221.54 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="R4MDUNnZ" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d4ac91d97so4515261f8f.2 for ; Tue, 15 Oct 2024 03:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728989798; x=1729594598; 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=pogTaxnIx9GVfNuRsM1CMn+dOQNZi7zNE6KFzO1rF9o=; b=R4MDUNnZ2emxVDEpF/HOL5mXozeFzOmC93PUrOd2GsNl4nbrqMwy1rRg0SRkheNQbk 6SrmAORbs0G9zFxK9gEt9Q8qTjNk3Svgo7z0X7Y7KljpWzUmYTHgQwaZuL4/su7l2rk0 Lrtg1QP/Q7ApBZmzZK9zADPrKx+rdvF87/4HioxzhDH+aimg92kyQfL41z0pc84fyEHQ 2kdhtW24Tk6+pVTJfeJvlhHe4iYCDrcdGlPhoNxbpBoiWp9mWHjDTigk5mLG9eNuiLc0 A3FSvgMInnZbXYRM3BvtZhZmpoCLGtzM8kDtnoDueF8FAb9HyRTojXplcPvow1S4hvh6 i8nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728989798; x=1729594598; 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=pogTaxnIx9GVfNuRsM1CMn+dOQNZi7zNE6KFzO1rF9o=; b=NvEcEQvBhNN6PYB/ILxpbGLkc4B25KfkJAhKaVbJMxHTculaBj5uIY3bC1VOiXdepc Pme2sRoGa7QH6A2Hu5lPC0Xz27PcKvgyTejYx8ce4HsMGcOL9f+Er0fI1X5IHTHT2wfR 0ihrHyttXqSjZwcn8cS3tmoX8kmw+lRjtBENx4kl67thK89xfzP+nao8K4fYNXudVJvd fmEDSOLOwGhA9AjCrv9OQTeV11KmSH6dY/MBdea3gnsDKys9X6sZ+sAaeaT4mb26k1FZ G4Bf09msWic51VqnUVplgoLEWDHpWnVPFw49cldQAm63HZMyuoTW0iw0uM85aqB+bdzM rv9g== X-Forwarded-Encrypted: i=1; AJvYcCW6QARoAI7AXCuiCN9lE7Ooey4i+l0iY7po8sshbRtL+SGVUbWpcvLqCAIVK/0bmh8tfYMkTCH6FnXM4sY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyi7oXFyPB3wxoC/UyVl5vkF5BOzLO/xcsdKfY/J+HvzwoI1cgm tKvvxXQBB+GvcDVVUeQY00Khzfc55frWqZG9c8oxNM4reohv9nRh2FvWl0ck32KJdfvoqPZmtlK G X-Google-Smtp-Source: AGHT+IHaQcsmINroBS/oshLFsL0DyQiCXt+piKC0psvps1tXB407wVpMCndVTrUzB5tU93TFFAtIqw== X-Received: by 2002:a5d:4f85:0:b0:37d:4833:38f5 with SMTP id ffacd0b85a97d-37d5ffa2e1dmr9783000f8f.30.1728989797970; Tue, 15 Oct 2024 03:56:37 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:d382:b11b:c441:d747]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d7fc403d3sm1254115f8f.101.2024.10.15.03.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 03:56:37 -0700 (PDT) From: Bartosz Golaszewski Date: Tue, 15 Oct 2024 12:56:18 +0200 Subject: [PATCH v3 6/6] gpiolib: notify user-space about in-kernel line state changes 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: <20241015-gpio-notify-in-kernel-events-v3-6-9edf05802271@linaro.org> References: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@linaro.org> In-Reply-To: <20241015-gpio-notify-in-kernel-events-v3-0-9edf05802271@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=7877; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=ePQ3NHmW799l+7FWll3i7+jrGwxa4SiDEOy93jWL4Mg=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnDkpfGeH1iFc5qP7f9JLNiUYAs12V185a8duPl 9uc5wXnvYeJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZw5KXwAKCRARpy6gFHHX cuckD/43OV4paP5znbk19rt07BJUmCyQe78m0AZFagLlQ9CibdcxoXAFh6lObSZxz6pshvthQ5G 7Z69tOPXueqtYRhVQLD52Z2nw2puiBCpmHDMLnsOO9Y4ZfJdrXYG6P3HXf5Fpf6s+xgoquUtGhj DKdnvisZDH5jZdsfxCbhFknQ5ORR2s6VUK3pxRTCcrHI7n8vObqRpZtKAUwJv21UNPoB21Vd1Dj wPv+TX/CuxUWvttrkcXqOu6vLsnwnagYyjrCgUTB4aFcf4jEx52GKHIajQrgZeVAcPAbmEXNtOn 0c3TL6+VV/wxVosNGNk+zkcg0TvPx5gIKFkwO1DGzr1arYeFSFsP4fIIsob9FwAwM49ybJM6s08 tsSGRWT7ZL0hSeirsrWMn41uSk+tKBEql5YJBxnvrGdBfHpVIMl+xNZ74/PHsnqozJLL0TFsMiu dnCrOiUBk2k5MXELo4io9HGuv2wwU77zF/iUgBEhFj5m2kt/R/7q7Ha7W58kMOqlNd7cybeY+tQ QSP902mbwJ/wMTSdxUaZWIg1xlLFHaTFA5PrlWNLaU7pEuD8o4wIbhNWztl/hvNbmkDWfi/7w1i Nv3EwTsloDKJ7p7uHv+h42R+qwRVxq5qjBmTPSfuGJp5U76mngTR1rguchSklS8wH3fGgm92R8E MLhl9ASXI76L8fQ== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski We currently only notify user-space about line config changes that are made from user-space. Any kernel config changes are not signalled. Let's improve the situation by emitting the events closer to the source. To that end let's call the relevant notifier chain from the functions setting direction, gpiod_set_config(), gpiod_set_consumer_name() and gpiod_toggle_active_low(). This covers all the options that we can inform the user-space about. We ignore events which don't have corresponding flags exported to user-space on purpose - otherwise the user would see a config-changed event but the associated line-info would remain unchanged. gpiod_direction_output/input() can be called from any context. Fortunately, we now emit line state events using an atomic notifier chain, so it's no longer an issue. Let's also add non-notifying wrappers around the direction setters in order to not emit superfluous reconfigure events when requesting the lines as the initial config should be part of the request notification. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 12 +++----- drivers/gpio/gpiolib.c | 71 ++++++++++++++++++++++++++++++++++++++++-= ---- drivers/gpio/gpiolib.h | 2 ++ 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index f8c69ef33888..1296e6cbcef7 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -196,8 +196,6 @@ static long linehandle_set_config(struct linehandle_sta= te *lh, if (ret) return ret; } - - gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); } return 0; } @@ -363,11 +361,11 @@ static int linehandle_create(struct gpio_device *gdev= , void __user *ip) if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { int val =3D !!handlereq.default_values[i]; =20 - ret =3D gpiod_direction_output(desc, val); + ret =3D gpiod_direction_output_nonotify(desc, val); if (ret) goto out_free_lh; } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); if (ret) goto out_free_lh; } @@ -1568,8 +1566,6 @@ static long linereq_set_config(struct linereq *lr, vo= id __user *ip) } =20 WRITE_ONCE(line->edflags, edflags); - - gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); } return 0; } @@ -1826,11 +1822,11 @@ static int linereq_create(struct gpio_device *gdev,= void __user *ip) if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { int val =3D gpio_v2_line_config_output_value(lc, i); =20 - ret =3D gpiod_direction_output(desc, val); + ret =3D gpiod_direction_output_nonotify(desc, val); if (ret) goto out_free_linereq; } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); if (ret) goto out_free_linereq; =20 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index fafa759ce743..4303b6a689af 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2673,6 +2673,18 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc= , unsigned int debounce) * 0 on success, or negative errno on failure. */ int gpiod_direction_input(struct gpio_desc *desc) +{ + int ret; + + ret =3D gpiod_direction_input_nonotify(desc); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; +} +EXPORT_SYMBOL_GPL(gpiod_direction_input); + +int gpiod_direction_input_nonotify(struct gpio_desc *desc) { int ret =3D 0; =20 @@ -2720,7 +2732,6 @@ int gpiod_direction_input(struct gpio_desc *desc) =20 return ret; } -EXPORT_SYMBOL_GPL(gpiod_direction_input); =20 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int v= alue) { @@ -2782,8 +2793,15 @@ static int gpiod_direction_output_raw_commit(struct = gpio_desc *desc, int value) */ int gpiod_direction_output_raw(struct gpio_desc *desc, int value) { + int ret; + VALIDATE_DESC(desc); - return gpiod_direction_output_raw_commit(desc, value); + + ret =3D gpiod_direction_output_raw_commit(desc, value); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); =20 @@ -2801,6 +2819,18 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); * 0 on success, or negative errno on failure. */ int gpiod_direction_output(struct gpio_desc *desc, int value) +{ + int ret; + + ret =3D gpiod_direction_output_nonotify(desc, value); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; +} +EXPORT_SYMBOL_GPL(gpiod_direction_output); + +int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value) { unsigned long flags; int ret; @@ -2863,7 +2893,6 @@ int gpiod_direction_output(struct gpio_desc *desc, in= t value) set_bit(FLAG_IS_OUT, &desc->flags); return ret; } -EXPORT_SYMBOL_GPL(gpiod_direction_output); =20 /** * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds. @@ -2942,13 +2971,34 @@ EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns); */ int gpiod_set_config(struct gpio_desc *desc, unsigned long config) { + int ret; + VALIDATE_DESC(desc); =20 CLASS(gpio_chip_guard, guard)(desc); if (!guard.gc) return -ENODEV; =20 - return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); + ret =3D gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); + if (ret =3D=3D 0) { + /* These are the only options we notify the userspace about. */ + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_BIAS_DISABLE: + case PIN_CONFIG_BIAS_PULL_DOWN: + case PIN_CONFIG_BIAS_PULL_UP: + case PIN_CONFIG_DRIVE_OPEN_DRAIN: + case PIN_CONFIG_DRIVE_OPEN_SOURCE: + case PIN_CONFIG_DRIVE_PUSH_PULL: + case PIN_CONFIG_INPUT_DEBOUNCE: + gpiod_line_state_notify(desc, + GPIO_V2_LINE_CHANGED_CONFIG); + break; + default: + break; + } + } + + return ret; } EXPORT_SYMBOL_GPL(gpiod_set_config); =20 @@ -3015,6 +3065,7 @@ void gpiod_toggle_active_low(struct gpio_desc *desc) { VALIDATE_DESC_VOID(desc); change_bit(FLAG_ACTIVE_LOW, &desc->flags); + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); } EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); =20 @@ -3659,9 +3710,15 @@ EXPORT_SYMBOL_GPL(gpiod_cansleep); */ int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) { + int ret; + VALIDATE_DESC(desc); =20 - return desc_set_label(desc, name); + ret =3D desc_set_label(desc, name); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); =20 @@ -4539,10 +4596,10 @@ int gpiod_configure_flags(struct gpio_desc *desc, c= onst char *con_id, =20 /* Process flags */ if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) - ret =3D gpiod_direction_output(desc, + ret =3D gpiod_direction_output_nonotify(desc, !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); else - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); =20 return ret; } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 2799157a1f6b..fc321d281346 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -155,6 +155,8 @@ int gpiod_set_array_value_complex(bool raw, bool can_sl= eep, int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); =20 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); +int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value); +int gpiod_direction_input_nonotify(struct gpio_desc *desc); =20 struct gpio_desc_label { struct rcu_head rh; --=20 2.43.0