From nobody Tue Nov 26 11:49:26 2024 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 BF0CD18FC9D for ; Fri, 18 Oct 2024 09:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242625; cv=none; b=TJ8gjgHURaydK2+LWTWrVKFBgWKC1HAxJ9s5sdnfgPRnf9Eeij0w2XS7rtQRIprIPMAZU7qx5IMhL/4XIj5ojn4Xal4qpB/Vo2Gb0pvjxN2EPzw+NRXjgvkJ9/z7N/wmDocFszABtY4bTt/NY+XwfahuRnp22NOsmG9G6oNJjcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242625; c=relaxed/simple; bh=POHh8g+2CvyZoU0L+E1KmaYOw5grZB9aKoaGo7YEz6Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aw1nNcLTd1x5FGRQBgrYY/0CkCB1Q97OXljolN5U5H6Cxvt/4GNZ48oq7cykSiExym4UMjid1uDTDbud0r4TF8xSItLeJUjo1XYLo9IfLolyaJSyEyc9JSA3HG7mamWi0p9qDgzTYMe5kHoVTQdirA4YvK2YJrLkp1KKqXkUuYA= 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=cmc6gHlW; arc=none smtp.client-ip=209.85.128.43 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="cmc6gHlW" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4315eeb2601so13979985e9.2 for ; Fri, 18 Oct 2024 02:10:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242622; x=1729847422; 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=cmc6gHlWIwopCwS5AKMP/V9VXtEuVuzDAepr8P3rRc8ZSL3liQUVbja3tHNHKVl9w1 dIjcMNr4Ll2jGa+p80EA1Q1YE0JINAeCqV7ved5sKmaGQLXb8aAR5CqabHgHFnaiafQ3 MFcjx/l6aEMinUSfAXRcc70DmJ5K2jbF97Q79xPE4c2xGuVv7b2olLVYPyoTpl1Ldo8H XIiOWTGAvgI2LmpHwH5I3DofzWjU/WeGQYHgOMr9p2s3BS2JsfSYfmK1tRMmaCJNuolL 8wIUqEUnfVXcL+QK427z5BUobmTL6w192nehJZMXlQeF3jeWc1pccvGka55T319wFDwD JOWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242622; x=1729847422; 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=BgmuNw5JbUAlCB2RTdbdQYanXfi0jkiUF9eqxfnJlmi0QA3UrMV8ta5g2Sws9VH+Bf fmFfRyQvoUERR5pSglAVKQBrJqmn8lnmBexgbgqxclNIn03dfugCNVCnGe4ZCS3aCAJq lSR5vY7FSHCgb5p2eJcSunWviK5kB4DIcl+Ci5MGXtlGJLXaadG+WoER9biwS5Llu1ZC y0VeuddixU8GL3kYHQYF32EFGeQpIdEkkIy+ELQUIA1VsWoIPXzFHGlZTLjnZQ8LJEwa QpbBRgUa5Gw4l7Ui4+bvgx3U0iKZoluYKubiw2RrR3WPE7AsYNLt+JPqfnN1jXfz3P2u zSHA== X-Forwarded-Encrypted: i=1; AJvYcCUi4+YRzeXsUzrDEZH/1iM16c832tlN44gOpF7MdeXwVKAm7m9qH8wqISGYyrbE8e5Yg0h/QFBlewHD0CU=@vger.kernel.org X-Gm-Message-State: AOJu0Yxr9IkopUch/ZyB5v1cyFGWNnuxf4AyI1wWSJM5HngBKGkQI25E 0HWQxAGxKRHw2aay/gpIyhsNmBQi6kOpG2EzO3X3s2pdjzxdO6pikw2po1/pLhnHgedGMPZRKkZ U X-Google-Smtp-Source: AGHT+IEVba/x2vfrV7MnhVpSziwVemnsNgRs635JXjcpaYVImGwzKfYkyV2Nszc2wVRKjFRC0DN47g== X-Received: by 2002:a05:600c:34d4:b0:42c:bae0:f05f with SMTP id 5b1f17b1804b1-4316163664emr15360435e9.13.1729242621898; Fri, 18 Oct 2024 02:10:21 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:20 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:09 +0200 Subject: [PATCH v5 1/8] 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: <20241018-gpio-notify-in-kernel-events-v5-1-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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/ZANAwAKARGnLqAUcddyAcsmYgBnEiX5N1KXBf9WUY2Laz9GAVO5bnVkySdSQ0wcp xnhXgiresGJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+QAKCRARpy6gFHHX cuhFD/4sdt+m349SvCqaLbgaJzhOalvyDBQBKP2uSU7rBKAUl7KbPkaSEm3cbHfiflIEGGSd10c Rm1KJP0+D8sztYsthxt8cScGxvroiwGs9FrMI/yNNirMviUpHP4z6LvEJ72Gxy1bZE0GABTvHAW wIHegWJmVM9VrPbJMnq5a22rOHdjGgnnApN18HDpK+Hr0fqVNLaPHYs82i8pLFLr+AFHr1lcbQj fyZ7P/eTQoIo9+JlCWDtSRggkCeKKToWsdspsR846GeX9BLZWVecfxIBBLlMKugugc/ctlGhWRr 3vbM/xOjLoo2szCNwFqbFm+ig68Jzh2V2O8vww1LZnBbI03MQIcQ7lTwMDbElv5z1YgjR9xB11V ssmQPQ1KBZVeaTMgI8UgbJQwIdph6U0Ox1Xso+C8cqsCr5mqxzg6Fd83GVyG6MOL4um5xFMeXz0 jRgV1sr5rJCi0YMB05eq8spzYSIkGrqjo0DCwQZCqTU4EZAei4kmAaL5s27xrH3NdF/5zC0Fjzy weCgbtc+BMGmMlBMwbiyyxaLrI+fhK08+M5QUIRN4J/khV4/9JydKA/iKGmLyEmnXfiN+51tlTy 6w7Vt6QfuXEbUxDfKaJDPqxeywj4c7Oosb9hq1e62dd+Lt6azJZDe1YSZ/VUICBAL2YgeQkdwf4 1c5VFIvaP5fSC8w== 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 Reviewed-by: Kent Gibson --- 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 Tue Nov 26 11:49:26 2024 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 32ABE18C92E for ; Fri, 18 Oct 2024 09:10:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242626; cv=none; b=QdIDpTSl5GNM8LZIdZ6Y8DVE/UCIW8JZSrhgAuwXNG3LQpEWqAX2Ti5Mt28fG4YoV6tYRkRCmH0Qy+SV0jP9k0NrkNydUHMPcReG6XW8wQXau9W7sHNGzCz4yc00rkyeO1jWQV4gydCttdGwHOz/x448BJlVCS1MTREf79AN/+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242626; c=relaxed/simple; bh=ND47jIbOYDmJs8MpSoNGSSJKSen6G5z88AQQSxF1llA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sODSFEWNBwk9QwmMkSrzcOhb3fC7LReZ6nNVOAZSCdBxMdH2tFr7/cYuoGFc7tFFW4nj+2xSt2C/8QbpI02sLcdMdhd3bVybZNRVHUyd0wftnEsfFsHiAAnRhf07dfYQkpoRAwIxWuJWtwAKyU5RotBGZheo7r6gsibqENwhnRk= 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=xjCahh9M; arc=none smtp.client-ip=209.85.128.52 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="xjCahh9M" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4314b316495so17862965e9.2 for ; Fri, 18 Oct 2024 02:10:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242623; x=1729847423; 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=sLpytUujQtrQRxKwGItE9MBF3PBkf2ocRMe5UxADIfc=; b=xjCahh9Mst/98YF9esMi2Cfz7eLRZoT41B5yhFSnamT/qQMD62YNHBNohtmlqsDFzW G4IMDo6pazOLl5LVmaBjNGUFKxqJhXWr/MojPF7Y7pQKyIkqBCqkdfkLLjKzB6Phngb2 Jn2hu6pIu3MjjMb7Ojbn1e/cX81HkXtXBewKkvnMYy+ip1rrNOnySQDOdV/etI0nk050 r4AFCtLJCIkZL2ILYla6snUwwYMaakmcetqN0jit6xyhPiszVOhY7y2IxjpWUc86HpMu qzns6+SIC6d02b1vD4rNHDG31DQgkvZ3RFcs3R1XqRiFgnCikUs3JrL5hTKTDResPpPN haZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242623; x=1729847423; 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=sLpytUujQtrQRxKwGItE9MBF3PBkf2ocRMe5UxADIfc=; b=WAR60m7fbJjzEnHmw+IMiriLLcQ/ZyTMxtLE9JILfLGjdPQmYZB00nIaMniE4HWy3I NGbLxwn37AqNlEAkifXQ2TohUJtW6MG/UsztC3x9aqTW+uxpA+hZbH4SHneqtPBct7bE 4IY3mR3FMfspXY2gaxPSwQvujNlPb7gdDr+QCooShs4vRQ+GyrUXVAJL6I947gEUgf/B /406vXz7eqxPKcXVz/m+6qMa16lkMVDSLFvDGz2yxvFQfzYo6cV6mBfGcERtiV47bg6h W1ruex1YWAfDvv5M+ivwbP0AF9l3dn3kS1EC6O7Sj/rJDQT21zTVrzWoGdV0YawdsL+n APdQ== X-Forwarded-Encrypted: i=1; AJvYcCX9LKzp8afMlNlGusBgB0VaRNRODtzQpOlUlwIx5I4fEavUUVoPVmHUy5qg0ELZKIpOuyQx9B5gWv7CAG8=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1DbjwoH5L2kuXSO9IELdHVgeyYSzA5Nz1eOPFXHqb6qILGBrM CXtDLSigKp9w0xpa/Vt4MVrHLXsFqtGfDlfuiBIMBGpkjJP3y8vfC4iuZ+1MA8o= X-Google-Smtp-Source: AGHT+IFCMIdzGCoR1Stri6zpATjzmnSwe19p+msFa8rRRyEbXbIPNr2a35ZEDvgCQun0IL97qDF2fw== X-Received: by 2002:a05:600c:4ed3:b0:431:4e3f:9dee with SMTP id 5b1f17b1804b1-43161636fcbmr10386125e9.4.1729242623517; Fri, 18 Oct 2024 02:10:23 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:23 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:10 +0200 Subject: [PATCH v5 2/8] gpiolib: unduplicate chip guard in set_config path 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: <20241018-gpio-notify-in-kernel-events-v5-2-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=2395; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=5bkoQOmE+e79NCUZkE2hkqB3kJJTpG+Rd7AZwxRE2E4=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX6hXHkiMrjbf6lTzXJR07uV8mFVAuQTiD5k kOzCzc77sCJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+gAKCRARpy6gFHHX cnBUEACPPBwp33JScr/PgbuGXaIiVAxqhCZaIQ5u5075zGxd9ymdgB6nmxIyvgRR5A9aUUZBESV FAWuvQjjPSGymT6U95dTe10VtsRWbCtrPnazCZ8IKaCRIas7z4mWSu1GPu+Au0DjUKAJWEvnK4s Uhr6mexIVi1DsHUwBkCmkqWJ19Iz/qESf3J5V3hT9F0bYoNfZZb6s8KjYwU+3XJi7i7Z9lhEL7p xNDANoQydhU0nXe4rDk0biFYdanzXYWayZjZIXjWhlR16GEEVo6onB9dgVmPFGr3koK1Kv+OHEp 4Fdgpr0MMjSi+i/RPBD0VmGFmz7Q9kR2xzzaI40uwFr4iPK/5mVWAkKPtknuq2FcGlk1wHp61sI GqfCIIliq5JiSwRy3dz1+ycalnPDB6xT7rnDVkhG0+Qe4hP9Au3J81csOFXY1S5NIBAC9I7Un2Z YWT/VkrPiWuzac9cQKjdIY6PqfHmUObL543xt5BhnBTATghihdaAQZozUKIIvt92OruUrV/G6kF H/uON1F6FeluHOCas45NTvykxUybKprxAvVaiBX1iB2ak0mXJ97nM4sV5L2mjIZMAMTGVEqwSty qaLMQMf9uOz6iwLTF/74utIQNLyaq8JEqo6vt9gusstbwMprSMVuarehIoQjuvqjoFMf1UmjrYw oSV6LiLrnG3Jllw== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski We don't need to guard the GPIO chip until its first dereference in gpio_do_set_config(). First: change the prototype of gpio_do_set_config() to take the GPIO line descriptor as argument, then move the gpio_chip protection into it and drop it in two places where it's done too early. This has the added benefit of making gpio_go_set_config() safe to use from outside of this compilation unit without taking the gdev SRCU read lock and will come in handy when we'll want to make it available to the character device code. Signed-off-by: Bartosz Golaszewski Reviewed-by: Kent Gibson --- drivers/gpio/gpiolib.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index c09464f70f73..b1ce213d3a23 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2562,13 +2562,16 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); * rely on gpio_request() having been called beforehand. */ =20 -static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset, - unsigned long config) +static int gpio_do_set_config(struct gpio_desc *desc, unsigned long config) { - if (!gc->set_config) + CLASS(gpio_chip_guard, guard)(desc); + if (!guard.gc) + return -ENODEV; + + if (!guard.gc->set_config) return -ENOTSUPP; =20 - return gc->set_config(gc, offset, config); + return guard.gc->set_config(guard.gc, gpio_chip_hwgpio(desc), config); } =20 static int gpio_set_config_with_argument(struct gpio_desc *desc, @@ -2577,12 +2580,8 @@ static int gpio_set_config_with_argument(struct gpio= _desc *desc, { unsigned long config; =20 - CLASS(gpio_chip_guard, guard)(desc); - if (!guard.gc) - return -ENODEV; - config =3D pinconf_to_config_packed(mode, argument); - return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); + return gpio_do_set_config(desc, config); } =20 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc, @@ -2944,11 +2943,7 @@ int gpiod_set_config(struct gpio_desc *desc, unsigne= d long config) { VALIDATE_DESC(desc); =20 - CLASS(gpio_chip_guard, guard)(desc); - if (!guard.gc) - return -ENODEV; - - return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); + return gpio_do_set_config(desc, config); } EXPORT_SYMBOL_GPL(gpiod_set_config); =20 --=20 2.43.0 From nobody Tue Nov 26 11:49:26 2024 Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.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 EA2B719049D for ; Fri, 18 Oct 2024 09:10:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242629; cv=none; b=mAo4Om3s/iKex/wQeJaPopBzejn4zKO5sTeedj4gHJZhIuXeqrmOHf5WTaB6rXLDpuxzg9Jjs7DMefurd34Q0namBE4BjaKs+qDi9ZB3w7FA31+QfRncBl2a1b1AIs8sWDl+MR4dSp6eV9zLoedQI52Xgcm+jGsnvd58gCf7CE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242629; c=relaxed/simple; bh=Y6ydoHY2HpsK8/L2hWNEthXCW5yOTtkkUFG5ZyOBLHI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g5N71NdVtp/NetaKLoSKHMtdCPGYLDIvmQfEsszelXDoLMpfyHU6x8xHGwy24xeiHUjxXmozDa1xrmnehLng8uonu1+mh+7opay2tHbhU0M1jFMnMynleF/HmMzagZiOxiN7DX93TgRbvSEoEs2PcIbZat4HrYGoihYJDpvbPAc= 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=SwJTnMK9; arc=none smtp.client-ip=209.85.167.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="SwJTnMK9" Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-539fe76e802so2312486e87.1 for ; Fri, 18 Oct 2024 02:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242625; x=1729847425; 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=lNIOspkDRS2AyiERRhXLD4LKdJKYWTfCk5EQAGqlBPE=; b=SwJTnMK9GCVVi2urUKXFUxH6dU86hoBallKMPRIyp5lkPST9Bx2w5Ufzytk+1zDFQE 4rYqI3gTSVwOY4W3WPwP1f1ug0ZweaYVdKJtYfHhXJ8XWjw+9zbb7SLxnnhmgVNcu0Qk nEv/iKStu6aT5ez6yr0zlDqXqlxPT7SZUsmLCHOMFVj2E1ix3+cnLV8xBdW47XHh4FjG omPtJ/87P780/DP6ftSmKf3YhrO9WsD1jc+ZFiVAYmg9cEHiEyAERCTlWYM6wxkG18We 6kyX4zLWKpcuQQ4X7Uub2PIN3DkjPP8Erv1fnRU1T/aTD0rqGn3STiKPPAZKydO9Tu3l toHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242625; x=1729847425; 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=lNIOspkDRS2AyiERRhXLD4LKdJKYWTfCk5EQAGqlBPE=; b=syi0hzwlldyHnqS1JMIonf1ZCbcKOX3v6xVo4kPVR427E+oA9VYXoRkpJZOAHD4V15 qXb0QGZNNMnPuWG7i6pwGXgVwLjWEQS520zR6FG+BA0wKGkv1w+x/illgFc682pkHkC6 hhC4AJ7yZ8oFM/l9k+eaVxf7sTPa29WCPvdMW4XkBLvoG3CRm/EhKzPBmp7dwSmQgdRI J+C+rBzsRt+MYmBo52YT0G7wewDYR2vbqmAj0g+GvB6PiepIyxPrgVuKUyjpZinLejwS H6VRuW3hxn4/0L/S2c29DXAoKMQOyR4PCvZ11IOdshFyTHpdhYl1NVn+KXQIWg1cuuao 0aAQ== X-Forwarded-Encrypted: i=1; AJvYcCV0qWgEaeTqgCTblz4ZQrbSDpsstIGC8K9TWQW2bloZqaZRbMEdLXxE02lOh9rsMolP1f72ZTqJl/8n+mk=@vger.kernel.org X-Gm-Message-State: AOJu0YxN8tqO9TE56bEoAsB2HFjjkN7O+E5C58Xw3GIP1Ja3ZbRKIsyH ZnsX1Ef+0Ftq3uw5HYmezm3B1qgkI3d5iOJZIoRfouFIBWeBBTAGgyQDUKZq3NY= X-Google-Smtp-Source: AGHT+IG9lDr7420PifdaZORYUPCkkifw/UqTKWLCb1V9LIKLZap4dBL495YpWfETodO3++FTjOGugA== X-Received: by 2002:a05:6512:108f:b0:538:9e40:94b with SMTP id 2adb3069b0e04-53a1546c877mr1068202e87.19.1729242624865; Fri, 18 Oct 2024 02:10:24 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:24 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:11 +0200 Subject: [PATCH v5 3/8] gpio: cdev: go back to storing debounce period in the GPIO descriptor 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: <20241018-gpio-notify-in-kernel-events-v5-3-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=13582; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=KMwpLTqbSzLk7Bonso7QQ+aG3uhStA029vtPSa2jEoU=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX6IzJTOuYbONcHyrBCFHVgpJd3a/0FaltnG 268oSgDitCJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+gAKCRARpy6gFHHX crw0D/46uyZzdn87PjkdoBPDF+HFpXlUofAy/WuVwbjo+7rdY3i0MYj2KcoX3OnsF7nGyRooads RBYGHo7EtGwuGR4O7+T/ljU/ExDLYOCJpBL/b0wvjftUo8adDqpDEHAhhfN65ToKVIpTq9OfMCt 3e6FaD6sM2jEeR6PwztBo2hhPhqeT2teqUKqHhRm6rQ0ItHIJcw3Y41xya3sHwXfmYBW5TVel1s Qxrom8EIcNpwPqk+Y0ZG2l+OPtz2T2ExuyxWenFsxLPULzFAjEsHaIBzbLaoj7GXf56cPEvyv8W rVVTUt64jTrYAYwkLrIq0dgs0nchdsmesOEAmI6wnjVC8uEYX6GfU/6HsqKI1QTl0i7wfdZatqy psFJ79cTVMI0atsInmeZoBpw0y6wOLII48v0suTyX4OiZjk8NhAlChGZZZIIfTpf0KCANK8xa4H ujdQlAmrxduMbDUGElWJM6JrI2SmhHFw1R04ssiCOibvNV1TM20UNNB48b6MFF9jZUg7Qu1//NW aGe7k7u38cxl8Pp0gGIzD21W6L4UItJccYJTWOnDoPo7c1QejkVomUi5rnaWUQt0J9aF7PFl6E8 pudRa+lxOjgPKnJDIkAnfq9hgTUhLStqrh2/nGfMFk2idEIN1LvJXtacZVk3scIvLUwNVA/g6t7 yLnn5rR0eNTm6+Q== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski This effectively reverts commits 9344e34e7992 ("gpiolib: cdev: relocate debounce_period_us from struct gpio_desc") and d8543cbaf979 ("gpiolib: remove debounce_period_us from struct gpio_desc") and goes back to storing the debounce period in microseconds in the GPIO descriptor We're doing it in preparation for notifying the user-space about in-kernel line config changes. Signed-off-by: Bartosz Golaszewski Reviewed-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 163 ++++++----------------------------------= ---- drivers/gpio/gpiolib.c | 18 ++++- drivers/gpio/gpiolib.h | 5 ++ 3 files changed, 43 insertions(+), 143 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index b0050250ac3a..d55d2a246d41 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -421,7 +420,6 @@ static int linehandle_create(struct gpio_device *gdev, = void __user *ip) =20 /** * struct line - contains the state of a requested line - * @node: to store the object in supinfo_tree if supplemental * @desc: the GPIO descriptor for this line. * @req: the corresponding line request * @irq: the interrupt triggered in response to events on this GPIO @@ -434,7 +432,6 @@ static int linehandle_create(struct gpio_device *gdev, = void __user *ip) * @line_seqno: the seqno for the current edge event in the sequence of * events for this line. * @work: the worker that implements software debouncing - * @debounce_period_us: the debounce period in microseconds * @sw_debounced: flag indicating if the software debouncer is active * @level: the current debounced physical level of the line * @hdesc: the Hardware Timestamp Engine (HTE) descriptor @@ -443,7 +440,6 @@ static int linehandle_create(struct gpio_device *gdev, = void __user *ip) * @last_seqno: the last sequence number before debounce period expires */ struct line { - struct rb_node node; struct gpio_desc *desc; /* * -- edge detector specific fields -- @@ -477,15 +473,6 @@ struct line { * -- debouncer specific fields -- */ struct delayed_work work; - /* - * debounce_period_us is accessed by debounce_irq_handler() and - * process_hw_ts() which are disabled when modified by - * debounce_setup(), edge_detector_setup() or edge_detector_stop() - * or can live with a stale version when updated by - * edge_detector_update(). - * The modifying functions are themselves mutually exclusive. - */ - unsigned int debounce_period_us; /* * sw_debounce is accessed by linereq_set_config(), which is the * only setter, and linereq_get_values(), which can live with a @@ -518,17 +505,6 @@ struct line { #endif /* CONFIG_HTE */ }; =20 -/* - * a rbtree of the struct lines containing supplemental info. - * Used to populate gpio_v2_line_info with cdev specific fields not contai= ned - * in the struct gpio_desc. - * A line is determined to contain supplemental information by - * line_has_supinfo(). - */ -static struct rb_root supinfo_tree =3D RB_ROOT; -/* covers supinfo_tree */ -static DEFINE_SPINLOCK(supinfo_lock); - /** * struct linereq - contains the state of a userspace line request * @gdev: the GPIO device the line request pertains to @@ -542,8 +518,7 @@ static DEFINE_SPINLOCK(supinfo_lock); * this line request. Note that this is not used when @num_lines is 1, as * the line_seqno is then the same and is cheaper to calculate. * @config_mutex: mutex for serializing ioctl() calls to ensure consistency - * of configuration, particularly multi-step accesses to desc flags and - * changes to supinfo status. + * of configuration, particularly multi-step accesses to desc flags. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq { @@ -559,103 +534,6 @@ struct linereq { struct line lines[] __counted_by(num_lines); }; =20 -static void supinfo_insert(struct line *line) -{ - struct rb_node **new =3D &(supinfo_tree.rb_node), *parent =3D NULL; - struct line *entry; - - guard(spinlock)(&supinfo_lock); - - while (*new) { - entry =3D container_of(*new, struct line, node); - - parent =3D *new; - if (line->desc < entry->desc) { - new =3D &((*new)->rb_left); - } else if (line->desc > entry->desc) { - new =3D &((*new)->rb_right); - } else { - /* this should never happen */ - WARN(1, "duplicate line inserted"); - return; - } - } - - rb_link_node(&line->node, parent, new); - rb_insert_color(&line->node, &supinfo_tree); -} - -static void supinfo_erase(struct line *line) -{ - guard(spinlock)(&supinfo_lock); - - rb_erase(&line->node, &supinfo_tree); -} - -static struct line *supinfo_find(struct gpio_desc *desc) -{ - struct rb_node *node =3D supinfo_tree.rb_node; - struct line *line; - - while (node) { - line =3D container_of(node, struct line, node); - if (desc < line->desc) - node =3D node->rb_left; - else if (desc > line->desc) - node =3D node->rb_right; - else - return line; - } - return NULL; -} - -static void supinfo_to_lineinfo(struct gpio_desc *desc, - struct gpio_v2_line_info *info) -{ - struct gpio_v2_line_attribute *attr; - struct line *line; - - guard(spinlock)(&supinfo_lock); - - line =3D supinfo_find(desc); - if (!line) - return; - - attr =3D &info->attrs[info->num_attrs]; - attr->id =3D GPIO_V2_LINE_ATTR_ID_DEBOUNCE; - attr->debounce_period_us =3D READ_ONCE(line->debounce_period_us); - info->num_attrs++; -} - -static inline bool line_has_supinfo(struct line *line) -{ - return READ_ONCE(line->debounce_period_us); -} - -/* - * Checks line_has_supinfo() before and after the change to avoid unnecess= ary - * supinfo_tree access. - * Called indirectly by linereq_create() or linereq_set_config() so line - * is already protected from concurrent changes. - */ -static void line_set_debounce_period(struct line *line, - unsigned int debounce_period_us) -{ - bool was_suppl =3D line_has_supinfo(line); - - WRITE_ONCE(line->debounce_period_us, debounce_period_us); - - /* if supinfo status is unchanged then we're done */ - if (line_has_supinfo(line) =3D=3D was_suppl) - return; - - /* supinfo status has changed, so update the tree */ - if (was_suppl) - supinfo_erase(line); - else - supinfo_insert(line); -} - #define GPIO_V2_LINE_BIAS_FLAGS \ (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ @@ -823,7 +701,7 @@ static enum hte_return process_hw_ts(struct hte_ts_data= *ts, void *p) line->total_discard_seq++; line->last_seqno =3D ts->seq; mod_delayed_work(system_wq, &line->work, - usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); + usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); } else { if (unlikely(ts->seq < line->line_seqno)) return HTE_CB_HANDLED; @@ -964,7 +842,7 @@ static irqreturn_t debounce_irq_handler(int irq, void *= p) struct line *line =3D p; =20 mod_delayed_work(system_wq, &line->work, - usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); + usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); =20 return IRQ_HANDLED; } @@ -1047,7 +925,7 @@ static int debounce_setup(struct line *line, unsigned = int debounce_period_us) /* try hardware */ ret =3D gpiod_set_debounce(line->desc, debounce_period_us); if (!ret) { - line_set_debounce_period(line, debounce_period_us); + WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); return ret; } if (ret !=3D -ENOTSUPP) @@ -1132,7 +1010,8 @@ static void edge_detector_stop(struct line *line) cancel_delayed_work_sync(&line->work); WRITE_ONCE(line->sw_debounced, 0); WRITE_ONCE(line->edflags, 0); - line_set_debounce_period(line, 0); + if (line->desc) + WRITE_ONCE(line->desc->debounce_period_us, 0); /* do not change line->level - see comment in debounced_value() */ } =20 @@ -1165,7 +1044,7 @@ static int edge_detector_setup(struct line *line, ret =3D debounce_setup(line, debounce_period_us); if (ret) return ret; - line_set_debounce_period(line, debounce_period_us); + WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); } =20 /* detection disabled or sw debouncer will provide edge detection */ @@ -1213,12 +1092,12 @@ static int edge_detector_update(struct line *line, gpio_v2_line_config_debounce_period(lc, line_idx); =20 if ((active_edflags =3D=3D edflags) && - (READ_ONCE(line->debounce_period_us) =3D=3D debounce_period_us)) + (READ_ONCE(line->desc->debounce_period_us) =3D=3D debounce_period_us)) return 0; =20 /* sw debounced and still will be...*/ if (debounce_period_us && READ_ONCE(line->sw_debounced)) { - line_set_debounce_period(line, debounce_period_us); + WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); /* * ensure event fifo is initialised if edge detection * is now enabled. @@ -1677,7 +1556,6 @@ static ssize_t linereq_read(struct file *file, char _= _user *buf, =20 static void linereq_free(struct linereq *lr) { - struct line *line; unsigned int i; =20 if (lr->device_unregistered_nb.notifier_call) @@ -1685,14 +1563,10 @@ static void linereq_free(struct linereq *lr) &lr->device_unregistered_nb); =20 for (i =3D 0; i < lr->num_lines; i++) { - line =3D &lr->lines[i]; - if (!line->desc) - continue; - - edge_detector_stop(line); - if (line_has_supinfo(line)) - supinfo_erase(line); - gpiod_free(line->desc); + if (lr->lines[i].desc) { + edge_detector_stop(&lr->lines[i]); + gpiod_free(lr->lines[i].desc); + } } kfifo_free(&lr->events); kfree(lr->label); @@ -2363,6 +2237,7 @@ static void gpio_v2_line_info_changed_to_v1( static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_v2_line_info *info) { + u32 debounce_period_us; unsigned long dflags; const char *label; =20 @@ -2435,6 +2310,14 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *= desc, info->flags |=3D GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags)) info->flags |=3D GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; + + debounce_period_us =3D READ_ONCE(desc->debounce_period_us); + if (debounce_period_us) { + info->attrs[info->num_attrs].id =3D GPIO_V2_LINE_ATTR_ID_DEBOUNCE; + info->attrs[info->num_attrs].debounce_period_us =3D + debounce_period_us; + info->num_attrs++; + } } =20 struct gpio_chardev_data { @@ -2540,7 +2423,6 @@ static int lineinfo_get(struct gpio_chardev_data *cde= v, void __user *ip, return -EBUSY; } gpio_desc_to_lineinfo(desc, &lineinfo); - supinfo_to_lineinfo(desc, &lineinfo); =20 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { if (watch) @@ -2633,7 +2515,6 @@ static int lineinfo_changed_notify(struct notifier_bl= ock *nb, chg.event_type =3D action; chg.timestamp_ns =3D ktime_get_ns(); gpio_desc_to_lineinfo(desc, &chg.info); - supinfo_to_lineinfo(desc, &chg.info); =20 ret =3D kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); if (ret) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index b1ce213d3a23..807bee86afd5 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2432,7 +2432,9 @@ static void gpiod_free_commit(struct gpio_desc *desc) #endif desc_set_label(desc, NULL); WRITE_ONCE(desc->flags, flags); - +#ifdef CONFIG_GPIO_CDEV + WRITE_ONCE(desc->debounce_period_us, 0); +#endif gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_RELEASED); } } @@ -2564,6 +2566,8 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); =20 static int gpio_do_set_config(struct gpio_desc *desc, unsigned long config) { + int ret; + CLASS(gpio_chip_guard, guard)(desc); if (!guard.gc) return -ENODEV; @@ -2571,7 +2575,17 @@ static int gpio_do_set_config(struct gpio_desc *desc= , unsigned long config) if (!guard.gc->set_config) return -ENOTSUPP; =20 - return guard.gc->set_config(guard.gc, gpio_chip_hwgpio(desc), config); + ret =3D guard.gc->set_config(guard.gc, gpio_chip_hwgpio(desc), config); +#ifdef CONFIG_GPIO_CDEV + /* + * Special case - if we're setting debounce period, we need to store + * it in the descriptor in case user-space wants to know it. + */ + if (!ret && pinconf_to_config_param(config) =3D=3D PIN_CONFIG_INPUT_DEBOU= NCE) + WRITE_ONCE(desc->debounce_period_us, + pinconf_to_config_argument(config)); +#endif + return ret; } =20 static int gpio_set_config_with_argument(struct gpio_desc *desc, diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 067197d61d57..8daba06eb472 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -165,6 +165,7 @@ struct gpio_desc_label { * @label: Name of the consumer * @name: Line name * @hog: Pointer to the device node that hogs this line (if any) + * @debounce_period_us: Debounce period in microseconds * * These are obtained using gpiod_get() and are preferable to the old * integer-based handles. @@ -202,6 +203,10 @@ struct gpio_desc { #ifdef CONFIG_OF_DYNAMIC struct device_node *hog; #endif +#ifdef CONFIG_GPIO_CDEV + /* debounce period in microseconds */ + unsigned int debounce_period_us; +#endif }; =20 #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) =3D=3D -ENOE= NT) --=20 2.43.0 From nobody Tue Nov 26 11:49:26 2024 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 C3DF119068E for ; Fri, 18 Oct 2024 09:10:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242629; cv=none; b=FHKAtaZlQvhXH6Ey42FmzuIbNpwK/U+SDSBWyzs6SVU7xSjiOrhtIJBGhY0MaSde3S580mJhiSV7nFRXJRD4VpEHDJ+SXXJzOpd0px/7ddICOJkfE+tWVZEs3+g0jQm9IgASYolsuR885gIwi8v3n8xdJ+06AAdYatARMPCgYKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242629; c=relaxed/simple; bh=z0csyQTjaj+C9yhlJD2YwA9SB2zPsh7lfK9AhF+081E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RBptPSPHdZhInmRWnsM7IFp/vl3W/jcMdYW6HPqB6YT1v03awaNFujZXpvLvh71tXPFI2fI7iRHCH5HnjbcvuVeXwpGW+Bvmh4h5/IP6njj886QlA6wLT3DJet6Vn7GbkBj4hhqPODIGTbA6kaXMIHgT3cE0h8/ZkyB3uOIDC18= 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=idSIUf6+; arc=none smtp.client-ip=209.85.128.45 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="idSIUf6+" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4315eeb2601so13980775e9.2 for ; Fri, 18 Oct 2024 02:10:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242626; x=1729847426; 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=OHDTJ05rGXvacOTESnBohjwJkgn6HYTuHVXiH2uivgs=; b=idSIUf6+u1mNZ50IjPJIVLHN5Tes9w9yW+APNi0ycGkjiFLJyVoko7nvNUSoVJsHSW mOawWkRTPd+gbhGpcBsfXOAnTXyVu2qSJgOpsBNxxOIn0wE63SrTpJtl41OicY9O08lw zYEVmvGvOvvbJyFAjFo1ux1bC1BCu46ZS5HGuCd/sHwrMDyiix2CnVqYHTy+9Lem04yd 6uKDkiZBWRoWxpvwn3uDo0tDbCuWLEgRRSJDYxntoUyrDtHOM96aIJA62mNIqtf+QLrj tN+EJ4Y8HA3YRmEm2kKrChGJeOkNz6kwWMxFgizC9dJhPKdUTMvrVRwkhT/quLHv2/H+ 618w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242626; x=1729847426; 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=OHDTJ05rGXvacOTESnBohjwJkgn6HYTuHVXiH2uivgs=; b=fwzwJHDOCqnjraaAraDk57L0wgEufhhoTCaQn0SkMBlw2rEgT2XTg9UQhxyd3LSiaK eOWRRKsL9SKy1jCklVIZHdR+foQ4WW61kc2RVg+LlfaQbdyriyHkCb96XDTgC6/iq03V fAwbAWT1pdqpgM5nKkwjaKd/mWLbOH+H3hPGyHebF5tjF2tyeNmXcz9Lqm5Z7uMeCWVy j7CvDu95/meclpoQVKkIkeZifSfwSmaQTvBca82bZrKxgfetSxlB6Nyb/oaKSI3tFFn6 UqsOQtG/4496eN4jo/PvGJfkkJpL9VLkq5g23S9kRZmj98WXQGp+EqAQA7I/tyAFFAQP I9fw== X-Forwarded-Encrypted: i=1; AJvYcCW7eQ9Rc0zFbUkkT2/Yo+rCLDnRT7VXg8q5HLJhbuzFQo55/ESCGy4t8OSJ5W4u3+XC5+mJtND1NUkSCIo=@vger.kernel.org X-Gm-Message-State: AOJu0Yx4/xbL9CLGjSqKYuZUsFjAkhKxa7yIp9OFwNuB5Qo3DzQaxTSW aeB6K185gQCfW1qg2ZuTBbLxzSAkoky4/vWb82riQXaPxAZ2euPlAqM3jFmI7Ww= X-Google-Smtp-Source: AGHT+IES1IoNjDrz0TOAvS0YIAGB73A/rzQcSM5bfAyLw2pzQkicscGsqdiDu7hrrbHf6vGBfzFz7Q== X-Received: by 2002:a05:600c:4f92:b0:431:557e:b40c with SMTP id 5b1f17b1804b1-43161686a3fmr13507805e9.27.1729242626147; Fri, 18 Oct 2024 02:10:26 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:25 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:12 +0200 Subject: [PATCH v5 4/8] 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: <20241018-gpio-notify-in-kernel-events-v5-4-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=2628; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=+3Qa4JomiJeerbr48n315kA/cYd1bccbsc5mhXSvDl0=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX6AMg2yIFfHM89i0pjfrYEikDVt2jKpX101 iy2kjiUPNKJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+gAKCRARpy6gFHHX cuRtEAC9Fh4vr20FzXpGw7RMH/B8XmRwgmQWWzWFk8Yi9bhqW3snQaNE03AksJiQBn5mEPvhkSN 83rg+KCopLioRjV3NZJZ7oHaBg3xo02hUtls8RuTlksKpBwn7p3CNpNChcEwgizv4IBxZ1aAwhN P2KBuuk/pvnVv/w2gDYoahFlhFiHr6QBB9OmQD9XzkjqWQoB5rQNIoRkAxcXnHZQrMGGT24EEn1 iFRdBcMxTvp4YLxFVg5tLnmHkxOYihXlT/ZSZ9RGP2h8ZjmAeI/AtRSHclH4V8//GnCCpW4zYQh XzAsUXOjFzH3/LRZ+8OKVvfIu7gzW8UHwCGTJlscvYAR6iN62F3fYkDa+45+W1DegjelRrmUcjK 1x2NyV5VpqrFdGRKzp04gI3uhHN1BAq8iRRPFSLgKK42+frvEw/CyXILBjsfvCE1Rw/8+xTQ/Vv K+6Ltia0k9mK/3xFeL8efu5JBxn7T91hrWyPSKhf/LntFrw+ZDLsSvU5Zvxw01RhBqO7UxAY+L5 WfiMcjZmqN1TGbfQ3+t6Yt+aCsmzJzSz8cIUCXrj8WtvNN+jmc9twkV8/Y67q3ckV6zZUUlCLoW MhiEMYYTYnTwIeHDqjJ7xIrLBcWSYlV0fZgSTfFBLYmAU9x0xhLNkF80wnUDI1zUfJLjPm3Vc+H ddlNGxe3m+3nkBw== 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 Reviewed-by: Kent Gibson --- 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 d55d2a246d41..0cba74381687 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2235,7 +2235,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) { u32 debounce_period_us; unsigned long dflags; @@ -2277,9 +2277,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; @@ -2385,7 +2388,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))) { @@ -2422,7 +2425,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); =20 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { if (watch) @@ -2514,7 +2517,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); =20 ret =3D kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); if (ret) --=20 2.43.0 From nobody Tue Nov 26 11:49:26 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 10B251917E7 for ; Fri, 18 Oct 2024 09:10:28 +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=1729242630; cv=none; b=LjthrxatE0CgqtYY0KnWEy9q4QTlZUyq/TjorPvNVzfs37O7zXalqITHt+z9JqO0RNVKsHCJdwLrGGu6KGdRXFiHdu3zP3nYTbyy9vCy2buOYL1cW1IhLkr7yDzuZCC2yz6m3SUq3PO+iTeUbxQKt6J61qawd9EewFMBDon090o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242630; c=relaxed/simple; bh=ShKnfVnA13yZAURxSbfkTfcYBLmCdqbi8FWGA7M737A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J9uld7v0xkDr1a87R6kWAA7+X8t5iYiqf/AwaZEEoQX/QNyw5/lkkcIHlvAJERiFhCqknE8ElpgQ052Idyh+9AmGmwnNedRYkOjITa8A4xCSpT2pzPNg7HYqxzYNxZEnfIdowdI9T8x3FrJMPKOLU8SVjldn/YA7dH9bWlzADEs= 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=0OrtOG6Y; 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="0OrtOG6Y" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4314c4cb752so18978495e9.2 for ; Fri, 18 Oct 2024 02:10:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242627; x=1729847427; 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=U0tIf0jtJWFltu4ODvVnK3F2kIEgN0qMoeq7qRt5Wac=; b=0OrtOG6YydfSWJiPhZYVGmXhQP16KXqVllp5aPSNV8gJxOBCh/r1X+7TNHHJlcy/78 5p3vK1jr2ZDmJH5phMOMMDyu8UzvL6kx8oYLrqzEyMjPohjRHvXMdLtYdA3QbSSWDadZ ifUAHDVQXy8z5Dzs+BtYnF2bkwM70GR3M16rjJd8lWVxwmjoBtfulXMm9ve6QIdhdcgo lhk4fQCrDn22iuYRhdDoy/04FUrzlgPEbNxLpxNpUoQR1YOKhhvN5rS2UluDGXDnPKu7 ImcEsKWBPCjKCoSoDt9/QORN63Z/J1XLi/y2WzxMl1401KEx/5LlNBIJRSevHoeMhuJw OBGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242627; x=1729847427; 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=U0tIf0jtJWFltu4ODvVnK3F2kIEgN0qMoeq7qRt5Wac=; b=Ed6c42mzfDtSGtalozAZ7F4T59pstaq2dqcFsuLkfUCrmm9ehQmm+iZRHMJl1xXxvW acLwIq5+95yxZxghbYAoKtJoWKtNrkcMSochRL/KdMauKhwhWsYpFL2iGGAgTYjLqsuK pqnJqgJKBKUPfjs5C1U8lcUCpbFPcw7yeLJw1RK8FRrwUEFD/m3ohmi5O3RN8LccgHLC VbN3KzVa190ldr9Wy8dEKhyB6JaWPSKkDLf61Ydo4FAqU4od4eoqH5KRGCxb8MPjxxA1 2R6rgXoysVWK+fyRcY4J5d7ylRMP1W6lBrped35Y5eVsPQ3gNPlmwmEmg3TeWN98LFtJ UlrQ== X-Forwarded-Encrypted: i=1; AJvYcCXKcaMsXrrC6dn1tSQpcUr2dLyqGQwazv3j5pl6F2WoghRXFy5F9BezUDg7gUzVmInHFUBAwQTzkyjPUw4=@vger.kernel.org X-Gm-Message-State: AOJu0Yzonzo3FEvda1h+jayzqLavUHqwFUfidukQZnxXD3cnZ6Kf1dti OQqYAy+qzxA9XU7utsK2+7jNWmWmKYjzT28zPXIa5pGo2+kMYAJI2aouNNhtUbo= X-Google-Smtp-Source: AGHT+IGzh7emw0kamIRsyCmcyboccj8tMVEfGoaug2/eeFdI230uT4IxNKyEXz3mEkzh8Ug8d7T5Ug== X-Received: by 2002:a05:600c:35d3:b0:431:5f1c:8359 with SMTP id 5b1f17b1804b1-4316163bc87mr14364015e9.15.1729242627290; Fri, 18 Oct 2024 02:10:27 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:26 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:13 +0200 Subject: [PATCH v5 5/8] 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: <20241018-gpio-notify-in-kernel-events-v5-5-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=0P2NY394anUazMVeFpGs/9LRMhoWr2qMp3sLio3SuhQ=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX6qgpb2193BrTFH70zRNYR224K0KltZ2UYm orfLeQF1HqJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+gAKCRARpy6gFHHX cm1uD/9V/+YzJf5zaldzKgAUqnBq0Kq0Zo1xnpIzc6qJF7vydT8E2DZZrUoEzon3SmCQeH21KS5 0pAFsASGdyT5YjuYDs3ggyzxsU3vPg+uBrQSrk8yNpTuK3TeA6x6ZGDcJDnaafeZnIuLoZABETu yDi922XFAfFsTmRf3sZr0viwEgwbTjrtcuST7J0vk5eavz61PDU0wKxIcNHLsU1FBfpNKPBZdxP pXQK+5N1sZBKEWuk7KAWGkfxvx7AjbBDdMF2UPoYCMUgMLWvz/SIDOdKXKb2+YmDQwvGm2ST3gg 4X0TRHoiU7MhlydTq7GxkJ+fYyede8QFU5qsuBrH4VNV+9Va9/GpJCbkCwrUIYtIKhJoqpJ0xan tCU6qiXUnkv5KYweMozJST3Hk114AmFjXmYU/Rlpkbew6mr52UqTP+eshSUhq8f68d8C02OGuwD adaErvEiFmGGiNbfWThiS4jx+Q3LvR5/pyJoS/ZL6L/B/SU28gbxZ6NcrN2Xlzrtb1Nkk50C4dQ 2qwLbE+LLhOErOluTCcVTPbsnu8xYlNCXIGbECwy5Ooe14e6EFvWYkVoViG2xghKWNso0fl4ymz 5LaatgZxIoQ++Jqv/lN5U91jwjVy/dUobjiOy70WR/HK1CI274BxMTUDjsEnIHgmlir9x8oize3 D2yrXJ3bo4Fvs+A== 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 Reviewed-by: Kent Gibson --- 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 0cba74381687..b242fdb1ad28 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2749,6 +2749,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; @@ -2765,6 +2770,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 8daba06eb472..8737e1641278 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 Tue Nov 26 11:49:26 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 9281F191F74 for ; Fri, 18 Oct 2024 09:10:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242632; cv=none; b=NVjgLeH9Qu1ZAnWrzB1No1cslx7rAtZz3COgBC3sWgXn6Wm+4YqBpMxKavA6CNQdQ4070dB6TTe0h+NrGmGx1Lu7nq0NCB/vG+NXGleyEJkrBB6lWiO0c2/5RQZP9uwDhxhINTXvui7awTYOOy3lOBu7fUKAqhA8dgrByEVzQgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242632; c=relaxed/simple; bh=PrpJFSn1v8nec0wiPaBWRUGeCDKvtDkJixy1vIPLnV0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=M1nkjcVpl+YsVRCG2HG/wxdbqiEFagdeJLa/j9pCJUQtOA9YhyAt95tCbdzTEdSOjx2sOGg0rh3SiOG26/uW+d+JToJ9D3IXqd9RpeYt1x7ZhywR4DrUNyIg8MTBuKxOM4cfbYIDVPJPykUcxrlOsfbN27g70Ka4V+6szuO0rDg= 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=IDJ/WP8t; arc=none smtp.client-ip=209.85.128.53 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="IDJ/WP8t" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-43152b79d25so17924455e9.1 for ; Fri, 18 Oct 2024 02:10:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242629; x=1729847429; 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=IQ9+jptp7Dks8GR7GteYZhyJSD9aHU2HeME8AuCmILE=; b=IDJ/WP8tEn1jvshLljmQkpKyXPpgyubb25HYdEze5iYWtFbetBj8ugn776nJQMwwEN kr4H06KtpaWVeIpaVfx5ALf2FD0ZPL5H+45HbIGqf6Zgux8r9nlC5/2460kx05eVxv4v hMEnNQZrfhgZunz/6GaEOi/elYL61f2kzUZrVaY/xXlK03uOzHQcPbs3mDuSjBOqqCrx Segres0XrLWa1CF74/0R2a03bjiX8tqhLSZpGlCth4D1rQGQ1XIlQvNifAjmPGkcAI3B 6UGGmXRklw7AhQxN4agL1dmBdlVwx+2dbWIwXlkuYsYukuirgRIBTOaGUMehvI/RAqPm lNGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242629; x=1729847429; 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=IQ9+jptp7Dks8GR7GteYZhyJSD9aHU2HeME8AuCmILE=; b=MJdRxzQB0ESa9ELEdUYm3SM3y5Zrgu9NbMOXp+pZ/VpfgOU0tTpMVmlttvj8UqPknJ 87pH5vyG/EpDHLWQBobtCSCINQwA6QktOwMrpxCRi9jj6VBlP7xNx4Xyg28zElvNAh9h +efXTCOf0mCUejV0vqefRpziIFZFC8qV9VP484/HyBiVgbVdbUErTdXdEmRx7HyTzmkh yB7oRrOgAX0QDBL5Lk+r80uY0DEz04Qy3pIUhM4C+HIKLG4W3gu52vpWxqnLQ2HPEcMm aum0tn4AEU+uS0Mox3V6sVNcXW6eqawUNyZands1Fa97w1RzrmvY5G0Q24iAKXWr5CbX SHDA== X-Forwarded-Encrypted: i=1; AJvYcCXSsYCfIYL53UBcm7bEc98uGNxmgBZ2GHBBw6gQfa58bOwErmsCAaarduhZQIpJ7dvoW2eLwR6qKxZJD30=@vger.kernel.org X-Gm-Message-State: AOJu0Yzv7cHdtGcREJa6tKDO9Uj6Ga940fU/xwwKu5XQoA+qWpF6YHVk 0kxjD0qosQdFlO566SGtKTqK7YtuzsAp0VEK8kPARQRdVq1IAzKcfxS7SMB/Z8c= X-Google-Smtp-Source: AGHT+IFmqL8sHA3yU+jyEcXkUGG9y3Ve5K2STpHvAB3B5giEyg4OKEDNCoHJjkz0nT9f6TRUOnZ6lg== X-Received: by 2002:a05:600c:3513:b0:431:4a83:2d80 with SMTP id 5b1f17b1804b1-431615c0697mr13134425e9.0.1729242628874; Fri, 18 Oct 2024 02:10:28 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:27 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:14 +0200 Subject: [PATCH v5 6/8] 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: <20241018-gpio-notify-in-kernel-events-v5-6-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=4814; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=8oH0OI5uVpWymK+8p6k+EMIaU2WzdTdaL9nSO2G6k4U=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX7hW6vtWqYx1czNkm1yo2NiV1EAvlNZn0x9 lL1z8mzDRaJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+wAKCRARpy6gFHHX crkbD/96p00C/wm4UJamFYsrbsS4zC9BxFflYyeS1729zFWFg85I9O6a5WQGMg8glJohrrkbls3 kDfgbdeQxPAEaWVoG/cWshAy8x09nXd8R+zMGWdeU3ZlhDr6iXR73N3x/d9rQ/vietCb0PAc7L9 HnsuVR/o/IZwwCj1xnNhv+hVRTUPEYx2J/V/CJc20V+D4F4KeyCNRhXnTrI9YdTvZRcgAfrZ8BU KRI7lIUGZCPtfCE28utFrO6cWvhWHzpR+Q7WuTjk/DxwbDFUVHql0EXl1GI1YgANs9eO9PtXtuG DzV/MmLGIEnlwQwFiaZhk8wjabZcrX6ODshIBGJ1vWUP9rlvTzgrehxquS3n3/vQVAzSCTPKrms SIy///AoIPQeSX+vJZjsupC5YfKpbP1UXYIiQEHhTEtDimWsXDGYW1UAl9MWFblX+rxT8ocmM6B DnkTIUuRN2pLkSPi8G37kzwmkX1aI/LpuDw9nUKM2lE2V2F+kSMGc5SEZObXNdY8YMP8EZ3rwVo gnwvs68lEapYLn1s/vEdt3AaJT2plXPTTz4r05WbTrW4WwPqI685N7yqQGVHqpXy0ZzUiyt7q21 YqRfDOdFRRa7v4IpwOpCpXXA1vtX6633P2ZfR0fUqDyIySuEitFrfTZT+72qfd9YLQWipwhxsr/ glsWgRus6XSrUkw== 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 Reviewed-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 82 +++++++++++++++++++++++++++++++++++++++--= ---- 1 file changed, 71 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index b242fdb1ad28..7759dca92f8b 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2333,6 +2333,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) @@ -2502,28 +2503,86 @@ 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); + /* + * 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); + /* 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; } @@ -2683,6 +2742,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 Tue Nov 26 11:49:26 2024 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.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 5AA5C191F89 for ; Fri, 18 Oct 2024 09:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242633; cv=none; b=JeS2aZ5bWFg2DdPzzbp8HAmECquPE4Xp7T6oEpvZ3ApNW7DZi+wP2ttPBcrXWUwxmm4hKV/TWml8W34VIGSzvrb4NxXP99J1Uulz+0y9ElmU3tdv5P6q87e+9fGt1QY3Wysa6LnoX7qj2t22bFKGotAK31i1Kjy8tNHFZYjBeF4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242633; c=relaxed/simple; bh=Jc6C6BtaJP7y1Wa50WUZYA4CGv0mhlupt88QoaGx7vU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rI5lQQOqQlR6R9vJQ6MtCf5Ot3rqRDcOFMoCQ8fCXJAX/tYpkSRri8ZMFcE6dwURIFPRcedqYDy4ku5Jb1w0sShfpoH0R1sCZDCVCRJb0MsShrm1K9TjceFl/Qnf786a6lUkDobwbFeOjJDwx4OaUjX4FYsFnOccuMlYt+aaUPA= 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=QjnCu8F4; arc=none smtp.client-ip=209.85.221.51 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="QjnCu8F4" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-37d8901cb98so1931096f8f.0 for ; Fri, 18 Oct 2024 02:10:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242630; x=1729847430; 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=REHoi0BzREleJqZ/Nl2V6XA6vWcoShbFDJgBvOZh4is=; b=QjnCu8F4EyUwiQRZeJdWDxCGEMqF3iRtckmg8lOh1THdh7kgkujkrlOlCt/xC5hkVh MapBnyAjQgF3aeUIDpthewlaI6Icqofs3z/kXP82xm+XcUKwm5LEjPkH9yyE7crVG7FY HHBIPdSUS2lleBPpXdViXiAIQ2wiboGZydAP5Y3V0FH8CheXAEMUvwr3LqUFIfiFcdqZ Vzt4IHSGWTSb9KPkxWQVtMK6UZs9/1AJwD5QtOM6lnGGKRmqxSRbwsWZ+ezeSKO8rMDc Kd1hlNirNqyljSfiRiCS2HlpCxCOj3Ym62Q+9y9rMKgXvlzFdRcC7N+DLHgSZ5Z7HSQi W47w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242630; x=1729847430; 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=REHoi0BzREleJqZ/Nl2V6XA6vWcoShbFDJgBvOZh4is=; b=b9E4sM+na/MJwKE8fgMWVJ7WFbVcQbkgUMmwYLMBafyq+1ixdExaIExmByJrfSdsrj v9G+7FXVTH9bxzpvManh5aEgtQVGXQXCoRmDnzAUVKkR9IqRdJZmIfCJ/7nYpcjpNccj 1VjJjIiWlEZ1lBQ3UAl0h9hHTKtU/Op46r1NwxCnXHzL9LmSNLx/LtI7+duGuITz+ViL +HreiI0L9P/TPp/LOfWBOddUnd2FaTDbMPAqq6KZ+AW80BJ1IK5SzhTxjAfgPcokX6tW 1zh+NwkIwwacrTm0lOQX92+XjpNcahmOx/XtXnQx1hntJPeGV9bERJ/W5VHdD+6T7rP6 4F9w== X-Forwarded-Encrypted: i=1; AJvYcCXu8W15cp2LM5ysNnUGAT49hnd8Kj8shZSWC+eJ/RbQ1wY+UEFB/Pb7dieQaaJU3KDxhJ8OPvSoUSx5oao=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8yvrqC7RxpQDZyaK58EO3BUdjhMcA+4K5dXMov31TeylV5Z+n ya9DYzPE6HrXGaSCuyc66RXxgrw2hZZkeNe5g7VPr2cAiXZPG6ipzTNkil8J45s= X-Google-Smtp-Source: AGHT+IF59FkLke48y/tED1sswyqQuEIFmITsdXrGTMYBeUWtpag4jSNeJzAy0fzaltXMN1VJAj4BfA== X-Received: by 2002:adf:e807:0:b0:374:bd00:d1e with SMTP id ffacd0b85a97d-37d93d43e56mr4314930f8f.3.1729242629630; Fri, 18 Oct 2024 02:10:29 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:29 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:15 +0200 Subject: [PATCH v5 7/8] 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: <20241018-gpio-notify-in-kernel-events-v5-7-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=JremVAHUv8l5XF85K04PimL7Ca9vBLgcOvsxmUmBC6Y=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX7Uvg6llp/YErZf7m6l3e5mMsEISb0Scjju p9Al4Fq1MSJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+wAKCRARpy6gFHHX chfkD/92Mnj6ZOwvRlmap25XjTh1CT9VdZosExN3r+p/dApyORvmONGN903XoP4XnIiXPEPKZi+ OBjf1Bxk06vJ+2xcTHh5yI0C/Oe70BOIQZ0qp7kNLOuAge0qL+ZQc3jiiYxIUtJ1vU5QXd8EqiP sJsJrW+lmTulSgKGy7fLDQ7NV/wyYgbfh+4NQPM+PISwj6Y8hjjIMyxNexDYu6v5d/lWJGV/hii i9vtJxEbo6wG130RIhBSuZpjjtSGkqhjmQNsujxY0/lQ26R031aWMp1JAFE4MZBpeoq7i1QVJcj c7KQkA7ajGhkOlG+8yEweWGtBROy5k/NKAsgVrOceoGanLmtivZosL8uA+GVA214/p41iaqqMeI HIn8AEOWE1RdLj5qtybCpbBpH/2/uCVgjBXeD8GeEEsxKvkgiTbKfqAtyKmo2+vfunRqHfK0Qt8 wWBMNb+FEZc7/Uw1Lh27XVR/Xq1kNMjw5JcgzjAlGtRvhKFn0WvV4+XH2C+9pvl1UMyUJgK30cV W2vr6LpoYTrceULzWoQwmX7MkkumRM/GzStfdKdFtr+Fe2g6yVvXr+5zDSwuQL4IgJs/FdcpZnN GJaxeAizbs9tzC0qM7dt0iIvCv2QphNVbupcLjBpRmyff+Yn9t6TzN/cptQNpKKr6yk8f51ziMK kTBYLlfK4qJlteA== 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 Reviewed-by: Kent Gibson --- 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 7759dca92f8b..cb4fb55e2696 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2729,8 +2729,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 @@ -2754,8 +2754,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); @@ -2779,8 +2779,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 807bee86afd5..83e85dbfdeed 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); @@ -4098,8 +4098,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 8737e1641278..a54be597d21a 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 Tue Nov 26 11:49:26 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 7505119006A for ; Fri, 18 Oct 2024 09:10:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242634; cv=none; b=lwn6OKTWNVmt3jEJoU4AWffa/hfTc9wR3wLTfhq1YKiBS3YGL3ovwVW8NKSRdZyPNA6E+dAZw4zaA+D+Z/YtfFubso6/srjRVgejIXoDqci09/q7FuJWSkH+I53X39j9lK0OEyZEkABJdJYlHuybl3+Kzzf2Bv7OKWP2tgeoaS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242634; c=relaxed/simple; bh=CJ5VsCM0XGaPG8OzBvjEY5DphbVsMrjIcObpqiwTyik=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kfFb/HfbjWuJaYZJCQD5e6wmxHg7SL5xAS0TNS6RfGX4sJ7FLwuAI9oVj1W29cj5xs7+qei+n1dv6zYQkvErvGO7Ee8n2rfGjNw8ZhptjH7htGjXwRPDyRAi1aCl0u1M4i5ysdbqp+hHn6BbyPal30RyprTouEdGmgXyCBKjKtk= 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=GJoXuOMN; arc=none smtp.client-ip=209.85.128.53 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="GJoXuOMN" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4315839a7c9so19430575e9.3 for ; Fri, 18 Oct 2024 02:10:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729242631; x=1729847431; 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=k02GeXnw+1nBLUipwP3WFikIgXvpxjewTuPpnyPj8Q8=; b=GJoXuOMNFap/zhlJb3S/MHRC4vDeCxdN+LN+c5JHfNsmSXMiyJdiL66qCYvSMs5h85 aVLQK/P6syKhkAP0oVLhsbyotIDK1AlC8srJoNI6E0XoM1gSlmIvWtCTR4qoP0ECbABZ PuIteTr17o3rB1GeiB3lo8wQ9JLyVbOMe2EAcl2Lnp82sJX44wKOfFPOZE4U4E8BwX78 XBj2kcl0wfZIwpIKep2h1wXD2OKGbDWidOITLTaYpwysMuXoXVoMu0+GK0o91K+QCW84 c+yccBxXxjNDGY6qTlM+iEYM14FfmHSvZjPevo4cPJ5qgsW+0O4DGH+IxpZADsdLfr1a q0Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729242631; x=1729847431; 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=k02GeXnw+1nBLUipwP3WFikIgXvpxjewTuPpnyPj8Q8=; b=VqaDfql3tZvIN8b/XElkc3YWWUR4DmGcNIyiiMfk6v2m0pp0cAYWRAQU2BmPOGjrf/ D2ST+ANniVCxPL89IHbCjWzZ3WXh8I5d23tILyjDOx7YqZbTuuZWarP5nOL3EuaK7O/X 9oOqxnksA3F3Rh5aHQiAsqRw8I7D7Kw5/urzkoMQslWFblU/2DVwCedshZhxR69F4Ja9 N1/mmHbJXGFv4ouptWbAER+G4yi9GQa3vf3thnwRIcUBkHG0w6139BSmbPphGgFUEHaH tZJCK5/D+QDXhiIe+m+RXLzL6jdMhJIsG/xupUutA4OcCZNZ3dqE0n8f1KY0nBgXLmfc pgJw== X-Forwarded-Encrypted: i=1; AJvYcCXirjzz6Tqcqz7Wq8Vj6HOTrmgMl744umYyN5/343LbtX7RRGONpBQNuByvw97/l+Aynhbg5pb5IfJvxAs=@vger.kernel.org X-Gm-Message-State: AOJu0YzrAAWn1UvSUgU0hEP2gDoFxKETH921dWQ1b7lcSYQbeLHC7gJC +0W7b0whkxorrfMu/EkielEUy534OTrYugv+uF7kFO4JEQhZsFsfzQ3rVdBL9FKInbxrVfVLGuJ I X-Google-Smtp-Source: AGHT+IGxEVutApt0OVmdT2CVXdu798GaQA18yrTCZNChoeciATmPUvq0qhJOVsQijmuf4Nailo53sA== X-Received: by 2002:adf:eb87:0:b0:37c:cf0b:e9e6 with SMTP id ffacd0b85a97d-37eb4ee704cmr1126622f8f.42.1729242630785; Fri, 18 Oct 2024 02:10:30 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:45a3:93e:5196:d8ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160dbe76bsm18700455e9.10.2024.10.18.02.10.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 02:10:29 -0700 (PDT) From: Bartosz Golaszewski Date: Fri, 18 Oct 2024 11:10:16 +0200 Subject: [PATCH v5 8/8] 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: <20241018-gpio-notify-in-kernel-events-v5-8-c79135e58a1c@linaro.org> References: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@linaro.org> In-Reply-To: <20241018-gpio-notify-in-kernel-events-v5-0-c79135e58a1c@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=10907; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=8nBKkdXkJ6I0ir5xbypWERtn55UT5bRb3x8VloeiYrU=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEiX7r9Rzk6obZFpg88BYf5zz7r1VIveDA4HLB LwiQSiomhCJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxIl+wAKCRARpy6gFHHX cgeAD/wNsUzUkaLNQpdtfJaZCM/rGVvG3X5nHExx/Ew7O0Qz55dv6qRpdeKmpSRokVNw5udlr8A +rX4hGJMt916ThlqTPfJ1sZ9oQw+H7WoaFWa+VV1/ydsc0ITvgeku71hxCFaYxPxyEtyHr5QmVv trgxfBy5HPo/JsHdch3L3QVf0RvGApiGnm2qCV/7/D6C3Ofd4jisZwWts9rz6rchIZUVzQC/Yid d4XOibA+qCigsCKrGigyUe8U2gU7zc2ajyPnvYfwZQj6IODwlBYBLkcOEwj/2bG1CJgbE5ca4nn EyTm39Kr8aoVjpkSrcZOWCfIyr4uYbRKwC8FY2AQEPiqxORBSRI/okFdesQpg4DIyPgICiWOf3r 7R1upqyx6YpIRMeeY4BZ6gZqswydekZPQvkl44CRcq1EJ5W45xnOBg5eWBvMNazaFsYtfgWUuUe IjcC1lXP0iO8/yS8GeNJ0VL14aijasNQ3t/7veNsJ8QrCzAevqcJe4Hj2UDGC8PchsOvgXLzjo+ K9RPPqiwFpxhqbbq71A1/k4B7/2L5IjBD9zDXNwr6YP4wenDH4trrJfb8ro9BNnSbnQrQODj+hv sqyb8DS1XljwsXzv56qX53Y/H2M2Q52jGgdiUWC5//OVo5pn/B3jYkyccWkYLpXREQZD2c20cJE Dro7nCsGtr3mPJA== 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. Use gpio_do_set_config() instead of gpiod_set_debounce() for configuring debouncing via hardware from the character device code to avoid multiple reconfigure events. Signed-off-by: Bartosz Golaszewski Reviewed-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 25 ++++++++----- drivers/gpio/gpiolib.c | 89 ++++++++++++++++++++++++++++++++++++++---= ---- drivers/gpio/gpiolib.h | 3 ++ 3 files changed, 94 insertions(+), 23 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index cb4fb55e2696..13d83675bf4f 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -187,11 +187,11 @@ static long linehandle_set_config(struct linehandle_s= tate *lh, if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { int val =3D !!gcnf.default_values[i]; =20 - ret =3D gpiod_direction_output(desc, val); + ret =3D gpiod_direction_output_nonotify(desc, val); if (ret) return ret; } else { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); if (ret) return ret; } @@ -362,11 +362,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; } @@ -922,8 +922,13 @@ static int debounce_setup(struct line *line, unsigned = int debounce_period_us) int ret, level, irq; char *label; =20 - /* try hardware */ - ret =3D gpiod_set_debounce(line->desc, debounce_period_us); + /* + * Try hardware. Skip gpiod_set_config() to avoid emitting two + * CHANGED_CONFIG line state events. + */ + ret =3D gpio_do_set_config(line->desc, + pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, + debounce_period_us)); if (!ret) { WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); return ret; @@ -1433,11 +1438,11 @@ static long linereq_set_config(struct linereq *lr, = void __user *ip) int val =3D gpio_v2_line_config_output_value(&lc, i); =20 edge_detector_stop(line); - ret =3D gpiod_direction_output(desc, val); + ret =3D gpiod_direction_output_nonotify(desc, val); if (ret) return ret; } else { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); if (ret) return ret; =20 @@ -1700,11 +1705,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 83e85dbfdeed..ae758ba6dc3d 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2564,7 +2564,7 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); * rely on gpio_request() having been called beforehand. */ =20 -static int gpio_do_set_config(struct gpio_desc *desc, unsigned long config) +int gpio_do_set_config(struct gpio_desc *desc, unsigned long config) { int ret; =20 @@ -2670,9 +2670,15 @@ static int gpio_set_bias(struct gpio_desc *desc) */ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounc= e) { - return gpio_set_config_with_argument_optional(desc, - PIN_CONFIG_INPUT_DEBOUNCE, - debounce); + int ret; + + ret =3D gpio_set_config_with_argument_optional(desc, + PIN_CONFIG_INPUT_DEBOUNCE, + debounce); + if (!ret) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } =20 /** @@ -2686,6 +2692,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 @@ -2733,7 +2751,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) { @@ -2795,8 +2812,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 @@ -2814,6 +2838,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; @@ -2843,7 +2879,7 @@ int gpiod_direction_output(struct gpio_desc *desc, in= t value) goto set_output_value; /* Emulate open drain by not actively driving the line high */ if (value) { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); goto set_output_flag; } } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) { @@ -2852,7 +2888,7 @@ int gpiod_direction_output(struct gpio_desc *desc, in= t value) goto set_output_value; /* Emulate open source by not actively driving the line low */ if (!value) { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); goto set_output_flag; } } else { @@ -2876,7 +2912,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. @@ -2955,9 +2990,30 @@ 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 - return gpio_do_set_config(desc, config); + ret =3D gpio_do_set_config(desc, config); + if (!ret) { + /* 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 @@ -3024,6 +3080,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 @@ -3668,9 +3725,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 @@ -4548,10 +4611,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 a54be597d21a..83690f72f7e5 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; @@ -258,6 +260,7 @@ struct gpio_desc *gpiod_find_and_request(struct device = *consumer, const char *label, bool platform_lookup_allowed); =20 +int gpio_do_set_config(struct gpio_desc *desc, unsigned long config); int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, unsigned long lflags, enum gpiod_flags dflags); int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounc= e); --=20 2.43.0