From patchwork Fri Oct 18 09:10:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 836871 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F3F0190067 for ; Fri, 18 Oct 2024 09:10:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242627; cv=none; b=gtcaAp8etUQIBMs0h9PNxsoLbQfzUp5FME8sBJTJZztRmw7K6eMqqgef0rCWUHXdtAufBN6TNI60rpr9LLnkZGdEkcLuPNhXWQ8HAstteoJYjHB2pp0eEPJSb1RQEBmEwgz7F2cjkFLMi7Au1A+ZD9l7z+VhlCd1U5jWnpLv6/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242627; c=relaxed/simple; bh=ND47jIbOYDmJs8MpSoNGSSJKSen6G5z88AQQSxF1llA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Qgd6PtdmP9OddRAd9hwKwN8JnydDkxJXTzEkQ6xdKtiMeBjxCVGsCT1POZYrsGWeWEF2y/ceeO1Vf+bzKpzRTwNyv8J9baxvNAxWUqOX/7k8VPfiGnWM3p7DaGWHLDGcsnnOsY2r1/QSuQFl/ccth030eBt+Rdd4FrlGKtY6UGI= 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.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="xjCahh9M" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4315e9e9642so11207155e9.0 for ; Fri, 18 Oct 2024 02:10:25 -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=agNQmwv3mpYk4vphYZJHgVQk9s8JjmvV+JGVb6lwOdH0JQY/l4FCgL9gPk4Pr3bhZj AVuW5RnLbbICsh69aISE9L6gjQS24Hy4jdZGPBsMwJPQpAzwJpQs1fNaw4HkSgyxDvSs KxTWvTgmj1QPsgbG4UTKVZVvHJjqppNI/9Ib8m1ZnkXBcMvzK5MIey8i9Ag3LmU/V3gu CkDRmJOon1VNuEeL2t4CbxGmHcynajwuGm+4aM+l2UyEkaPA9jzlYF9uWYpYTZrSfRAz Zxow4ec3LOFuOgRjgL/sFzoxF7A6QGOnxGCdH1QMpDeUAxcVtVOYvApYaa2uz7SM2Sk4 U+ZA== X-Gm-Message-State: AOJu0YydvSe4cwTdNfdA7mRBdqT8BaavzEyIC1NMEBt1w6pyGUposjqj KKo2I5qoEiwO7C+cZkw0ebC6GmyxDJ5+bdQHmv9KX0UZ+vx8ZEGoiRvIUQwfTHngIO0/sgI48Bo m 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-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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. */ -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; - return gc->set_config(gc, offset, config); + return guard.gc->set_config(guard.gc, gpio_chip_hwgpio(desc), config); } 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; - CLASS(gpio_chip_guard, guard)(desc); - if (!guard.gc) - return -ENODEV; - config = 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); } static int gpio_set_config_with_argument_optional(struct gpio_desc *desc, @@ -2944,11 +2943,7 @@ int gpiod_set_config(struct gpio_desc *desc, unsigned long config) { VALIDATE_DESC(desc); - 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); From patchwork Fri Oct 18 09:10:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 836870 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 2CD7A19149E for ; Fri, 18 Oct 2024 09:10:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242630; cv=none; b=ZnaZWDnHDULrp1dCsx0e1Y+7B7cDLlMp1Hl4eZpil8Cf4B2bFSCJaLvj70W65AOivFq4Lhr5Bs9MOUxCLqRLQu9g8UDhqdG7TNJBrgndGZzT6h1++H8Pp6N3syYT/z5o6biIQd7Kx0VyWmqkjwRmdapuIScHsO+teMeCTCVnmMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242630; c=relaxed/simple; bh=z0csyQTjaj+C9yhlJD2YwA9SB2zPsh7lfK9AhF+081E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XPzYZsoS5GFF2PYJxXUHSp5s9w8ZDVU1/wf9qk+tLzcC737C5yMJ8T7U2A3v1OPoEFVxwKXgIzxWVnXBrAmSZfxXucVJzPq9zG8e/k08AjV3jK+hHtvn43XtvhI4VLlaeSpjD6zcDko/MMtRtlyPAiN75+tQwKtF89rNhxfxgbw= 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.49 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-f49.google.com with SMTP id 5b1f17b1804b1-4314f38d274so24500075e9.1 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=lAf0mrHqi96rKAwZlwcfiAlvROWmBDW2/JaWRQfpmt4hu6jIM8YZrxB92ceB6fClHW 8jraIXcAGK4Fd6TQWSe+RqYw/b2OroEs4lFyk2TToEYnxZOueyuVHqSwGs6mFTWjfSgF TtVgyjeZo8v0PhtC9k9VhwiibaHvjiItiqDnnIs7l1UipWf6pTXvT5Ns4N92fdeuHjPW Eqm280kXeTPDfDphODRgSh3zUE05pnJeS28jz/3ofOqGZ4x4JIrUW9ed+0UXU2wkM8dK IWXPcEhA4R/yI8F9DM2N23RIr5dxLWEwFSmf2p8RIaDlaHTD67dwDhgR38nUjJz/IoBf NOfg== X-Gm-Message-State: AOJu0YwUIztGovZEeju2mh7l+hKcBZ5D3bDL6kYi9tel7dTG9d1Mn/wR SYPQFGQcyZjD5JAXQ4np7Nma3AxIMdnHUbGpuKBLW9whH/7uq43kVNSsWGv1FaW6t9/V/sta5w2 N 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-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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 */ 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 |= GPIO_V2_LINE_FLAG_USED; + } else if (!atomic) { + if (!pinctrl_gpio_can_use_line(guard.gc, info->offset)) + info->flags |= GPIO_V2_LINE_FLAG_USED; + } if (test_bit(FLAG_IS_OUT, &dflags)) info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; @@ -2385,7 +2388,7 @@ static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, return -EBUSY; } - gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_desc_to_lineinfo(desc, &lineinfo_v2, false); gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { @@ -2422,7 +2425,7 @@ static int lineinfo_get(struct gpio_chardev_data *cdev, 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); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { if (watch) @@ -2514,7 +2517,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, memset(&chg, 0, sizeof(chg)); chg.event_type = action; chg.timestamp_ns = ktime_get_ns(); - gpio_desc_to_lineinfo(desc, &chg.info); + gpio_desc_to_lineinfo(desc, &chg.info, false); ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); if (ret) From patchwork Fri Oct 18 09:10:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 836869 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 14A18191F85 for ; Fri, 18 Oct 2024 09:10:30 +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=1729242632; cv=none; b=KGYZjmtsuK6cfRKVFvAzpdz7Qy58I9hVafomWZpj1cf+maY56hbohocedX3cGaVOxk2GdGyNogy1sSH3yNZXjen1ry9zrkLyJ32V8bFa8jIU9Tq7PM4n49q8tLVFep3VFOMHdWFnM179ws2EVsPXDF5aTDOe0ZRhOVHrkEEQFHw= 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.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="IDJ/WP8t" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-43118c9a955so17846225e9.3 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=RUNK3MmIFKXLnkaVD9x9pjt3sI0oLtMJsSEpSmr0E7Ll7SozkI0PQIEaqlxBWAJNlU 5jb6VbSBgFU8aXwV6cnBOLi+OhDQYAv5rnRffVIX0jrgcmpUik0lGVX0/R92GLzYWWrn tvZrYRng+Pn7/I8zSZfJRTpNzxYIVQG8Y+YAnbs2CEzZ1z6HN8I8yHH8PayqWUZTFTr7 +Uqo3nHFrkR8GhKexImCAGz1gxF5b8cCUZxXjN0xjyToZEqFX+NDUa79SCb+ha2ZxL5B LDijMVHtABS2yRlf1eZHtUpwT1m2HXtNLR7yj1nVjftLnlfl63hDxYgnK3MSeBPHXFDf ZK8w== X-Gm-Message-State: AOJu0YzMesFz3j6SMN4Lp+GLgq0Owt8TqVpnoqI4/kZo+g6UTU28rs3m bFAVIecPwDUYGD63o14lFBo7HOBVCaTlWUcxG+8UQljio/QCbik8HRyP2ZGRCqMpghWw7/k2hJB 2 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-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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; }; static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) @@ -2502,28 +2503,86 @@ static long gpio_ioctl_compat(struct file *file, unsigned int cmd, } #endif +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 = + 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 = srcu_dereference(ctx->gdev->chip, &ctx->gdev->srcu); + if (gc && + !pinctrl_gpio_can_use_line(gc, ctx->chg.info.offset)) + ctx->chg.info.flags |= GPIO_V2_LINE_FLAG_USED; + } + } + + ret = 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 = 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 = data; - int ret; if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) return NOTIFY_DONE; - memset(&chg, 0, sizeof(chg)); - chg.event_type = action; - chg.timestamp_ns = 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. + */ - ret = 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 = 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 = action; + ctx->chg.timestamp_ns = ktime_get_ns(); + gpio_desc_to_lineinfo(desc, &ctx->chg.info, true); + /* Keep the GPIO device alive until we emit the event. */ + ctx->gdev = gpio_device_get(desc->gdev); + ctx->cdev = 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); return NOTIFY_OK; } @@ -2683,6 +2742,7 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) goto out_unregister_line_notifier; file->private_data = cdev; + cdev->fp = file; ret = nonseekable_open(inode, file); if (ret) From patchwork Fri Oct 18 09:10:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 836868 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 245EB1922E9 for ; Fri, 18 Oct 2024 09:10:32 +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=1729242635; cv=none; b=gN6kdYxoQfG2pHstr8LArF025Ikqvi9ZgfLUn1aWn4dd1Xk/Rr/pNj3QCuuj2y7gi/uIqK36ygg3f0p984N6c2uql2iB7tFvNK0g8qnUQfhJnSV0zpJY5BAPf6kiEeCHDMx38QghrNqfP4MRjXWPsfqFKlan+TPLnD0EN5b/O+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729242635; c=relaxed/simple; bh=CJ5VsCM0XGaPG8OzBvjEY5DphbVsMrjIcObpqiwTyik=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dY4etEvBqKCUuim/LEBzWaK14gXnNP2EMqHq4724BRWfmpVkTGQnbkXpuQkuO4xnIWFwQ4jfSBf9KH/4DyGSYEexC2GqccKDjLFQGRXcv0pTq7xl9o9aJKQ9/Z28HngfR0nz7+8yxMOiznT7PiFAOQWCpb20Wdk6tIXA0/K0BF8= 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.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="GJoXuOMN" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-37d70df0b1aso1435135f8f.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=oqR5c1zvNOpUhPxrZGKOxThSjyN2oKzsRPwLwNH+z8tO186RP6liws+xo+AllAle4k eyF1YcNP/gE07wtlq3puvsgHRPVZxfUJi9d5rIE8hFUppoYeIjF2eAxdcmrOTHpckBmV qUXjknu/5NqhXf7945Od1kys0vVx6Wbz+EN2PsFF0xyNyiSo60qaAf34HIWgLZI02dO6 zR5YVPDmWvK2G1MYauvUkLDzmAC3DO9jFVLClhGDCYX3Oofb7sHQYucOLzr5u5m7GI4b VOKBadH3y/Bx5L4zVxU7tgeoQ6uaJZjZlFXBNVDoNCpAuxn2/mw0f0YlmliPGOblISef ynTg== X-Gm-Message-State: AOJu0YxFgK7caW+5LRmtytaKJMCD+Zl1WsJGCykN/xAAfSIhcloAyt/L Yo9TGPVzshV5T598I09TyWH+tFmbHT+0fWmJuyaX44b5r6sN3033cWbE5DcHfDbxma/ZFqH7Fmy E 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-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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_state *lh, if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { int val = !!gcnf.default_values[i]; - ret = gpiod_direction_output(desc, val); + ret = gpiod_direction_output_nonotify(desc, val); if (ret) return ret; } else { - ret = gpiod_direction_input(desc); + ret = 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 = !!handlereq.default_values[i]; - ret = gpiod_direction_output(desc, val); + ret = gpiod_direction_output_nonotify(desc, val); if (ret) goto out_free_lh; } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { - ret = gpiod_direction_input(desc); + ret = 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; - /* try hardware */ - ret = gpiod_set_debounce(line->desc, debounce_period_us); + /* + * Try hardware. Skip gpiod_set_config() to avoid emitting two + * CHANGED_CONFIG line state events. + */ + ret = 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 = gpio_v2_line_config_output_value(&lc, i); edge_detector_stop(line); - ret = gpiod_direction_output(desc, val); + ret = gpiod_direction_output_nonotify(desc, val); if (ret) return ret; } else { - ret = gpiod_direction_input(desc); + ret = gpiod_direction_input_nonotify(desc); if (ret) return ret; @@ -1700,11 +1705,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { int val = gpio_v2_line_config_output_value(lc, i); - ret = gpiod_direction_output(desc, val); + ret = gpiod_direction_output_nonotify(desc, val); if (ret) goto out_free_linereq; } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { - ret = gpiod_direction_input(desc); + ret = gpiod_direction_input_nonotify(desc); if (ret) goto out_free_linereq; 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. */ -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; @@ -2670,9 +2670,15 @@ static int gpio_set_bias(struct gpio_desc *desc) */ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce) { - return gpio_set_config_with_argument_optional(desc, - PIN_CONFIG_INPUT_DEBOUNCE, - debounce); + int ret; + + ret = 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; } /** @@ -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 = gpiod_direction_input_nonotify(desc); + if (ret == 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 = 0; @@ -2733,7 +2751,6 @@ int gpiod_direction_input(struct gpio_desc *desc) return ret; } -EXPORT_SYMBOL_GPL(gpiod_direction_input); static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) { @@ -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 = gpiod_direction_output_raw_commit(desc, value); + if (ret == 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); @@ -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 = gpiod_direction_output_nonotify(desc, value); + if (ret == 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, int value) goto set_output_value; /* Emulate open drain by not actively driving the line high */ if (value) { - ret = gpiod_direction_input(desc); + ret = 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, int value) goto set_output_value; /* Emulate open source by not actively driving the line low */ if (!value) { - ret = gpiod_direction_input(desc); + ret = gpiod_direction_input_nonotify(desc); goto set_output_flag; } } else { @@ -2876,7 +2912,6 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) set_bit(FLAG_IS_OUT, &desc->flags); return ret; } -EXPORT_SYMBOL_GPL(gpiod_direction_output); /** * 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); - return gpio_do_set_config(desc, config); + ret = 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); @@ -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); @@ -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); - return desc_set_label(desc, name); + ret = desc_set_label(desc, name); + if (ret == 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); @@ -4548,10 +4611,10 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, /* Process flags */ if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) - ret = gpiod_direction_output(desc, + ret = gpiod_direction_output_nonotify(desc, !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); else - ret = gpiod_direction_input(desc); + ret = gpiod_direction_input_nonotify(desc); 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_sleep, int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); 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); 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); +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 debounce);