From patchwork Fri Sep 9 12:13:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 604413 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 63BF0ECAAD3 for ; Fri, 9 Sep 2022 12:18:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231716AbiIIMR7 (ORCPT ); Fri, 9 Sep 2022 08:17:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231470AbiIIMRb (ORCPT ); Fri, 9 Sep 2022 08:17:31 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E6995E548 for ; Fri, 9 Sep 2022 05:14:45 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id bj14so2367603wrb.12 for ; Fri, 09 Sep 2022 05:14:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=bU+AQcsXemk1KKgCjaU3gvykVFjEj4Zm9kHKe+L3+Kw=; b=iQvtBX5HeVBZTuTdnbFs4s01K5RT+yZYm6ePr4u4EJ+AkSd5bnDWuyTEcnduTKl/Cc QqI4HK/OP7ub73phvUth6wA8baupqBKEN13e1dGepyahVa09iWbeKHrwoJ5TV2Jyh6Bg KTipzfmLLk6wLSvqeEb0DPAi8xWyOSKwVLlM1AvumslzQJ0DLXw4kAWnNojrms050jcq RyhogLD29Rc5Scrjca1VXI4C2Sp6oHtEYRNAQVw+6ahdysLqTKe3nHqm+EmK99FT1ZaB pstlvCeTu0ieogclGb6Oaylmnz6mVlNsbUXe5lF+yA5xfz4SKl1zRnShwXtp5eUQSx46 B9Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=bU+AQcsXemk1KKgCjaU3gvykVFjEj4Zm9kHKe+L3+Kw=; b=IjRYFfwRBQIH8DDBRWehuaf7ywvRHl9sFrlCyGAQ85OmmoFA3uN0MtJS+bWQwKRBVl OJIEyXw/yZ9x5GUS+pcuuYmk7WAmjD+cXyWSro43Ufg3XctKTjHwvDk5H0Nn5D1fdWmI zpDizkNu8LHPuaTZfyZA23AR9S9/VhHAYEJMmsMzW2bDezofzHyFRL5j6CG9LOBJ6Rxw Jpt1iofxMogoxh1kpqxSaytXwREGOtPSCYpwN7RZm6lS0mKxSBODr4TVz59JmrQFCuN4 HVKRcVhAclxZyt1awvUAK5Nz6fOPcXQAUoCBg+TMeb4VFHA8jtaDbK2JnMpz9/4ajo5m xkBQ== X-Gm-Message-State: ACgBeo0pNoGShBRDpZQouBqL7Sv8hysafIw5vwpcDe0EJ6qNJ1bhALnH /MsdvdD+dEsMgqiieh4WboboLw== X-Google-Smtp-Source: AA6agR5ob+DApxRqNiEDoa+ZxnDvTCPp4w6FFXtlz8ogbbKZ7zdj1OifeBdTWLRBEHye6hGCV6yTQQ== X-Received: by 2002:a05:6000:11d0:b0:228:8d5d:f2e9 with SMTP id i16-20020a05600011d000b002288d5df2e9mr7863995wrx.207.1662725615413; Fri, 09 Sep 2022 05:13:35 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:a421:c10c:c626:3f52]) by smtp.gmail.com with ESMTPSA id l10-20020adfe58a000000b0022863395912sm379005wrm.53.2022.09.09.05.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 05:13:35 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 1/2] gpiolib: un-inline gpiod_request_user() Date: Fri, 9 Sep 2022 14:13:28 +0200 Message-Id: <20220909121329.42004-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220909121329.42004-1-brgl@bgdev.pl> References: <20220909121329.42004-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Pull this bit of code into gpiolib.c as we're soon be calling certain symbols static in this compilation unit. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 11 +++++++++++ drivers/gpio/gpiolib.h | 12 +----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index cc9c0a12259e..6768734b9e15 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2009,6 +2009,17 @@ int gpiod_request(struct gpio_desc *desc, const char *label) return ret; } +int gpiod_request_user(struct gpio_desc *desc, const char *label) +{ + int ret; + + ret = gpiod_request(desc, label); + if (ret == -EPROBE_DEFER) + ret = -ENODEV; + + return ret; +} + static bool gpiod_free_commit(struct gpio_desc *desc) { bool ret = false; diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index d900ecdbac46..b35deb08a7f5 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -179,19 +179,9 @@ struct gpio_desc { #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) int gpiod_request(struct gpio_desc *desc, const char *label); +int gpiod_request_user(struct gpio_desc *desc, const char *label); void gpiod_free(struct gpio_desc *desc); -static inline int gpiod_request_user(struct gpio_desc *desc, const char *label) -{ - int ret; - - ret = gpiod_request(desc, label); - if (ret == -EPROBE_DEFER) - ret = -ENODEV; - - return ret; -} - int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, unsigned long lflags, enum gpiod_flags dflags); int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); From patchwork Fri Sep 9 12:13:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 604414 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 3C131C6FA82 for ; Fri, 9 Sep 2022 12:17:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231667AbiIIMRc (ORCPT ); Fri, 9 Sep 2022 08:17:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231685AbiIIMRI (ORCPT ); Fri, 9 Sep 2022 08:17:08 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFE3011E for ; Fri, 9 Sep 2022 05:14:22 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id j26so1216840wms.0 for ; Fri, 09 Sep 2022 05:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=TJ4GfQdS18kjHFzyMVfKhwUm6gcXBWn+mssbcPQeJwE=; b=oN+I2pZAMqI3F5MeqhWKzXHDmX8Vcv6SECSIdW4HUtd1bBI2PS0RstBczHMiXFmLx2 iPULfg56vRYZrIC/ZqZocF5LvapN/pLM3Sb/uFSs32AeOql+cbzefQgx+jpxO9/nTYIG DxwPzwNjpuffSzbZYkt9ZZ5wYiIuQ+64ehufT7ox7+tV6obS1IMaiDFxtl+9gkV8J3zC RB49BeuCvypsRAWsa4x4hJ7qs0Zf5mXrlhRbz5FLn70d/PvDo0XYxbiC2aFzqcyqOIiA YMOlv9ga7nVU8qMl5Q2QmR+teVJvjA44Ncz7NUo95O+5OTb5/s0CcZCp0JsokwrZ079n gH+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=TJ4GfQdS18kjHFzyMVfKhwUm6gcXBWn+mssbcPQeJwE=; b=ACx0rOxlP69R062he9oxMy1Fp5VnzmI03Sjd37ilV5rp9oi7Mi9MgBpSz7EhTBdeeZ i6W/5DffVCTh4pVCQSiqcsS1GYp93JRkpRhh+WJsKoNnUX6YV0c7pbpETSSZi3CjPyXu KafNwLYqZfmE8a6woAusEIVGoAJhKSWt25Do8ogR0DIfHjVpw0do1TQbmyzN2OnISp12 kWHpQHv5FIGHDcq3hk8LPGbKHCqUXfPQJ8jGVbFjj/oIZFhNHVBqXD2oJHShzxA2tkvz iUtc2t1gzuSsdJWYY42+sC0CWuNmiBDhDB8f/JCXzFSpNPTL5OMLyr/gttnSbziyzXSt 5+pg== X-Gm-Message-State: ACgBeo3FegZqNVLB2x7eyzWUjcuj9DNHE/DMJjM0hntX9i8rXO/2THoa dHeasTN3n0eQ2Za1Rnl9aIF1koTTjdJdBQ== X-Google-Smtp-Source: AA6agR7FEMeGmHB066ypHBK/GDBz2fh/IZgxjLvt0P89XdLeGs68qyApP3Eftizp90z5O1zcBhMMIw== X-Received: by 2002:a05:600c:6009:b0:3a5:b069:5d34 with SMTP id az9-20020a05600c600900b003a5b0695d34mr5250244wmb.115.1662725616388; Fri, 09 Sep 2022 05:13:36 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:a421:c10c:c626:3f52]) by smtp.gmail.com with ESMTPSA id l10-20020adfe58a000000b0022863395912sm379005wrm.53.2022.09.09.05.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 05:13:35 -0700 (PDT) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 2/2] gpiolib: cdev: export the consumer's PID Date: Fri, 9 Sep 2022 14:13:29 +0200 Message-Id: <20220909121329.42004-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220909121329.42004-1-brgl@bgdev.pl> References: <20220909121329.42004-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org It's useful for user-space to be able to know the PIDs of processes holding GPIO lines in case they have the permissions and need to kill them. Extend the gpio_v2_line_info structure with the consumer_pid field that's set to the PID of the user-space process or 0 if the user lives in the kernel. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-cdev.c | 2 ++ drivers/gpio/gpiolib.c | 24 +++++++++++++++++++----- drivers/gpio/gpiolib.h | 2 ++ include/uapi/linux/gpio.h | 5 ++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index f8041d4898d1..9b6d518680dc 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -2120,6 +2120,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (desc->label) strscpy(info->consumer, desc->label, sizeof(info->consumer)); + info->consumer_pid = desc->pid; + /* * Userspace only need to know that the kernel is using this GPIO so * it can't use it. diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 6768734b9e15..0c9d1639b04d 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -96,6 +96,11 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label) d->label = label; } +static inline void desc_set_pid(struct gpio_desc *d, pid_t pid) +{ + d->pid = pid; +} + /** * gpio_to_desc - Convert a GPIO number to its descriptor * @gpio: global GPIO number @@ -1892,7 +1897,8 @@ EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); * on each other, and help provide better diagnostics in debugfs. * They're called even less than the "set direction" calls. */ -static int gpiod_request_commit(struct gpio_desc *desc, const char *label) +static int +gpiod_request_commit(struct gpio_desc *desc, const char *label, pid_t pid) { struct gpio_chip *gc = desc->gdev->chip; int ret; @@ -1913,6 +1919,7 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { desc_set_label(desc, label ? : "?"); + desc_set_pid(desc, pid); } else { ret = -EBUSY; goto out_free_unlock; @@ -1987,7 +1994,8 @@ static int validate_desc(const struct gpio_desc *desc, const char *func) return; \ } while (0) -int gpiod_request(struct gpio_desc *desc, const char *label) +static int +gpiod_request_with_pid(struct gpio_desc *desc, const char *label, pid_t pid) { int ret = -EPROBE_DEFER; struct gpio_device *gdev; @@ -1996,7 +2004,7 @@ int gpiod_request(struct gpio_desc *desc, const char *label) gdev = desc->gdev; if (try_module_get(gdev->owner)) { - ret = gpiod_request_commit(desc, label); + ret = gpiod_request_commit(desc, label, pid); if (ret) module_put(gdev->owner); else @@ -2009,11 +2017,16 @@ int gpiod_request(struct gpio_desc *desc, const char *label) return ret; } +int gpiod_request(struct gpio_desc *desc, const char *label) +{ + return gpiod_request_with_pid(desc, label, 0); +} + int gpiod_request_user(struct gpio_desc *desc, const char *label) { int ret; - ret = gpiod_request(desc, label); + ret = gpiod_request_with_pid(desc, label, current->pid); if (ret == -EPROBE_DEFER) ret = -ENODEV; @@ -2042,6 +2055,7 @@ static bool gpiod_free_commit(struct gpio_desc *desc) } kfree_const(desc->label); desc_set_label(desc, NULL); + desc_set_pid(desc, 0); clear_bit(FLAG_ACTIVE_LOW, &desc->flags); clear_bit(FLAG_REQUESTED, &desc->flags); clear_bit(FLAG_OPEN_DRAIN, &desc->flags); @@ -2140,7 +2154,7 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc, return desc; } - ret = gpiod_request_commit(desc, label); + ret = gpiod_request_commit(desc, label, 0); if (ret < 0) return ERR_PTR(ret); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index b35deb08a7f5..d1535677e162 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -165,6 +165,8 @@ struct gpio_desc { /* Connection label */ const char *label; + /* Consumer's PID (if consumer is in user-space, otherwise 0) */ + pid_t pid; /* Name of the GPIO */ const char *name; #ifdef CONFIG_OF_DYNAMIC diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index cb9966d49a16..37f10021d1aa 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -219,6 +219,8 @@ struct gpio_v2_line_request { * gpio_v2_line_flag, such as %GPIO_V2_LINE_FLAG_ACTIVE_LOW, * %GPIO_V2_LINE_FLAG_OUTPUT etc, added together. * @attrs: the configuration attributes associated with the line + * @consumer_pid: process ID of the user-space consumer or 0 if the consumer + * lives in kernel space * @padding: reserved for future use */ struct gpio_v2_line_info { @@ -228,8 +230,9 @@ struct gpio_v2_line_info { __u32 num_attrs; __aligned_u64 flags; struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX]; + __s32 consumer_pid; /* Space reserved for future use. */ - __u32 padding[4]; + __u32 padding[3]; }; /**