mm/damon/core: introduce address range type damos filter

Patch series "Extend DAMOS filters for address ranges and DAMON monitoring
targets"

There are use cases that need to apply DAMOS schemes to specific address
ranges or DAMON monitoring targets.  NUMA nodes in the physical address
space, special memory objects in the virtual address space, and monitoring
target specific efficient monitoring results snapshot retrieval could be
examples of such use cases.  This patchset extends DAMOS filters feature
for such cases, by implementing two more filter types, namely address
ranges and DAMON monitoring types.

Patches sequence
----------------

The first seven patches are for the address ranges based DAMOS filter. 
The first patch implements the filter feature and expose it via DAMON
kernel API.  The second patch further expose the feature to users via
DAMON sysfs interface.  The third and fourth patches implement unit tests
and selftests for the feature.  Three patches (fifth to seventh) updating
the documents follow.

The following six patches are for the DAMON monitoring target based DAMOS
filter.  The eighth patch implements the feature in the core layer and
expose it via DAMON's kernel API.  The ninth patch further expose it to
users via DAMON sysfs interface.  Tenth patch add a selftest, and two
patches (eleventh and twelfth) update documents.

[1] https://lore.kernel.org/damon/20230728203444.70703-1-sj@kernel.org/


This patch (of 13):

Users can know special characteristic of specific address ranges.  NUMA
nodes or special objects or buffers in virtual address space could be such
examples.  For such cases, DAMOS schemes could required to be applied to
only specific address ranges.  Implement yet another type of DAMOS filter
for the purpose.

Note that the existing filter types, namely anon pages and memcg DAMOS
filters needed page level type check.  Because such check can be done
efficiently in the opertions set layer, those filters are handled in
operations set layer.  Specifically, only paddr operations set
implementation supports these filters.  Also, because statistics counting
is done in the DAMON core layer, the regions that filtered out by these
filters are counted as tried but failed to the statistics.

Unlike those, address range based filters can efficiently handled in the
core layer.  Hence, do the handling in the layer, and count the regions
that filtered out by those as the scheme has not tried for the region. 
This difference should clearly documented.

Link: https://lkml.kernel.org/r/20230802214312.110532-1-sj@kernel.org
Link: https://lkml.kernel.org/r/20230802214312.110532-2-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: Brendan Higgins <brendanhiggins@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
SeongJae Park 2023-08-02 21:43:00 +00:00 committed by Andrew Morton
parent ea7f03a441
commit ab9bda001b
2 changed files with 68 additions and 6 deletions

View File

@ -226,16 +226,24 @@ struct damos_stat {
* enum damos_filter_type - Type of memory for &struct damos_filter
* @DAMOS_FILTER_TYPE_ANON: Anonymous pages.
* @DAMOS_FILTER_TYPE_MEMCG: Specific memcg's pages.
* @DAMOS_FILTER_TYPE_ADDR: Address range.
* @NR_DAMOS_FILTER_TYPES: Number of filter types.
*
* The support of each filter type is up to running &struct damon_operations.
* &enum DAMON_OPS_PADDR is supporting all filter types, while
* &enum DAMON_OPS_VADDR and &enum DAMON_OPS_FVADDR are not supporting any
* filter types.
* The anon pages type and memcg type filters are handled by underlying
* &struct damon_operations as a part of scheme action trying, and therefore
* accounted as 'tried'. In contrast, other types are handled by core layer
* before trying of the action and therefore not accounted as 'tried'.
*
* The support of the filters that handled by &struct damon_operations depend
* on the running &struct damon_operations.
* &enum DAMON_OPS_PADDR supports both anon pages type and memcg type filters,
* while &enum DAMON_OPS_VADDR and &enum DAMON_OPS_FVADDR don't support any of
* the two types.
*/
enum damos_filter_type {
DAMOS_FILTER_TYPE_ANON,
DAMOS_FILTER_TYPE_MEMCG,
DAMOS_FILTER_TYPE_ADDR,
NR_DAMOS_FILTER_TYPES,
};
@ -244,18 +252,20 @@ enum damos_filter_type {
* @type: Type of the page.
* @matching: If the matching page should filtered out or in.
* @memcg_id: Memcg id of the question if @type is DAMOS_FILTER_MEMCG.
* @addr_range: Address range if @type is DAMOS_FILTER_TYPE_ADDR.
* @list: List head for siblings.
*
* Before applying the &damos->action to a memory region, DAMOS checks if each
* page of the region matches to this and avoid applying the action if so.
* Note that the check support is up to &struct damon_operations
* implementation.
* Support of each filter type depends on the running &struct damon_operations
* and the type. Refer to &enum damos_filter_type for more detai.
*/
struct damos_filter {
enum damos_filter_type type;
bool matching;
union {
unsigned short memcg_id;
struct damon_addr_range addr_range;
};
struct list_head list;
};

View File

@ -877,6 +877,56 @@ static void damos_update_stat(struct damos *s,
s->stat.sz_applied += sz_applied;
}
static bool __damos_filter_out(struct damon_target *t, struct damon_region *r,
struct damos_filter *filter)
{
bool matched = false;
unsigned long start, end;
switch (filter->type) {
case DAMOS_FILTER_TYPE_ADDR:
start = ALIGN_DOWN(filter->addr_range.start, DAMON_MIN_REGION);
end = ALIGN_DOWN(filter->addr_range.end, DAMON_MIN_REGION);
/* inside the range */
if (start <= r->ar.start && r->ar.end <= end) {
matched = true;
break;
}
/* outside of the range */
if (r->ar.end <= start || end <= r->ar.start) {
matched = false;
break;
}
/* start before the range and overlap */
if (r->ar.start < start) {
damon_split_region_at(t, r, start - r->ar.start);
matched = false;
break;
}
/* start inside the range */
damon_split_region_at(t, r, end - r->ar.start);
matched = true;
break;
default:
break;
}
return matched == filter->matching;
}
static bool damos_filter_out(struct damon_target *t, struct damon_region *r,
struct damos *s)
{
struct damos_filter *filter;
damos_for_each_filter(filter, s) {
if (__damos_filter_out(t, r, filter))
return true;
}
return false;
}
static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
struct damon_region *r, struct damos *s)
{
@ -894,6 +944,8 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
goto update_stat;
damon_split_region_at(t, r, sz);
}
if (damos_filter_out(t, r, s))
return;
ktime_get_coarse_ts64(&begin);
if (c->callback.before_damos_apply)
err = c->callback.before_damos_apply(c, t, r, s);