From patchwork Thu Oct 17 08:14:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 836367 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.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 A0BDB1D279F for ; Thu, 17 Oct 2024 08:14:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152871; cv=none; b=Y9bCwEyzM5Ru1uUk15iuoKtx7lOxvX/dsooqCWeFwMNSuk1n7hOYn77IAF46BJTOkKVx+UY7vsLy7ZachMlQAzU4MU43udqrpPwAWxisoEVCPgP+VmexPnsl9EYm2pG4lp42TILBN8oEZxRkoYiC5U4T4gQ5/ef1vrO0R1P0MYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152871; c=relaxed/simple; bh=POHh8g+2CvyZoU0L+E1KmaYOw5grZB9aKoaGo7YEz6Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sf9NaG7HVFcJ8ZS2QQ0A+PL2wp3spzvsl5hbyhtOgNelnQsqgINMNrKwNP+lOjlCcX1RTIWOODSru8hG3lj0v2MTUGjFBvnL/nAXLYgboNmdLVxmF3QtjM3pWP7BGVQio9BwRB2Rwd2KaQj4CQX426uTo3LgY/IdWQHxVxLWrAc= 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=TxxGpTwx; arc=none smtp.client-ip=209.85.167.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="TxxGpTwx" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-539e59dadebso978517e87.0 for ; Thu, 17 Oct 2024 01:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729152864; x=1729757664; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=OB/wBkvSEUCyn6Pce3Sw/d9S+YTAmxmeVBF6YPbGUIs=; b=TxxGpTwxES3o/RKFvyDwnAMwVyIgnpZSed7J8NEJ/2+KBPx3rxk78RFPvly6bPPPe1 sqIk15Tq8ARXGtSEqR2RiKHUYrrF2SeP1XfPdw8lIan3oVEKTbD7VqjrlV/mVv9G7aCb fugp19M8vdz27uQGm+YHSDHGTVekkqonePHdQJJ6qBPGVwlWHRDSUZT5r0MQKNxfUASe EcJPBezCjjUIMm5b9QgmEXWW7EXxydKIzFzL3yhT7r4QBOFv7gpe+S6cW+Zo4AzJgmVW fBozIrlhvdtIk4s0Jw0BOxyDpQgQ7Mdco0j37KYeWGR3XXV0RzVjDWelpy0KMlK0yk2c U/Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729152864; x=1729757664; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OB/wBkvSEUCyn6Pce3Sw/d9S+YTAmxmeVBF6YPbGUIs=; b=Sy4sLFRkHj9DBpO8/UVarCIc0dDoxORsQvkEUD/oaAmnJp3NinswmFpWHRhQ6cjVUq TPEz/okURGBhojZeMR6bLu2WBJpjtx1cvamOrnb2r8u3nU4vazg+jHh24uR4jB+j4rY5 p/qrkqBcyntqA9JvuEGTAoXvp/CdSKjdXrkg8djS7GXHsZknWb8z1Gxpk6aaCGvDCxKP 93WlpyWHXfJUQrjkLu5mBFsXB9PhEf8MsDOxXCn5hRvTkQ2DH4Dw5mzVIG1jpT3vcjU2 5UtWlktB8awDN6q3QB5qnUsZBWkaAfFF+4yRVxahIgqLMcZxXQ5aVyEzTzG63a2n3uap L50w== X-Gm-Message-State: AOJu0YzucPiR4qPRxxJCT7b3Q1KDhOprswUfQk6FCiZvkSuG3P6agTHc G9Wa1r2x4Tz+1InonCaXWSMMA+1+xylGWVtuiYyJaGIIXnsdZiZgeYv2zOtRL3M= X-Google-Smtp-Source: AGHT+IFRnHFVacQn63nVwfrSGciFYzsNocWAWHtg4UziLMgw64RaHnmxTESwoE68vQdpohDATLOkvw== X-Received: by 2002:a05:6512:ea0:b0:539:f471:bcfa with SMTP id 2adb3069b0e04-53a03f18296mr4890823e87.14.1729152864395; Thu, 17 Oct 2024 01:14:24 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:3831:fc61:16eb:d0df]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43158c61ae2sm17855695e9.46.2024.10.17.01.14.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 01:14:24 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 17 Oct 2024 10:14:09 +0200 Subject: [PATCH v4 1/8] gpiolib: notify user-space when a driver requests its own desc Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241017-gpio-notify-in-kernel-events-v4-1-64bc05f3be0c@linaro.org> References: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@linaro.org> In-Reply-To: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=782; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=zMsMSJX1Nl0+caHE+JZSEzX7KhbE4YG1tQGrQmi8r44=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEMdcHXhTx0y5m8JPij1Ef1Zc5QZ1ycidlcQUc uaji8i8gw+JAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxDHXAAKCRARpy6gFHHX cjAaD/9yKJH6txh1M0Oi2hTCivfnX3YO/dSu5lRS6NPhWUNYQ4V5h76Ns8k1wDh1FvKOHTlJsNA xrBqiIiFSfXdIf97ceyyAVcRKPR8vCF8O0chLadgqDA5cSo+A8JN8GfxLFQn+dUnBJYdYBE8Ehg dZ+Kf+lFAAGRutsrDWQrMrWbU7Ww1VYsAHL/K90Mbu/KCwMfQeJ8Gg9hvWX/vjaIAG6kaAUkM6c trHoGhz6X5GvoxIHKaH6tjJMvljez3/1cKVBu52Me2yGGirgYKG+wDKZVmRawRjMxwAMBht+MD7 gyRZBKZOacWI2hyO6h4ExRkPEMeSuV1Krbjh5w7AFnnMV0z4LraN/eIoXFIUUceTlTaYibi9cDn vxJ80ysO79/jbXUYYzdcUkYTZPyV32+zt0kP70m4jxf9PxAj/8rTN3tyH989qXNLBSELjGosILQ 67bZsa/y2oNE5hxbLDAjp/ig+9hETn1gFZiNa5bLn+pjgFjrcW+hEAhT8WuNbjkAxOqmUvKJkwA gk5jorR6IWr9hoQhE1iGDQ7+cpTKAZ/xu4ZTnpVsKG7AhYcOIC/8W459XD7PoP9EH+1KAqzNQkX Poy39h3m3noDJFx5GwjYBg92KuD8hYpnT7UbODhVPNg+s5GFcTQzvUrsJpwf0oYcM4AswcYdRCa TGMw9nDmyscoFfA== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski We notify user-space about lines being requested from user-space or by drivers calling gpiod_get() but not when drivers request their own lines so add the missing call to gpiod_line_state_notify(). Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 97346b746ef5..c09464f70f73 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2532,6 +2532,8 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc, return ERR_PTR(ret); } + gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); + return desc; } EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); From patchwork Thu Oct 17 08:14: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: 836368 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DE8D1D043A for ; Thu, 17 Oct 2024 08:14:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152871; cv=none; b=IlPCIiWHB8FnRMQLTsPvaTeLI3/U8l4wINu6WxIAikmOZ0Di4/lTGqXE+Oqn02tniUf/Y/joAw3MUuUdp8Petoxuu531izjof5lCBGB5sO9oKgeC99sQmt7uWitr4jjXohOm8gcK5cHdYUEl6xJTsRlKrhrsyYOcnUrvBTVOH1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152871; c=relaxed/simple; bh=ND47jIbOYDmJs8MpSoNGSSJKSen6G5z88AQQSxF1llA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=S/ivHz7g5VX5jtH+v7GYcfKeZ09tJMDc8G7dDmljrDKnyliI/0xhPjTdt7DEtIj87oMzcK8057beFkY44KwtEyzCUUHPlEVWcwJEuwZRP5ICNnFNlhh88jHQYMAksLgZEB4LH4SmOGCpYengnsaVNn1vB27APnB2SyCgSwVWvAk= 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=hswZcMdZ; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="hswZcMdZ" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-431195c3538so5768205e9.3 for ; Thu, 17 Oct 2024 01:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729152865; x=1729757665; 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=hswZcMdZDg6hFDe6alxWVTtCmypqu68euJ+95ao7Rz+pRYOgIbkKmiF/w4+3cb11Z+ 78vbBHe7s5/9BK8rDrtXUk21UVSlNrT7jT4TBi3ng8DWPw6huE/fBM8ilwiwTIbUfJ66 j42rkwXra2k9U73tya8IqMnj1DwYl6FX7IhePwZFF4NZjIjvRRT8FJRNGYKKAXuk0Km7 2iWB73yA4zlt89BcuKsK+mESOrUBmSfPBZmcfQgwsZdxUycDcBIMX4mcE2frH+LjgQQ8 KNwbCZvwyIqh2C7mzDNRsw/T0E/03U8tK+bVTd9V9y/k54HlR5GW1ssG1DOHbbMeQKk3 1EHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729152865; x=1729757665; 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=qptMGBAHHjhjuOKv8ljhSBl66Z1J2+GP6T2/23Tol54WOouSEvLdMZGXSR9mngSQrR udolne1L7iZlqvoN4vuf9dSQoaivIySnjFQWH2MlPP43VVFAFgjWeF5x5Hul8E712b9e wP6gk1zcvABe78B+ATYc10lPXTs3858p4d1ZKjQuMyswRwVNt8MnhhvSY8CC0nQbER2A l9nccIidQo8fbSQ5juwU4wxhr3i7Fs/oNUEHyIDppDLY6yFJJWPPKTi0DOfs3vTK7rg7 A4qfGIgud4al0X9u04IFTdV+ma15/GRsCHXJFKhRSoATNyj9qUeXI8mIQhUCeKmkTH7c 4J/Q== X-Gm-Message-State: AOJu0Yx6h0vLCibCYDC4cxhO5MrNCgv4tWhV0Ow4JsA7IP7pdtiKyTwB qVK4cDHdq/+wlTx+4KyGg3tIadD6VTw0ZUGM41rdPQ3RIcGJ8m3V4zFU+LGxH5I= X-Google-Smtp-Source: AGHT+IHzF2qT2sTor36rFrvkN3Jdnog6ETuCHp5zH8opv/EtydEdzHVKUwFbUeojf/ZStUGQnKNYmg== X-Received: by 2002:a05:600c:314f:b0:431:52c4:1069 with SMTP id 5b1f17b1804b1-43152c412eamr36318205e9.8.1729152865107; Thu, 17 Oct 2024 01:14:25 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:3831:fc61:16eb:d0df]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43158c61ae2sm17855695e9.46.2024.10.17.01.14.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 01:14:24 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 17 Oct 2024 10:14:10 +0200 Subject: [PATCH v4 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: <20241017-gpio-notify-in-kernel-events-v4-2-64bc05f3be0c@linaro.org> References: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@linaro.org> In-Reply-To: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@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/ZANAwAKARGnLqAUcddyAcsmYgBnEMdcC3OI1Ir1Rau9V0RSZUWiGg3TGO6oiTtTw F6cv6JTpD+JAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxDHXAAKCRARpy6gFHHX cjU+EACP3LgjIItD0S2u03ccGO+2y1ZSqg0lUrZM8TUzO903niSMQUprfHC6Jx8nrNXMnLPSA4J JwJIrEBTfgzgohscQGfcDX3N6PBccujjHsScvu1L8oA1SNDzXj5vomZ1PKiQU+mm86h0k36EfRQ eQjQBSVxoCKQAVuOYnaiaugxv6dIA3DaHZYnO+1XgTH0Hlyn8GcWhh6t4SZ3k3EbUyhNnpNNiBj zJuAm7eyRm1/hSDrXoEF+jhJK5tgEb54n2Ev/eDolo6xClytxATDy/d9HiBt/cA+fa3Ih7/vq8+ UKaJSEhqrkEckWS16uzwPDWx9b/y786y2a0lvGwMcSxXxWzdS1eU+2LBPQmhDAW7FqW0+6AzfKU h2mlFiOlAzYEkyFNe/hdJR5cFOfzV435FDH0q3jZxQ6fMyL5/cuDh8Q73Xw/toVvVnt0pUGRVaL QZx65x/kJgje9LKVa9YTn5iMWB6tKYKT2SOGdaAgu6g4UUNbGLqxvxR2N473U/ieg74CYCzNs1b fieV78I4ek9ic4e6Qi+gC07CZGd12EfaQUFloxbphdmN9+MoiTfoZCDmijZhTkYzTMYJx4jHcuy UZaI5nAhcuEB2PY4MGbK4p0sqfSXKYmR0MxF2ZgWSnejtiEaVQhpPnyGpSIpOADGW+UwH0eAwV6 b/iqhtolvtslxQw== 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 Thu Oct 17 08:14:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 836366 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 5FFDB1D54D6 for ; Thu, 17 Oct 2024 08:14:31 +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=1729152875; cv=none; b=YRWFuYyWN5bh6uzQB2WfsqkeQTv4cCRiDwmSlbs7F4qyFAmUP6v+mSwlk7E+9csi6h4lNxyxD4zeXcRYsA0cfiDqFwfAGn2TU1SMHiaAaNQ5y/y229lRNlN7Dq7XknvWTEDyp663hUG/psWAYeSwEHK9PfBECb8Pkrk7opWocXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152875; c=relaxed/simple; bh=ShKnfVnA13yZAURxSbfkTfcYBLmCdqbi8FWGA7M737A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WMjdrs3ZoBMOYYZX8UnqG019NX6cnWrxJrQhCNGPpI1X6IrG1THJUsgcwayiuXmoNwzqVL+YzSlvFD6BkWntV4GsOmNEO69XdQ8FpZZ9RIa8VbFythRhL/t78cB7kXbp7w6D1yeAO4kUh0pkIigu868SzWBmonXzytNV+Ka/Tv4= 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=GTwMu2rx; 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="GTwMu2rx" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-43055b43604so7269815e9.3 for ; Thu, 17 Oct 2024 01:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729152869; x=1729757669; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=U0tIf0jtJWFltu4ODvVnK3F2kIEgN0qMoeq7qRt5Wac=; b=GTwMu2rxDD2gO/6jJjiZ8eoFRQ5zG6r0m/jb67a7KIKYrZek/qE00hT/k59q5nxUze MoT1Ft/vNeEkadbafCeva+e5fnSC5qmIDEnvz9FJlAAAe8vSETSOVDqhXCIyAVb3pqRG HQFA9LzIrIJ8KCBARZnAu9vlxtwkDsAQUbrGjUl+aouB+O/3ZxEqTzSiaq10bd/Pw8Fw 7ecHNG9WghLHHwvEpreCJRDiO6OBCNnIQxGsTG6fXEjITbQPL78nECY6rVM5w02sT+kk OkxaeJs2Sf/cA4N4MDCDJUHO9ca5SDr5idc3q+Cd3jVx2O3109UywMKjFndMkBH9Ut9F RCWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729152869; x=1729757669; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U0tIf0jtJWFltu4ODvVnK3F2kIEgN0qMoeq7qRt5Wac=; b=f9vPl7mvx+qsyRAkVmF/2V6l999fxwx3qo1Nvy8oWLwdx94UquF4k26TJ4+4TqGmza L1MtT2angYg6MZlpcpa11Us01Sac+98Gt9S19oJTRtcLXB2XkimdNGzh3zlAUwPBbxMw 1bxT8ZxRu6AtLxNDBD3mnGWLD+RrB12QE8DKifEMlE/cgtKg06UNiX6dJ1yqxlihPC3l jLEYKEI+qw5uAHt+ODKzDnvfPmDwzzj9r9ue9ZWbMNJzFe7oEwNerZvLAyW35GfU7+5R ePvE0XBcRkDeT3Qy11GzBp+kJJzruZd8SoMND6jBmHn36GhyAr37GGRCWTjGfZDHtyw4 xuWg== X-Gm-Message-State: AOJu0Yy/PduWL8gZUETqLdcgV0KKwIPEoiJxysImPCrFpxy02wWgDz24 PyuIdYj8BOjr1aeBtjLw+pbPtvT3ORY5VIe/E6OrWBnjko54DHacTSuwYKdjNH41tx1OHtgrZkg x X-Google-Smtp-Source: AGHT+IGMTAgLIra3AwC4js0TpQefOyARSjB66JJbjktTXIzzaEulPCK8EZEQPJ+r+DlSkK0sB46KMg== X-Received: by 2002:a05:600c:19c6:b0:430:54a4:5ad7 with SMTP id 5b1f17b1804b1-4314a295480mr58184505e9.1.1729152869469; Thu, 17 Oct 2024 01:14:29 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:3831:fc61:16eb:d0df]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43158c61ae2sm17855695e9.46.2024.10.17.01.14.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 01:14:28 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 17 Oct 2024 10:14:13 +0200 Subject: [PATCH v4 5/8] gpiolib: add a per-gpio_device line state notification workqueue Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241017-gpio-notify-in-kernel-events-v4-5-64bc05f3be0c@linaro.org> References: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@linaro.org> In-Reply-To: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2506; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=0P2NY394anUazMVeFpGs/9LRMhoWr2qMp3sLio3SuhQ=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnEMddVOmN85FNOqbwEfYAB3HukIVwmEvIMEHLF B9/yJf8WlyJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxDHXQAKCRARpy6gFHHX crDmEAC4/89OesDABkpdoqYlHW2PfH2sn5oG7KRFR6qFTp3t+zpvYXzRCZ3KSOg+WxuFTkMCjdE WxSJ1BiaA8TlB+kGKCP37xD4HuzMrtd52hp17AkFYV/4giuJAwrdeSrB44Lvqj1oav6Wk7RRA27 LFQzd0vnWSPytvCq3awxuUriJ9eMWSz9XWfzGplHlT3kAokxiRZnd1rTEJegDPMcLzdgkstZ0Dx ORlmkBmK62iKsW31zkGdBhsVJ0+tMJkYv6ince22DmpKULMe8llCRWgFH8Eq44rR727oST7rA4O nGEx08FDtwXGc4C0LcBd7ned7i8ei6CQ+8TfFArV5UDYpgWpFYg8my5ezJYWkkvo15X+Vw//PO8 K+wCLDq7MdqBfLOpxDi753gpXrcG0wFmg1xJMbfoBxi+fBPZvJR9dywonDOJ0p5C4iqEdSZFa0N 2FQtTqvW/xbVSLXxOoWn4wcCrrJ2EcHot0jb23igDjtgAS/i0YFYdPrdNUTLyhc65Etqv+dRxU1 TM3mMlYGLS2zYslBhHXrcfCY5ipKGc0PsnZaS5N1i0/XMp48lQI+ea2khElxFX2EJJEVH9WIHQB Q/Kv8Qnx7reGIbHWSqXIMApnRG/c40yMcrb7a0iE4F5IA2s6vqz6V+rhwDD5PgY6BNgjOj0fs6M TXbNga2uxdaIVrw== 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 0cba74381687..b242fdb1ad28 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2749,6 +2749,11 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) gdev->chrdev.owner = THIS_MODULE; gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); + gdev->line_state_wq = alloc_ordered_workqueue(dev_name(&gdev->dev), + WQ_HIGHPRI); + if (!gdev->line_state_wq) + return -ENOMEM; + ret = cdev_device_add(&gdev->chrdev, &gdev->dev); if (ret) return ret; @@ -2765,6 +2770,7 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) void gpiolib_cdev_unregister(struct gpio_device *gdev) { + destroy_workqueue(gdev->line_state_wq); cdev_device_del(&gdev->chrdev, &gdev->dev); blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 8daba06eb472..8737e1641278 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -17,6 +17,7 @@ #include #include #include +#include #define GPIOCHIP_NAME "gpiochip" @@ -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; From patchwork Thu Oct 17 08:14: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: 836365 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.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 185111D26E8 for ; Thu, 17 Oct 2024 08:14:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152877; cv=none; b=s6NEUuy3XZIXY3T7t1F4+fhFOn+K4sLZJBHYNj/5OLwtXrxZN3DKt2KpL/LUEHoM/5mIpa2xP3hdxw5pNR8rQPsLXKLpzmYn7oKBOxAfBprHjQzfbVKjqNoDdNdbNXy15uuAVSBE8vRs8ZZ+MeJvTOIQr+K1bK3mGTljc6jp5Dc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729152877; c=relaxed/simple; bh=PrpJFSn1v8nec0wiPaBWRUGeCDKvtDkJixy1vIPLnV0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=b+CFVImn+88miyzU/et0Nf4XWfwVgna7pfcKNbIOhM+bmig+WWWXMSOhD3+YNoeqrWpUp9GJYzgDySm0t5pqseHtspXTHV1Goss/EjN2H0DS2MOmfo4RjiV1eo9UNtFfYb1c2B2bzKNrrjOKpG0F3FNlBw8sTqF11JhzVB96hCo= 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=Qo9Y/87l; arc=none smtp.client-ip=209.85.221.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="Qo9Y/87l" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-37d462c91a9so398496f8f.2 for ; Thu, 17 Oct 2024 01:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729152871; x=1729757671; 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=Qo9Y/87lO3P/nODRAVmqG/Xg3U+B0Vb6jfcD6zo5PFIdtLnsWzcS47FhPwFI/Tcixn f/oOqPZvehFR/Ej4rIgWgfA0g6psGOHIoGUBuH7P8uCoa6XZQE0hCllEpwZrf/OGHlFj bgMh2orUInAvXSktP25wQojYkLDwj3Vp8LezdtdPTTaM/hBSqsBKxT3xRO0OJOYMKyx0 w9DZAodfl2EmS0yW796hEKBRB0HpzXCox3HrSxGnGo31abRBQzoLn1XQykIghw+jr5V5 z+/Nnj4c3MIMWFA6cHaLGOe5hbRFx7pSfLfTLH3iSE5bdEnOIgzzWO3aNcJHlNp11uFE 7nVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729152871; x=1729757671; 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=QYuKrVpB9D9ZpMLOC7X/nf0leSwdXcUqH8De+7IbcARLeQ8nQlENSeURL5ivOmQuJV wDJX5GvDgn5MUHm24exGJSFgQqljxmN3GrGY2p0FvJ9M00wNIRCceBLEsaR0+gJNbUd1 A0R1JALfIBHtzxdE4PmEKSJRNQw+IHJIWfw7KtBOdhd2oYOJ6Yb8GYuzOxLVd/MW3Ysy PPJPE+kh3/usqvYGDt3vj8r9o4m8zHhYdYcWy3NDHBeSRPCgJV5vgsM2NvU/SnWYcIcC mGF7IW3Gpl2M6QH1ovAnCkGtkhj3lBrMtAT/TiWMR7zkIQsFqiE4tkj10XzQ89sa7aDs 0rlg== X-Gm-Message-State: AOJu0Yw+UKUkh3MVVQViyYjVtc9cdsF2jlQadXXw53RKgCbrxPOrTKg8 rcmxigHXWlZecDmSLtxRSANQgZg3EUIkzF2rHdlpAJpcAjg4SYyhZGRLQCamFKA= X-Google-Smtp-Source: AGHT+IFp91K7aB+pkhTWU5PY3/I1G7XdXkQZ/Im9BRoGn8XOuM5lVIYcdL35JpTmPY+jKUHhfHHsvw== X-Received: by 2002:a5d:53cd:0:b0:37c:c4bc:181c with SMTP id ffacd0b85a97d-37d55184de1mr14688262f8f.11.1729152871206; Thu, 17 Oct 2024 01:14:31 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:3831:fc61:16eb:d0df]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43158c61ae2sm17855695e9.46.2024.10.17.01.14.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 01:14:29 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 17 Oct 2024 10:14:14 +0200 Subject: [PATCH v4 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: <20241017-gpio-notify-in-kernel-events-v4-6-64bc05f3be0c@linaro.org> References: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@linaro.org> In-Reply-To: <20241017-gpio-notify-in-kernel-events-v4-0-64bc05f3be0c@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/ZANAwAKARGnLqAUcddyAcsmYgBnEMdeWUyYkvPl7I78riyiWYJVx4BYSRlNCtrtg LE7hAGq8XaJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxDHXgAKCRARpy6gFHHX cihHEADF1jNbnrL74iwG7sGU7ba916dZuIrsffm24qBZWXgej8JqhlYO/O0rKDMnAppERCpjsL4 mfLFjLN/u/RTLOBxRy6cgQpCoWeDKYkj7KKf/6T9dR63GiErCusSOCSYLzV3QbBpB6yI79AAxsV 6skhRcLNNIEpE1VUPdAdByRNmb3ncm0mIofSuLOfElrvVsqSo9ArZEgqcNVs8acW7IuclxoGQPN wjaWvBJhWen03AkdLwoKQUQQ2w2UeKVpSlfSENK2+GHfy+VpmNv+OQWbLg6FRc6gv2WxIVinM2i c4PrGc8BcZAGhu24WZcqkcEZVziOQ6JjDeZA584FZYxRiv+wa/JKVO7ajppIRN5prkSY9mzGBSl eTBqufaeTaUvBqyg9gavvx2zmBEGvmC2sNNX53lFO+ZLA9BlxC5Kgnwle1awYMWp1cVtIyTqK5O RUP56s+Dia7t2C2BA+rC1PP2c/z9j9HGSPZiUqCbVzTc+waKu0j+86yVbDpl7BHIU41VVWXICs4 XlZTgM3Jo2+DtnslQcNMsiS5D016n4IVQu6+zfikJP2v9i7BgWXmiZVkchJPKsf4eBtQqMH4H1L 2r5laGqKrRp3JIwhJt5PoNHSSHwhCCMCkgWblDrruWFQ+T1y1k7aRggDdve39bJG5VfQve/h0Xn y4THTRKYjkR2pog== 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)