mm/zswap: fix inconsistent charging when zswap_store_page() fails
Subsystems (3)
PATCH v2 1/1 mm/zswap: fix inconsistent charging when zswap_store_page() fails
Commit b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.
However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages, zswap_entry_free() uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.
This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap
$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo reboot
Two warnings are:
in mm/memcontrol.c:163, function obj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));
in mm/page_counter.c:60, function page_counter_cancel():
if (WARN_ONCE(new < 0, "page_counter underflow: %ld nr_pages=%lu\n",
new, nr_pages))
While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
---
v1->v2:
Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
mm/zswap.c +10 -4
@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true; +charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes); put_pool:zswap_pool_put (pool); put_objcg:
Signature · 1 line
2.47.1
Quoted 69 lines · Hyeonggon Yoo
Commit
b7c0ccdfbafd("mm: zswap: support large folios inzswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages,zswap_entry_free()uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootTwo warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ld nr_pages=%lu\n",
new,nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-4@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages);
mm/zswap.c bool zswap_store(struct folio *folio) · line 1590
Quoted 10 lines
mm/zswap.c +8
-0ret = true; +charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);
Thanks for fixing this!
Having to charge just to uncharge right after is annoying. Ideally we'd just clear entry->objcg if we fail before charging, but we don't have a direct reference to the entries here and another tree lookup is not ideal either.
I guess we may be able to improve this handling once [1] lands, as we can move the charging logic into zswap_store_folio() where we'd have access to the entries.
For now, would the control flow be easier if we move the charge ahead of the zswap_store_page() loop instead? There is an existing if (objcg) block there as well.
Quoted 7 lines
mm/zswap.c +0
-0put_pool:zswap_pool_put (pool); put_objcg:Signature · 1 line
2.47.1
Quoted 97 lines
-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, January 28, 2025 11:04 AM
To: Hyeonggon Yoo <[email protected]>
Cc: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>; [email protected]; [email protected]
Subject: Re: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent charging whenzswap_store_page()failsQuoted 15 lines · Hyeonggon Yoo
Commit
b7c0ccdfbafd("mm: zswap: support large folios inzswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages,zswap_entry_free()uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootQuoted 6 lines
Two warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ldnr_pages=%lu\n",
Quoted 43 lines
new, nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-3@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages);mm/zswap.c bool zswap_store(struct folio *folio) · line 1585
Quoted 10 lines
mm/zswap.c +3
-1- }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true; +charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio,
mm/zswap.c bool zswap_store(struct folio *folio) · line 1590
Quoted 5 lines
mm/zswap.c +5
-0+* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);Thanks for fixing this!
Quoted 8 lines
Having to charge just to uncharge right after is annoying. Ideally we'd just clear
entry->objcgif we fail before charging, but we don't have a direct reference to the entries here and another tree lookup is not ideal either.I guess we may be able to improve this handling once [1] lands, as we can move the charging logic into
zswap_store_folio()where we'd have access to the entries.
Thanks Yosry. I agree, we can improve this handling in [1]. I will add this to my list.
Quoted 4 lines
For now, would the control flow be easier if we move the charge ahead of the
zswap_store_page()loop instead? There is an existing if (objcg) block there as well.
I just replied with a suggestion to move the objcg charging and incrementing zswap_stored_pages to be per successful xarray store, within zswap_store_page() itself. Please let me know if this would be a good solution for the hotfix.
Thanks,
Kanchana
Quoted 11 lines
[1]https://lore.kernel.org/linux-mm/20241221063119.29140-12- [email protected]/
Quoted 7 lines
mm/zswap.c +0
-1put_pool:zswap_pool_put (pool); put_objcg: --2.47.1
Hi Hyeonggon,
Quoted 84 lines
-----Original Message-----
From: Hyeonggon Yoo <[email protected]>
Sent: Tuesday, January 28, 2025 10:55 AM
To: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>; Yosry Ahmed <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>
Cc: [email protected]; Hyeonggon Yoo <[email protected]>; [email protected]
Subject: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent charging whenzswap_store_page()failsCommit
b7c0ccdfbafd("mm: zswap: support large folios inzswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages,zswap_entry_free()uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootTwo warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ld
nr_pages=%lu\n",new, nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-4@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;
mm/zswap.c bool zswap_store(struct folio *folio) · line 1591
Quoted 10 lines
mm/zswap.c +8
-0+charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);
Thanks for finding this bug! I am thinking it might make sense to charge and increment the zswap_stored_pages counter in zswap_store_page(). Something like:
mm/zswap.c +4 -6
@@ -1504,11 +1504,14 @@ static ssize_t zswap_store_page(struct page *page, entry->pool = pool; entry->swpentry = page_swpentry; entry->objcg = objcg; +if (objcg) +obj_cgroup_charge_zswap (objcg, entry->length); entry->referenced = true;if (entry->length) {INIT_LIST_HEAD (&entry->lru);zswap_lru_add (&zswap_list_lru, entry); } +atomic_long_inc (&zswap_stored_pages);return entry->length;
@@ -1526,7 +1529,6 @@ bool zswap_store(struct folio *folio)struct obj_cgroup *objcg =NULL ;struct mem_cgroup *memcg =NULL ;struct zswap_pool *pool; -size_t compressed_bytes =0 ;bool ret = false;long index;
@@ -1569,15 +1571,11 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) goto put_pool; - compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - } -atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;
What do you think?
Yosry, Nhat, Johannes, please let me know if this would be a cleaner approach. If so, I don't think we would be losing a lot of performance by not doing the one-time charge per folio, but please let me know your thoughts as well.
Thanks,
Kanchana
Quoted 5 lines
mm/zswap.c +0
-0put_pool:zswap_pool_put (pool);
put_objcg:
--
2.47.1
Quoted 146 lines · Kanchana P
Hi Hyeonggon,
Quoted 84 lines
-----Original Message-----
From: Hyeonggon Yoo <[email protected]>
Sent: Tuesday, January 28, 2025 10:55 AM
To: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>; Yosry Ahmed <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>
Cc: [email protected]; Hyeonggon Yoo <[email protected]>; [email protected]
Subject: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent charging whenzswap_store_page()failsCommit
b7c0ccdfbafd("mm: zswap: support large folios inzswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages,zswap_entry_free()uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootTwo warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ld
nr_pages=%lu\n",new, nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-4@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;mm/zswap.c bool zswap_store(struct folio *folio) · line 1591
Quoted 10 lines
mm/zswap.c +8
-0+charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);Thanks for finding this bug! I am thinking it might make sense to charge and increment the
zswap_stored_pagescounter inzswap_store_page(). Something like:mm/zswap.c +4
-6@@ -1504,11 +1504,14 @@ static ssize_t zswap_store_page(struct page *page, entry->pool = pool; entry->swpentry = page_swpentry; entry->objcg = objcg; +if (objcg) +obj_cgroup_charge_zswap (objcg, entry->length); entry->referenced = true;if (entry->length) {INIT_LIST_HEAD (&entry->lru);zswap_lru_add (&zswap_list_lru, entry); } +atomic_long_inc (&zswap_stored_pages);return entry->length;@@ -1526,7 +1529,6 @@ bool zswap_store(struct folio *folio)struct obj_cgroup *objcg =NULL ;struct mem_cgroup *memcg =NULL ;struct zswap_pool *pool; -size_t compressed_bytes =0 ;bool ret = false;long index;@@ -1569,15 +1571,11 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) goto put_pool; - compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - } -atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;
Quoted 6 lines
What do you think?
Yosry, Nhat, Johannes, please let me know if this would be a cleaner approach. If so, I don't think we would be losing a lot of performance by not doing the one-time charge per folio, but please let me know your thoughts as well.
This is certainly cleaner, and thanks for catching that zswap_stored_pages cleanup is also wrong.
I am not sure if this has meaningful impact on performance, but it seems like we are doing a bit more work in the common success case to avoid the work in the uncommon failure case.
Moving the charge (and atomic addition) above the zswap_store_page() loop would be doing the opposite, albeit less clean.
I don't feel strongly either way, but I slightly prefer the latter.
Quoted 11 lines
Thanks,
KanchanaQuoted 5 lines
mm/zswap.c +0
-0put_pool:zswap_pool_put (pool);
put_objcg:
--
2.47.1
Quoted 150 lines · Yosry Ahmed
Quoted 148 lines · Kanchana P
Hi Hyeonggon,
Quoted 84 lines
-----Original Message-----
From: Hyeonggon Yoo <[email protected]>
Sent: Tuesday, January 28, 2025 10:55 AM
To: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>; Yosry Ahmed <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>
Cc: [email protected]; Hyeonggon Yoo <[email protected]>; [email protected]
Subject: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent charging whenzswap_store_page()failsCommit
b7c0ccdfbafd("mm: zswap: support large folios inzswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages,zswap_entry_free()uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootTwo warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ld
nr_pages=%lu\n",new, nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-4@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;mm/zswap.c bool zswap_store(struct folio *folio) · line 1591
Quoted 10 lines
mm/zswap.c +8
-0+charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);Thanks for finding this bug! I am thinking it might make sense to charge and increment the
zswap_stored_pagescounter inzswap_store_page(). Something like:mm/zswap.c +4
-6@@ -1504,11 +1504,14 @@ static ssize_t zswap_store_page(struct page *page, entry->pool = pool; entry->swpentry = page_swpentry; entry->objcg = objcg; +if (objcg) +obj_cgroup_charge_zswap (objcg, entry->length); entry->referenced = true;if (entry->length) {INIT_LIST_HEAD (&entry->lru);zswap_lru_add (&zswap_list_lru, entry); } +atomic_long_inc (&zswap_stored_pages);return entry->length;@@ -1526,7 +1529,6 @@ bool zswap_store(struct folio *folio)struct obj_cgroup *objcg =NULL ;struct mem_cgroup *memcg =NULL ;struct zswap_pool *pool; -size_t compressed_bytes =0 ;bool ret = false;long index;@@ -1569,15 +1571,11 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) goto put_pool; - compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - } -atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;What do you think?
Quoted 7 lines
Quoted 4 lines
Yosry, Nhat, Johannes, please let me know if this would be a cleaner approach. If so, I don't think we would be losing a lot of performance by not doing the one-time charge per folio, but please let me know your thoughts as well.
This is certainly cleaner, and thanks for catching that
zswap_stored_pagescleanup is also wrong.
Oh, yeah. Thanks for catching zswap_stored_pages.
Quoted 3 lines
I am not sure if this has meaningful impact on performance, but it seems like we are doing a bit more work in the common success case to avoid the work in the uncommon failure case.
Right, but at the same time I think we need some evaluation to sacrifice readability for performance. No need to be in a hurry to optimize when fixing a bug, I think.
Quoted 2 lines
Moving the charge (and atomic addition) above the
zswap_store_page()loop would be doing the opposite, albeit less clean.
I think that wouldn't work because zswap won't uncharge for pages that zswap_store_page() fails to store. And we don't know how many pages are going to be stored in zswap in advance.
IMO v2 of this patch is efficient and it works while charging just to uncharge doesn't look great.
Quoted 1 line
I don't feel strongly either way, but I slightly prefer the latter.
I'd prefer writing more readable code because it's a hotfix. Let me post v3 with Sridhar's feedback (the former) adjusted!
Quoted 136 lines · Kanchana P
Hi Hyeonggon,
Quoted 84 lines
-----Original Message-----
From: Hyeonggon Yoo <[email protected]>
Sent: Tuesday, January 28, 2025 10:55 AM
To: Sridhar, Kanchana P <[email protected]>; Johannes Weiner <[email protected]>; Yosry Ahmed <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>
Cc: [email protected]; Hyeonggon Yoo <[email protected]>; [email protected]
Subject: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent charging whenzswap_store_page()failsCommit
b7c0ccdfbafd("mm: zswap: support large folios inzswap_store()") skips charging any zswapped base pages when it failed to zswap the entire folio.However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back.
When freeing zswap entries for those pages,zswap_entry_free()uncharges the pages that were not previously charged, causing zswap charging to become inconsistent.This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootTwo warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ld
nr_pages=%lu\n",new, nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed immediately.
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-4@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;mm/zswap.c bool zswap_store(struct folio *folio) · line 1591
Quoted 10 lines
mm/zswap.c +8
-0+charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);Thanks for finding this bug! I am thinking it might make sense to charge and increment the
zswap_stored_pagescounter inzswap_store_page(). Something like:mm/zswap.c +3
-2@@ -1504,11 +1504,14 @@ static ssize_t zswap_store_page(struct page *page, entry->pool = pool; entry->swpentry = page_swpentry; entry->objcg = objcg; +if (objcg) +obj_cgroup_charge_zswap (objcg, entry->length); entry->referenced = true;if (entry->length) {INIT_LIST_HEAD (&entry->lru);zswap_lru_add (&zswap_list_lru, entry); } +atomic_long_inc (&zswap_stored_pages);return entry->length;@@ -1526,7 +1529,6 @@ bool zswap_store(struct folio *folio)struct obj_cgroup *objcg =NULL ;struct mem_cgroup *memcg =NULL ;struct zswap_pool *pool; -size_t compressed_bytes =0 ;bool ret = false;long index;@@ -1569,15 +1571,11 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) goto put_pool; - compressed_bytes += bytes; }
mm/zswap.c bool zswap_store(struct folio *folio) · line 1581
Quoted 10 lines
mm/zswap.c +1
-4-if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - } -atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;
Hi Sridhar, It looks much clearer!
And we can optimize if it turns out to be worth the complexity.
May I ask your permission to add your Signed-off-by: and Co-developed-by: ? I'm afraid to use this without your confirmation due to the Developer's Certificate of Origin.
Best,
Hyeonggon
Quoted 6 lines
Quoted 5 lines
mm/zswap.c +0
-0put_pool:zswap_pool_put (pool);
put_objcg:
--
2.47.1
Quoted 166 lines
-----Original Message-----
From: Hyeonggon Yoo <[email protected]>
Sent: Tuesday, January 28, 2025 10:40 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Johannes Weiner <[email protected]>; Yosry Ahmed <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>; [email protected]; [email protected]
Subject: Re: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent charging whenzswap_store_page()failsQuoted 7 lines · Kanchana P
Hi Hyeonggon,
Quoted 4 lines
-----Original Message-----
From: Hyeonggon Yoo <[email protected]>
Sent: Tuesday, January 28, 2025 10:55 AM
To: Sridhar, Kanchana P <[email protected]>; JohannesWeiner
Quoted 7 lines
Quoted 7 lines
<[email protected]>; Yosry Ahmed <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Andrew Morton <akpm@linux- foundation.org>
Cc: [email protected]; Hyeonggon Yoo <[email protected]>; [email protected]
Subject: [PATCH v2 mm-hotfixes] mm/zswap: fix inconsistent chargingwhen
Quoted 3 lines
Quoted 3 lines
zswap_store_page()failsCommit
b7c0ccdfbafd("mm: zswap: support large folios in
zswap_store()")Quoted 1 line
Quoted 1 line
skips charging any zswapped base pages when it failed to zswap the
entire
Quoted 5 lines
Quoted 5 lines
folio.
However, when some base pages are zswapped but it failed to zswap the entire folio, the zswap operation is rolled back. When freeing zswap entries for those pages,
zswap_entry_free()uncharges
Quoted 28 lines
Quoted 28 lines
the pages that were not previously charged, causing zswap charging to become inconsistent.
This inconsistency triggers two warnings with following steps:
# On a machine with 64GiB of RAM and 36GiB of zswap$ stress-ng --bigheap 2 # wait until the OOM-killer kills stress-ng $ sudo rebootTwo warnings are:
inmm/memcontrol.c:163, functionobj_cgroup_release():
WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));in
mm/page_counter.c:60, functionpage_counter_cancel():
if (WARN_ONCE(new < 0, "page_counterunderflow: %ld
nr_pages=%lu\n",new, nr_pages))While objcg events should only be accounted for when the entire folio is zswapped, objcg charging should be performed regardlessly. Fix accordingly.
After resolving the inconsistency, these warnings disappear.
Fixes b7c0ccdfbafd ("mm: zswap: support large folios in zswap_store()")Signed-off-by Hyeonggon Yoo <[email protected]>---
Quoted 6 lines
Quoted 6 lines
v1->v2:Fixed objcg events being accounted for on zswap failure.
Fixed the incorrect description. I misunderstood that the base pages are going to be stored in zswap, but their zswap entries are freed
immediately.
mm/zswap.c static ssize_t zswap_store_page(struct page
Quoted 50 lines
Quoted 30 lines
Added a comment on why it charges pages that are going to be removed from zswap.
mm/zswap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/zswap.c +2
-4@@ -1568,20 +1568,26 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) - goto put_pool; + goto charge_zswap; compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - }atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages); ret = true;mm/zswap.c bool zswap_store(struct folio *folio) · line 1591
Quoted 10 lines
mm/zswap.c +8
-0+charge_zswap: +/* +* Charge zswapped pages even when it failed to zswap the entire folio, +* because zswap_entry_free() will uncharge them anyway. +* Otherwise zswap charging will become inconsistent. +*/ +if (objcg) +obj_cgroup_charge_zswap (objcg, compressed_bytes);Thanks for finding this bug! I am thinking it might make sense to charge and increment the
zswap_stored_pagescounter inzswap_store_page(). Something like:mm/zswap.c +0
-0@@ -1504,11 +1504,14 @@ static ssize_t zswap_store_page(struct page*page,
Quoted 38 lines
mm/zswap.c +4
-6entry->pool = pool; entry->swpentry = page_swpentry; entry->objcg = objcg; +if (objcg) +obj_cgroup_charge_zswap (objcg, entry->length); entry->referenced = true;if (entry->length) {INIT_LIST_HEAD (&entry->lru);zswap_lru_add (&zswap_list_lru, entry); } +atomic_long_inc (&zswap_stored_pages);return entry->length;@@ -1526,7 +1529,6 @@ bool zswap_store(struct folio *folio)struct obj_cgroup *objcg =NULL ;struct mem_cgroup *memcg =NULL ;struct zswap_pool *pool; -size_t compressed_bytes =0 ;bool ret = false;long index;@@ -1569,15 +1571,11 @@ bool zswap_store(struct folio *folio) bytes =zswap_store_page (page, objcg, pool);if (bytes <0 ) goto put_pool; - compressed_bytes += bytes; } -if (objcg) { -obj_cgroup_charge_zswap (objcg, compressed_bytes); +if (objcg)count_objcg_events (objcg,ZSWPOUT , nr_pages); - } -atomic_long_add (nr_pages, &zswap_stored_pages);count_vm_events (ZSWPOUT , nr_pages);
Quoted 6 lines
mm/zswap.c bool zswap_store(struct folio *folio) · line 1581
Quoted 1 line
mm/zswap.c +0
-0ret = true;Hi Sridhar, It looks much clearer!
And we can optimize if it turns out to be worth the complexity.May I ask your permission to add your Signed-off-by: and Co-developed-by: ?
Yes, please go ahead Hyeonggon.
Thanks!
Kanchana
Quoted 12 lines
I'm afraid to use this without your confirmation due to the Developer's Certificate of Origin.
Best,
HyeonggonQuoted 6 lines
Quoted 5 lines
mm/zswap.c +0
-0put_pool:zswap_pool_put (pool);
put_objcg:
--
2.47.1