Message ID | 20230519050702.3681791-1-chris.packham@alliedtelesis.co.nz |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp998514vqo; Thu, 18 May 2023 22:24:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Ro6AsTscxrOzF5jqyMSKP2hajVTrn0wpNkRgjRLo03zCpkZHNrfgSlfyt174wCir4q04i X-Received: by 2002:a05:6a20:a5a6:b0:105:55a7:d5ff with SMTP id bc38-20020a056a20a5a600b0010555a7d5ffmr937520pzb.28.1684473855163; Thu, 18 May 2023 22:24:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684473855; cv=none; d=google.com; s=arc-20160816; b=n/5Wq7JrYzDzyeQ/gIE6kMVs0DKAIRVtKUHUI2CS+Yq78rb4EBtdtxcXkqsKgXz8Gu OsCc5MzavMKIRgil7XfWxKQ4PKVImD+PDvY+3XoavZNXlg+GeWN+nvTtPaBPS09fZZ2k BrLTIlyTeAXBEzTcc+u/Ae5YaGwFEhbMUOggt6g9nczpBFo2xiCQB6x9z0SWtG7tOgat etR7zeogrAtOTw4cH+XR5h6Hdu191u0cj6ThMXqExdN0Fin5Ge9xXZP8NWudjf0kuW0O N/6m/2EoOvdW10pbRbs5ZRqAf/SqaJeyWM62PgouexTDiliziY84bQWRIHOesiBckOVw uOoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=32gSY5w+PANxi1B/B+/bHMGQXs4kLuI5sUqJUzM4hx8=; b=vrXpjHUuvuS8opX2vi7K1IXXA58uj85p4UZgNkcQa5x1XOT6QEyZQUxWUUbMlAR3gO wDIPYaC8YJusE529Gl5+uOha0b+NFPQE6ElMyjh2G6gCheqrNHvEInf2Mcm1lTR8zatB RfRAQk0Lv2TSpROgXgmiAMi9Xlq+G6idjGu7X9KkbWdCB48yoWn+iigtmVdbcjpt25js Lfi1Cc3+WCx5YqYc/2axKQ1CkNMKk8r4zLZwlwOCPr0B54OsPknEeK3DDlaePuwlB8Fz zyeWd0yY4GmT4COsFr+DxtJiDzpL4nXL/Vp86s5XKK3sWq1qyOHT8r04VGKsgOwcQ+iX pwPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alliedtelesis.co.nz header.s=mail181024 header.b="j9oZFf/d"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=alliedtelesis.co.nz Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bk13-20020a056a02028d00b005347ac15c24si2295852pgb.421.2023.05.18.22.24.00; Thu, 18 May 2023 22:24:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@alliedtelesis.co.nz header.s=mail181024 header.b="j9oZFf/d"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=alliedtelesis.co.nz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229975AbjESFHX (ORCPT <rfc822;cscallsign@gmail.com> + 99 others); Fri, 19 May 2023 01:07:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjESFHW (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 19 May 2023 01:07:22 -0400 Received: from gate2.alliedtelesis.co.nz (gate2.alliedtelesis.co.nz [IPv6:2001:df5:b000:5::4]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 200C419C for <linux-kernel@vger.kernel.org>; Thu, 18 May 2023 22:07:18 -0700 (PDT) Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id 7FBD12C01D8; Fri, 19 May 2023 17:07:05 +1200 (NZST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1684472825; bh=32gSY5w+PANxi1B/B+/bHMGQXs4kLuI5sUqJUzM4hx8=; h=From:To:Cc:Subject:Date:From; b=j9oZFf/dluAWoTYulElN+w8l9fvAQUkGal799tiCONo1/vUAPfFO4ctkMWEg4Y8Ys 03Fcou3vxiU6I4OBtjwjdQG3UURU07meQI3ORqxtj1sKtwphzNbVpnS/hHSdyyZqYb d4CJrKJFsyJW3XILr4HhOspau1qxtbQ4+kKdpFhzu8lDia8AL5Q1GZIzpRGBEpWewB v5bkn4Sf9ZoPJkan60Ru2u5erpGfrEJREuYZ6HPWJ3DqYCLFEczFbHp33c8zKubHxw QthnIoYe7fApSt2aj8VUXkcNETlpsb8RgoMP8oSL8f3ea+Brn2CMWntFGMJbtsmDQn RpddfrO9Ev/LQ== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8,2,6,11305) id <B646703f90000>; Fri, 19 May 2023 17:07:05 +1200 Received: from chrisp-dl.ws.atlnz.lc (chrisp-dl.ws.atlnz.lc [10.33.22.30]) by pat.atlnz.lc (Postfix) with ESMTP id 4AB3513EDAE; Fri, 19 May 2023 17:07:05 +1200 (NZST) Received: by chrisp-dl.ws.atlnz.lc (Postfix, from userid 1030) id 4731B28DACC; Fri, 19 May 2023 17:07:05 +1200 (NZST) From: Chris Packham <chris.packham@alliedtelesis.co.nz> To: linus.walleij@linaro.org, brgl@bgdev.pl, andy.shevchenko@gmail.com, johan@kernel.org, maz@kernel.org, warthog618@gmail.com Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Chris Packham <chris.packham@alliedtelesis.co.nz> Subject: [PATCH v2] gpiolib: Avoid side effects in gpio_is_visible() Date: Fri, 19 May 2023 17:07:02 +1200 Message-Id: <20230519050702.3681791-1-chris.packham@alliedtelesis.co.nz> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SEG-SpamProfiler-Analysis: v=2.3 cv=cLieTWWN c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=P0xRbXHiH_UA:10 a=Op8SjpqLE0gF3w78xgEA:9 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766298857402893663?= X-GMAIL-MSGID: =?utf-8?q?1766298857402893663?= |
Series |
[v2] gpiolib: Avoid side effects in gpio_is_visible()
|
|
Commit Message
Chris Packham
May 19, 2023, 5:07 a.m. UTC
On a system with pca9555 GPIOs that have been exported via sysfs the
following warning could be triggered on kexec().
WARNING: CPU: 0 PID: 265 at drivers/gpio/gpiolib.c:3411 gpiochip_disable_irq
Call trace:
gpiochip_disable_irq
machine_crash_shutdown
__crash_kexec
panic
sysrq_reset_seq_param_set
__handle_sysrq
write_sysrq_trigger
The warning is triggered because there is an irq_desc for the GPIO but
it does not have the FLAG_USED_AS_IRQ set. This is because when the GPIO
is exported via gpiod_export(), gpio_is_visible() is used to determine
if the "edge" attribute should be provided but in doing so it ends up
calling gpiochip_to_irq() which creates the irq_desc.
Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual
intended creation of the irq_desc comes via edge_store() when requested
by the user.
Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
Notes:
Changes in v2:
- expand commit message to (hopefully) better describe the problem and
solution
- drop the inaccurate fixes tag
drivers/gpio/gpiolib-sysfs.c | 2 --
1 file changed, 2 deletions(-)
Comments
On Fri, May 19, 2023 at 8:07 AM Chris Packham <chris.packham@alliedtelesis.co.nz> wrote: > > On a system with pca9555 GPIOs that have been exported via sysfs the > following warning could be triggered on kexec(). > > WARNING: CPU: 0 PID: 265 at drivers/gpio/gpiolib.c:3411 gpiochip_disable_irq > Call trace: > gpiochip_disable_irq > machine_crash_shutdown > __crash_kexec > panic > sysrq_reset_seq_param_set > __handle_sysrq > write_sysrq_trigger > > The warning is triggered because there is an irq_desc for the GPIO but > it does not have the FLAG_USED_AS_IRQ set. This is because when the GPIO > is exported via gpiod_export(), gpio_is_visible() is used to determine > if the "edge" attribute should be provided but in doing so it ends up > calling gpiochip_to_irq() which creates the irq_desc. > > Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual > intended creation of the irq_desc comes via edge_store() when requested > by the user. To me it still sounds like a hack and the real solution should be done differently/elsewhere. Also I'm worrying that not having this file visible or not may affect existing user space custom scripts we will never hear about. P.S. TBH, I don't care much about sysfs, so if this patch finds its way upstream, I won't be unhappy.
On Fri, May 19, 2023 at 12:09 PM Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > On Fri, May 19, 2023 at 8:07 AM Chris Packham > <chris.packham@alliedtelesis.co.nz> wrote: > > > > On a system with pca9555 GPIOs that have been exported via sysfs the > > following warning could be triggered on kexec(). > > > > WARNING: CPU: 0 PID: 265 at drivers/gpio/gpiolib.c:3411 gpiochip_disable_irq > > Call trace: > > gpiochip_disable_irq > > machine_crash_shutdown > > __crash_kexec > > panic > > sysrq_reset_seq_param_set > > __handle_sysrq > > write_sysrq_trigger > > > > The warning is triggered because there is an irq_desc for the GPIO but > > it does not have the FLAG_USED_AS_IRQ set. This is because when the GPIO > > is exported via gpiod_export(), gpio_is_visible() is used to determine > > if the "edge" attribute should be provided but in doing so it ends up > > calling gpiochip_to_irq() which creates the irq_desc. > > > > Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual > > intended creation of the irq_desc comes via edge_store() when requested > > by the user. > > To me it still sounds like a hack and the real solution should be done > differently/elsewhere. > > Also I'm worrying that not having this file visible or not may affect > existing user space custom scripts we will never hear about. > > P.S. TBH, I don't care much about sysfs, so if this patch finds its > way upstream, I won't be unhappy. > Same. Which is why - if there'll be no more objections, I will apply it. Bart
On Fri, May 26, 2023 at 03:01:01PM +0200, Bartosz Golaszewski wrote: > On Fri, May 19, 2023 at 12:09 PM Andy Shevchenko > <andy.shevchenko@gmail.com> wrote: > > > > On Fri, May 19, 2023 at 8:07 AM Chris Packham > > <chris.packham@alliedtelesis.co.nz> wrote: > > > > > > On a system with pca9555 GPIOs that have been exported via sysfs the > > > following warning could be triggered on kexec(). > > > > > > WARNING: CPU: 0 PID: 265 at drivers/gpio/gpiolib.c:3411 gpiochip_disable_irq > > > Call trace: > > > gpiochip_disable_irq > > > machine_crash_shutdown > > > __crash_kexec > > > panic > > > sysrq_reset_seq_param_set > > > __handle_sysrq > > > write_sysrq_trigger > > > > > > The warning is triggered because there is an irq_desc for the GPIO but > > > it does not have the FLAG_USED_AS_IRQ set. This is because when the GPIO > > > is exported via gpiod_export(), gpio_is_visible() is used to determine > > > if the "edge" attribute should be provided but in doing so it ends up > > > calling gpiochip_to_irq() which creates the irq_desc. > > > > > > Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual > > > intended creation of the irq_desc comes via edge_store() when requested > > > by the user. > > > > To me it still sounds like a hack and the real solution should be done > > differently/elsewhere. > > > > Also I'm worrying that not having this file visible or not may affect > > existing user space custom scripts we will never hear about. > > > > P.S. TBH, I don't care much about sysfs, so if this patch finds its > > way upstream, I won't be unhappy. > > > > Same. Which is why - if there'll be no more objections, I will apply it. I don't think this should be applied. It's still not clear from the commit message why gpiochip_disable_irq() is called for a line which has not been requested. That seems like what should be fixed, not changing some behaviour in the gpio sysfs interface which has been there since forever (e.g. do not create the edge attributes for gpios that cannot be used as interrupts). There are other ways that mappings can be created (e.g. a gpio that requested as as interrupt and then released) which would trigger the same warning it seems. Fix the root cause, don't just paper over the symptom. Johan
On 27/05/23 01:23, Johan Hovold wrote: > On Fri, May 26, 2023 at 03:01:01PM +0200, Bartosz Golaszewski wrote: >> On Fri, May 19, 2023 at 12:09 PM Andy Shevchenko >> <andy.shevchenko@gmail.com> wrote: >>> On Fri, May 19, 2023 at 8:07 AM Chris Packham >>> <chris.packham@alliedtelesis.co.nz> wrote: >>>> On a system with pca9555 GPIOs that have been exported via sysfs the >>>> following warning could be triggered on kexec(). >>>> >>>> WARNING: CPU: 0 PID: 265 at drivers/gpio/gpiolib.c:3411 gpiochip_disable_irq >>>> Call trace: >>>> gpiochip_disable_irq >>>> machine_crash_shutdown >>>> __crash_kexec >>>> panic >>>> sysrq_reset_seq_param_set >>>> __handle_sysrq >>>> write_sysrq_trigger >>>> >>>> The warning is triggered because there is an irq_desc for the GPIO but >>>> it does not have the FLAG_USED_AS_IRQ set. This is because when the GPIO >>>> is exported via gpiod_export(), gpio_is_visible() is used to determine >>>> if the "edge" attribute should be provided but in doing so it ends up >>>> calling gpiochip_to_irq() which creates the irq_desc. >>>> >>>> Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual >>>> intended creation of the irq_desc comes via edge_store() when requested >>>> by the user. >>> To me it still sounds like a hack and the real solution should be done >>> differently/elsewhere. >>> >>> Also I'm worrying that not having this file visible or not may affect >>> existing user space custom scripts we will never hear about. >>> >>> P.S. TBH, I don't care much about sysfs, so if this patch finds its >>> way upstream, I won't be unhappy. >>> >> Same. Which is why - if there'll be no more objections, I will apply it. > I don't think this should be applied. > > It's still not clear from the commit message why gpiochip_disable_irq() > is called for a line which has not been requested. The code that does the calling is in machine_kexec_mask_interrupts(). The problem is that for some irq_chips irq_mask is set to the disable function. The disable call immediately after the mask call does check to see if the irq is not already disabled. > That seems like what > should be fixed, not changing some behaviour in the gpio sysfs interface > which has been there since forever (e.g. do not create the edge > attributes for gpios that cannot be used as interrupts). I don't disagree with the sentiment. The problem is there doesn't appear to be an API that can tell if a GPIO pin is capable of being an irq without actually converting it into one. > There are other ways that mappings can be created (e.g. a gpio that > requested as as interrupt and then released) which would trigger the > same warning it seems. I've tried a few of those cases and haven't been able to provoke the same warning. gpio_sysfs_free_irq() seems to clear whatever flags gpiochip_disable_irq() is complaining about. > Fix the root cause, don't just paper over the symptom. I think maybe there is a compromise where I do something in gpiochip_to_irq() instead of gpio_is_visible(). I'm not entirely sure what that something is > > Johan
On 29/05/23 09:21, Chris Packham wrote: > > On 27/05/23 01:23, Johan Hovold wrote: >> On Fri, May 26, 2023 at 03:01:01PM +0200, Bartosz Golaszewski wrote: >>> On Fri, May 19, 2023 at 12:09 PM Andy Shevchenko >>> <andy.shevchenko@gmail.com> wrote: >>>> On Fri, May 19, 2023 at 8:07 AM Chris Packham >>>> <chris.packham@alliedtelesis.co.nz> wrote: >>>>> On a system with pca9555 GPIOs that have been exported via sysfs the >>>>> following warning could be triggered on kexec(). >>>>> >>>>> WARNING: CPU: 0 PID: 265 at drivers/gpio/gpiolib.c:3411 >>>>> gpiochip_disable_irq >>>>> Call trace: >>>>> gpiochip_disable_irq >>>>> machine_crash_shutdown >>>>> __crash_kexec >>>>> panic >>>>> sysrq_reset_seq_param_set >>>>> __handle_sysrq >>>>> write_sysrq_trigger >>>>> >>>>> The warning is triggered because there is an irq_desc for the GPIO >>>>> but >>>>> it does not have the FLAG_USED_AS_IRQ set. This is because when >>>>> the GPIO >>>>> is exported via gpiod_export(), gpio_is_visible() is used to >>>>> determine >>>>> if the "edge" attribute should be provided but in doing so it ends up >>>>> calling gpiochip_to_irq() which creates the irq_desc. >>>>> >>>>> Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual >>>>> intended creation of the irq_desc comes via edge_store() when >>>>> requested >>>>> by the user. >>>> To me it still sounds like a hack and the real solution should be done >>>> differently/elsewhere. >>>> >>>> Also I'm worrying that not having this file visible or not may affect >>>> existing user space custom scripts we will never hear about. >>>> >>>> P.S. TBH, I don't care much about sysfs, so if this patch finds its >>>> way upstream, I won't be unhappy. >>>> >>> Same. Which is why - if there'll be no more objections, I will apply >>> it. >> I don't think this should be applied. >> >> It's still not clear from the commit message why gpiochip_disable_irq() >> is called for a line which has not been requested. > > The code that does the calling is in machine_kexec_mask_interrupts(). > The problem is that for some irq_chips irq_mask is set to the disable > function. The disable call immediately after the mask call does check > to see if the irq is not already disabled. > >> That seems like what >> should be fixed, not changing some behaviour in the gpio sysfs interface >> which has been there since forever (e.g. do not create the edge >> attributes for gpios that cannot be used as interrupts). > > I don't disagree with the sentiment. The problem is there doesn't > appear to be an API that can tell if a GPIO pin is capable of being an > irq without actually converting it into one. > >> There are other ways that mappings can be created (e.g. a gpio that >> requested as as interrupt and then released) which would trigger the >> same warning it seems. > I've tried a few of those cases and haven't been able to provoke the > same warning. gpio_sysfs_free_irq() seems to clear whatever flags > gpiochip_disable_irq() is complaining about. >> Fix the root cause, don't just paper over the symptom. > I think maybe there is a compromise where I do something in > gpiochip_to_irq() instead of gpio_is_visible(). I'm not entirely sure > what that something is >> Ironically I tried to revisit my fix but found I was no longer able to reproduce the issue. Turns out commit 7dd3d9bd873f ("gpiolib: fix allocation of mixed dynamic/static GPIOs") has fixed it for me but I don't entirely understand how.
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 530dfd19d7b5..f859dcd1cbf3 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -362,8 +362,6 @@ static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr, if (!show_direction) mode = 0; } else if (attr == &dev_attr_edge.attr) { - if (gpiod_to_irq(desc) < 0) - mode = 0; if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags)) mode = 0; }