Skip to content

Interrupts can't be configured for GPIO outputs with 3.18.5 #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fivdi opened this issue Feb 7, 2015 · 14 comments
Closed

Interrupts can't be configured for GPIO outputs with 3.18.5 #808

fivdi opened this issue Feb 7, 2015 · 14 comments

Comments

@fivdi
Copy link

fivdi commented Feb 7, 2015

With previous versions of the kernel it was possible to configure interrupts for GPIO outputs. With 3.18.5 this is no longer possible. This can be seen in the commands sequence below. Any attempt to set the edge to rising, falling, or both results in an error.

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.18.5-v7+ #225 SMP PREEMPT Fri Jan 30 18:53:55 GMT 2015 armv7l GNU/Linux
pi@raspberrypi ~ $ cd /sys/class/gpio/
pi@raspberrypi /sys/class/gpio $ echo 4 > export 
pi@raspberrypi /sys/class/gpio $ ls gpio4/
active_low  device  direction  edge  power  subsystem  uevent  value
pi@raspberrypi /sys/class/gpio $ echo out > gpio4/direction 
pi@raspberrypi /sys/class/gpio $ cat gpio4/direction 
out
pi@raspberrypi /sys/class/gpio $ echo rising > gpio4/edge 
-bash: echo: write error: Input/output error
pi@raspberrypi /sys/class/gpio $ echo falling > gpio4/edge 
-bash: echo: write error: Device or resource busy
pi@raspberrypi /sys/class/gpio $ echo both > gpio4/edge 
-bash: echo: write error: Device or resource busy
pi@raspberrypi /sys/class/gpio $ cat gpio4/edge 
none
pi@raspberrypi /sys/class/gpio $ 
@popcornmix
Copy link
Collaborator

Duplicate of #791

@fivdi
Copy link
Author

fivdi commented Feb 7, 2015

@popcornmix could you reopen this issue please? It's definitely not a duplicate of #791. #791 is about permissions, this issue has nothing to do with permissions, it's about a feature that was available with previous kernels but no longer available in 3.18.5. In particular, it's no longer possible to configure interrupts for a GPIO output in 3.18.5, even then when the user has the permission to access all the appropriate sysfs files.

In the sequence of command shown in the first post above, the user has the permission to access all the appropriate sysfs files and the permissions are functioning correctly. The use has these permission because udev rules were defined to give the user the permissions.

Below is the same sequence of commands, this time for root. As can be seen, the errors also occur for root, so it's not a permission issue.

pi@raspberrypi ~ $ sudo -s
root@raspberrypi:/home/pi# uname -a
Linux raspberrypi 3.18.5-v7+ #225 SMP PREEMPT Fri Jan 30 18:53:55 GMT 2015 armv7l GNU/Linux
root@raspberrypi:/home/pi# cd /sys/class/gpio/
root@raspberrypi:/sys/class/gpio# echo 4 > export 
root@raspberrypi:/sys/class/gpio# ls gpio4/
active_low  device  direction  edge  power  subsystem  uevent  value
root@raspberrypi:/sys/class/gpio# echo out > gpio4/direction 
root@raspberrypi:/sys/class/gpio# cat gpio4/direction 
out
root@raspberrypi:/sys/class/gpio# echo rising > gpio4/edge 
bash: echo: write error: Input/output error
root@raspberrypi:/sys/class/gpio# echo falling > gpio4/edge 
bash: echo: write error: Device or resource busy
root@raspberrypi:/sys/class/gpio# echo both > gpio4/edge 
bash: echo: write error: Device or resource busy
root@raspberrypi:/sys/class/gpio# cat gpio4/edge 
none
root@raspberrypi:/sys/class/gpio# 

@pelwell pelwell reopened this Feb 7, 2015
@pelwell
Copy link
Contributor

pelwell commented Feb 7, 2015

Could you try the same experiment but with Device Tree disabled? Just add "device_tree=" to config.txt and reboot.

@fivdi
Copy link
Author

fivdi commented Feb 7, 2015

Below is the output for root with the device tree disabled. The errors are still there.

Linux raspberrypi 3.18.5-v7+ #225 SMP PREEMPT Fri Jan 30 18:53:55 GMT 2015 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Feb  7 19:00:09 2015 from wherever
pi@raspberrypi ~ $ tail -5 /boot/config.txt 

gpu_mem=16

device_tree=

pi@raspberrypi ~ $ cd /sys/class/gpio/
pi@raspberrypi /sys/class/gpio $ sudo -s
root@raspberrypi:/sys/class/gpio# echo 4 > export 
root@raspberrypi:/sys/class/gpio# ls gpio4/
active_low  direction  edge  power  subsystem  uevent  value
root@raspberrypi:/sys/class/gpio# echo out > gpio4/direction 
root@raspberrypi:/sys/class/gpio# cat gpio4/direction 
out
root@raspberrypi:/sys/class/gpio# echo rising > gpio4/edge 
bash: echo: write error: Input/output error
root@raspberrypi:/sys/class/gpio# echo falling > gpio4/edge 
bash: echo: write error: Device or resource busy
root@raspberrypi:/sys/class/gpio# echo both > gpio4/edge 
bash: echo: write error: Device or resource busy
root@raspberrypi:/sys/class/gpio# cat gpio4/edge 
none
root@raspberrypi:/sys/class/gpio# 

@fivdi
Copy link
Author

fivdi commented Feb 7, 2015

And thank you for reopening the issue :)

@pelwell
Copy link
Contributor

pelwell commented Feb 7, 2015

Interesting - I really thought it was going to be due to a bug/difference in the gpio driver; we use pinctrl-bcm2835 when DT is enabled.

There are a number of issues at the moment - jumping up to 3.18, enabling DT and launching the Pi 2 all on the same day has made for a busy week - but this will get fixed ASAP.

@fivdi
Copy link
Author

fivdi commented Feb 7, 2015

Great, many thanks!

Yes, I'd say you all had a busy week, but there were a hell of a lot more ups than downs.

@notro
Copy link
Contributor

notro commented Feb 7, 2015

Why do you set the direction to output before setting irq flags?

$ sudo -s
# cd /sys/class/gpio/
# echo 4 > export
# cat gpio4/direction
in
# echo rising > gpio4/edge
# echo both > gpio4/edge
# echo none > gpio4/edge
# echo out > gpio4/direction
# echo both > gpio4/edge
bash: echo: write error: Input/output error
#

@fivdi
Copy link
Author

fivdi commented Feb 7, 2015

Because I want an interrupt to be generated when the output changes. I realize that most people only use interrupts on inputs, but it's also possible for outputs on many machines. This also worked with previous versions of the kernel on the Pi.

@notro
Copy link
Contributor

notro commented Feb 7, 2015

I get this error in the kernel log:

[  116.540282] GPIO chip pinctrl-bcm2835: gpio_lock_as_irq: tried to flag a GPIO set as output for IRQ
[  116.540296] gpio-4 (sysfs): failed to flag the GPIO for IRQ

Call chain: gpio_edge_store() -> gpio_setup_irq() -> gpio_lock_as_irq()

gpio_lock_as_irq() was added in 3.13:

int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
{
        if (offset >= chip->ngpio)
                return -EINVAL;

        if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
                chip_err(chip,
                          "%s: tried to flag a GPIO set as output for IRQ\n",
                          __func__);
                return -EIO;
        }

        set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
        return 0;
}

Ref: http://lxr.free-electrons.com/ident?i=gpio_lock_as_irq

@pelwell
Copy link
Contributor

pelwell commented Feb 7, 2015

Odd as the request is, it seems a bit draconian to disallow what must be a valid operation for the hardware.

@notro
Copy link
Contributor

notro commented Feb 7, 2015

Here's the commit that introduced this: torvalds/linux@d468bf9

@pelwell
Copy link
Contributor

pelwell commented Feb 7, 2015

So fivdi, it looks as though it will also fail to work on other machines after 3.13. Are you wanting the ability to monitor any changes on any outputs, or one in particular? Would using another gpio as input just to monitor that output not work?

We have the ability to patch the generic kernel code, but we would want a very good reason to do so.

@fivdi
Copy link
Author

fivdi commented Feb 8, 2015

The software I have can currently monitor any changes on any outputs. It will be possible to modify that software to detect when the outputs are changed per software rather than per hardware as is currently the case. The timing will be a little different, but it should work.

Although I don't actually agree with the claim that detecting interrupts on outputs is a bogus usage scenario (torvalds/linux@d468bf9), I don't have a very good reason for patching the generic kernel code either.

Once again, thank you all for your help.

@fivdi fivdi closed this as completed Feb 8, 2015
popcornmix pushed a commit that referenced this issue Jun 6, 2022
commit 4584666 upstream.

Remove WARNs that sanity check that KVM never lets a triple fault for L2
escape and incorrectly end up in L1.  In normal operation, the sanity
check is perfectly valid, but it incorrectly assumes that it's impossible
for userspace to induce KVM_REQ_TRIPLE_FAULT without bouncing through
KVM_RUN (which guarantees kvm_check_nested_state() will see and handle
the triple fault).

The WARN can currently be triggered if userspace injects a machine check
while L2 is active and CR4.MCE=0.  And a future fix to allow save/restore
of KVM_REQ_TRIPLE_FAULT, e.g. so that a synthesized triple fault isn't
lost on migration, will make it trivially easy for userspace to trigger
the WARN.

Clearing KVM_REQ_TRIPLE_FAULT when forcibly leaving guest mode is
tempting, but wrong, especially if/when the request is saved/restored,
e.g. if userspace restores events (including a triple fault) and then
restores nested state (which may forcibly leave guest mode).  Ignoring
the fact that KVM doesn't currently provide the necessary APIs, it's
userspace's responsibility to manage pending events during save/restore.

  ------------[ cut here ]------------
  WARNING: CPU: 7 PID: 1399 at arch/x86/kvm/vmx/nested.c:4522 nested_vmx_vmexit+0x7fe/0xd90 [kvm_intel]
  Modules linked in: kvm_intel kvm irqbypass
  CPU: 7 PID: 1399 Comm: state_test Not tainted 5.17.0-rc3+ #808
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:nested_vmx_vmexit+0x7fe/0xd90 [kvm_intel]
  Call Trace:
   <TASK>
   vmx_leave_nested+0x30/0x40 [kvm_intel]
   vmx_set_nested_state+0xca/0x3e0 [kvm_intel]
   kvm_arch_vcpu_ioctl+0xf49/0x13e0 [kvm]
   kvm_vcpu_ioctl+0x4b9/0x660 [kvm]
   __x64_sys_ioctl+0x83/0xb0
   do_syscall_64+0x3b/0xc0
   entry_SYSCALL_64_after_hwframe+0x44/0xae
   </TASK>
  ---[ end trace 0000000000000000 ]---

Fixes: cb6a32c ("KVM: x86: Handle triple fault in L2 without killing L1")
Cc: [email protected]
Cc: Chenyi Qiang <[email protected]>
Signed-off-by: Sean Christopherson <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
popcornmix pushed a commit that referenced this issue Jun 6, 2022
commit 4584666 upstream.

Remove WARNs that sanity check that KVM never lets a triple fault for L2
escape and incorrectly end up in L1.  In normal operation, the sanity
check is perfectly valid, but it incorrectly assumes that it's impossible
for userspace to induce KVM_REQ_TRIPLE_FAULT without bouncing through
KVM_RUN (which guarantees kvm_check_nested_state() will see and handle
the triple fault).

The WARN can currently be triggered if userspace injects a machine check
while L2 is active and CR4.MCE=0.  And a future fix to allow save/restore
of KVM_REQ_TRIPLE_FAULT, e.g. so that a synthesized triple fault isn't
lost on migration, will make it trivially easy for userspace to trigger
the WARN.

Clearing KVM_REQ_TRIPLE_FAULT when forcibly leaving guest mode is
tempting, but wrong, especially if/when the request is saved/restored,
e.g. if userspace restores events (including a triple fault) and then
restores nested state (which may forcibly leave guest mode).  Ignoring
the fact that KVM doesn't currently provide the necessary APIs, it's
userspace's responsibility to manage pending events during save/restore.

  ------------[ cut here ]------------
  WARNING: CPU: 7 PID: 1399 at arch/x86/kvm/vmx/nested.c:4522 nested_vmx_vmexit+0x7fe/0xd90 [kvm_intel]
  Modules linked in: kvm_intel kvm irqbypass
  CPU: 7 PID: 1399 Comm: state_test Not tainted 5.17.0-rc3+ #808
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:nested_vmx_vmexit+0x7fe/0xd90 [kvm_intel]
  Call Trace:
   <TASK>
   vmx_leave_nested+0x30/0x40 [kvm_intel]
   vmx_set_nested_state+0xca/0x3e0 [kvm_intel]
   kvm_arch_vcpu_ioctl+0xf49/0x13e0 [kvm]
   kvm_vcpu_ioctl+0x4b9/0x660 [kvm]
   __x64_sys_ioctl+0x83/0xb0
   do_syscall_64+0x3b/0xc0
   entry_SYSCALL_64_after_hwframe+0x44/0xae
   </TASK>
  ---[ end trace 0000000000000000 ]---

Fixes: cb6a32c ("KVM: x86: Handle triple fault in L2 without killing L1")
Cc: [email protected]
Cc: Chenyi Qiang <[email protected]>
Signed-off-by: Sean Christopherson <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
popcornmix pushed a commit that referenced this issue Jun 6, 2022
commit 4584666 upstream.

Remove WARNs that sanity check that KVM never lets a triple fault for L2
escape and incorrectly end up in L1.  In normal operation, the sanity
check is perfectly valid, but it incorrectly assumes that it's impossible
for userspace to induce KVM_REQ_TRIPLE_FAULT without bouncing through
KVM_RUN (which guarantees kvm_check_nested_state() will see and handle
the triple fault).

The WARN can currently be triggered if userspace injects a machine check
while L2 is active and CR4.MCE=0.  And a future fix to allow save/restore
of KVM_REQ_TRIPLE_FAULT, e.g. so that a synthesized triple fault isn't
lost on migration, will make it trivially easy for userspace to trigger
the WARN.

Clearing KVM_REQ_TRIPLE_FAULT when forcibly leaving guest mode is
tempting, but wrong, especially if/when the request is saved/restored,
e.g. if userspace restores events (including a triple fault) and then
restores nested state (which may forcibly leave guest mode).  Ignoring
the fact that KVM doesn't currently provide the necessary APIs, it's
userspace's responsibility to manage pending events during save/restore.

  ------------[ cut here ]------------
  WARNING: CPU: 7 PID: 1399 at arch/x86/kvm/vmx/nested.c:4522 nested_vmx_vmexit+0x7fe/0xd90 [kvm_intel]
  Modules linked in: kvm_intel kvm irqbypass
  CPU: 7 PID: 1399 Comm: state_test Not tainted 5.17.0-rc3+ #808
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:nested_vmx_vmexit+0x7fe/0xd90 [kvm_intel]
  Call Trace:
   <TASK>
   vmx_leave_nested+0x30/0x40 [kvm_intel]
   vmx_set_nested_state+0xca/0x3e0 [kvm_intel]
   kvm_arch_vcpu_ioctl+0xf49/0x13e0 [kvm]
   kvm_vcpu_ioctl+0x4b9/0x660 [kvm]
   __x64_sys_ioctl+0x83/0xb0
   do_syscall_64+0x3b/0xc0
   entry_SYSCALL_64_after_hwframe+0x44/0xae
   </TASK>
  ---[ end trace 0000000000000000 ]---

Fixes: cb6a32c ("KVM: x86: Handle triple fault in L2 without killing L1")
Cc: [email protected]
Cc: Chenyi Qiang <[email protected]>
Signed-off-by: Sean Christopherson <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants