Skip to content

Commit 58fdb65

Browse files
pixelclusterptr1337
authored andcommitted
cgroup/dmem: Add queries for protection values
Callers can use this feedback to be more aggressive in making space for allocations of a cgroup if they know it is protected. These are counterparts to memcg's mem_cgroup_below_{min,low}. Signed-off-by: Natalie Vock <natalie.vock@gmx.de>
1 parent 591cd65 commit 58fdb65

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

include/linux/cgroup_dmem.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ void dmem_cgroup_uncharge(struct dmem_cgroup_pool_state *pool, u64 size);
2424
bool dmem_cgroup_state_evict_valuable(struct dmem_cgroup_pool_state *limit_pool,
2525
struct dmem_cgroup_pool_state *test_pool,
2626
bool ignore_low, bool *ret_hit_low);
27+
bool dmem_cgroup_below_min(struct dmem_cgroup_pool_state *root,
28+
struct dmem_cgroup_pool_state *test);
29+
bool dmem_cgroup_below_low(struct dmem_cgroup_pool_state *root,
30+
struct dmem_cgroup_pool_state *test);
2731

2832
void dmem_cgroup_pool_state_put(struct dmem_cgroup_pool_state *pool);
2933
#else
@@ -59,6 +63,18 @@ bool dmem_cgroup_state_evict_valuable(struct dmem_cgroup_pool_state *limit_pool,
5963
return true;
6064
}
6165

66+
static inline bool dmem_cgroup_below_min(struct dmem_cgroup_pool_state *root,
67+
struct dmem_cgroup_pool_state *test)
68+
{
69+
return false;
70+
}
71+
72+
static inline bool dmem_cgroup_below_low(struct dmem_cgroup_pool_state *root,
73+
struct dmem_cgroup_pool_state *test)
74+
{
75+
return false;
76+
}
77+
6278
static inline void dmem_cgroup_pool_state_put(struct dmem_cgroup_pool_state *pool)
6379
{ }
6480

kernel/cgroup/dmem.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,68 @@ int dmem_cgroup_try_charge(struct dmem_cgroup_region *region, u64 size,
694694
}
695695
EXPORT_SYMBOL_GPL(dmem_cgroup_try_charge);
696696

697+
/**
698+
* dmem_cgroup_below_min() - Tests whether current usage is within min limit.
699+
*
700+
* @root: Root of the subtree to calculate protection for, or NULL to calculate global protection.
701+
* @test: The pool to test the usage/min limit of.
702+
*
703+
* Return: true if usage is below min and the cgroup is protected, false otherwise.
704+
*/
705+
bool dmem_cgroup_below_min(struct dmem_cgroup_pool_state *root,
706+
struct dmem_cgroup_pool_state *test)
707+
{
708+
if (root == test || !pool_parent(test))
709+
return false;
710+
711+
if (!root) {
712+
for (root = test; pool_parent(root); root = pool_parent(root))
713+
{}
714+
}
715+
716+
/*
717+
* In mem_cgroup_below_min(), the memcg pendant, this call is missing.
718+
* mem_cgroup_below_min() gets called during traversal of the cgroup tree, where
719+
* protection is already calculated as part of the traversal. dmem cgroup eviction
720+
* does not traverse the cgroup tree, so we need to recalculate effective protection
721+
* here.
722+
*/
723+
dmem_cgroup_calculate_protection(root, test);
724+
return page_counter_read(&test->cnt) <= READ_ONCE(test->cnt.emin);
725+
}
726+
EXPORT_SYMBOL_GPL(dmem_cgroup_below_min);
727+
728+
/**
729+
* dmem_cgroup_below_low() - Tests whether current usage is within low limit.
730+
*
731+
* @root: Root of the subtree to calculate protection for, or NULL to calculate global protection.
732+
* @test: The pool to test the usage/low limit of.
733+
*
734+
* Return: true if usage is below low and the cgroup is protected, false otherwise.
735+
*/
736+
bool dmem_cgroup_below_low(struct dmem_cgroup_pool_state *root,
737+
struct dmem_cgroup_pool_state *test)
738+
{
739+
if (root == test || !pool_parent(test))
740+
return false;
741+
742+
if (!root) {
743+
for (root = test; pool_parent(root); root = pool_parent(root))
744+
{}
745+
}
746+
747+
/*
748+
* In mem_cgroup_below_low(), the memcg pendant, this call is missing.
749+
* mem_cgroup_below_low() gets called during traversal of the cgroup tree, where
750+
* protection is already calculated as part of the traversal. dmem cgroup eviction
751+
* does not traverse the cgroup tree, so we need to recalculate effective protection
752+
* here.
753+
*/
754+
dmem_cgroup_calculate_protection(root, test);
755+
return page_counter_read(&test->cnt) <= READ_ONCE(test->cnt.elow);
756+
}
757+
EXPORT_SYMBOL_GPL(dmem_cgroup_below_low);
758+
697759
static int dmem_cgroup_region_capacity_show(struct seq_file *sf, void *v)
698760
{
699761
struct dmem_cgroup_region *region;

0 commit comments

Comments
 (0)