hid: hid-core: Fix a sleep-in-atomic-context bug in __hid_request()
PATCH v2 1/1 hid: hid-core: Fix a sleep-in-atomic-context bug in __hid_request()
hid_alloc_report_buf() has to be called with GFP_ATOMIC in __hid_request(), because there are the following callchains leading to __hid_request() being an atomic context:
picolcd_send_and_wait (acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
picolcd_reset (acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_play (acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_set_autocenter_ffex (acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
This bug is found by my static analysis tool DSAC.
---
v2:
* Make the description more human readable.
Thanks Jiri for good advice.
--- drivers/hid/hid-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/hid-core.c +1 -1
@@ -1442,7 +1442,7 @@ void __hid_request(struct hid_device *hid, struct hid_report *report,int ret;u32 len; - buf =hid_alloc_report_buf (report,GFP_KERNEL ); + buf =hid_alloc_report_buf (report,GFP_ATOMIC );if (!buf)return ;
Signature · 1 line
2.17.0
Jiri Kosina
maintainerQuoted 19 lines · Jia-Ju Bai
hid_alloc_report_buf()has to be called withGFP_ATOMICin__hid_request(), because there are the following callchains leading to__hid_request()being an atomic context:
picolcd_send_and_wait(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
picolcd_reset(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_play(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
Quoted 4 lines
lg4ff_set_autocenter_ffex(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
Hm, so it's always drivers calling out into core in atomic context. So either we take this, and put our bets on being able to allocate the buffer without sleeping, or actually fix the few drivers (it's just lg4ff and picolcd at the end of the day) not to do that, and explicitly anotate __hid_request() with might_sleep().
Hmm?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 21 lines · Jiri Kosina
Quoted 19 lines · Jia-Ju Bai
hid_alloc_report_buf()has to be called withGFP_ATOMICin__hid_request(), because there are the following callchains leading to__hid_request()being an atomic context:
picolcd_send_and_wait(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
picolcd_reset(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_play(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
Quoted 7 lines
Quoted 4 lines
lg4ff_set_autocenter_ffex(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)Hm, so it's always drivers calling out into core in atomic context. So either we take this, and put our bets on being able to allocate the buffer without sleeping,
In my opinion, I prefer this way.
Best wishes,
Jia-Ju Bai
Quoted 8 lines
or actually fix the few drivers (it's just lg4ff and picolcd at the end of the day) not to do that, and explicitly anotate
__hid_request()withmight_sleep().Hmm?
Thanks,
Jiri Kosina
maintainerQuoted 19 lines · Jia-Ju Bai
Quoted 19 lines
Quoted 19 lines
picolcd_send_and_wait(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
picolcd_reset(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_play(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_set_autocenter_ffex(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
Quoted 5 lines
Quoted 3 lines
Hm, so it's always drivers calling out into core in atomic context. So either we take this, and put our bets on being able to allocate the buffer without sleeping,
In my opinion, I prefer this way.
Why? Forcing all the report buffer to be limited to be non-sleeping allocations just because of two drivers, looks like an overkill, and actually calls for more issues (as GFP_ATOMIC is of course in principle less likely to succeed).
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 21 lines · Jiri Kosina
Quoted 19 lines · Jia-Ju Bai
Quoted 19 lines
Quoted 19 lines
picolcd_send_and_wait(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
picolcd_reset(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_play(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
lg4ff_set_autocenter_ffex(acquire a spinlock)
hid_hw_request
__hid_request
hid_alloc_report_buf(GFP_KERNEL)
Quoted 9 lines
Quoted 4 lines
Quoted 3 lines
Hm, so it's always drivers calling out into core in atomic context. So either we take this, and put our bets on being able to allocate the buffer without sleeping,
In my opinion, I prefer this way.
Why? Forcing all the report buffer to be limited to be non-sleeping allocations just because of two drivers, looks like an overkill, and actually calls for more issues (as
GFP_ATOMICis of course in principle less likely to succeed).
Okay, I thought that using GFP_ATOMIC is the simplest way to fix these bugs.
But I check the Linux kernel code again, and find that hid_hw_request() are called at many places. So changing this function may affect many drivers.
I agree to only change the two drivers, and explicitly anotate __hid_request() with might_sleep().
Best wishes,
Jia-Ju Bai
Jiri Kosina
maintainerQuoted 11 lines · Jia-Ju Bai
Quoted 4 lines
Why? Forcing all the report buffer to be limited to be non-sleeping allocations just because of two drivers, looks like an overkill, and actually calls for more issues (as
GFP_ATOMICis of course in principle less likely to succeed).Okay, I thought that using
GFP_ATOMICis the simplest way to fix these bugs. But I check the Linux kernel code again, and find thathid_hw_request()are called at many places.
So changing this function may affect many drivers.
I agree to only change the two drivers, and explicitly anotate__hid_request()withmight_sleep().
Thanks. Are you planning to submit a patch to do that?
Signature · 2 lines
Jiri Kosina SUSE Labs