memcg: correctly order reading PCG_USED and pc->mem_cgroup
Subsystems (2)
Johannes Weiner
maintainerRFC PATCH 1/1 memcg: correctly order reading PCG_USED and pc->mem_cgroup
The placement of the read-side barrier is confused: the writer first sets pc->mem_cgroup, then PCG_USED. The read-side barrier has to be between testing PCG_USED and reading pc->mem_cgroup.
--- mm/memcontrol.c | 27 +++++++++------------------ 1 files changed, 9 insertions(+), 18 deletions(-)
I am a bit dumbfounded as to why this has never had any impact. I see two scenarios where charging can race with LRU operations:
One is shmem pages on swapoff. They are on the LRU when charged as page cache, which could race with isolation/putback. This seems sufficiently rare.
The other case is a swap cache page being charged while somebody else had it isolated. mem_cgroup_lru_del_before_commit_swapcache() would see the page isolated and skip it. The commit then has to race with putback, which could see PCG_USED but not pc->mem_cgroup, and crash with a NULL pointer dereference. This does sound a bit more likely.
Any idea? Am I missing something?
mm/memcontrol.c +9 -18
@@ -836,13 +836,12 @@ void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)return ; pc =lookup_page_cgroup (page); -/* -* Used bit is set without atomic ops but after smp_wmb(). -* For making pc->mem_cgroup visible, insert smp_rmb() here. -*/ -smp_rmb ();/* unused or root page is not rotated. */ -if (!PageCgroupUsed (pc) ||mem_cgroup_is_root (pc->mem_cgroup)) +if (!PageCgroupUsed (pc)) +return ; +/* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ +smp_rmb (); +if (mem_cgroup_is_root (pc->mem_cgroup))return ; mz =page_cgroup_zoneinfo (pc);list_move (&pc->lru, &mz->lists[lru]);
@@ -857,14 +856,10 @@ void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)return ; pc =lookup_page_cgroup (page);VM_BUG_ON (PageCgroupAcctLRU (pc)); -/* -* Used bit is set without atomic ops but after smp_wmb(). -* For making pc->mem_cgroup visible, insert smp_rmb() here. -*/ -smp_rmb ();if (!PageCgroupUsed (pc))return ; - +/* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ +smp_rmb (); mz =page_cgroup_zoneinfo (pc);/* huge page split is done under lru_lock. so, we have no races. */ MEM_CGROUP_ZSTAT (mz, lru) +=1 <<compound_order (page);
@@ -1031,14 +1026,10 @@ mem_cgroup_get_reclaim_stat_from_page(struct page *page)return NULL ; pc =lookup_page_cgroup (page); -/* -* Used bit is set without atomic ops but after smp_wmb(). -* For making pc->mem_cgroup visible, insert smp_rmb() here. -*/ -smp_rmb ();if (!PageCgroupUsed (pc))return NULL ; - +/* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ +smp_rmb (); mz =page_cgroup_zoneinfo (pc);if (!mz)return NULL ;
Signature · 8 lines
1.7.3.4 -- 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/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 16 lines · On Wed, 19 Jan 2011 13:03:19 +0100 Johannes Weiner <[email protected]> wrote:
The placement of the read-side barrier is confused: the writer first sets
pc->mem_cgroup, thenPCG_USED. The read-side barrier has to be between testingPCG_USEDand readingpc->mem_cgroup.Signed-off-by Johannes Weiner <[email protected]>--- mm/memcontrol.c | 27 +++++++++------------------ 1 files changed, 9 insertions(+), 18 deletions(-)I am a bit dumbfounded as to why this has never had any impact. I see two scenarios where charging can race with LRU operations:
One is shmem pages on swapoff. They are on the LRU when charged as page cache, which could race with isolation/putback. This seems sufficiently rare.
Quoted 8 lines
The other case is a swap cache page being charged while somebody else had it isolated.
mem_cgroup_lru_del_before_commit_swapcache()would see the page isolated and skip it. The commit then has to race with putback, which could seePCG_USEDbut notpc->mem_cgroup, and crash with a NULL pointer dereference. This does sound a bit more likely.Any idea? Am I missing something?
I think troubles happen only when PCG_USED bit was found but pc->mem_cgroup is NULL. Hmm.
set pc->mem_cgroup
write_barrier
set USED bit.
read_barrier
check USED bit
access pc->mem_cgroup
So, is there a case which only USED bit can be seen ? Anyway, your patch is right.
Quoted 67 lines
mm/memcontrol.c +9
-18@@ -836,13 +836,12 @@ void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)return ; pc =lookup_page_cgroup (page); -/* -* Used bit is set without atomic ops but after smp_wmb(). -* For making pc->mem_cgroup visible, insert smp_rmb() here. -*/ -smp_rmb ();/* unused or root page is not rotated. */ -if (!PageCgroupUsed (pc) ||mem_cgroup_is_root (pc->mem_cgroup)) +if (!PageCgroupUsed (pc)) +return ; +/* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ +smp_rmb (); +if (mem_cgroup_is_root (pc->mem_cgroup))return ; mz =page_cgroup_zoneinfo (pc);list_move (&pc->lru, &mz->lists[lru]);@@ -857,14 +856,10 @@ void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)return ; pc =lookup_page_cgroup (page);VM_BUG_ON (PageCgroupAcctLRU (pc)); -/* -* Used bit is set without atomic ops but after smp_wmb(). -* For making pc->mem_cgroup visible, insert smp_rmb() here. -*/ -smp_rmb ();if (!PageCgroupUsed (pc))return ; - +/* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ +smp_rmb (); mz =page_cgroup_zoneinfo (pc);/* huge page split is done under lru_lock. so, we have no races. */ MEM_CGROUP_ZSTAT (mz, lru) +=1 <<compound_order (page);@@ -1031,14 +1026,10 @@ mem_cgroup_get_reclaim_stat_from_page(struct page *page)return NULL ; pc =lookup_page_cgroup (page); -/* -* Used bit is set without atomic ops but after smp_wmb(). -* For making pc->mem_cgroup visible, insert smp_rmb() here. -*/ -smp_rmb ();if (!PageCgroupUsed (pc))return NULL ; - +/* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ +smp_rmb (); mz =page_cgroup_zoneinfo (pc);if (!mz)return NULL ;Signature · 8 lines
1.7.3.4 -- 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/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
--
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/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 16 lines · On Wed, 19 Jan 2011 13:03:19 +0100 Johannes Weiner <[email protected]> wrote:
The placement of the read-side barrier is confused: the writer first sets
pc->mem_cgroup, thenPCG_USED. The read-side barrier has to be between testingPCG_USEDand readingpc->mem_cgroup.Signed-off-by Johannes Weiner <[email protected]>--- mm/memcontrol.c | 27 +++++++++------------------ 1 files changed, 9 insertions(+), 18 deletions(-)I am a bit dumbfounded as to why this has never had any impact. I see two scenarios where charging can race with LRU operations:
One is shmem pages on swapoff. They are on the LRU when charged as page cache, which could race with isolation/putback. This seems sufficiently rare.
Quoted 8 lines
The other case is a swap cache page being charged while somebody else had it isolated.
mem_cgroup_lru_del_before_commit_swapcache()would see the page isolated and skip it. The commit then has to race with putback, which could seePCG_USEDbut notpc->mem_cgroup, and crash with a NULL pointer dereference. This does sound a bit more likely.Any idea? Am I missing something?
pc->mem_cgroup is not cleared even when the page is freed, so NULL pointer dereference can happen only when it's the first time the page is used. But yes, even if it's not the first time, this means pc->mem_cgroup may be wrong.
Anyway, I welcome this patch.
Thanks,
Daisuke Nishimura.
--
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/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Johannes Weiner
maintainerQuoted 25 lines · KAMEZAWA Hiroyuki
Quoted 22 lines · On Wed, 19 Jan 2011 13:03:19 +0100 Johannes Weiner <[email protected]> wrote:
The placement of the read-side barrier is confused: the writer first sets
pc->mem_cgroup, thenPCG_USED. The read-side barrier has to be between testingPCG_USEDand readingpc->mem_cgroup.Signed-off-by Johannes Weiner <[email protected]>--- mm/memcontrol.c | 27 +++++++++------------------ 1 files changed, 9 insertions(+), 18 deletions(-)I am a bit dumbfounded as to why this has never had any impact. I see two scenarios where charging can race with LRU operations:
One is shmem pages on swapoff. They are on the LRU when charged as page cache, which could race with isolation/putback. This seems sufficiently rare.
The other case is a swap cache page being charged while somebody else had it isolated.
mem_cgroup_lru_del_before_commit_swapcache()would see the page isolated and skip it. The commit then has to race with putback, which could seePCG_USEDbut notpc->mem_cgroup, and crash with a NULL pointer dereference. This does sound a bit more likely.
Quoted 5 lines
Quoted 2 lines
Any idea? Am I missing something?
I think troubles happen only when
PCG_USEDbit was found butpc->mem_cgroupis NULL. Hmm.
Correct. Well, or get linked to the wrong LRU list and subsequently prevent removal of both cgroups because it's impossible to empty them.
Quoted 9 lines
set
pc->mem_cgroup
write_barrier
set USED bit.
read_barrier
check USED bit
accesspc->mem_cgroupSo, is there a case which only USED bit can be seen ?
That's what I am not quite sure about. As said, I think it can happen when swap cache charging races with reclaim or migration.
When the two loads of the used bit and the memcg pointer get reordered, it could observe a set PCG_USED and a stale/unset pc->mem_cgroup.
For example:
swap minor fault: vmscan:
lookup_swap_cache()
unlock_page()lock_page()mem_cgroup_try_charge_swapin()
putback_lru_page() mem_cgroup_add_lru_list() p = pc->mem_cgroup pc->mem_cgroup = FOO smp_wmb() pc->flags |= PCG_USED f = pc->flags if (!(f & PCG_USED)) return *p /* bang */
Quoted 3 lines
Anyway, your patch is right.
Thank you.
Hannes
--
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/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>