mm: zswap: fix data loss on SWP_SYNCHRONOUS_IO devices
Subsystems (2)
Johannes Weiner
maintainerPATCH 1/1 mm: zswap: fix data loss on SWP_SYNCHRONOUS_IO devices
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.
--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
mm/zswap.c +19 -4
@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;
@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)
@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }return true; }
Signature · 1 line
2.44.0
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by swapcache_prepare() setting SWAP_HAS_CACHE or so?
Anyway, this LGTM.
Quoted 55 lines
--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)mm/zswap.c +19
-5@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }return true; } --2.44.0
Barry Song
Acked-byQuoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.
Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>
Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
Quoted 55 lines
Anyway, this LGTM.
Acked-by Yosry Ahmed <[email protected]>Quoted 50 lines
--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)mm/zswap.c +19
-4@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }
Quoted 4 lines
Quoted 4 lines
mm/zswap.c +0
-1return true; } --2.44.0
Thanks
Barry
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?
Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 4 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
Chengming Zhou
reviewerQuoted 42 lines · Yosry Ahmed
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
This makes me wonder who is responsible for marking folio dirty in this swapcache bypass case? Should we call folio_mark_dirty() after the swap_read_folio()?
Quoted 48 lines · Chengming Zhou
Quoted 46 lines · Yosry Ahmed
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 4 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
Quoted 7 lines
Quoted 4 lines
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
This makes me wonder who is responsible for marking folio dirty in this swapcache bypass case? Should we call
folio_mark_dirty()after theswap_read_folio()?
In shrink_folio_list(), we try to add anonymous folios to the swapcache if they are not there before checking if they are dirty. add_to_swap() calls folio_mark_dirty(), so this should take care of it. There is an interesting comment there though. It says that PTE should be dirty, so unmapping the folio should have already marked it as dirty by the time we are adding it to the swapcache, except for the MADV_FREE case.
However, I think we actually unmap the folio after we add it to the swapcache in shrink_folio_list(). Also, I don't immediately see why the PTE would be dirty. In do_swap_page(), making the PTE dirty seems to be conditional on the fault being a write fault, but I didn't look thoroughly, maybe I missed it. It is also possible that the comment is just outdated.
Chengming Zhou
reviewerQuoted 55 lines · Yosry Ahmed
Quoted 53 lines · Chengming Zhou
Quoted 50 lines · Yosry Ahmed
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 4 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
Quoted 6 lines
Quoted 2 lines
This makes me wonder who is responsible for marking folio dirty in this swapcache bypass case? Should we call
folio_mark_dirty()after theswap_read_folio()?In
shrink_folio_list(), we try to add anonymous folios to the swapcache if they are not there before checking if they are dirty.add_to_swap()callsfolio_mark_dirty(), so this should take care of
Right, thanks for your clarification, so should be no problem here. Although it was a fix just for MADV_FREE case.
Quoted 4 lines
it. There is an interesting comment there though. It says that PTE should be dirty, so unmapping the folio should have already marked it as dirty by the time we are adding it to the swapcache, except for the
MADV_FREEcase.
It seems to say the folio will be dirtied when unmap later, supposing the PTE is dirty.
Quoted 4 lines
However, I think we actually unmap the folio after we add it to the swapcache in
shrink_folio_list(). Also, I don't immediately see why the PTE would be dirty. Indo_swap_page(), making the PTE dirty seems
If all anon pages on LRU list are faulted by write, it should be true. We could just use the zero page if faulted by read, right?
Quoted 3 lines
to be conditional on the fault being a write fault, but I didn't look thoroughly, maybe I missed it. It is also possible that the comment is just outdated.
Yeah, dirty is only marked on write fault.
Thanks.
Quoted 67 lines · Chengming Zhou
Quoted 55 lines · Yosry Ahmed
Quoted 53 lines · Chengming Zhou
Quoted 50 lines · Yosry Ahmed
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 4 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
Quoted 6 lines
Quoted 2 lines
This makes me wonder who is responsible for marking folio dirty in this swapcache bypass case? Should we call
folio_mark_dirty()after theswap_read_folio()?In
shrink_folio_list(), we try to add anonymous folios to the swapcache if they are not there before checking if they are dirty.add_to_swap()callsfolio_mark_dirty(), so this should take care ofRight, thanks for your clarification, so should be no problem here. Although it was a fix just for
MADV_FREEcase.
Quoted 7 lines
Quoted 4 lines
it. There is an interesting comment there though. It says that PTE should be dirty, so unmapping the folio should have already marked it as dirty by the time we are adding it to the swapcache, except for the
MADV_FREEcase.It seems to say the folio will be dirtied when unmap later, supposing the PTE is dirty.
Oh yeah it could mean that the folio will be dirted later.
Quoted 8 lines
Quoted 5 lines
However, I think we actually unmap the folio after we add it to the swapcache in
shrink_folio_list(). Also, I don't immediately see why the PTE would be dirty. Indo_swap_page(), making the PTE dirty seemsIf all anon pages on LRU list are faulted by write, it should be true. We could just use the zero page if faulted by read, right?
This applies for the initial fault that creates the folio, but this is a swap fault. It could be a read fault and in that case we still need to make the folio dirty because it's not in the swapcache and we need to write it out if it's reclaimed, right?
Quoted 8 lines
Quoted 4 lines
to be conditional on the fault being a write fault, but I didn't look thoroughly, maybe I missed it. It is also possible that the comment is just outdated.
Yeah, dirty is only marked on write fault.
Thanks.
Chengming Zhou
reviewerQuoted 84 lines · Yosry Ahmed
Quoted 67 lines · Chengming Zhou
Quoted 55 lines · Yosry Ahmed
Quoted 53 lines · Chengming Zhou
Quoted 50 lines · Yosry Ahmed
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 4 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
Quoted 6 lines
Quoted 2 lines
This makes me wonder who is responsible for marking folio dirty in this swapcache bypass case? Should we call
folio_mark_dirty()after theswap_read_folio()?In
shrink_folio_list(), we try to add anonymous folios to the swapcache if they are not there before checking if they are dirty.add_to_swap()callsfolio_mark_dirty(), so this should take care ofRight, thanks for your clarification, so should be no problem here. Although it was a fix just for
MADV_FREEcase.Quoted 7 lines
Quoted 4 lines
it. There is an interesting comment there though. It says that PTE should be dirty, so unmapping the folio should have already marked it as dirty by the time we are adding it to the swapcache, except for the
MADV_FREEcase.It seems to say the folio will be dirtied when unmap later, supposing the PTE is dirty.
Oh yeah it could mean that the folio will be dirted later.
Quoted 6 lines
Quoted 5 lines
However, I think we actually unmap the folio after we add it to the swapcache in
shrink_folio_list(). Also, I don't immediately see why the PTE would be dirty. Indo_swap_page(), making the PTE dirty seems
Quoted 7 lines
Quoted 2 lines
If all anon pages on LRU list are faulted by write, it should be true. We could just use the zero page if faulted by read, right?
This applies for the initial fault that creates the folio, but this is a swap fault. It could be a read fault and in that case we still need to make the folio dirty because it's not in the swapcache and we need to write it out if it's reclaimed, right?
Yes, IMHO I think it should be marked as dirty here.
But it should be no problem with that unconditional folio_mark_dirty() in add_to_swap(). Not sure if there are other issues.
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
to be conditional on the fault being a write fault, but I didn't look thoroughly, maybe I missed it. It is also possible that the comment is just outdated.
Yeah, dirty is only marked on write fault.
Thanks.
Quoted 94 lines · Chengming Zhou
Quoted 84 lines · Yosry Ahmed
Quoted 67 lines · Chengming Zhou
Quoted 55 lines · Yosry Ahmed
Quoted 53 lines · Chengming Zhou
Quoted 50 lines · Yosry Ahmed
Quoted 31 lines · Barry Song
Quoted 14 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Acked-by Barry Song <[email protected]>Quoted 5 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?Quoted 4 lines
It seems that Kairui's patch comprehensively addresses the issue at hand. Johannes's solution, on the other hand, appears to align zswap behavior more closely with that of a traditional swap device, only releasing an entry when the corresponding swap slot is freed, particularly in the sync-io case.
It actually worked out quite well that Kairui's fix landed shortly before this bug was reported, as this fix wouldn't have been possible without it as far as I can tell.
Quoted 4 lines
Johannes' patch has inspired me to consider whether zRAM could achieve a comparable outcome by immediately releasing objects in swap cache scenarios. When I have the opportunity, I plan to experiment with zRAM.
That would be interesting. I am curious if it would be as straightforward in zram to just mark the folio as dirty in this case like zswap does, given its implementation as a block device.
Quoted 6 lines
Quoted 2 lines
This makes me wonder who is responsible for marking folio dirty in this swapcache bypass case? Should we call
folio_mark_dirty()after theswap_read_folio()?In
shrink_folio_list(), we try to add anonymous folios to the swapcache if they are not there before checking if they are dirty.add_to_swap()callsfolio_mark_dirty(), so this should take care ofRight, thanks for your clarification, so should be no problem here. Although it was a fix just for
MADV_FREEcase.Quoted 7 lines
Quoted 4 lines
it. There is an interesting comment there though. It says that PTE should be dirty, so unmapping the folio should have already marked it as dirty by the time we are adding it to the swapcache, except for the
MADV_FREEcase.It seems to say the folio will be dirtied when unmap later, supposing the PTE is dirty.
Oh yeah it could mean that the folio will be dirted later.
Quoted 6 lines
Quoted 5 lines
However, I think we actually unmap the folio after we add it to the swapcache in
shrink_folio_list(). Also, I don't immediately see why the PTE would be dirty. Indo_swap_page(), making the PTE dirty seemsQuoted 7 lines
Quoted 2 lines
If all anon pages on LRU list are faulted by write, it should be true. We could just use the zero page if faulted by read, right?
This applies for the initial fault that creates the folio, but this is a swap fault. It could be a read fault and in that case we still need to make the folio dirty because it's not in the swapcache and we need to write it out if it's reclaimed, right?
Quoted 4 lines
Yes, IMHO I think it should be marked as dirty here.
But it should be no problem with that unconditional
folio_mark_dirty()inadd_to_swap(). Not sure if there are other issues.
I don't believe there are any issues now. Dirtying the folio in add_to_swap() was introduced before SWP_SYNCHRONOUS_IO, so I guess things have always worked.
I think we should update the comment there though to mention that dirtying the folio is also needed for this case (not just MADV_FREE), or dirty the PTE during the fault. Otherwise, if someone is making MADV_FREE changes they could end up breaking SWP_SYNCHRONOUS_IO faults.
Adding Minchan here in case he can confirm that we in fact rely on add_to_swap()->folio_mark_dirty() for SWP_SYNCHRONOUS_IO to work as intended.
Johannes Weiner
maintainerQuoted 20 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>
Quoted 4 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?
I don't think it's necessary, as zswap isn't doing anything special here. It's up to the caller to follow the generic swap exclusion protocol that zswap also adheres to. So IMO the relevant comment should be, and is, above that swapcache_prepare() in do_swap_page().
Quoted 3 lines
Anyway, this LGTM.
Acked-by Yosry Ahmed <[email protected]>
Thanks!
Quoted 27 lines · Johannes Weiner
Quoted 20 lines · Yosry Ahmed
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>Quoted 4 lines
Do we also want to mention somewhere (commit log or comment) that keeping the entry in the tree is fine because we are still protected from concurrent loads/invalidations/writeback by
swapcache_prepare()settingSWAP_HAS_CACHEor so?
Quoted 4 lines
I don't think it's necessary, as zswap isn't doing anything special here. It's up to the caller to follow the generic swap exclusion protocol that zswap also adheres to. So IMO the relevant comment should be, and is, above that
swapcache_prepare()indo_swap_page().
From the perspective of someone looking at the zswap code, it isn't immediately clear what protects the zswap entry in the non-exclusive load case from being freed from under us. At some point we had a refcount, then we used to remove it from the tree under lock so others wouldn't have access to it. Now it's less clear because we rely on protection outside of zswap code.
We also document other places where we rely on the swapcache for synchronization, so I think it may be worth briefly mentioning this here as well, especially that in this code we explicitly check for the folio not being in the swapcache. That said, I don't feel strongly about it. Tracking down the SWP_SYNCHRONOUS_IO code should eventually make it clear. Also, the commit log will end up having a link to this thread anyway so the details are not completely unfindable :)
Quoted 11 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>
Very nice solution!
Thanks.
Quoted 52 lines
--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)mm/zswap.c +19
-4@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }return true; }
Quoted 71 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)mm/zswap.c +19
-5@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }return true; } --
Quoted 2 lines
2.44.0
Good solution and makes great sense to me.
Thanks.
Quoted 57 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)mm/zswap.c +15
-1@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)
mm/zswap.c bool zswap_load(struct folio *folio) · line 1669
Quoted 10 lines
mm/zswap.c +4
-3@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }
This LGTM!
Quoted 12 lines · Johannes Weiner
Zhongkun He reports data corruption when combining zswap with zram.
The issue is the exclusive loads we're doing in zswap. They assume that all reads are going into the swapcache, which can assume authoritative ownership of the data and so the zswap copy can go.
However, zram files are marked
SWP_SYNCHRONOUS_IO, and faults will try to bypass the swapcache. This results in an optimistic read of the swap data into a page that will be dismissed if the fault fails due to races. In this case, zswap mustn't drop its authoritative copy.
Quoted 6 lines
Link https://lore.kernel.org/all/CACSyD1N+dUvsu8=zV9P691B9bVq33erwOXNTmEaUbi9DrDeJzw@mail.gmail.com/ open hereReported-by Zhongkun He <[email protected]>Fixes b9c91c43412f ("mm: zswap: support exclusive loads")Cc [email protected] [6.5+]Signed-off-by Johannes Weiner <[email protected]>Tested-by Zhongkun He <[email protected]>
The change looks good to me. Thanks for the cleaner solution. It has conflict with the zswap rb-tree to xarray patch though. I will resolve the conflict and resubmit the zswap xarray patch.
Chris
Quoted 55 lines
--- mm/zswap.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)mm/zswap.c +19
-5@@ -1622,6 +1622,7 @@ bool zswap_load(struct folio *folio)swp_entry_t swp = folio->swap;pgoff_t offset =swp_offset (swp);struct page *page = &folio->page; +bool swapcache =folio_test_swapcache (folio);struct zswap_tree *tree =swap_zswap_tree (swp);struct zswap_entry *entry;u8 *dst;@@ -1634,7 +1635,20 @@ bool zswap_load(struct folio *folio)spin_unlock (&tree->lock);return false; } -zswap_rb_erase (&tree->rbroot, entry); +/* +* When reading into the swapcache, invalidate our entry. The +* swapcache can be the authoritative owner of the page and +* its mappings, and the pressure that results from having two +* in-memory copies outweighs any benefits of caching the +* compression work. +* +* (Most swapins go through the swapcache. The notable +* exception is the singleton fault on SWP_SYNCHRONOUS_IO +* files, which reads into a private page and may free it if +* the fault fails. We remain the primary owner of the entry.) +*/ +if (swapcache) +zswap_rb_erase (&tree->rbroot, entry);spin_unlock (&tree->lock);if (entry->length)@@ -1649,9 +1663,10 @@ bool zswap_load(struct folio *folio)if (entry->objcg)count_objcg_event (entry->objcg,ZSWPIN ); -zswap_entry_free (entry); - -folio_mark_dirty (folio); +if (swapcache) { +zswap_entry_free (entry); +folio_mark_dirty (folio); + }return true; } --2.44.0