Skip to content

Commit e399257

Browse files
YHNdnzjakpm00
authored andcommitted
mm/memcontrol: respect zswap.writeback setting from parent cg too
Currently, the behavior of zswap.writeback wrt. the cgroup hierarchy seems a bit odd. Unlike zswap.max, it doesn't honor the value from parent cgroups. This surfaced when people tried to globally disable zswap writeback, i.e. reserve physical swap space only for hibernation [1] - disabling zswap.writeback only for the root cgroup results in subcgroups with zswap.writeback=1 still performing writeback. The inconsistency became more noticeable after I introduced the MemoryZSwapWriteback= systemd unit setting [2] for controlling the knob. The patch assumed that the kernel would enforce the value of parent cgroups. It could probably be workarounded from systemd's side, by going up the slice unit tree and inheriting the value. Yet I think it's more sensible to make it behave consistently with zswap.max and friends. [1] https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate#Disable_zswap_writeback_to_use_the_swap_space_only_for_hibernation [2] systemd/systemd#31734 Link: https://lkml.kernel.org/r/[email protected] Fixes: 501a06f ("zswap: memcontrol: implement zswap writeback disabling") Signed-off-by: Mike Yuan <[email protected]> Reviewed-by: Nhat Pham <[email protected]> Acked-by: Yosry Ahmed <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Michal Koutný <[email protected]> Cc: Muchun Song <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent a3f6a89 commit e399257

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

Documentation/admin-guide/cgroup-v2.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,10 @@ The following nested keys are defined.
17171717
entries fault back in or are written out to disk.
17181718

17191719
memory.zswap.writeback
1720-
A read-write single value file. The default value is "1". The
1721-
initial value of the root cgroup is 1, and when a new cgroup is
1722-
created, it inherits the current value of its parent.
1720+
A read-write single value file. The default value is "1".
1721+
Note that this setting is hierarchical, i.e. the writeback would be
1722+
implicitly disabled for child cgroups if the upper hierarchy
1723+
does so.
17231724

17241725
When this is set to 0, all swapping attempts to swapping devices
17251726
are disabled. This included both zswap writebacks, and swapping due

mm/memcontrol.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,8 +3613,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
36133613
memcg1_soft_limit_reset(memcg);
36143614
#ifdef CONFIG_ZSWAP
36153615
memcg->zswap_max = PAGE_COUNTER_MAX;
3616-
WRITE_ONCE(memcg->zswap_writeback,
3617-
!parent || READ_ONCE(parent->zswap_writeback));
3616+
WRITE_ONCE(memcg->zswap_writeback, true);
36183617
#endif
36193618
page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
36203619
if (parent) {
@@ -5320,7 +5319,14 @@ void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size)
53205319
bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)
53215320
{
53225321
/* if zswap is disabled, do not block pages going to the swapping device */
5323-
return !zswap_is_enabled() || !memcg || READ_ONCE(memcg->zswap_writeback);
5322+
if (!zswap_is_enabled())
5323+
return true;
5324+
5325+
for (; memcg; memcg = parent_mem_cgroup(memcg))
5326+
if (!READ_ONCE(memcg->zswap_writeback))
5327+
return false;
5328+
5329+
return true;
53245330
}
53255331

53265332
static u64 zswap_current_read(struct cgroup_subsys_state *css,

0 commit comments

Comments
 (0)