From nobody Wed Nov 27 12:58:54 2024 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBC8F1A072A for ; Thu, 10 Oct 2024 09:10:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551439; cv=none; b=AjOaMrnf/q/chofauob8ltKl49mUM5Bh2M2PK9KSXl4I9FUjt8bLgI1msgUs+Fr/+v/9uFBePArKyA9yStiAg7iT/kU6vuJNySsc+PY4OFn91v7GLSKsXwdmWL6dlPpSwhEjwrtTHwdz/TULiQFASmrtHNDlBCNPUFeSsd+aOAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551439; c=relaxed/simple; bh=HOsD1l6BWkiOE3cTTGyktI+iRLFXrUG9jIksamM5M7w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kC0bkA2ucC7BHfdRtnPnX4XtDfVZjR2lw9JpoyqVni3g8iYH1BzuCCS8qqeWixsexiV++KDFcyX96ulKZ4OGq0JT6Z33BN9CZGrOxn4CsdJFCAA0MsJ5I5HLlPXHDoXD/4Z3/NPQOul4w/ufKAHDTz7nqDgSi4RnfabJNfQ5DZw= 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=WoWZUyUd; arc=none smtp.client-ip=209.85.128.41 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="WoWZUyUd" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-42cb57f8b41so7600595e9.0 for ; Thu, 10 Oct 2024 02:10:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551434; x=1729156234; 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=cBqp7nH7FRGHJQzUMuYpdjXDVKUs1/NZNMC262ksw1A=; b=WoWZUyUdNegPF+SMDH9OH/u0gE2tl1KZBppR2UDGAZUdI+sSPTQS1Uvyschd7Wxwwm mmgjjgkCBB/f61ktA0W8QFyW7alrE9gsOk2KRLZA1B/1KszneodccT2vAhU9c7wDNVpT 0MCdldPGXGuoB7+Sc9XyKv+qTZT0Ua5lVHEUP3TH/W2TjynR/uqIok91lzZZ5dY7EBlF 8D9vxXcdk8UXfHICsGzY2I2RLc4KGsgsxsDQzdPS5XPt2TY+CHMod7tQ7zOm1fn8mLlv D3kIEs1WR0r4bcvjqf31/7pDFQmVnE5FUo6h/BsNkA9ECoR/ZhqGYdUJwX/bnzfS5W2p 9/RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551434; x=1729156234; 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=cBqp7nH7FRGHJQzUMuYpdjXDVKUs1/NZNMC262ksw1A=; b=D4ReJbl6TBuqi0EZTBOSGf3tTWwN4CPLK5gafuN4LDdvtng2JaiJBrIhnJjypLsr/u CzdrFgejgTYH/ZBei+czZENyLcrOnWAHDePZq3HKf5a86DJum1aw7+KVLDxJSHydrNBo njcrGk3yL8hHnkhndks4vKOrfhweRhOpyP6ZYFSLcvCardlw6vRo+7xq/EjBfG/fSctd J5NK924Y7oQAuMHQB9B4AaHFE1qGECqINOlTYXquQ6e7uQeY23UrdTUKbKGt7IThL9gK RBYKWbyFC2qi700Rvy/XRy9nEU38bXtsbLJkOyD9dIsU0Ik0xRgyQJ/nnaPJ0YQsJc9j VcBQ== X-Forwarded-Encrypted: i=1; AJvYcCV/QjjnxJWGaUOjpkYyHhPyviFooj8VMUdSqf6bDC0M/5ypo8r05M98kZOvmCVJudHggjzlNNzH/JcMdMQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzW1NDy01924NFIHeOfPdcKTWLEPNoksHXma1qE/QlBARUdlfXB Dq0JzQJh/J93FGaccX54zJIYWF2bHRBwHG4pad+7EwpP07FS/2AbI1wFmS+so+c= X-Google-Smtp-Source: AGHT+IG0ctsdanhgvXysGVwcpiJpoOdfhXfTc6wITqmuAD30mTyVbRhP1kONzsTctAZyusfu5354Jw== X-Received: by 2002:a05:600c:444c:b0:42c:ba83:3f00 with SMTP id 5b1f17b1804b1-430ccf04348mr61698305e9.1.1728551433797; Thu, 10 Oct 2024 02:10:33 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:32 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:22 +0200 Subject: [PATCH v2 1/6] gpiolib: notify user-space when a driver requests its own desc Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-1-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=782; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=6tmLH+37mKaXiUj7ddtJmg9R7u1Uj5EdOPkbC/L1bkA=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oGX9HZAw7QZx6h9L/RjkHntjdlLxeoIu7Gj JtOceg0XtWJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBgAKCRARpy6gFHHX ctPuD/4iNfOhDJ+p4xU8aHQz5tf3wn/TtiBL9etxDYXIEz/fLXTTu+1jBBdOOrhmMML4p8oNA4A fWYrsY7WAsz6zUvAddLaRYLn/ULagpQFCL5UFdMPTw3zU0Px71mGNQmDHsVOm4n6pOzQetZ0zEx qRGGdrdM6qpi4PmGyUsWMtxBAED7rzu9UJ1BMqlDTiXWSk+fncn2yXrxJ8617ZY+D7rrghV5uE+ H5ftFS77ZCML0ysVralby3kYyHVy0H9lhJYrOn4cgaCRF7AEBAvXA6ZRgltgrfvE1GP6JRpwdfp s4B4faLLjOTDHNGWoN42U7RJOZs5zR5QiZcNWh+JkCW7VO7LxuGF4jJb2lnRJ4eZdLUSC0Qe+M0 zrgUaOUiTpnL4Ehy5MN0tfIMKZ3NZ7pyoHmlPYgAf3UqzCa+ychG8fIiiXq96HjebQ4EXbD1HBH CTzng9IgiBlJOVV5zgLIlyjs0oFWopf4d0Wbro+dO2j+MzipTX4qqRgEcl7v4b7OzW9O9LlFkoW hZHh9y3EPFoZ0zXxHAtl4z4CSgGP7CGf+oLF57dTqubLX3dZe9N45nB1b5PdInskRglWrdDVx6Z Au20pjm5kCOcirEKQvLWha9WGNjsPUn2N2wi295CzwqkMLnbepxgn9GH+TYfqre2UaHM8RdJ00p bb5Z6JIyW7MiuSA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski We notify user-space about lines being requested from user-space or by drivers calling gpiod_get() but not when drivers request their own lines so add the missing call to gpiod_line_state_notify(). Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 0d1e10654d4c..839036b116a2 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2532,6 +2532,8 @@ struct gpio_desc *gpiochip_request_own_desc(struct gp= io_chip *gc, return ERR_PTR(ret); } =20 + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); + return desc; } EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); --=20 2.43.0 From nobody Wed Nov 27 12:58:54 2024 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 C511A18C03D for ; Thu, 10 Oct 2024 09:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551438; cv=none; b=GEjJmhMeFxJYZDHzk+UqdTb8w1Y6/9sNufezbktl/6Cw9zWWfdOABAikhdZKYJA9gfHCFrcNr3yeuGp7UD89KcHlJSvZjvdm/3h/Gg8EWbw66W3W/lRHkjm4dgBFUt0JZ4KB9dQ0SFfG639hz0pqH86v1a6avgOsNctwC6mZhOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551438; c=relaxed/simple; bh=Md/Hr1eDm9gZYFElgpFpc1dlcVMo6FQJMk2O8wt4kIk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OmnECcutK2SHqzLR5g4adgnMzI55g7uAE8p0OyIbM5YIWtTd4NT9Q5tmHqg2HR9gdwpJH/yHXu7HqcKHygxSIXhwjtio/9KouMwbqVut0fCjMDHi1DEnHezQ9beyTXilMKTp8XEkY3s1CnZ3NhsGCJoxqiuJGz5BGhAtDO/gVdc= 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=XVJQeAiy; arc=none smtp.client-ip=209.85.128.46 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="XVJQeAiy" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-43118c9a955so2911445e9.3 for ; Thu, 10 Oct 2024 02:10:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551435; x=1729156235; 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=2U+DP+Po3cvK+C6hThGA3u/rzX4mk6Ylhzlg9JhIjgE=; b=XVJQeAiy00Avhx9JhO4zlrjYQR7CwYBDPcnnJPBNTairgjUmAtXoiJi5nf5ztcBYcn YeX4mi8ZxyS/uX6QtU2rmHJIcQhTyIExqFoGZVL9g0TRz2sE1BwQ3im7MRe4GHxwc1NC 0N7Vmnqh5zQSwB7dFKXys+ub7xhbcRohswVK5Mhufs52TszWewmSzYcI2t86+++EuyKU Ojck+tOeeblGYeu+obXpEJ3ELy/Dsfv8YSt6BHtIzontQnJsi4Tyg7xEG2er1g/IRb9j d6YSdnQyUHcW82QUfLL8Pc09+cK9XLm/zms336dgcjlRl3gp2FdoE77AtP1tohLbJc+2 tR5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551435; x=1729156235; 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=2U+DP+Po3cvK+C6hThGA3u/rzX4mk6Ylhzlg9JhIjgE=; b=v1L9HotVrrW24PbMY+HoPULgAujrdyBC3lQZhqoHpIcz3zE+sHhuqQH9gHL1ygFNbb OjZHrGC64tGxPlEq/xdoV8EzThfJMCBlY1PPZhLeuHJNJV+esRvnHqXkFe4byf8+5K4x MJP487UAeKp7rJe3RsXmzGvZIMRTNrIcQQzg/Fq8Rk41mdlTu6Cxs0s7UUA1KfJDw1Qk M+KFIA/tH6TKCs0QjndJHWbZvhX/1b9Edsj04GAstTo9v8PHdb95ougC2bLrrlCaPvdf s4F7WXukJfrWyOWJv8faj5wwbckTW7xnxOGipk8KPdxbwahsiu5BR6ZVcKDN0QrIvnKZ q5bw== X-Forwarded-Encrypted: i=1; AJvYcCVqvQYlA65pOimNIAu7NuiH+DzTDrvF7QSVLEvo6nakVI6lNC8K3RYEBs9FzzR+8aqCm0Sk+PYXw8tgdcc=@vger.kernel.org X-Gm-Message-State: AOJu0Ywbv92hPtwVp1HJZvEbqfrURjbA7H24j8wX/FybXZFmBrXdxcrD 1SboyIoXJM3eqE71yEe5qw7Bk+H8Nrvn5VU9KzsRYExhHVUNL094IXSGSdCJhL0= X-Google-Smtp-Source: AGHT+IGfYH5TMRPFMNDfM7MKJx1q8rzTx6kEhvkJP3Xs1zFlvZk3XQ747bwhNr1VAbjBZTXKbKv8Zg== X-Received: by 2002:a05:600c:1c29:b0:42c:bae0:f05b with SMTP id 5b1f17b1804b1-430ccef6313mr52014515e9.1.1728551435094; Thu, 10 Oct 2024 02:10:35 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:34 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:23 +0200 Subject: [PATCH v2 2/6] gpio: cdev: prepare gpio_desc_to_lineinfo() for being called from atomic Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-2-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2675; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=hJXOR0yJYCJfsL+TmH/jw016J3uAhH62oK/KOdi1k0I=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oGjSwEYXib+XZ21p/SuNORc1oC0p1EE0/8J drAnwmFJVKJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBgAKCRARpy6gFHHX cutVD/9gzcyiCBHbwNEqZFcoHr14MA1mOkJnkB2Rp7I2Y8AKcoBpD69jha+emJQO5FcolxravSe /8w/w6VAKG9oyG99STzDtyF+bBgT5uoNPDbgGfU82WqKKgH+6mnbOFvZyEXmFovpkhDtcU3SCwN DLb1Ar3klfSexGW1k1pt1dVThBx2kmexzeCHgtFFaqTJksXPjyCp+BGIRjJXaW5BdoKTUlRmL6J Y9j5jZkMGNKPgFohUp9+5W6hy2Qch4dIJh9iOBS/VAX6KHboD+1VM7kjTKUBPX7EsQUzcO4BYCO Tqnn+cFBzb3Kf/iCDj6zT/422IGOK8IWVOnNnkROXYpv+Kr8Ng0mQqYaiupGxRu4IYy2uTYqWJM si7GpoGbrmf2DL/84lb3cLptpOeycWGKHVlRx+QkQ5b40nHVrePDh8uWTicxNqPgxDKmAHsn+Dv s3ozdcWPGRDld8ncE99rx1JHkAlILg9tiDfIy5a0BakXLbsrIVFUw45j/Ap6WienPNSYC4gEqZZ V/SgXUXVdNHwz0eUCjCikEAWoMY4wl5XmXSqRPdWnU0RbuKMBN5ALUjyU7rx95YZslhp1svKK08 qNLi0Zozzo7YZAZFc9ZfbG3vRIPKu5Lrc4VEpnBG8I2gpdvZlVO5hCjKFOcX1+9ESx4955RBHTZ Vu45kdUpjcIod1w== 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 | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index b0050250ac3a..8c48a53b4fa8 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2361,7 +2361,7 @@ static void gpio_v2_line_info_changed_to_v1( #endif /* CONFIG_GPIO_CDEV_V1 */ =20 static void gpio_desc_to_lineinfo(struct gpio_desc *desc, - struct gpio_v2_line_info *info) + struct gpio_v2_line_info *info, bool atomic) { unsigned long dflags; const char *label; @@ -2402,9 +2402,13 @@ 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; +=09 + if (!atomic) { + if (!pinctrl_gpio_can_use_line(guard.gc, info->offset)) + info->flags |=3D GPIO_V2_LINE_FLAG_USED; + } =20 if (test_bit(FLAG_IS_OUT, &dflags)) info->flags |=3D GPIO_V2_LINE_FLAG_OUTPUT; @@ -2502,7 +2506,7 @@ static int lineinfo_get_v1(struct gpio_chardev_data *= cdev, void __user *ip, return -EBUSY; } =20 - gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_desc_to_lineinfo(desc, &lineinfo_v2, false); gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); =20 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { @@ -2539,7 +2543,7 @@ static int lineinfo_get(struct gpio_chardev_data *cde= v, void __user *ip, if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) return -EBUSY; } - gpio_desc_to_lineinfo(desc, &lineinfo); + gpio_desc_to_lineinfo(desc, &lineinfo, false); supinfo_to_lineinfo(desc, &lineinfo); =20 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { @@ -2632,7 +2636,7 @@ static int lineinfo_changed_notify(struct notifier_bl= ock *nb, memset(&chg, 0, sizeof(chg)); chg.event_type =3D action; chg.timestamp_ns =3D ktime_get_ns(); - gpio_desc_to_lineinfo(desc, &chg.info); + gpio_desc_to_lineinfo(desc, &chg.info, false); supinfo_to_lineinfo(desc, &chg.info); =20 ret =3D kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); --=20 2.43.0 From nobody Wed Nov 27 12:58:54 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 60CB4BE6F for ; Thu, 10 Oct 2024 09:10:38 +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=1728551440; cv=none; b=exu3fi0KWHvb336JkqQJ3ZN/KIHTWsBhuOOMy1YtHUJqQIxXh+bEc5D8gQ8g+EEWbhYh/Ti0B8A+AYVmsktrDfour2ouer+d3x8iD4ChEX0gqL4XO0TGG5BXBPn7b8XPlZf6CuJj3neOoa/SWo62DtRQdwclAs012mgzi3Ll1cM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551440; c=relaxed/simple; bh=MX5vq2Zw3twCnQ0vfBjrVYXjMbbJ4a2HM8Fa/3xG0y0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QSZ5qJqYAz4g5WtbkcC37/fZhTQgMXnX0mYIxyf40GcwKFoncvMuarGeKAPeGqt9Z6kKTzFnlDGizyy52nR2XyQ/lTj2HVKk5JemmR1CPVqSCIASUDyqpdEtICCDmeWwnf0XvmSJRBlTW2UTwvhGje4loFRVrGzYtOXUj9XpmfI= 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=R+ShqlCb; 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="R+ShqlCb" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-43117917eb9so3801665e9.0 for ; Thu, 10 Oct 2024 02:10:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551437; x=1729156237; 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=D00NY3Cf/2JtyTIZtI5Vh9lbTo/8NdpSRAvorBZeEPs=; b=R+ShqlCb0TjCOz1jF6BeS4ray2L315uZvqAreieRpfecXQgwtroAM9igJjXZagHvdQ c89s5uqIuy2V0ejNxyAJjfhBj1yVougAqrKazwibWsaZoiJ31pJnqF4MQXK+RexKzkXg TH/iqQa05k2bjwAWB0etlEf/NbtZgpjXAQaxmqWJ6lBlhA3n+qPaISxK1PhFm4nOYzMq dZ6h04h+TKom5Spe3T9lXW9zk2dxHUxeMirUiLjuRc0H58bfJQ2wPyor9en3VTAwvCyL rW3qnOkcYkrAPF+AAcgI4J4f+e5zGLDvsAArnFZYUgB6g+e2KYSlK1uyvaAddovEBnOd pyGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551437; x=1729156237; 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=D00NY3Cf/2JtyTIZtI5Vh9lbTo/8NdpSRAvorBZeEPs=; b=wWCQA6gZwUYKeekowbgPGULLLg2O5QLjFBpQ+uHOnQ6GzLNlCfcv1o2hxk8nWE4VIc 58dBP2jTKRAtY+4O6HKo2PAy/LqgEbD2Q5j6YMr8eeBM0tU0CbvTUKEquNdxoiwUoEHn c05ec10oboX5Z2cXN8oTUFKMYPTFqlkkBhJdtBTRId+R3oQHixPThrPi9kHUmXj66ie1 v1gU9L+gHh7FgnsF7E9GTZWpA4/a2aMfu0rXZ9wGyfFA0LxjoHE/8hRRTvYkA/qgAwKH iYMOWCErbVIx/Ggox0hLEdKPPloZXGOxAbTwO0lSyqjtFVDe16MNtT2M5ZGX39cYmOWG IxFw== X-Forwarded-Encrypted: i=1; AJvYcCUQOaZ2WHkgf8GXm0Bor3OuGUyTGlQULCMws2ztjkgXzmKf+rHtWjlMzrmoTuMphzLuSmK3Ec7PHLAr094=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3YQiGxufHcgoC4ez3EoOIZ9Qoci8zOLm8z+D5tCSkRYlhxmYV FsHOSMY2leZXxB/EKg3EXAgqXlS7kc2S3hNukCRU+XUi/T0KrLBkorhKXenf6nk= X-Google-Smtp-Source: AGHT+IGx2rbn6DR2nrQTzVhwJTUvzHvjPmLVLKKmcjkIKfyWANxiyhhnnZqHAVnf00qwcKLpIZz9GA== X-Received: by 2002:a05:600c:b92:b0:42c:b942:1bba with SMTP id 5b1f17b1804b1-430d59b77c6mr39947815e9.27.1728551436712; Thu, 10 Oct 2024 02:10:36 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:35 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:24 +0200 Subject: [PATCH v2 3/6] gpiolib: add a per-gpio_device line state notification workqueue Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-3-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2506; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=MiiIteAMQR4VVa0+5869LlwwwlxisL1Uf9ne26/dPV4=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oGhyjG4BkUy5dAGcbZ87CfLoX8TjP7VsVC8 NjG9KUqPnGJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBgAKCRARpy6gFHHX cuXdD/0bsnzDAzpD2eUzxe3HvLyNCZfLsljAWOxJ+eSgooWmfgouon2xYO4JKbfIJgvgmB7fDKl rMq92ReWHT59gkF3Ca9byG+29J/DDGsvdpdSpw8B1O1M7sbpV6xdjZ2BP8NqqA8nZNP8qL3Y9KC QVN9tgM3N8sfp4f7H+2D14x3A5sBbvjxSAIL1jOhVwvSDi+lFuQ5ZznsfzYe2zuort1790cTndk WKZaaOrNsaSHtGw7clF/UrZEO6gHKGD75KXBEC4juq7x7z9Uq0y6brICAQjPYUmp5iuDZJWVqax sh9NnTafiXXcyy/T0u0wyhvyBhndUzTynzsK9d41tj3QFWuM5YbFB0PedsStMF5s6+4BYF1PmSh n1vbqyGoAea/6rEgic1jU6/oZzwOBPLcM3pbbFYnEdY4JBlCzR/ymKQGsPYnwezcRVPvc5gv4Iu bGKDBd2NgqKtGHuOkuc/WCh4Oi/IYhc8JjTEltnATnYBkqh5QHMNL1wQFs3vXgEMzd5f4elYJUH 1ScU6oW0T21pP51rixl6CGOOuy5mKcXAqzmidsUVQrNVTiorZjHYL5KEGoSPuJpBRIRqb4qk6Fs NGCawyhSAIXy6zsoYfN9u1mPvk/VzeI6X8h3tJz8WHJn8Pa7sYTbu19xpm2Ck/wCq9Pnu84c78d Kr0OAC10kp+5CHQ== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski In order to prepare the line state notification mechanism for working in atomic context as well, add a dedicated, high-priority, ordered workqueue to GPIO device which will be used to queue the events fron any context for them to be emitted always in process context. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 6 ++++++ drivers/gpio/gpiolib.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 8c48a53b4fa8..dec6c9a6005f 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2869,6 +2869,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; @@ -2885,6 +2890,7 @@ int gpiolib_cdev_register(struct gpio_device *gdev, d= ev_t devt) =20 void gpiolib_cdev_unregister(struct gpio_device *gdev) { + destroy_workqueue(gdev->line_state_wq); cdev_device_del(&gdev->chrdev, &gdev->dev); blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 067197d61d57..d24cd9e8b17c 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -17,6 +17,7 @@ #include #include #include +#include =20 #define GPIOCHIP_NAME "gpiochip" =20 @@ -44,6 +45,8 @@ * @list: links gpio_device:s together for traversal * @line_state_notifier: used to notify subscribers about lines being * requested, released or reconfigured + * @line_state_wq: used to emit line state events from a separate thread in + * process context * @device_notifier: used to notify character device wait queues about the= GPIO * device being unregistered * @srcu: protects the pointer to the underlying GPIO chip @@ -70,6 +73,7 @@ struct gpio_device { void *data; struct list_head list; struct blocking_notifier_head line_state_notifier; + struct workqueue_struct *line_state_wq; struct blocking_notifier_head device_notifier; struct srcu_struct srcu; =20 --=20 2.43.0 From nobody Wed Nov 27 12:58:54 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 8AEA71BC074 for ; Thu, 10 Oct 2024 09:10:39 +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=1728551441; cv=none; b=EV0dpdUWM6ID7be4TH3+yRJqoBGusVtrA9OTyNzhVisrcM1csJ5iirbJtwkP8f0gbPe+O4iTZUSZYVIqE2SFDzbBMpIxkJq01lgSPur+CNn0H9x5/RRlMyLVRHWPL/qXVun3QxZ5NEJBtXu/96V9iQLP6d6/vdtwX52vBj6n/pU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551441; c=relaxed/simple; bh=mVy8M39xpgHamH10lQJesyfPKmnaqpskSJdZZ6zu3FU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W2sGuiMKFtZyRBtGxpK062R4xbTvOE0QZh1wbYaFPGAafXFe6Ew/GAZuM8oqn9Km5KFu52hNsxKKOXZYLAEk/19Fes8PzKG82F7APV9PgRM+NXyX4qY5u23YN+s/lL9teeOKNHVR2T2Q03OY8FOJYyk/EaI3lTnJ5TN00sz73+A= 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=aS1f1M9O; 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="aS1f1M9O" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-42cbb08a1a5so5977135e9.3 for ; Thu, 10 Oct 2024 02:10:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551438; x=1729156238; 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=9xHrQaBWU1wo1cXgJKaVBgoXiXzmKNYjJCV9azRNaU4=; b=aS1f1M9OgAiC/ygUoYhVV6fqeRsMC8/Ny6UvAFbXZLnSdamPATabsWWjoJZnRDDkF+ KYONT0oEhxpoXQsqkTNdRUkGviy9ZZK6MUypKdF7n5xP9Lld4BlvPKhDjfkfl3OBqiJ2 7gd2CATyZ28CLg/Uy2/c3MUplRT4RHNgZtole4YuOuAUdudGhECcSIA+SRF7yfDBDQtk efN/a2WLL4p/lN58uJ6i9m7KYJxZSXkkX6jQ7JOCEPT5bfFQbHq5Rop1aVqK7FVavpts +sgr7BYE0ocHSjvlcqDc2xHPTVDTQ33bw2Nu6lAfmZdOftcR77IvI6zs3shFB+IN6MOc MC7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551438; x=1729156238; 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=9xHrQaBWU1wo1cXgJKaVBgoXiXzmKNYjJCV9azRNaU4=; b=oUyfYmnDKQ0PwisPJc7XKvRNHdsZGhgByasV36R26LOWL6k9UScU1Uq7PbKUA6E4ca Vheud8uqMMrh0MjxlLoGCtjAw8NAJCfbfaWXqmGhmgWR8vAFZn686aSHWl+Fo1mNucNB GGygJ/ZaeXQV5Y9R49eIYZr8KLawW8WGIyFg/KLdQvp10/Y29PckD+ocljDuq69sTcSm 85FJ9XEeGMyFRDzcogpBC75uKht8Cm/LxfD+lUSghMmmgGNykH1Er6MW3b0pvuGJHkEb 98VZcmEcU56Av9KPy2eSl7I543y9IOVjffq+IocPBAFPysimzwKDSxa2Llqv78Iqm4s3 eOfA== X-Forwarded-Encrypted: i=1; AJvYcCV2XDVAhYltNUMRFGPBbR660Mf+hulUUKJIm320hTDOvy+BNM5J4DR26dfifGYOHnltvp6qfYxqQGjf1gk=@vger.kernel.org X-Gm-Message-State: AOJu0YxyoeqU9+Gu5JMp509RlvFC09uFq5lIJp7vh7d+MePdzFQ487wh MN5na4qxcG3N3TJkCrG89HNmW3baeyHON4w4Ga5/rwIPK3dJcOJo2YWn89ECY1o= X-Google-Smtp-Source: AGHT+IGWnTMl52kdHU0OAALf275O2mhqkYoiP4N91jpTbJiZZSEu9KAs2g4/XaDGDW7uURA9AwOptg== X-Received: by 2002:a05:600c:1f11:b0:42c:b995:20d9 with SMTP id 5b1f17b1804b1-430d6faca5fmr48557505e9.28.1728551437933; Thu, 10 Oct 2024 02:10:37 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:37 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:25 +0200 Subject: [PATCH v2 4/6] gpio: cdev: put emitting the line state events on a workqueue Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-4-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4357; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=sCvTb92VsU9o5sQdk/sTByAevcKbdTEudD1FU5KTG6Q=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oHslqxMHergOZ85ymfnNVnjvhx3UNOSbjhy 2Tk58ON3MaJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBwAKCRARpy6gFHHX cpHhD/9U+MxzygvDhzTtOpl7hxtef7JseBNg7YFZUWg3KraN6xZf2ymhT7rzcDY+X4/yhxSGLm2 beutzqT3WHKrBUJ+uNbipqgdi3i+W8CjU/pr1Ok1KWwjqPTJiWtWEtWnrbTA55n1wLAYiKPxLNj Gv8GmuK66jlM1tlJSo/FZkpU6dTi40qQqg4FGaJGci7CWj4HsB6rB6sh0Sqii40nqLXUsQbLA++ hzAL+Et9JLk1akIAWA9y3h4Dq8+lVuntGVWzJAkqvbIm5xbJL6xKXHjXKBqx/kNyGtuOcSnsIUQ Pknm5X74ARS926scOPe6N7KLI54Pu3ib/B+SD+aTKoT9VIRHRnS7fwWgfwQA/QN5s1G/mIfiqwZ zcfBu5hT+ct6hlZTB25+n39OUwmUQvI/YmN2liHsLwjtGg5fKlwpojb351lJQxohmnhf0TjsWKz 4UXNDuwnR4LmXIlRA2qY7SR5fr5AF4/3zBY/CAMTBslwDKbSKQUhUFbxFRlWw+JVbJvciWPJOrF vxuUQQq24KLXzoB6urAc1t6jr3uamnw92u0NKLcH1IiCl2YLwQEV5qWB1mto8PKbUtX+0tm/mKa Dcpp10U8G5Z/vkJpGSdCqk3mvjVQHpgEmzilP68dMZTtoixEh2UdHHi0G1FF7/lKFVTGsGi+gi0 AOz2k5r6wbw5zeQ== 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 | 72 +++++++++++++++++++++++++++++++++++++----= ---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index dec6c9a6005f..2677134b52cd 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2451,6 +2451,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) @@ -2621,29 +2622,75 @@ 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; + + scoped_guard(srcu, &ctx->gdev->srcu) { + gc =3D srcu_dereference(ctx->gdev->chip, &ctx->gdev->srcu); + if (!gc) + return; + + /* + * 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. + */ + if (!pinctrl_gpio_can_use_line(gc, ctx->chg.info.offset)) + ctx->chg.info.flags |=3D GPIO_V2_LINE_FLAG_USED; + } + + ret =3D kfifo_in_spinlocked(&ctx->cdev->events, &ctx->chg, 1, + &ctx->cdev->wait.lock); + if (ret) + wake_up_poll(&ctx->cdev->wait, EPOLLIN); + else + pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); + + gpio_device_put(ctx->gdev); + fput(ctx->cdev->fp); + kfree(ctx); +} + static int lineinfo_changed_notify(struct notifier_block *nb, unsigned long action, void *data) { struct gpio_chardev_data *cdev =3D container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); - struct gpio_v2_line_info_changed chg; + struct lineinfo_changed_ctx *ctx; struct gpio_desc *desc =3D data; - int ret; =20 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) return NOTIFY_DONE; =20 - memset(&chg, 0, sizeof(chg)); - chg.event_type =3D action; - chg.timestamp_ns =3D ktime_get_ns(); - gpio_desc_to_lineinfo(desc, &chg.info, false); - supinfo_to_lineinfo(desc, &chg.info); + ctx =3D kzalloc(sizeof(*ctx), GFP_ATOMIC); + if (!ctx) { + pr_err("Failed to allocate memory for line info notification\n"); + return NOTIFY_DONE; + } =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->chg.event_type =3D action; + ctx->chg.timestamp_ns =3D ktime_get_ns(); + gpio_desc_to_lineinfo(desc, &ctx->chg.info, true); + supinfo_to_lineinfo(desc, &ctx->chg.info); + /* Keep the GPIO device alive until we emit the event. */ + ctx->gdev =3D gpio_device_get(desc->gdev); + ctx->cdev =3D cdev; + /* Keep the file descriptor alive too. */ + get_file(ctx->cdev->fp); + + INIT_WORK(&ctx->work, lineinfo_changed_func); + queue_work(ctx->gdev->line_state_wq, &ctx->work); =20 return NOTIFY_OK; } @@ -2803,6 +2850,7 @@ static int gpio_chrdev_open(struct inode *inode, stru= ct file *file) goto out_unregister_line_notifier; =20 file->private_data =3D cdev; + cdev->fp =3D file; =20 ret =3D nonseekable_open(inode, file); if (ret) --=20 2.43.0 From nobody Wed Nov 27 12:58:54 2024 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52DC41AF4E9 for ; Thu, 10 Oct 2024 09:10:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551443; cv=none; b=snMzdX9HB95UkcMwAZqpQKOU/9ztrFQex89XWJiZP4MW3mHu5ksLz/m214FrarIFz1b5oopDLPz8uut7zR037HgqONLmFh7pMuAgWSpYoEmmtR6kTA8ZflTWivfehT4TrWHR95jK5LMNKS1W1P3pRktvaDZsdbK5thKcH92LTkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551443; c=relaxed/simple; bh=aW/b/T1d54Yns4/Dn9IWAFr7kazZPhss/gdB8BCgwcc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NbG+BL35OuKdk6B4XHsqU6YfCbL9N+4QCr3WNT1Wp8kmIvcP5swX2p3boFevb9tTzmIw9dyF5gqANFHivbl0ROWmJiLr4pWwbigeWcmaLSVRquSi0rM530x779EuAz7pkR/7dPhZpl5SH2rmSc67qnMVW/AVdEzMsiQgcLPgg+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=kt3iZLm3; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="kt3iZLm3" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-431137d12a5so6072265e9.1 for ; Thu, 10 Oct 2024 02:10:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551440; x=1729156240; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=KvoKEm861qnmPYL94kUVnrmcLydUdJpjADRRQZ6lwKU=; b=kt3iZLm3KuFGROtMehJlQW7OvGk47PgthNdLV/hfyLd6ckH4rKE9ijGgmsugHpmy0O 5MPsk0YptJVmMi+uv5aZHYCjcAn1gJwOPAYCnyn2XnGdXFNo68gx3izCHl5exnun+4/X 5E5p+TCb3R/+jkSLdVovVSytk8Vtp/HOUU/ZBwaKlVtsay57etuWGRY1moo1XFP60v5w AzctradVUKoc305FY//P/7YEDkVq/9sFjB0CkIM8c0O/VPEO+A0ByBRsoLBXVbtT0fZu wK3RNcnnZDBSUtjjmnCncrASLrQVXhOFJHR7uaMdX7zO/umB4J0LtnA0cvqyL2pc50sx aVQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551440; x=1729156240; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KvoKEm861qnmPYL94kUVnrmcLydUdJpjADRRQZ6lwKU=; b=eGPgGvpQx+7Lq+RnX73g19bDxITvvaeFfbtqcjt5IN21jm2qdBwOBVYqTr5PcOWyqp zY4552g084g95yr0uwWu200aOXhdDLwrl2mzPCsyK2AlVm6hZ++mafnnoTQ9M/AR3QQS 8+izCixXRyiMXY7vUKYytlgXY0PMyDeY6z96c3LLdIznKU7eVLhBOM/JNwnBjMRad6m0 h0kXkvD6LFXFzoqXf37fe6YIZhX6n5S2jicymlgnVd9bow8OsXMA1A26Qv79ucfUdW/J rANSy0ErSyXT5rDuUj5XSrKwEtmjIGhr4xefl03pzDUhxKGcPmAQ1PHrsxZ+YMbu/naY S9DA== X-Forwarded-Encrypted: i=1; AJvYcCVlgIvNOSxy7qQ80maYhv6OXuGsdcxWSeYrd4wtsnBtRrJ9QcpIFRW0G8rc1RA97XD/iz/ZtjqGZBL1T44=@vger.kernel.org X-Gm-Message-State: AOJu0YwJjvUGpyc9S7wjQnPqnoXGTUX3SQGclNmb1bKTiR1f0xjaVnkQ 79/zmHi2dhnkgWIF3nzdMbqiCcgtHV+1nFUpvxs6sT45PA5KhttO9/+n767mbIs= X-Google-Smtp-Source: AGHT+IFKSWpq+jqGFW9ejz23rt/KEV5YFr6YCkz/jzb4YhUhAucXzJ3D8WvGbchMf1Jov1MtKswThQ== X-Received: by 2002:a5d:5745:0:b0:37d:3742:a5a7 with SMTP id ffacd0b85a97d-37d3aa31aecmr3851200f8f.33.1728551439662; Thu, 10 Oct 2024 02:10:39 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:38 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:26 +0200 Subject: [PATCH v2 5/6] gpiolib: switch the line state notifier to atomic Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-5-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4156; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=q03bW6eyBOMt/E/qq9Du/B83t4iIgrEc2Jdh/e3KSKE=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oHqntgTP9D3GRBLflqkJjoa4tgqazn3xsg4 G/OwOJGX5CJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBwAKCRARpy6gFHHX cvPDD/9Ik0eOYYJZbp+zkQrkn9NONoXJK2PKerMqcD83cWWnZ7Y7Qrn9NJnLv2Y2WrK4H/u6fzG p/Csa+5KLQkxwKZAOjs9xqhswL7sOtc4t1ROjuncQADKNEHseWriisvGsdgZIEwSMHOyHYVonBd HBvoHur5hAAFjybhap4idMToqe2JysZYGdGSs74zhTxDCVgmhZfss62uChi7heOlrpy5cF/j3aN ICPlANAsajmm2iw0iFTvsl9mO2vcUqZ7n0edZehxzCVoksXW9zdKWSl8ht1gFUgJKtX8ToFH2Rc E8fURcreYxIYpfz6gech/st9Slg39/xTVLSkjqmpO3Q6DbBNBWYM6djSz120O+Vlp9acpTPZBQD kwmNEUypsaCzbBEZwX1v7P74mY9veus6Tk8Tj3+6z0QMgeqF60c8VkAMNnVJ6MWZEeONs+nd/Ml NI4zEkxGyHfrkGEC8tRBfemImPJCl9LKVxm5LzwQvF6iDIkfAuOxkgmwoNagJQTVaRFFoeaFn46 IPsPmx3vGy3rBk4+JqE7TVvr48FObFhHcPu5ujx1DfRJX5wDBWPg63J2UF6EQvBPBC3N8vUmyWY DbilOiqpuqqUYibh4xmm6mYEMCxtU5HR5BYYuFiGvVAv/1dbVwyxWJ/XqS75eRtp5FEYVSTHvQS O9oCwfKy46HK/Xg== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski With everything else ready, we can now switch to using the atomic notifier for line state events which will allow us to notify user-space about direction changes from atomic context. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 22 ++++++++++++++++------ drivers/gpio/gpiolib.c | 6 +++--- drivers/gpio/gpiolib.h | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 2677134b52cd..7eae0b17a1d6 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2673,6 +2673,16 @@ static int lineinfo_changed_notify(struct notifier_b= lock *nb, if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) return NOTIFY_DONE; =20 + /* + * This is called from atomic context (with a spinlock taken by the + * atomic notifier chain). Any sleeping calls must be done outside of + * this function in process context of the dedicated workqueue. + * + * Let's gather as much info as possible from the descriptor and + * postpone just the call to pinctrl_gpio_can_use_line() until the work + * is executed. + */ + ctx =3D kzalloc(sizeof(*ctx), GFP_ATOMIC); if (!ctx) { pr_err("Failed to allocate memory for line info notification\n"); @@ -2837,8 +2847,8 @@ static int gpio_chrdev_open(struct inode *inode, stru= ct file *file) cdev->gdev =3D gpio_device_get(gdev); =20 cdev->lineinfo_changed_nb.notifier_call =3D lineinfo_changed_notify; - ret =3D blocking_notifier_chain_register(&gdev->line_state_notifier, - &cdev->lineinfo_changed_nb); + ret =3D atomic_notifier_chain_register(&gdev->line_state_notifier, + &cdev->lineinfo_changed_nb); if (ret) goto out_free_bitmap; =20 @@ -2862,8 +2872,8 @@ static int gpio_chrdev_open(struct inode *inode, stru= ct file *file) blocking_notifier_chain_unregister(&gdev->device_notifier, &cdev->device_unregistered_nb); out_unregister_line_notifier: - blocking_notifier_chain_unregister(&gdev->line_state_notifier, - &cdev->lineinfo_changed_nb); + atomic_notifier_chain_unregister(&gdev->line_state_notifier, + &cdev->lineinfo_changed_nb); out_free_bitmap: gpio_device_put(gdev); bitmap_free(cdev->watched_lines); @@ -2887,8 +2897,8 @@ static int gpio_chrdev_release(struct inode *inode, s= truct file *file) =20 blocking_notifier_chain_unregister(&gdev->device_notifier, &cdev->device_unregistered_nb); - blocking_notifier_chain_unregister(&gdev->line_state_notifier, - &cdev->lineinfo_changed_nb); + atomic_notifier_chain_unregister(&gdev->line_state_notifier, + &cdev->lineinfo_changed_nb); bitmap_free(cdev->watched_lines); gpio_device_put(gdev); kfree(cdev); diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 839036b116a2..9b10f47832d5 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1026,7 +1026,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, = void *data, } } =20 - BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); + ATOMIC_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); =20 ret =3D init_srcu_struct(&gdev->srcu); @@ -4089,8 +4089,8 @@ EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); =20 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action) { - blocking_notifier_call_chain(&desc->gdev->line_state_notifier, - action, desc); + atomic_notifier_call_chain(&desc->gdev->line_state_notifier, + action, desc); } =20 /** diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index d24cd9e8b17c..2799157a1f6b 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -72,7 +72,7 @@ struct gpio_device { const char *label; void *data; struct list_head list; - struct blocking_notifier_head line_state_notifier; + struct atomic_notifier_head line_state_notifier; struct workqueue_struct *line_state_wq; struct blocking_notifier_head device_notifier; struct srcu_struct srcu; --=20 2.43.0 From nobody Wed Nov 27 12:58:54 2024 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.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 D2FEB1BDA98 for ; Thu, 10 Oct 2024 09:10:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551445; cv=none; b=Ygy2phyvCIm+6mkYInDVQDembfH0TzxabLF+NfBY0m9vDFlN6lZ58hh3kHl2f3r3bKcLfHmJ+rqlc/xah7RJm+Spu6pyy3OujId653JrnzrO3Qw9MXcZw2BXALb7ut4nnwarYV8lPDQlEIJZHtaxZjmjQIvgv0lyBPgOaPMIXnU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728551445; c=relaxed/simple; bh=lRTi6HjXmQIuZIsD84KI0wNrDkAd80UhqzFW40a1TLg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=k4v3GVU28XUuhnmqJ4nfzHPvJqaDB31arFzB1i5Ay4nGt0fMI83nYofupURkN2PfyzpOxa9wU9gdX23ZXsMgGdRE6aQY9+byQJzfAoHAaJyUec4QYPpBFK+vSfvFgTF8ryxT8gJ3qXt96FlF7u5ScgoZ/TljaCnvlDMsJf763jE= 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=DuuZNZXE; arc=none smtp.client-ip=209.85.167.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="DuuZNZXE" Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-537a399e06dso696224e87.1 for ; Thu, 10 Oct 2024 02:10:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1728551441; x=1729156241; 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=i8XNd2FyPKbVaB0YC1j3Yikqd7FynNynIkX/mW8PTT0=; b=DuuZNZXEwu0QfpZ3UKoTw6gjYqc31U5t36afJyFQqgnvWSAU37FIcY4HcmhHQDkug4 CLdqbDJAMjfOwhUgUfXlo2GOvpXuJw0zHDRxPIswdGeLy2tvOmKC8pnUg7GxXnB4ub2S gHzjc9IWPOcR2oNgycHpXassbJUeDE2ucFaibSWS5PQBFuGcv9+tlKnQIk0kQnqdWdRP PlCMu35CykGeLH/jRQ+C1SSEjPjf/TMg3INBsM5cCtvSUFQh+KYSjscS6COyjw6GS6pm m3F1HkEMWTeowL+Vz4I287joCChqbjyIiclAN4UhJhj93vu0HbLQED+3wCp2Nm9vuQwO l3Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728551441; x=1729156241; 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=i8XNd2FyPKbVaB0YC1j3Yikqd7FynNynIkX/mW8PTT0=; b=VvF99Vlv3kOLAqyIquNkwYTF7sMdiPP5GeUSDivniO4TemYiArL4O9R+5DyXHR9h16 TCDZ1tu5bLXyETrHQiHM3dFGrSVDm8mS/1NoJXAEo6fXhFIz7ugcxfejJ5UD/I7tbVYC dWX0bQRpL55t9w0RlkReqsWgfP/dweBX/crW9FPTDnAbHaqzLm+XOUX6qOVIQtONrXCe PmKnD1YY0OPeLUjWG9QOwwvQX2oQWSUlQHUIzHVfNNVqYPgyJwsDgV/Z3ADg/hX3M34+ W8jKDzenOOxuQwiU1PfoElsx0I8JDYWLGhvQNzvrFRkkICsAOaXFfnNtMt/bQfmLJRLN LpdA== X-Forwarded-Encrypted: i=1; AJvYcCUc9pRka8SHYenRXckHGN9XzYxMzm/ykVg4iqqNKMQXiB25U408ycsE5l61UmqP8AXj4QvxDwIyILa1XNA=@vger.kernel.org X-Gm-Message-State: AOJu0YwIDnCdyaTVseDUfC5ZYEgg3gmP4QtO0t6cIhPS32aX2JXgvynV 1LZSmtpcMEh+L+x8sum51IXqRX9DY4EOfGOlSpxy3JgDGH4XZ9w7KIDv/qc7zUw= X-Google-Smtp-Source: AGHT+IERDCVsUENLDZYEKR3zfT9VSqgA2XcAgI+POUXG44TxCfT+MeiSsTB+KLnZ8ERHSmI05whyTg== X-Received: by 2002:a05:6512:2207:b0:539:8b49:8934 with SMTP id 2adb3069b0e04-539c49613c0mr3449314e87.52.1728551440818; Thu, 10 Oct 2024 02:10:40 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:5e5d:bfca:20cc:60ae]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-431182ff785sm9913305e9.13.2024.10.10.02.10.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 02:10:39 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 10 Oct 2024 11:10:27 +0200 Subject: [PATCH v2 6/6] gpiolib: notify user-space about in-kernel line state changes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241010-gpio-notify-in-kernel-events-v2-6-b560411f7c59@linaro.org> References: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> In-Reply-To: <20241010-gpio-notify-in-kernel-events-v2-0-b560411f7c59@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7877; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=emdseATUbYgPk+Xsat6ZPWRy+qomt58wS6OT5lF8raA=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnB5oHpr48emy3umBNf/nLJsFZqEjdB0aSjCUFf ePcCNg9q4KJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZweaBwAKCRARpy6gFHHX ckITD/oDPoKNQxYhDR9e0SHIrSjxle0eORVcn8MyYyedENcVWOVwInnhAb/zR1YxBRaVWZZuz0J gKsO36NSWe18hI0aMVmc/jG+JkLcyzHWOK5bZEqUqIfQPryjcCVaCICM6ytp6U2IJtACfXZN/D+ O308vDc++VVW4vlRop3sLa62Gzy7KR7vIsbAzXuf8ALtYqCmkKLDPyx73HGy+8aB4hZTEMTIXpJ z0/0mz6MIllXxhShm4H0xAUQg+wdsFCl+3z47WlihhlpR0aG6GgFjRv2kLlgMkxVx2HRBaJhbhI npeBtEY3Pu41PVXmOrxpwGWIRC4X0U3hVsQtesy274WugpPyRtv+V3SZz53/qbLbCVinLhfAswn eCnrkosLLnyFQ5WnJMO8BUEWwlHWZE6S38j0F/51n2pDKu3NzN2n4zT6SVKlEXCrj/25nC/knDv z5lpKYkG5kySSCkAhTBvL6l0qq2tmN5HJOPGVWmL2YtOMOezSLKiT0RCpxmy4C13Rz40abNLFq5 WAXEtdsTx33aEY6+FHskvnK788ix89Su3kNRFUfaUoPnubH7WCDUD+J22tGgEnBAllumv1Epw2g FV0GjjJcimaVdGR0ZBTzx2tHRdpdtyEc+TTn0tVrTrRUl67sN4/2EIJGYcB329KwK2bqu9uy/U3 iSzwIGfzk+unCGA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski We currently only notify user-space about line config changes that are made from user-space. Any kernel config changes are not signalled. Let's improve the situation by emitting the events closer to the source. To that end let's call the relevant notifier chain from the functions setting direction, gpiod_set_config(), gpiod_set_consumer_name() and gpiod_toggle_active_low(). This covers all the options that we can inform the user-space about. We ignore events which don't have corresponding flags exported to user-space on purpose - otherwise the user would see a config-changed event but the associated line-info would remain unchanged. gpiod_direction_output/input() can be called from any context. Fortunately, we now emit line state events using an atomic notifier chain, so it's no longer an issue. Let's also add non-notifying wrappers around the direction setters in order to not emit superfluous reconfigure events when requesting the lines as the initial config should be part of the request notification. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 12 +++----- drivers/gpio/gpiolib.c | 71 ++++++++++++++++++++++++++++++++++++++++-= ---- drivers/gpio/gpiolib.h | 2 ++ 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 7eae0b17a1d6..ff702fe59ecd 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -196,8 +196,6 @@ static long linehandle_set_config(struct linehandle_sta= te *lh, if (ret) return ret; } - - gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); } return 0; } @@ -363,11 +361,11 @@ static int linehandle_create(struct gpio_device *gdev= , void __user *ip) if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { int val =3D !!handlereq.default_values[i]; =20 - ret =3D gpiod_direction_output(desc, val); + ret =3D gpiod_direction_output_nonotify(desc, val); if (ret) goto out_free_lh; } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); if (ret) goto out_free_lh; } @@ -1568,8 +1566,6 @@ static long linereq_set_config(struct linereq *lr, vo= id __user *ip) } =20 WRITE_ONCE(line->edflags, edflags); - - gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); } return 0; } @@ -1826,11 +1822,11 @@ static int linereq_create(struct gpio_device *gdev,= void __user *ip) if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { int val =3D gpio_v2_line_config_output_value(lc, i); =20 - ret =3D gpiod_direction_output(desc, val); + ret =3D gpiod_direction_output_nonotify(desc, val); if (ret) goto out_free_linereq; } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); if (ret) goto out_free_linereq; =20 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 9b10f47832d5..0986854ab1ed 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2673,6 +2673,18 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc= , unsigned int debounce) * 0 on success, or negative errno on failure. */ int gpiod_direction_input(struct gpio_desc *desc) +{ + int ret; + + ret =3D gpiod_direction_input_nonotify(desc); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; +} +EXPORT_SYMBOL_GPL(gpiod_direction_input); + +int gpiod_direction_input_nonotify(struct gpio_desc *desc) { int ret =3D 0; =20 @@ -2720,7 +2732,6 @@ int gpiod_direction_input(struct gpio_desc *desc) =20 return ret; } -EXPORT_SYMBOL_GPL(gpiod_direction_input); =20 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int v= alue) { @@ -2782,8 +2793,15 @@ static int gpiod_direction_output_raw_commit(struct = gpio_desc *desc, int value) */ int gpiod_direction_output_raw(struct gpio_desc *desc, int value) { + int ret; + VALIDATE_DESC(desc); - return gpiod_direction_output_raw_commit(desc, value); + + ret =3D gpiod_direction_output_raw_commit(desc, value); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); =20 @@ -2801,6 +2819,18 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); * 0 on success, or negative errno on failure. */ int gpiod_direction_output(struct gpio_desc *desc, int value) +{ + int ret; + + ret =3D gpiod_direction_output_nonotify(desc, value); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; +} +EXPORT_SYMBOL_GPL(gpiod_direction_output); + +int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value) { unsigned long flags; int ret; @@ -2863,7 +2893,6 @@ int gpiod_direction_output(struct gpio_desc *desc, in= t value) set_bit(FLAG_IS_OUT, &desc->flags); return ret; } -EXPORT_SYMBOL_GPL(gpiod_direction_output); =20 /** * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds. @@ -2942,13 +2971,34 @@ EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns); */ int gpiod_set_config(struct gpio_desc *desc, unsigned long config) { + int ret; + VALIDATE_DESC(desc); =20 CLASS(gpio_chip_guard, guard)(desc); if (!guard.gc) return -ENODEV; =20 - return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); + ret =3D gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); + if (ret =3D=3D 0) { + /* These are the only options we notify the userspace about. */ + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_BIAS_DISABLE: + case PIN_CONFIG_BIAS_PULL_DOWN: + case PIN_CONFIG_BIAS_PULL_UP: + case PIN_CONFIG_DRIVE_OPEN_DRAIN: + case PIN_CONFIG_DRIVE_OPEN_SOURCE: + case PIN_CONFIG_DRIVE_PUSH_PULL: + case PIN_CONFIG_INPUT_DEBOUNCE: + gpiod_line_state_notify(desc, + GPIO_V2_LINE_CHANGED_CONFIG); + break; + default: + break; + } + } + + return ret; } EXPORT_SYMBOL_GPL(gpiod_set_config); =20 @@ -3015,6 +3065,7 @@ void gpiod_toggle_active_low(struct gpio_desc *desc) { VALIDATE_DESC_VOID(desc); change_bit(FLAG_ACTIVE_LOW, &desc->flags); + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); } EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); =20 @@ -3659,9 +3710,15 @@ EXPORT_SYMBOL_GPL(gpiod_cansleep); */ int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) { + int ret; + VALIDATE_DESC(desc); =20 - return desc_set_label(desc, name); + ret =3D desc_set_label(desc, name); + if (ret =3D=3D 0) + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); + + return ret; } EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); =20 @@ -4539,10 +4596,10 @@ int gpiod_configure_flags(struct gpio_desc *desc, c= onst char *con_id, =20 /* Process flags */ if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) - ret =3D gpiod_direction_output(desc, + ret =3D gpiod_direction_output_nonotify(desc, !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); else - ret =3D gpiod_direction_input(desc); + ret =3D gpiod_direction_input_nonotify(desc); =20 return ret; } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 2799157a1f6b..fc321d281346 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -155,6 +155,8 @@ int gpiod_set_array_value_complex(bool raw, bool can_sl= eep, int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); =20 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); +int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value); +int gpiod_direction_input_nonotify(struct gpio_desc *desc); =20 struct gpio_desc_label { struct rcu_head rh; --=20 2.43.0