From patchwork Wed Aug 16 12:20:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 714476 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C81B0C04E69 for ; Wed, 16 Aug 2023 12:21:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245070AbjHPMVB (ORCPT ); Wed, 16 Aug 2023 08:21:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245074AbjHPMUj (ORCPT ); Wed, 16 Aug 2023 08:20:39 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4DF72136 for ; Wed, 16 Aug 2023 05:20:38 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id 5b1f17b1804b1-3fe32016bc8so58550545e9.1 for ; Wed, 16 Aug 2023 05:20:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1692188437; x=1692793237; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dBA1IVl8UDrUaAnJphV0hWGX9hyQzKWTZG3aW+04r/w=; b=uMRrmYNn8OhxPbzEV75US9J4Qp1kutfTE6aCNbx3JPT8CgSYkIreOHTMZoTuI5E5K2 DJsFU3qL0dW2325cH96CCoG1sT6euM3YvFl7WpN/VOK1qNRXyrczMNVu1fTlia4xI5zA i1B7qrZQN3sGfET4pinIppZaDWQ4T5lAp5/39j2/hNY4GOndSb/nlr8anxQvZGVxPQ7T CepXRxPuOkmtqwGJBTF972YXnn9VhjWRXOs/msMFpVywbDgfTVSbFy636vLJjyToh+aC Vgk3/zzLy0c+RMbnZGMh9a6bqaEW4c9K+yM16WJcWZcixZJyiYyTlcNRhBvjxADi61RT HBRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692188437; x=1692793237; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dBA1IVl8UDrUaAnJphV0hWGX9hyQzKWTZG3aW+04r/w=; b=EemBvt3zP5ud9EBHuRLSJG+9nOeNvhYzaGfdo5TgtMbyGI0yVWdDMFW0exbTVcXWZ0 V7VsTEgPDWW8bXXIRLrLh8RGiiHIZMf+EIay33ZXWciLFgjrs492vekOFZ9Nns5zWLx6 xaqSySNEFweTHjdEpsDvQiDv2XeehCtx9FvuudAPVzkDjDF0ajBWDYl24XbQmDMoS2iv fhc/CHJ+XP2rPFQNIVSB97FFR9PHEvTeI3bxlh0n8YyfjvV4gked7oHUjZLtM7QvKWbS WdQLyxQJnKVHsW4UoJrU407++1hvbNH2ny883CTv/3eJnniyLee2mLN6FN4ctApM0byE gC9Q== X-Gm-Message-State: AOJu0YxPj+6THvYc8onunfdexhM9SQu1Ab4rGiBMyhhgogvFjyTRfA5r rSYZs98OW6rqdYTXfo7Lkkya3A== X-Google-Smtp-Source: AGHT+IG1i8Q4sLfL31T9MsIoX7SkF8QEPazR8Ad7Rpp4u1ezh5QqceyHDr+yieA/tOxrDBu6ETL2lQ== X-Received: by 2002:a05:600c:215:b0:3fc:a8:dc3c with SMTP id 21-20020a05600c021500b003fc00a8dc3cmr1215136wmi.37.1692188436544; Wed, 16 Aug 2023 05:20:36 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:55ba:c083:817:86f]) by smtp.gmail.com with ESMTPSA id e6-20020adffd06000000b003196b1bb528sm14566112wrr.64.2023.08.16.05.20.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 05:20:36 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 1/5] gpio: cdev: ignore notifications other than line status changes Date: Wed, 16 Aug 2023 14:20:28 +0200 Message-Id: <20230816122032.15548-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230816122032.15548-1-brgl@bgdev.pl> References: <20230816122032.15548-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski In preparation for extending the role of the GPIO device's blocking notifier, make sure the callback used by the character device's file descriptor data checks the action argument and only reacts to one of the line state change values. Also: relax the kerneldoc describing the notifier as it will have more responsibilities soon. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 35 +++++++++++++++++++++++------------ drivers/gpio/gpiolib.h | 3 +-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 0a33971c964c..062521e1a9e0 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2502,22 +2502,33 @@ static int lineinfo_changed_notify(struct notifier_block *nb, { struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); struct gpio_v2_line_info_changed chg; - struct gpio_desc *desc = data; + struct gpio_desc *desc; int ret; - if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) - return NOTIFY_DONE; + switch (action) { + case GPIO_V2_LINE_CHANGED_REQUESTED: + case GPIO_V2_LINE_CHANGED_RELEASED: + case GPIO_V2_LINE_CHANGED_CONFIG: + desc = data; - memset(&chg, 0, sizeof(chg)); - chg.event_type = action; - chg.timestamp_ns = ktime_get_ns(); - gpio_desc_to_lineinfo(desc, &chg.info); + if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) + return NOTIFY_DONE; - 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"); + memset(&chg, 0, sizeof(chg)); + chg.event_type = action; + chg.timestamp_ns = ktime_get_ns(); + gpio_desc_to_lineinfo(desc, &chg.info); + + 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"); + break; + default: + return NOTIFY_DONE; + } return NOTIFY_OK; } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index cca81375f127..de7b3b60f7ca 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -38,8 +38,7 @@ * or name of the IP component in a System on Chip. * @data: per-instance data assigned by the driver * @list: links gpio_device:s together for traversal - * @notifier: used to notify subscribers about lines being requested, released - * or reconfigured + * @notifier: used to notify subscribers about gpio_device events * @sem: protects the structure from a NULL-pointer dereference of @chip by * user-space operations when the device gets unregistered during * a hot-unplug event From patchwork Wed Aug 16 12:20:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 714474 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 336D1C04FE1 for ; Wed, 16 Aug 2023 12:21:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245080AbjHPMVC (ORCPT ); Wed, 16 Aug 2023 08:21:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245077AbjHPMUk (ORCPT ); Wed, 16 Aug 2023 08:20:40 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BACB26A9 for ; Wed, 16 Aug 2023 05:20:39 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fe45481edfso65447095e9.1 for ; Wed, 16 Aug 2023 05:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1692188438; x=1692793238; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Trnt4Pw2hw6B7q451s8X3BgS42qMamduwaoaH3QQYrE=; b=ulS9QVaSFuNvBOqE7Xubi/r/LMXRdE02Lqy2ZbQH4oDNKyILIHn2fhDWOi+gqi7r+U W0JdLscK/cnETb9nWSYYYeVGioW8YfPlgdOu7B0WfeNUdXqU5tSHp8KSXawPPEY1ycNj P8Oqk4hvRGwxcrTWI3b1pY0Vnb6BcBUgrE7pD6XLOc/ZnHLrs3lcNwc+fGDBMSjnxoyv 30Ju+pEKnaUGarxLYPNjcxWkxK+DHq6yiQBYAu0Ui2evDDTMia2W8XcJ458mW/Kzya+w E6U4f8oQaYTAuors24+dgK4BP7s0HxJ64RCatn3qFS6nnQFfNaa9XuAR5Nu16ri2C0Za Reyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692188438; x=1692793238; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Trnt4Pw2hw6B7q451s8X3BgS42qMamduwaoaH3QQYrE=; b=P2Z4TIpmv1oGnPgsutKNE64O85/iQY061toJpomdb4WEYA/2mXkCuCBzp+zqs5wpNF Cup4gXFS9P/qUlmjaM02fXw14HUsShg4tQ3MHoWyxCYNqSvXGmDbiXatoyOnERcW1Y+J W18T3parQPJaoVmTFTKl8ttUYXzQirrEWWSVrMYiFAlvkdwMZjyB7qb/MTkFJQAVGPrB NyMiSzVXslCM4k/fKXKmPdJYa/0HMWJtFVardsJk01Z3k8SKamVAXAZvbJ61cvaEXIt6 yTaqg5BlTzuG22cqew2qeMAjtherTWZeAch70bbJQD3oNwGZEEB0afEctvEX4Ue2w231 J+Zg== X-Gm-Message-State: AOJu0YyBJLQVnRS9tI7OH9gqJXuiUrm46qORKYhVmw8wzkstZqH5bXjW vA8gWj48WDRqWxkkk2zpL+OIWA== X-Google-Smtp-Source: AGHT+IF5bC76eebhPqmm3ah8vcookk4hc3A2nk+zD/7O6fCoclca0m3Og5OKvb7VpbIFS/iJnd+wtw== X-Received: by 2002:a7b:c044:0:b0:3fe:2b76:3d7 with SMTP id u4-20020a7bc044000000b003fe2b7603d7mr1266689wmc.10.1692188437483; Wed, 16 Aug 2023 05:20:37 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:55ba:c083:817:86f]) by smtp.gmail.com with ESMTPSA id e6-20020adffd06000000b003196b1bb528sm14566112wrr.64.2023.08.16.05.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 05:20:37 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 2/5] gpio: cdev: rename the notifier block and notify callback Date: Wed, 16 Aug 2023 14:20:29 +0200 Message-Id: <20230816122032.15548-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230816122032.15548-1-brgl@bgdev.pl> References: <20230816122032.15548-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Rename the notifier block in struct gpio_chardev_data and its callback to a more generic name as it will soon gain more functionality than only handling line state changes. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 062521e1a9e0..660d2e057451 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2319,7 +2319,7 @@ struct gpio_chardev_data { struct gpio_device *gdev; wait_queue_head_t wait; DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); - struct notifier_block lineinfo_changed_nb; + struct notifier_block nb; unsigned long *watched_lines; #ifdef CONFIG_GPIO_CDEV_V1 atomic_t watch_abi_version; @@ -2494,11 +2494,11 @@ static long gpio_ioctl_compat(struct file *file, unsigned int cmd, static struct gpio_chardev_data * to_gpio_chardev_data(struct notifier_block *nb) { - return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); + return container_of(nb, struct gpio_chardev_data, nb); } -static int lineinfo_changed_notify(struct notifier_block *nb, - unsigned long action, void *data) +static int gpio_chardev_notify(struct notifier_block *nb, unsigned long action, + void *data) { struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); struct gpio_v2_line_info_changed chg; @@ -2681,9 +2681,8 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) INIT_KFIFO(cdev->events); cdev->gdev = gpio_device_get(gdev); - cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; - ret = blocking_notifier_chain_register(&gdev->notifier, - &cdev->lineinfo_changed_nb); + cdev->nb.notifier_call = gpio_chardev_notify; + ret = blocking_notifier_chain_register(&gdev->notifier, &cdev->nb); if (ret) goto out_free_bitmap; @@ -2698,8 +2697,7 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file) return ret; out_unregister_notifier: - blocking_notifier_chain_unregister(&gdev->notifier, - &cdev->lineinfo_changed_nb); + blocking_notifier_chain_unregister(&gdev->notifier, &cdev->nb); out_free_bitmap: gpio_device_put(gdev); bitmap_free(cdev->watched_lines); @@ -2722,8 +2720,7 @@ static int gpio_chrdev_release(struct inode *inode, struct file *file) struct gpio_device *gdev = cdev->gdev; bitmap_free(cdev->watched_lines); - blocking_notifier_chain_unregister(&gdev->notifier, - &cdev->lineinfo_changed_nb); + blocking_notifier_chain_unregister(&gdev->notifier, &cdev->nb); gpio_device_put(gdev); kfree(cdev); From patchwork Wed Aug 16 12:20:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 714188 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74C42C001E0 for ; Wed, 16 Aug 2023 12:21:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236656AbjHPMVC (ORCPT ); Wed, 16 Aug 2023 08:21:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245080AbjHPMUl (ORCPT ); Wed, 16 Aug 2023 08:20:41 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA21626AD for ; Wed, 16 Aug 2023 05:20:39 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3fe2d218eedso60889985e9.0 for ; Wed, 16 Aug 2023 05:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1692188438; x=1692793238; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yc+T77NE6nVI9PLsQkqlrr/xBBkFkUhqwF3/jEMdIPg=; b=Y/ihd289nZXKIxyoTl17TgP7aQSTwe8RkeUix/vGZxejIeeD90O9aXAMaaLoZqpaac HcZ3xiPoE4uRN+essCNgN7+tZIC4MiyUOtgfNI97sNNRZfgXpT5njdMiVV0TGx1WstN1 8AKMqMaxGU9X1Yq6k6O9ZJ8qCTJZ3o10pQJreea0WqsGGa961/VRl+sBoliccF5IYjeW 7HV4F1MuA93c7rN4giB5MK3p7phz/qWBDDwbjIw5SpjHXyLcILimvFY2GDaeLWExD9KH /zH56Ir3WIiJCNzq+y1XfixOQWbt3WuyPadBKM8djazEi/FhNDxOa2X0dC1JTuIPBTLo Nv6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692188438; x=1692793238; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yc+T77NE6nVI9PLsQkqlrr/xBBkFkUhqwF3/jEMdIPg=; b=b0K2jmYWgQSeSgJyXRsVEdKcB2EVEBFA5qjBTtirI0whXV7nRC6whH92GcK1rLIezG GYcjYV3kPNABNH/AFyn6GBsckHOcv7BWcRTjYWxcX0q8aYcqRSQyl4tTY068qUTwPimd /yPtKhzQddGTxJ5h/voURQLi77MF9znxwkLDa6HFqMsI6xQZRV724t4lRhrp4KRRe1M9 /wT1f81eVTEKoD/Bkm0AJ4jAGTDgPYA831gMdIabQVBaOj4BYOAdYbv27GIMOafnzSid gLTOv7yc0EoyM0lgKqzMQQkKcy2T1wVtE66hR0nZENC9gYABiIpj73buf4nQ9F4YzCm2 HA8g== X-Gm-Message-State: AOJu0YwR/sd9UTMdmVwSidG7T1DqRITRnPdsQqu0JvffRP0Dt4FCG3e6 A64zhmULfCfEgT/MumWcQlaE1dKmgU1jqx/NgDouCQ== X-Google-Smtp-Source: AGHT+IFEJKQoq2tV1RnY7W0jsL0DzfdsoPDjKqgbbV9zKHFRDerYe1M182sG4pkSRUTPPLCYg90rtw== X-Received: by 2002:a05:600c:1c17:b0:3fe:485f:ed1b with SMTP id j23-20020a05600c1c1700b003fe485fed1bmr1241928wms.28.1692188438347; Wed, 16 Aug 2023 05:20:38 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:55ba:c083:817:86f]) by smtp.gmail.com with ESMTPSA id e6-20020adffd06000000b003196b1bb528sm14566112wrr.64.2023.08.16.05.20.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 05:20:37 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 3/5] gpio: cdev: wake up chardev poll() on device unbind Date: Wed, 16 Aug 2023 14:20:30 +0200 Message-Id: <20230816122032.15548-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230816122032.15548-1-brgl@bgdev.pl> References: <20230816122032.15548-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Emit a new notifier event right after the character device was unregistered and receive it in the notifier owned by the descriptor associated with the character device file. Upon reception, wake up the wait queue so that the user-space be forced out of poll() and need to go into a new system call which will then fail due to the chip being gone. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 660d2e057451..eb8c0cb71da4 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -32,6 +32,12 @@ #include "gpiolib.h" #include "gpiolib-cdev.h" +/* + * Let's use a higher value for gpio_device notifications to not conflict with + * UAPI defines that can also serve as notifier action arguments. + */ +#define GPIO_CDEV_UNREGISTERED 1000 + /* * Array sizes must ensure 64-bit alignment and not create holes in the * struct packing. @@ -2526,6 +2532,9 @@ static int gpio_chardev_notify(struct notifier_block *nb, unsigned long action, else pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); break; + case GPIO_CDEV_UNREGISTERED: + wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR); + break; default: return NOTIFY_DONE; } @@ -2761,4 +2770,6 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) void gpiolib_cdev_unregister(struct gpio_device *gdev) { cdev_device_del(&gdev->chrdev, &gdev->dev); + blocking_notifier_call_chain(&gdev->notifier, + GPIO_CDEV_UNREGISTERED, NULL); } From patchwork Wed Aug 16 12:20:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 714186 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20258C04FDF for ; Wed, 16 Aug 2023 12:21:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245077AbjHPMVD (ORCPT ); Wed, 16 Aug 2023 08:21:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245082AbjHPMUm (ORCPT ); Wed, 16 Aug 2023 08:20:42 -0400 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBA00109 for ; Wed, 16 Aug 2023 05:20:40 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2b9f48b6796so98224111fa.3 for ; Wed, 16 Aug 2023 05:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1692188439; x=1692793239; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Am7bI6bfSitSW1lki/Nh1/0VtCPZ62yUJw5gPNa+qeo=; b=w0B7/+FuLacJhBAv6aShs3+zb2UZ8ABq2mnih8O4abteYETwWx5tR4GeIUJwHSS7pI NAXRnsgA3EtFmLbrgxFb8l1u2uBWAFgOR183W0dacaSWX325Vh8KcUBZDpRiB1vNZdMH 5FbSjSyWs5UT5iqI4HJYe9YQ5dAKCKDaVa7CLesL4faIioCl4Dk2+C+RtLcwvHHQ/DDh 41R/Dng25t16C+1szjVA4rC3ReVPaO08GO/TTWGkNDwopLH3oW4JnjIZwCseW4zmowW4 am1D1LJ7aYnmrL0BB48IOJjgYLxmjY6z6Wi/oUfGGSurA3NMHn2OqhLCwVindZ7R5Lcv bcTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692188439; x=1692793239; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Am7bI6bfSitSW1lki/Nh1/0VtCPZ62yUJw5gPNa+qeo=; b=Rbrk9tPlHrR68fkgkKe7TYYuvrU4PN2Vypw5genvNENr4Bug0rTilq29f7pUjObfK1 OR1vBxQmd5b2RhmIaacdJ3bpG98TGnib74vIMI4gE/U3jJQePO4rMiw712bx5Q2nuqq4 r/iLybVaH4YyuvhFD5e08mkucKJeEeEBrVa5Nb4YssL018Xpc0xQvrLEqeGo/f5x14I7 3j8vjiJEWnejH+9mLcJyz2ol60jniwsVwII9CsfbmKWm+e721IUhplXcOjidLh7QJgOo dIOnHihG7Pku3ZNvieW4vZB3pYAn3HSS1XwKPwbB6BudCC3ZOaw6GE+3lVTRPd0KFFoY UMYA== X-Gm-Message-State: AOJu0YwOJKRilrxYTu6bgnAviP493LffMqzfid4lKAD4jm7BSrVMtp2a inBMkD7Rzf9uvmzS/AwHWN6T1Q== X-Google-Smtp-Source: AGHT+IENColA7BIp/lKtNBrXBzmg8CdpZ/MoVKtFHesq7ljpKBW2+rbY2oBTtgZjc3gsboJdLLhpTA== X-Received: by 2002:a2e:9ac6:0:b0:2b6:a6e7:5afa with SMTP id p6-20020a2e9ac6000000b002b6a6e75afamr1498211ljj.12.1692188439174; Wed, 16 Aug 2023 05:20:39 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:55ba:c083:817:86f]) by smtp.gmail.com with ESMTPSA id e6-20020adffd06000000b003196b1bb528sm14566112wrr.64.2023.08.16.05.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 05:20:38 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 4/5] gpio: cdev: wake up linereq poll() on device unbind Date: Wed, 16 Aug 2023 14:20:31 +0200 Message-Id: <20230816122032.15548-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230816122032.15548-1-brgl@bgdev.pl> References: <20230816122032.15548-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Extend struct linereq with a notifier block and use it to receive the GPIO device unregister event. Upon reception, wake up the wait queue so that the user-space be forced out of poll() and need to go into a new system call which will then fail due to the chip being gone. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index eb8c0cb71da4..0b21ea04fa52 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -563,6 +563,7 @@ struct line { * @wait: wait queue that handles blocking reads of events * @event_buffer_size: the number of elements allocated in @events * @events: KFIFO for the GPIO events + * @nb: notifier block for receiving gpio_device notifications * @seqno: the sequence number for edge events generated on all lines in * this line request. Note that this is not used when @num_lines is 1, as * the line_seqno is then the same and is cheaper to calculate. @@ -577,11 +578,17 @@ struct linereq { wait_queue_head_t wait; u32 event_buffer_size; DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); + struct notifier_block nb; atomic_t seqno; struct mutex config_mutex; struct line lines[]; }; +static struct linereq *to_linereq(struct notifier_block *nb) +{ + return container_of(nb, struct linereq, nb); +} + #define GPIO_V2_LINE_BIAS_FLAGS \ (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ @@ -1573,6 +1580,10 @@ static void linereq_free(struct linereq *lr) { unsigned int i; + if (lr->nb.notifier_call) + blocking_notifier_chain_unregister(&lr->gdev->notifier, + &lr->nb); + for (i = 0; i < lr->num_lines; i++) { if (lr->lines[i].desc) { edge_detector_stop(&lr->lines[i]); @@ -1623,6 +1634,22 @@ static const struct file_operations line_fileops = { #endif }; +static int linereq_notify(struct notifier_block *nb, unsigned long action, + void *data) +{ + struct linereq *lr = to_linereq(nb); + + switch (action) { + case GPIO_CDEV_UNREGISTERED: + wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR); + break; + default: + return NOTIFY_DONE; + } + + return NOTIFY_OK; +} + static int linereq_create(struct gpio_device *gdev, void __user *ip) { struct gpio_v2_line_request ulr; @@ -1733,6 +1760,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) offset); } + lr->nb.notifier_call = linereq_notify; + ret = blocking_notifier_chain_register(&gdev->notifier, &lr->nb); + if (ret) + goto out_free_linereq; + fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); if (fd < 0) { ret = fd; From patchwork Wed Aug 16 12:20:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 714187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC803C04A94 for ; Wed, 16 Aug 2023 12:21:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245073AbjHPMVD (ORCPT ); Wed, 16 Aug 2023 08:21:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245084AbjHPMUm (ORCPT ); Wed, 16 Aug 2023 08:20:42 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A270A26AA for ; Wed, 16 Aug 2023 05:20:41 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fe8a158fcbso45420825e9.2 for ; Wed, 16 Aug 2023 05:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1692188440; x=1692793240; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dP05q+RMOmNQ4Gl3VVSG0OrbP/ydNsWsk2YHlDd9aGg=; b=34QaAQx3vESmEiokl+YyNZrO0ncceSOT4XSxS0lcblrN+BAodcHQU1H2gh+RX91BhJ aD7f1IE/5OfXZ0S3X0l1ZqhjxW5OroxThQt5F2ue3iVsUt1jBs0YAPYrS8SDa5n+UogO PFfKDGsH2Ht8j5A6xDDPEp9/BOIqoLFMR+UQD2AVlqoRbJ6w0jNFv0STGrjDsff+KC02 7jAEU/SXhZzjU/tRTeSIh/p9WV4ncvfa+chMbxPsKYdshIdK6vFxuXqg2suKmDSbgUwz cQElJ3lI11PWBYyqD2PcfGBIvujXpFmfMdikcgwBgFzGo0uD/R7DYvFdlUHqzexvTNyO I7mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692188440; x=1692793240; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dP05q+RMOmNQ4Gl3VVSG0OrbP/ydNsWsk2YHlDd9aGg=; b=c1x9P5tYgk8jesDlXrsSNoZFLaW8p07wvbGlsa5g7+YmoezkocYphIansAxT1DPy+k VFMVssGIci1y5La1znJ0HLenhFJm4gw6+7ec41EUMhfcS81T5W4AFkBWfhbkFZKD24qF GEbVH/689kDi/NtEUA2XS5Er+ZedA3IdkEyKB5snyH7MFNuOrZF09b/8AlRFndUzZ0du Nc+lFvweX4TsU9uK/Guxuj3OJZfnPbKee4O+9SoEqqfT/z+hPp1s96+4szC/1AI2Ln/X C+ddrMpzFDmGhxWCaRS0tZvIjBdfs62PZtEGlQhUd94SeJF6wNyjOJ0ohajunU2YztYP 0UKw== X-Gm-Message-State: AOJu0YyFAdNr0fusrs3YnaxgxctQ5swZi2INXuII0ANTV3TB3OAM5k7Q TZkzJ4Zp5fTuwZ7d/EpIAfXlIQ== X-Google-Smtp-Source: AGHT+IEV+ik5O//GvPEkYWy5rfWG/+BGwXrQ9jf7vpe8YseN9XyYNkJC6rCwpUPFnzdZtXteJF79cQ== X-Received: by 2002:a7b:cd8c:0:b0:3fa:9767:c816 with SMTP id y12-20020a7bcd8c000000b003fa9767c816mr1446313wmj.39.1692188440111; Wed, 16 Aug 2023 05:20:40 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:55ba:c083:817:86f]) by smtp.gmail.com with ESMTPSA id e6-20020adffd06000000b003196b1bb528sm14566112wrr.64.2023.08.16.05.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 05:20:39 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 5/5] gpio: cdev: wake up lineevent poll() on device unbind Date: Wed, 16 Aug 2023 14:20:32 +0200 Message-Id: <20230816122032.15548-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230816122032.15548-1-brgl@bgdev.pl> References: <20230816122032.15548-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Extend struct lineevent_data with a notifier block and use it to receive the GPIO device unregister event. Upon reception, wake up the wait queue so that the user-space be forced out of poll() and need to go into a new system call which will then fail due to the chip being gone. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 0b21ea04fa52..bb6a011f7857 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1830,9 +1830,15 @@ struct lineevent_state { int irq; wait_queue_head_t wait; DECLARE_KFIFO(events, struct gpioevent_data, 16); + struct notifier_block nb; u64 timestamp; }; +static struct lineevent_state *to_lineevent_state(struct notifier_block *nb) +{ + return container_of(nb, struct lineevent_state, nb); +} + #define GPIOEVENT_REQUEST_VALID_FLAGS \ (GPIOEVENT_REQUEST_RISING_EDGE | \ GPIOEVENT_REQUEST_FALLING_EDGE) @@ -1947,6 +1953,9 @@ static ssize_t lineevent_read(struct file *file, char __user *buf, static void lineevent_free(struct lineevent_state *le) { + if (le->nb.notifier_call) + blocking_notifier_chain_unregister(&le->gdev->notifier, + &le->nb); if (le->irq) free_irq(le->irq, le); if (le->desc) @@ -2084,6 +2093,22 @@ static irqreturn_t lineevent_irq_handler(int irq, void *p) return IRQ_WAKE_THREAD; } +static int lineevent_notify(struct notifier_block *nb, unsigned long action, + void *data) +{ + struct lineevent_state *le = to_lineevent_state(nb); + + switch (action) { + case GPIO_CDEV_UNREGISTERED: + wake_up_poll(&le->wait, EPOLLIN | EPOLLERR); + break; + default: + return NOTIFY_DONE; + } + + return NOTIFY_OK; +} + static int lineevent_create(struct gpio_device *gdev, void __user *ip) { struct gpioevent_request eventreq; @@ -2175,6 +2200,11 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) INIT_KFIFO(le->events); init_waitqueue_head(&le->wait); + le->nb.notifier_call = lineevent_notify; + ret = blocking_notifier_chain_register(&gdev->notifier, &le->nb); + if (ret) + goto out_free_le; + /* Request a thread to read the events */ ret = request_threaded_irq(irq, lineevent_irq_handler,