solve deadlock caused by memory allocation with I/O
Subsystems (13)
v4 cover solve deadlock caused by memory allocation with I/O
This patchset try to solve one deadlock problem which might be caused by memory allocation with block I/O during runtime PM and block device error handling path. Traditionly, the problem is addressed by passing GFP_NOIO statically to mm, but that is not a effective solution, see detailed description in patch 1's commit log.
This patch set introduces one process flag and trys to fix the deadlock problem on block device/network device during runtime PM or usb bus reset.
The 1st one is the change on include/sched.h and mm.
The 2nd patch introduces the flag of memalloc_noio on 'dev_pm_info', and pm_runtime_set_memalloc_noio(), so that PM Core can teach mm to not allocate mm with GFP_IOFS during the runtime_resume callback only on device with the flag set.
The following 2 patches apply the introduced pm_runtime_set_memalloc_noio() to mark all devices as memalloc_noio_resume in the path from the block or network device to the root device in device tree.
The last 2 patches are applied again PM and USB subsystem to demonstrate how to use the introduced mechanism to fix the deadlock problem.
Change logs:
V4:
- patches from the 2nd to the 6th changed
- call pm_runtime_set_memalloc_noio() after device_add() as pointed by Alan
- set PF_MEMALLOC_NOIO during runtime_suspend()
V3:
- patch 2/6 and 5/6 changed, see their commit log
- remove RFC from title since several guys have expressed that it is a reasonable solution
V2:
- remove changes on 'may_writepage' and 'may_swap'(1/6)
- unset GFP_IOFS in try_to_free_pages() path(1/6)
- introduce pm_runtime_set_memalloc_noio()
- only apply the meachnism on block/network device and its ancestors for runtime resume context
V1:
- take Minchan's change to avoid the check in alloc_page hot path
- change the helpers' style into save/restore as suggested by Alan
- memory allocation with no io in usb bus reset path for all devices as suggested by Greg and Oliver
block/genhd.c | 9 +++++ drivers/base/power/runtime.c | 89 +++++++++++++++++++++++++++++++++++++++++- drivers/usb/core/hub.c | 13 ++++++ include/linux/pm.h | 1 + include/linux/pm_runtime.h | 3 ++ include/linux/sched.h | 10 +++++ mm/page_alloc.c | 10 ++++- mm/vmscan.c | 12 ++++++ net/core/net-sysfs.c | 5 +++
Thanks,
--
Ming Lei
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 1/6 mm: teach mm by current context info to not do I/O during memory allocation
This patch introduces PF_MEMALLOC_NOIO on process flag('flags' field of 'struct task_struct'), so that the flag can be set by one task to avoid doing I/O inside memory allocation in the task's context.
The patch trys to solve one deadlock problem caused by block device, and the problem may happen at least in the below situations:
- during block device runtime resume, if memory allocation with GFP_KERNEL is called inside runtime resume callback of any one of its ancestors(or the block device itself), the deadlock may be triggered inside the memory allocation since it might not complete until the block device becomes active and the involed page I/O finishes. The situation is pointed out first by Alan Stern. It is not a good approach to convert all GFP_KERNEL[1] in the path into GFP_NOIO because several subsystems may be involved(for example, PCI, USB and SCSI may be involved for usb mass stoarage device, network devices involved too in the iSCSI case)
- during block device runtime suspend, because runtime resume need to wait for completion of concurrent runtime suspend.
- during error handling of usb mass storage deivce, USB bus reset will be put on the device, so there shouldn't have any memory allocation with GFP_KERNEL during USB bus reset, otherwise the deadlock similar with above may be triggered. Unfortunately, any usb device may include one mass storage interface in theory, so it requires all usb interface drivers to handle the situation. In fact, most usb drivers don't know how to handle bus reset on the device and don't provide .pre_set() and .post_reset() callback at all, so USB core has to unbind and bind driver for these devices. So it is still not practical to resort to GFP_NOIO for solving the problem.
Also the introduced solution can be used by block subsystem or block drivers too, for example, set the PF_MEMALLOC_NOIO flag before doing actual I/O transfer.
It is not a good idea to convert all these GFP_KERNEL in the affected path into GFP_NOIO because these functions doing that may be implemented as library and will be called in many other contexts.
In fact, memalloc_noio() can convert some of current static GFP_NOIO allocation into GFP_KERNEL back in other non-affected contexts, at least almost all GFP_NOIO in USB subsystem can be converted into GFP_KERNEL after applying the approach and make allocation with GFP_IO only happen in runtime resume/bus reset/block I/O transfer contexts generally.
[1], several GFP_KERNEL allocation examples in runtime resume path
- pci subsystemacpi_os_allocate
<-acpi_ut_allocate <-ACPI_ALLOCATE_ZEROED <-acpi_evaluate_object <-__acpi_bus_set_power <-acpi_bus_set_power <-acpi_pci_set_power_state <-platform_pci_set_power_state <-pci_platform_power_transition <-__pci_complete_power_transition <-pci_set_power_state <-pci_restore_standard_config <-pci_pm_runtime_resume
- usb subsystemusb_get_status
<-finish_port_resume
<-usb_port_resume
<-generic_resume
<-usb_resume_device
<-usb_resume_both
<-usb_runtime_resume
- some individual usb drivers
usblp, uvc, gspca, most of dvb-usb-v2 media drivers, cpia2, az6007, ....
That is just what I have found. Unfortunately, this allocation can only be found by human being now, and there should be many not found since any function in the resume path(call tree) may allocate memory with GFP_KERNEL.
---
v4:
- fix comment
v3:
- no change
v2:
- remove changes on 'may_writepage' and 'may_swap' because that isn't related with the patchset, and can't introduce I/O in allocation path if GFP_IOFS is unset, so handing 'may_swap' and may_writepage on GFP_NOIO or GFP_NOFS should be a mm internal thing, and let mm guys deal with that, :-).
Looks clearing the two may_XXX flag only excludes dirty pages
and anon pages for relaiming, and the behaviour should be decided
by GFP FLAG, IMO.
- unset GFP_IOFS in try_to_free_pages() path since alloc_page_buffers()
and dma_alloc_from_contiguous may drop into the path, as pointed by KAMEZAWA Hiroyuki
v1:
- take Minchan's change to avoid the check in alloc_page hot path
- change the helpers' style into save/restore as suggested by Alan Stern
--- include/linux/sched.h | 10 ++++++++++ mm/page_alloc.c | 10 +++++++++- mm/vmscan.c | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-)
include/linux/sched.h +10 -0
@@ -1805,6 +1805,7 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *#define PF_FROZEN 0x00010000/* frozen for system suspend */ #define PF_FSTRANS 0x00020000/* inside a filesystem transaction */ #define PF_KSWAPD 0x00040000/* I am kswapd */ +#define PF_MEMALLOC_NOIO 0x00080000/* Allocating memory without IO involved */ #define PF_LESS_THROTTLE 0x00100000/* Throttle me less: I clean memory */ #define PF_KTHREAD 0x00200000/* I am a kernel thread */ #define PF_RANDOMIZE 0x00400000/* randomize virtual address space */
@@ -1842,6 +1843,15 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *#define tsk_used_math (p) ((p)->flags & PF_USED_MATH)#define used_math () tsk_used_math(current) +#define memalloc_noio () (current->flags & PF_MEMALLOC_NOIO) +#define memalloc_noio_save (flag) do { \ + (flag) = current->flags & PF_MEMALLOC_NOIO; \ + current->flags |= PF_MEMALLOC_NOIO; \ +} while (0) +#define memalloc_noio_restore (flag) do { \ + current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flag; \ +} while (0) +/* * task->jobctl flags */
mm/page_alloc.c +9 -1
@@ -2634,10 +2634,18 @@ retry_cpuset: page =get_page_from_freelist (gfp_mask|__GFP_HARDWALL, nodemask, order, zonelist, high_zoneidx, alloc_flags, preferred_zone, migratetype); -if (unlikely (!page)) +if (unlikely (!page)) { +/* +* Runtime PM, block IO and its error handling path +* can deadlock because I/O on the device might not +* complete. +*/ +if (unlikely (memalloc_noio ())) + gfp_mask &= ~GFP_IOFS ; page =__alloc_pages_slowpath (gfp_mask, order, zonelist, high_zoneidx, nodemask, preferred_zone, migratetype); + }trace_mm_page_alloc (page, order, gfp_mask, migratetype);
mm/vmscan.c +12 -0
@@ -2304,6 +2304,12 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order, .gfp_mask = sc.gfp_mask, }; +if (unlikely (memalloc_noio ())) { + gfp_mask &= ~GFP_IOFS ; + sc.gfp_mask = gfp_mask; + shrink.gfp_mask = sc.gfp_mask; + } +throttle_direct_reclaim (gfp_mask, zonelist, nodemask);/*
@@ -3304,6 +3310,12 @@ static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) };unsigned long nr_slab_pages0, nr_slab_pages1; +if (unlikely (memalloc_noio ())) { + gfp_mask &= ~GFP_IOFS ; + sc.gfp_mask = gfp_mask; + shrink.gfp_mask = sc.gfp_mask; + } +cond_resched ();/* * We need to be able to allocate from the reserves for RECLAIM_SWAP
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerRe: [PATCH v4 1/6] mm: teach mm by current context info to not do I/O during memory allocation
Quoted 40 lines · On Sat, 3 Nov 2012 16:35:09 +0800 Ming Lei <[email protected]> wrote:
This patch introduces
PF_MEMALLOC_NOIOon processflag('flags' field of 'struct task_struct'), so that the flag can be set by one task to avoid doing I/O inside memory allocation in the task's context.The patch trys to solve one deadlock problem caused by block device, and the problem may happen at least in the below situations:
- during block device runtime resume, if memory allocation with
GFP_KERNELis called inside runtime resume callback of any one of its ancestors(or the block device itself), the deadlock may be triggered inside the memory allocation since it might not complete until the block device becomes active and the involed page I/O finishes. The situation is pointed out first by Alan Stern. It is not a good approach to convert all GFP_KERNEL[1] in the path intoGFP_NOIObecause several subsystems may be involved(for example, PCI, USB and SCSI may be involved for usb mass stoarage device, network devices involved too in the iSCSI case)- during block device runtime suspend, because runtime resume need to wait for completion of concurrent runtime suspend.
- during error handling of usb mass storage deivce, USB bus reset will be put on the device, so there shouldn't have any memory allocation with
GFP_KERNELduring USB bus reset, otherwise the deadlock similar with above may be triggered. Unfortunately, any usb device may include one mass storage interface in theory, so it requires all usb interface drivers to handle the situation. In fact, most usb drivers don't know how to handle bus reset on the device and don't provide .pre_set() and .post_reset() callback at all, so USB core has to unbind and bind driver for these devices. So it is still not practical to resort toGFP_NOIOfor solving the problem.Also the introduced solution can be used by block subsystem or block drivers too, for example, set the
PF_MEMALLOC_NOIOflag before doing actual I/O transfer.It is not a good idea to convert all these
GFP_KERNELin the affected path intoGFP_NOIObecause these functions doing that may be implemented as library and will be called in many other contexts.
Quoted 6 lines
In fact,
memalloc_noio()can convert some of current staticGFP_NOIOallocation intoGFP_KERNELback in other non-affected contexts, at least almost allGFP_NOIOin USB subsystem can be converted intoGFP_KERNELafter applying the approach and make allocation withGFP_IOonly happen in runtime resume/bus reset/block I/O transfer contexts generally.
It's unclear from the description why we're also clearing __GFP_FS in this situation.
If we can avoid doing this then there will be a very small gain: there are some situations in which a filesystem can clean pagecache without performing I/O.
It doesn't appear that the patch will add overhead to the alloc/free hotpaths, which is good.
Quoted 16 lines
...
include/linux/sched.h +1
-0@@ -1805,6 +1805,7 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *#define PF_FROZEN 0x00010000/* frozen for system suspend */ #define PF_FSTRANS 0x00020000/* inside a filesystem transaction */ #define PF_KSWAPD 0x00040000/* I am kswapd */ +#define PF_MEMALLOC_NOIO 0x00080000/* Allocating memory without IO involved */ #define PF_LESS_THROTTLE 0x00100000/* Throttle me less: I clean memory */ #define PF_KTHREAD 0x00200000/* I am a kernel thread */ #define PF_RANDOMIZE 0x00400000/* randomize virtual address space */ @@ -1842,6 +1843,15 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *#define tsk_used_math (p) ((p)->flags & PF_USED_MATH)#define used_math () tsk_used_math(current)
include/linux/sched.h extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t * · line 1854
Quoted 10 lines
include/linux/sched.h +9
-0+#define memalloc_noio () (current->flags & PF_MEMALLOC_NOIO) +#define memalloc_noio_save (flag) do { \ + (flag) = current->flags & PF_MEMALLOC_NOIO; \ + current->flags |= PF_MEMALLOC_NOIO; \ +} while (0) +#define memalloc_noio_restore (flag) do { \ + current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flag; \ +} while (0) +
Again with the ghastly macros. Please, do this properly in regular old C, as previously discussed. It really doesn't matter what daft things local_irq_save() did 20 years ago. Just do it right!
Also, you can probably put the unlikely() inside memalloc_noio() and avoid repeating it at all the callsites.
And it might be neater to do:
/*
* Nice comment goes here
*/
static inline gfp_t memalloc_noio_flags(gfp_t flags)
{
if (unlikely(current->flags & PF_MEMALLOC_NOIO))
flags &= ~GFP_IOFS;
return flags;
}Quoted 6 lines
include/linux/sched.h +0
-0* task->jobctl flags */
include/linux/sched.h unsigned long try_to_free_pages(struct zonelist *zonelist, int order, · line 2311
Quoted 8 lines
include/linux/sched.h +5
-0@@ -2304,6 +2304,12 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order, .gfp_mask = sc.gfp_mask, }; +if (unlikely (memalloc_noio ())) { + gfp_mask &= ~GFP_IOFS ; + sc.gfp_mask = gfp_mask; + shrink.gfp_mask = sc.gfp_mask; + }
We can avoid writing to shrink.gfp_mask twice. And maybe sc.gfp_mask as well. Unclear, I didn't think about it too hard ;)
include/linux/sched.h unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
Quoted 6 lines
include/linux/sched.h +0
-0throttle_direct_reclaim (gfp_mask, zonelist, nodemask);/*
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v4 1/6] mm: teach mm by current context info to not do I/O during memory allocation
Quoted 7 lines · Andrew Morton
It's unclear from the description why we're also clearing
__GFP_FSin this situation.If we can avoid doing this then there will be a very small gain: there are some situations in which a filesystem can clean pagecache without performing I/O.
Firstly, the patch follows the policy in the system suspend/resume situation, in which the __GFP_FS is cleared, and basically the problem is very similar with that in system PM path.
Secondly, inside shrink_page_list(), pageout() may be triggered on dirty anon page if __GFP_FS is set.
IMO, if performing I/O can be completely avoided when __GFP_FS is set, the flag can be kept, otherwise it is better to clear it in the situation.
Quoted 3 lines
It doesn't appear that the patch will add overhead to the alloc/free hotpaths, which is good.
Thanks for previous Minchan's comment.
Quoted 18 lines
Quoted 18 lines
...
include/linux/sched.h +1
-0@@ -1805,6 +1805,7 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *#define PF_FROZEN 0x00010000/* frozen for system suspend */ #define PF_FSTRANS 0x00020000/* inside a filesystem transaction */ #define PF_KSWAPD 0x00040000/* I am kswapd */ +#define PF_MEMALLOC_NOIO 0x00080000/* Allocating memory without IO involved */ #define PF_LESS_THROTTLE 0x00100000/* Throttle me less: I clean memory */ #define PF_KTHREAD 0x00200000/* I am a kernel thread */ #define PF_RANDOMIZE 0x00400000/* randomize virtual address space */ @@ -1842,6 +1843,15 @@ extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *#define tsk_used_math (p) ((p)->flags & PF_USED_MATH)#define used_math () tsk_used_math(current)
Quoted 13 lines
include/linux/sched.h extern void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t * · line 1854
Quoted 9 lines
include/linux/sched.h +9
-0+#define memalloc_noio () (current->flags & PF_MEMALLOC_NOIO) +#define memalloc_noio_save (flag) do { \ + (flag) = current->flags & PF_MEMALLOC_NOIO; \ + current->flags |= PF_MEMALLOC_NOIO; \ +} while (0) +#define memalloc_noio_restore (flag) do { \ + current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flag; \ +} while (0) +Again with the ghastly macros. Please, do this properly in regular old C, as previously discussed. It really doesn't matter what daft things
local_irq_save()did 20 years ago. Just do it right!
OK, I will take inline function in -v5.
Quoted 10 lines
Also, you can probably put the
unlikely()insidememalloc_noio()and avoid repeating it at all the callsites.And it might be neater to do:
/*
* Nice comment goes here
*/
static inlinegfp_tmemalloc_noio_flags(gfp_t flags)
Quoted 5 lines
{
if (unlikely(current->flags & PF_MEMALLOC_NOIO))
flags &= ~GFP_IOFS;
return flags;
}But without the check in callsites, some local variables will be write two times,
so it is better to not do it.
Quoted 10 lines
Quoted 10 lines
include/linux/sched.h +0
-0* task->jobctl flags */ @@ -2304,6 +2304,12 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order, .gfp_mask = sc.gfp_mask, };
Quoted 8 lines
include/linux/sched.h unsigned long try_to_free_pages(struct zonelist *zonelist, int order, · line 2311
Quoted 5 lines
include/linux/sched.h +5
-0+if (unlikely (memalloc_noio ())) { + gfp_mask &= ~GFP_IOFS ; + sc.gfp_mask = gfp_mask; + shrink.gfp_mask = sc.gfp_mask; + }We can avoid writing to shrink.gfp_mask twice. And maybe sc.gfp_mask as well. Unclear, I didn't think about it too hard ;)
Yes, we can do it by initializing 'shrink' local variable just after the branch, so one writing is enough. Will do it in -v5.
Thanks,
--
Ming Lei
Andrew Morton
maintainerRe: [PATCH v4 1/6] mm: teach mm by current context info to not do I/O during memory allocation
Quoted 5 lines · On Wed, 7 Nov 2012 11:11:24 +0800 Ming Lei <[email protected]> wrote:
Quoted 4 lines · Andrew Morton
It's unclear from the description why we're also clearing
__GFP_FSin this situation.
Quoted 7 lines
Quoted 3 lines
If we can avoid doing this then there will be a very small gain: there are some situations in which a filesystem can clean pagecache without performing I/O.
Firstly, the patch follows the policy in the system suspend/resume situation, in which the
__GFP_FSis cleared, and basically the problem is very similar with that in system PM path.
I suspect that code is wrong. Or at least, suboptimal.
Quoted 2 lines
Secondly, inside
shrink_page_list(),pageout()may be triggered on dirty anon page if__GFP_FSis set.
pageout() should be called if GFP_FS is set or if GFP_IO is set and the IO is against swap.
And that's what we want to happen: we want to enter the fs to try to turn dirty pagecache into clean pagecache without doing IO. If we in fact enter the device drivers when GFP_IO was not set then that's a bug which we should fix.
Quoted 2 lines
IMO, if performing I/O can be completely avoided when
__GFP_FSis set, the flag can be kept, otherwise it is better to clear it in the situation.
yup.
Quoted 16 lines
Quoted 10 lines
Also, you can probably put the
unlikely()insidememalloc_noio()and avoid repeating it at all the callsites.And it might be neater to do:
/*
* Nice comment goes here
*/
static inlinegfp_tmemalloc_noio_flags(gfp_t flags)Quoted 5 lines
{ if (unlikely(current->flags & PF_MEMALLOC_NOIO)) flags &= ~GFP_IOFS; return flags; }
Quoted 3 lines
But without the check in callsites, some local variables will be write two times,
so it is better to not do it.
I don't see why - we just modify the incoming gfp_t at the start of the function, then use it.
It gets a bit tricky with those struct initialisations. Things like
struct foo bar {
.a = a1,
.b = b1,
};should not be turned into
struct foo bar {
.a = a1,
};
bar.b = b1;and we don't want to do
struct foo bar { };
bar.a = a1;
bar.b = b1;either, because these are indeed a double-write. But we can do
struct foo bar {
.flags = (flags = memalloc_noio_flags(flags)),
.b = b1,
};which is a bit arcane but not toooo bad. Have a think about it...
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v4 1/6] mm: teach mm by current context info to not do I/O during memory allocation
Quoted 10 lines · Andrew Morton
Quoted 4 lines
Firstly, the patch follows the policy in the system suspend/resume situation, in which the
__GFP_FSis cleared, and basically the problem is very similar with that in system PM path.I suspect that code is wrong. Or at least, suboptimal.
Quoted 2 lines
Secondly, inside
shrink_page_list(),pageout()may be triggered on dirty anon page if__GFP_FSis set.
Quoted 7 lines
pageout()should be called ifGFP_FSis set or ifGFP_IOis set and the IO is against swap.And that's what we want to happen: we want to enter the fs to try to turn dirty pagecache into clean pagecache without doing IO. If we in fact enter the device drivers when
GFP_IOwas not set then that's a bug which we should fix.
OK, I got it, and I'll not clear GFP_FS in -v5.
Quoted 51 lines
Quoted 3 lines
IMO, if performing I/O can be completely avoided when
__GFP_FSis set, the flag can be kept, otherwise it is better to clear it in the situation.yup.
Quoted 16 lines
Quoted 10 lines
Also, you can probably put the
unlikely()insidememalloc_noio()and avoid repeating it at all the callsites.And it might be neater to do:
/*
* Nice comment goes here
*/
static inlinegfp_tmemalloc_noio_flags(gfp_t flags)Quoted 5 lines
{ if (unlikely(current->flags & PF_MEMALLOC_NOIO)) flags &= ~GFP_IOFS; return flags; }Quoted 3 lines
But without the check in callsites, some local variables will be write two times,
so it is better to not do it.I don't see why - we just modify the incoming
gfp_tat the start of the function, then use it.It gets a bit tricky with those struct initialisations. Things like
struct foo bar { .a = a1, .b = b1, };should not be turned into
struct foo bar { .a = a1, }; bar.b = b1;and we don't want to do
struct foo bar { }; bar.a = a1; bar.b = b1;
Quoted 8 lines
either, because these are indeed a double-write. But we can do
struct foo bar { .flags = (flags = memalloc_noio_flags(flags)), .b = b1, };which is a bit arcane but not toooo bad. Have a think about it...
Got it, looks memalloc_noio_flags() neater, and I will take it in v5.
Thanks,
--
Ming Lei
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 2/6 PM / Runtime: introduce pm_runtime_set_memalloc_noio()
In reply to Ming LeiThe patch introduces the flag of memalloc_noio in 'struct dev_pm_info' to help PM core to teach mm not allocating memory with GFP_KERNEL flag for avoiding probable deadlock.
As explained in the comment, any GFP_KERNEL allocation inside runtime_resume() or runtime_suspend() on any one of device in the path from one block or network device to the root device in the device tree may cause deadlock, the introduced pm_runtime_set_memalloc_noio() sets or clears the flag on device in the path recursively.
---
v4:
- rename memalloc_noio_resume as memalloc_noio
- remove pm_runtime_get_memalloc_noio()
- add comments on pm_runtime_set_memalloc_noio
v3:
- introduce pm_runtime_get_memalloc_noio()
- hold one global lock on pm_runtime_set_memalloc_noio
- hold device power lock when accessing memalloc_noio_resume flag suggested by Alan Stern
- implement pm_runtime_set_memalloc_noio without recursion suggested by Alan Stern
v2:
- introduce pm_runtime_set_memalloc_noio()
--- drivers/base/power/runtime.c | 57 ++++++++++++++++++++++++++++++++++++++++++ include/linux/pm.h | 1 + include/linux/pm_runtime.h | 3 +++ 3 files changed, 61 insertions(+)
drivers/base/power/runtime.c +57 -0
@@ -124,6 +124,63 @@ unsigned long pm_runtime_autosuspend_expiration(struct device *dev) }EXPORT_SYMBOL_GPL (pm_runtime_autosuspend_expiration); +static int dev_memalloc_noio (struct device *dev,void *data) +{ +return dev->power.memalloc_noio; +} + +/* +* pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag. +* @dev: Device to handle. +* @enable: True for setting the flag and False for clearing the flag. +* +* Set the flag for all devices in the path from the device to the +* root device in the device tree if @enable is true, otherwise clear +* the flag for devices in the path whose siblings don't set the flag. +* +* The function should only be called by block device, or network +* device driver for solving the deadlock problem during runtime +* resume/suspend: +* if memory allocation with GFP_KERNEL is called inside runtime +* resume/suspend callback of any one of its ancestors(or the +* block device itself), the deadlock may be triggered inside the +* memory allocation since it might not complete until the block +* device becomes active and the involed page I/O finishes. The +* situation is pointed out first by Alan Stern. Network device +* are involved in iSCSI kind of situation. +* +* The lock of dev_hotplug_mutex is held in the function for handling +* hotplug race because pm_runtime_set_memalloc_noio() may be called +* in async probe(). +* +* The function should be called between device_add() and device_del() +* on the affected device(block/network device). +*/ +void pm_runtime_set_memalloc_noio (struct device *dev,bool enable) +{ +static DEFINE_MUTEX (dev_hotplug_mutex); + +mutex_lock (&dev_hotplug_mutex); +for (;;) { +/* hold power lock since bitfield is not SMP-safe. */ +spin_lock_irq (&dev->power.lock); + dev->power.memalloc_noio = enable; +spin_unlock_irq (&dev->power.lock); + + dev = dev->parent; + +/* only clear the flag for one device if all +* children of the device don't set the flag. +*/ +if (!dev || (!enable && + device_for_each_child(dev, NULL, + dev_memalloc_noio))) +break ; + } +mutex_unlock (&dev_hotplug_mutex); +} +EXPORT_SYMBOL_GPL (pm_runtime_set_memalloc_noio); +/** * rpm_check_suspend_allowed - Test whether a device may be suspended. * @dev: Device to test.
include/linux/pm.h +1 -0
@@ -538,6 +538,7 @@ struct dev_pm_info { unsigned int irq_safe:1; unsigned int use_autosuspend:1; unsigned int timer_autosuspends:1; + unsigned int memalloc_noio:1;enum rpm_request request;enum rpm_status runtime_status;int runtime_error;
include/linux/pm_runtime.h +3 -0
@@ -47,6 +47,7 @@ extern void pm_runtime_set_autosuspend_delay(struct device *dev, int delay);extern unsigned long pm_runtime_autosuspend_expiration (struct device *dev);extern void pm_runtime_update_max_time_suspended (struct device *dev,s64 delta_ns); +extern void pm_runtime_set_memalloc_noio (struct device *dev,bool enable);static inline bool pm_children_suspended (struct device *dev) {
@@ -149,6 +150,8 @@ static inline void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) {}static inline unsigned long pm_runtime_autosuspend_expiration (struct device *dev) {return 0 ; } +static inline void pm_runtime_set_memalloc_noio (struct device *dev, +bool enable){}#endif /* !CONFIG_PM_RUNTIME */
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerRe: [PATCH v4 2/6] PM / Runtime: introduce pm_runtime_set_memalloc_noio()
Quoted 11 lines · On Sat, 3 Nov 2012 16:35:10 +0800 Ming Lei…
The patch introduces the flag of
memalloc_noioin 'structdev_pm_info' to help PM core to teach mm not allocating memory withGFP_KERNELflag for avoiding probable deadlock.As explained in the comment, any
GFP_KERNELallocation insideruntime_resume()orruntime_suspend()on any one of device in the path from one block or network device to the root device in the device tree may cause deadlock, the introducedpm_runtime_set_memalloc_noio()sets or clears the flag on device in the path recursively.
checkpatch finds a number of problems with this patch, all of which should be fixed. Please always use checkpatch.
Quoted 45 lines
index 3148b10..d477924 100644
drivers/base/power/runtime.c +38
-0@@ -124,6 +124,63 @@ unsigned long pm_runtime_autosuspend_expiration(struct device *dev) }EXPORT_SYMBOL_GPL (pm_runtime_autosuspend_expiration); +static int dev_memalloc_noio (struct device *dev,void *data) +{ +return dev->power.memalloc_noio; +} + +/* +* pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag. +* @dev: Device to handle. +* @enable: True for setting the flag and False for clearing the flag. +* +* Set the flag for all devices in the path from the device to the +* root device in the device tree if @enable is true, otherwise clear +* the flag for devices in the path whose siblings don't set the flag. +* +* The function should only be called by block device, or network +* device driver for solving the deadlock problem during runtime +* resume/suspend: +* if memory allocation with GFP_KERNEL is called inside runtime +* resume/suspend callback of any one of its ancestors(or the +* block device itself), the deadlock may be triggered inside the +* memory allocation since it might not complete until the block +* device becomes active and the involed page I/O finishes. The +* situation is pointed out first by Alan Stern. Network device +* are involved in iSCSI kind of situation. +* +* The lock of dev_hotplug_mutex is held in the function for handling +* hotplug race because pm_runtime_set_memalloc_noio() may be called +* in async probe(). +* +* The function should be called between device_add() and device_del() +* on the affected device(block/network device). +*/ +void pm_runtime_set_memalloc_noio (struct device *dev,bool enable) +{ +static DEFINE_MUTEX (dev_hotplug_mutex); + +mutex_lock (&dev_hotplug_mutex); +for (;;) {
drivers/base/power/runtime.c unsigned long pm_runtime_autosuspend_expiration(struct device *dev) · line 174
Quoted 10 lines
drivers/base/power/runtime.c +10
-0+/* hold power lock since bitfield is not SMP-safe. */ +spin_lock_irq (&dev->power.lock); + dev->power.memalloc_noio = enable; +spin_unlock_irq (&dev->power.lock); + + dev = dev->parent; + +/* only clear the flag for one device if all +* children of the device don't set the flag. +*/
Such a comment is usually laid out as
/* * Only ...
More significantly, the comment describes what the code is doing but not why the code is doing it. The former is (usually) obvious from reading the C, and the latter is what good code comments address.
And it's needed in this case. Why does the code do this?
Also, can a device have more than one child? If so, the code doesn't do what the comment says it does.
drivers/base/power/runtime.c unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
Quoted 11 lines
drivers/base/power/runtime.c +8
-0+if (!dev || (!enable && + device_for_each_child(dev, NULL, + dev_memalloc_noio))) +break ; + } +mutex_unlock (&dev_hotplug_mutex); +} +EXPORT_SYMBOL_GPL (pm_runtime_set_memalloc_noio);
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v4 2/6] PM / Runtime: introduce pm_runtime_set_memalloc_noio()
Quoted 3 lines · Andrew Morton
checkpatch finds a number of problems with this patch, all of which should be fixed. Please always use checkpatch.
Sorry for missing the check.
Quoted 8 lines
drivers/base/power/runtime.c unsigned long pm_runtime_autosuspend_expiration(struct device *dev) · line 174
Quoted 3 lines
drivers/base/power/runtime.c +3
-0+/* only clear the flag for one device if all +* children of the device don't set the flag. +*/ Such a comment is usually laid out as
/*
* Only ...
Will do it in -v5.
Quoted 5 lines
More significantly, the comment describes what the code is doing but not why the code is doing it. The former is (usually) obvious from reading the C, and the latter is what good code comments address.
And it's needed in this case. Why does the code do this?
Suppose both two usb scsi disks which share the same usb configuration(device) set the device memalloc_noio flag, and its ancestors' memalloc_noio flag should be cleared only after both the two usb scsi disk's flags have been cleared.
OK, we'll add comment on clearing flag.
Quoted 3 lines
Also, can a device have more than one child? If so, the code doesn't do what the comment says it does.
It should do that because device_for_each_child() returns true immediately only if dev_memalloc_noio() for one child returns true.
Quoted 9 lines
drivers/base/power/runtime.c unsigned long pm_runtime_autosuspend_expiration(struct device *dev) · line 182
Quoted 9 lines
drivers/base/power/runtime.c +8
-0+if (!dev || (!enable && + device_for_each_child(dev, NULL, + dev_memalloc_noio))) +break ; + } +mutex_unlock (&dev_hotplug_mutex); +} +EXPORT_SYMBOL_GPL (pm_runtime_set_memalloc_noio);
Thanks,
--
Ming Lei
PATCH v4 3/6 block/genhd.c: apply pm_runtime_set_memalloc_noio on block devices
In reply to Ming LeiThis patch applyes the introduced pm_runtime_set_memalloc_noio on block device so that PM core will teach mm to not allocate memory with GFP_IOFS when calling the runtime_resume and runtime_suspend callback for block devices and its ancestors.
---
v4:
- call pm_runtime_set_memalloc_noio(ddev, true) after device_add
--- block/genhd.c | 9 +++++++++ 1 file changed, 9 insertions(+)
block/genhd.c +9 -0
@@ -18,6 +18,7 @@#include <linux/mutex.h> #include <linux/idr.h> #include <linux/log2.h> +#include <linux/pm_runtime.h> #include "blk.h"
@@ -532,6 +533,13 @@ static void register_disk(struct gendisk *disk)return ; } } + +/* avoid probable deadlock caused by allocating memory with +* GFP_KERNEL in runtime_resume callback of its all ancestor +* deivces +*/ +pm_runtime_set_memalloc_noio (ddev, true); + disk->part0.holder_dir =kobject_create_and_add ("holders" , &ddev->kobj); disk->slave_dir =kobject_create_and_add ("slaves" , &ddev->kobj);
@@ -661,6 +669,7 @@ void del_gendisk(struct gendisk *disk) disk->driverfs_dev =NULL ;if (!sysfs_deprecated)sysfs_remove_link (block_depr,dev_name (disk_to_dev (disk))); +pm_runtime_set_memalloc_noio (disk_to_dev (disk), false);device_del (disk_to_dev (disk)); }EXPORT_SYMBOL (del_gendisk);
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerRe: [PATCH v4 3/6] block/genhd.c: apply pm_runtime_set_memalloc_noio on block devices
block/genhd.c static void register_disk(struct gendisk *disk) · line 537
Quoted 13 lines · On Sat, 3 Nov 2012 16:35:11 +0800 Ming Lei <[email protected]> wrote:
This patch applyes the introduced
pm_runtime_set_memalloc_noioon block device so that PM core will teach mm to not allocate memory withGFP_IOFSwhen calling theruntime_resumeandruntime_suspendcallback for block devices and its ancestors....
block/genhd.c +2
-0@@ -532,6 +533,13 @@ static void register_disk(struct gendisk *disk)return ; } } + +/* avoid probable deadlock caused by allocating memory with
Again, please fix the comment style. Take a look at the rest of this file!
block/genhd.c static void register_disk(struct gendisk *disk) · line 539
Quoted 2 lines
block/genhd.c +2
-0+* GFP_KERNEL in runtime_resume callback of its all ancestor +* deivces
typo
block/genhd.c static void register_disk(struct gendisk *disk) · line 540
Quoted 1 line
block/genhd.c +1
-0+*/
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 4/6 net/core: apply pm_runtime_set_memalloc_noio on network devices
In reply to Ming LeiDeadlock might be caused by allocating memory with GFP_KERNEL in runtime_resume and runtime_suspend callback of network devices in iSCSI situation, so mark network devices and its ancestor as 'memalloc_noio' with the introduced pm_runtime_set_memalloc_noio().
---
v4:
- call pm_runtime_set_memalloc_noio(ddev, true) after device_add
--- net/core/net-sysfs.c | 5 +++++ 1 file changed, 5 insertions(+)
net/core/net-sysfs.c +5 -0
@@ -22,6 +22,7 @@#include <linux/vmalloc.h> #include <linux/export.h> #include <linux/jiffies.h> +#include <linux/pm_runtime.h> #include <net/wext.h> #include "net-sysfs.h"
@@ -1386,6 +1387,8 @@ void netdev_unregister_kobject(struct net_device * net)remove_queue_kobjects (net); +pm_runtime_set_memalloc_noio (dev, false); +device_del (dev); }
@@ -1421,6 +1424,8 @@ int netdev_register_kobject(struct net_device *net)return error; } +pm_runtime_set_memalloc_noio (dev, true); +return error; }
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 5/6 PM / Runtime: force memory allocation with no I/O during Runtime PM callbcack
In reply to Ming LeiThis patch applies the introduced memalloc_noio_save() and memalloc_noio_restore() to force memory allocation with no I/O during runtime_resume/runtime_suspend callback on device with the flag of 'memalloc_noio' set.
---
v4:
- runtime_suspend need this too because rpm_resume may wait for completion of concurrent runtime_suspend, so deadlock still may be triggered in runtime_suspend path.
--- drivers/base/power/runtime.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
drivers/base/power/runtime.c +30 -2
@@ -368,6 +368,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)int (*callback)(struct device *);struct device *parent =NULL ;int retval; +unsigned int noio_flag;trace_rpm_suspend (dev, rpmflags);
@@ -477,7 +478,20 @@ static int rpm_suspend(struct device *dev, int rpmflags)if (!callback && dev->driver && dev->driver->pm) callback = dev->driver->pm->runtime_suspend; - retval =rpm_callback (callback, dev); +/* +* Deadlock might be caused if memory allocation with GFP_KERNEL +* happens inside runtime_suspend callback of one block device's +* ancestor or the block device itself. Network device might be +* thought as part of iSCSI block device, so network device and +* its ancestor should be marked as memalloc_noio. +*/ +if (dev->power.memalloc_noio) { +memalloc_noio_save (noio_flag); + retval =rpm_callback (callback, dev); +memalloc_noio_restore (noio_flag); + }else { + retval =rpm_callback (callback, dev); + }if (retval) goto fail;
@@ -560,6 +574,7 @@ static int rpm_resume(struct device *dev, int rpmflags)int (*callback)(struct device *);struct device *parent =NULL ;int retval =0 ; +unsigned int noio_flag;trace_rpm_resume (dev, rpmflags);
@@ -709,7 +724,20 @@ static int rpm_resume(struct device *dev, int rpmflags)if (!callback && dev->driver && dev->driver->pm) callback = dev->driver->pm->runtime_resume; - retval =rpm_callback (callback, dev); +/* +* Deadlock might be caused if memory allocation with GFP_KERNEL +* happens inside runtime_resume callback of one block device's +* ancestor or the block device itself. Network device might be +* thought as part of iSCSI block device, so network device and +* its ancestor should be marked as memalloc_noio. +*/ +if (dev->power.memalloc_noio) { +memalloc_noio_save (noio_flag); + retval =rpm_callback (callback, dev); +memalloc_noio_restore (noio_flag); + }else { + retval =rpm_callback (callback, dev); + }if (retval) {__update_runtime_status (dev,RPM_SUSPENDED );pm_runtime_cancel_pending (dev);
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 6/6 USB: forbid memory allocation with I/O during bus reset
In reply to Ming LeiIf one storage interface or usb network interface(iSCSI case) exists in current configuration, memory allocation with GFP_KERNEL during usb_device_reset() might trigger I/O transfer on the storage interface itself and cause deadlock because the 'us->dev_mutex' is held in .pre_reset() and the storage interface can't do I/O transfer when the reset is triggered by other interface, or the error handling can't be completed if the reset is triggered by the storage itself(error handling path).
---
v4:
- mark current memalloc_noio for every usb device reset
--- drivers/usb/core/hub.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/usb/core/hub.c +13 -0
@@ -5044,6 +5044,7 @@ int usb_reset_device(struct usb_device *udev) {int ret;int i; +unsigned int noio_flag;struct usb_host_config *config = udev->actconfig; if (udev->state == USB_STATE_NOTATTACHED ||
@@ -5053,6 +5054,17 @@ int usb_reset_device(struct usb_device *udev)return -EINVAL ; } +/* +* Don't allocate memory with GFP_KERNEL in current +* context to avoid possible deadlock if usb mass +* storage interface or usbnet interface(iSCSI case) +* is included in current configuration. The easist +* approach is to do it for every device reset, +* because the device 'memalloc_noio' flag may have +* not been set before reseting the usb device. +*/ +memalloc_noio_save (noio_flag); +/* Prevent autosuspend during the reset */ usb_autoresume_device (udev);
@@ -5097,6 +5109,7 @@ int usb_reset_device(struct usb_device *udev) }usb_autosuspend_device (udev); +memalloc_noio_restore (noio_flag);return ret; }EXPORT_SYMBOL_GPL (usb_reset_device);
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 5 lines · On Sat, 3 Nov 2012 16:35:08 +0800 Ming Lei <[email protected]> wrote:
This patchset try to solve one deadlock problem which might be caused by memory allocation with block I/O during runtime PM and block device error handling path. Traditionly, the problem is addressed by passing
GFP_NOIOstatically to mm, but that is not a effective solution, see detailed description in patch 1's commit log.
It generally looks OK to me. I have a few comments and I expect to grab v5.
Rafael, your thoughts?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 3 lines · Andrew Morton
It generally looks OK to me. I have a few comments and I expect to grab v5.
Andrew, thanks for your review, and I will prepare -v5 later.
Thanks,
--
Ming Lei
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>