Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3499375ybv; Tue, 25 Feb 2020 02:25:24 -0800 (PST) X-Google-Smtp-Source: APXvYqwbQd5ju2KJ3stpVRj4thoCdXgSpmqLGXzYwZMWX5JJCZokSCX04pPi7/Vt2jlQdRXbvhmK X-Received: by 2002:a05:6808:209:: with SMTP id l9mr729211oie.112.1582626323972; Tue, 25 Feb 2020 02:25:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582626323; cv=none; d=google.com; s=arc-20160816; b=y8gDuGML6Jyuid0mWoM0kSJpEVPcWYt9lp5ic5P068FUesmVNRWYgbSRFm91a+d7qq Bygjsxxgk6W9ERWJe5Ordgv21FauJ/Ki5kguTYuiEktgyR7wJX2IiNZi6aCY1OYBmNDj +X9vkZDMxXQmtEsdpX6dxZAfKNObpDrHK9fDmooP80XUjZwfI9hSnGsEaqT2tIfOnQw/ Y8Fi9DwOF3dhtqT+x5XUu7a66Ui08G03owk1/40jFnL3geju8WBTE9//9CWZMOYOGll2 rXEMH4B/J8ohAezsbpNyfeFyuM3g5r34HxXtKMiS0nW7fwb/u7tH4NWL85eKWQi/52ti ShgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:dkim-signature; bh=cruTKgEz/zvbs2SVoNSJW8F372P48VGKaxC33GYXoBQ=; b=e6VZ9CbJ5N3xuNzC5w+jjwOSQsJAMHD2QlEgilo2OzPluAzQ7+brb2NYf2clKRU+aB y5Nqhuq8LuShprLUeR/1POav0XE6S+Znli5qKl73C3L1G74qwYLk92gNqrstZCyUrYe+ WcP7B7pYy+3NJ4ALVpgNNWU2dDuj5zwuKqf63/u7F7NWPF5tcYZt86SCUJIgNHJJAV/P Wcur6fr2bLqmQKwnED+5ZIvSBQBAA2uQ7U7rld+W8OKsI4flrXRSUH3qRE10Yta2CbJ5 sWlsaFABl269gYHFwOAGnCxnBds8TTwpW3dMOtA6tWcvr+EohZ1/Bojv6fYmH5VI9DgL WaJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=foyIz9S8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h139si6056628oib.85.2020.02.25.02.25.11; Tue, 25 Feb 2020 02:25:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=foyIz9S8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730206AbgBYKYr (ORCPT + 99 others); Tue, 25 Feb 2020 05:24:47 -0500 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:7129 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729952AbgBYKYq (ORCPT ); Tue, 25 Feb 2020 05:24:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582626284; x=1614162284; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=cruTKgEz/zvbs2SVoNSJW8F372P48VGKaxC33GYXoBQ=; b=foyIz9S8DrTmHs9yBQrQGV7/z5nGfoObDKmJJq4CEUyNfN8VzJ1odON7 e07oqlTIZWZ4y4mWLxzNlxAYnPnhrfns2P0buhTOSAUGfxK2ya9YkLQpE DfjChOsPSJdm2TQ5kufRv1LHXyPYEvPmnddds4GC/Qhm7ZZdUVpcVwno7 Q=; IronPort-SDR: skDApPe28e6C99O4I83Nc9x8cxfcwci/1dB5YpuvxzA1OelYtHZr7N/2C1xJW/PeFnKfYapmpW C3WNkJeV6o6Q== X-IronPort-AV: E=Sophos;i="5.70,483,1574121600"; d="scan'208";a="18459914" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 25 Feb 2020 10:24:44 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com (Postfix) with ESMTPS id 885E2A2B82; Tue, 25 Feb 2020 10:24:34 +0000 (UTC) Received: from EX13D31EUA001.ant.amazon.com (10.43.165.15) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Tue, 25 Feb 2020 10:24:33 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 25 Feb 2020 10:24:22 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC v3 3/7] mm/damon: Implement data access monitoring-based operation schemes Date: Tue, 25 Feb 2020 11:22:56 +0100 Message-ID: <20200225102300.23895-4-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200225102300.23895-1-sjpark@amazon.com> References: <20200225102300.23895-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D30UWC001.ant.amazon.com (10.43.162.128) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: SeongJae Park In many cases, users might use DAMON for simple data access awared memory management optimizations such as applying an operation scheme to a memory region of a specific size having a specific access frequency for a specific time. For example, "page out a memory region larger than 100 MiB but having a low access frequency more than 10 minutes", or "Use THP for a memory region larger than 2 MiB having a high access frequency for more than 2 seconds". To minimize users from spending their time for implementation of such simple data access monitoring-based operation schemes, this commit makes DAMON to handle such schemes directly. With this commit, users can simply specify their desired schemes to DAMON. Each of the schemes is composed with conditions for filtering of the target memory regions and desired memory management action for the target. In specific, the format is:: The filtering conditions are size of memory region, number of accesses to the region monitored by DAMON, and the age of the region. The age of region is incremented periodically but reset when its addresses or access frequency has significanly changed or the action of a scheme has applied. For the action, current implementation supports only a few of madvise() hints, ``MADV_WILLNEED``, ``MADV_COLD``, ``MADV_PAGEOUT``, ``MADV_HUGEPAGE``, and ``MADV_NOHUGEPAGE``. Signed-off-by: SeongJae Park --- include/linux/damon.h | 24 ++++++++ mm/damon.c | 134 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index 50fbe308590e..8cb2452579ee 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -36,6 +36,27 @@ struct damon_task { struct list_head list; }; +/* Data Access Monitoring-based Operation Scheme */ +enum damos_action { + DAMOS_WILLNEED, + DAMOS_COLD, + DAMOS_PAGEOUT, + DAMOS_HUGEPAGE, + DAMOS_NOHUGEPAGE, + DAMOS_ACTION_LEN, +}; + +struct damos { + unsigned int min_sz_region; + unsigned int max_sz_region; + unsigned int min_nr_accesses; + unsigned int max_nr_accesses; + unsigned int min_age_region; + unsigned int max_age_region; + enum damos_action action; + struct list_head list; +}; + struct damon_ctx { unsigned long sample_interval; unsigned long aggr_interval; @@ -58,6 +79,7 @@ struct damon_ctx { struct rnd_state rndseed; struct list_head tasks_list; /* 'damon_task' objects */ + struct list_head schemes_list; /* 'damos' objects */ /* callbacks */ void (*sample_cb)(struct damon_ctx *context); @@ -66,6 +88,8 @@ struct damon_ctx { int damon_set_pids(struct damon_ctx *ctx, unsigned long *pids, ssize_t nr_pids); +int damon_set_schemes(struct damon_ctx *ctx, + struct damos **schemes, ssize_t nr_schemes); int damon_set_recording(struct damon_ctx *ctx, unsigned int rbuf_len, char *rfile_path); int damon_set_attrs(struct damon_ctx *ctx, unsigned long s, unsigned long a, diff --git a/mm/damon.c b/mm/damon.c index e08dba2e2717..a4d6dff60b1f 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -11,6 +11,7 @@ #define CREATE_TRACE_POINTS +#include #include #include #include @@ -24,6 +25,8 @@ #include #include +#include "internal.h" + #define damon_get_task_struct(t) \ (get_pid_task(find_vpid(t->pid), PIDTYPE_PID)) @@ -45,6 +48,12 @@ #define damon_for_each_task_safe(ctx, t, next) \ list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) +#define damon_for_each_schemes(ctx, r) \ + list_for_each_entry(r, &(ctx)->schemes_list, list) + +#define damon_for_each_schemes_safe(ctx, s, next) \ + list_for_each_entry_safe(s, next, &(ctx)->schemes_list, list) + #define MAX_RFILE_PATH_LEN 256 /* Get a random number in [l, r) */ @@ -190,6 +199,27 @@ static void damon_destroy_task(struct damon_task *t) damon_free_task(t); } +static void damon_add_scheme(struct damon_ctx *ctx, struct damos *s) +{ + list_add_tail(&s->list, &ctx->schemes_list); +} + +static void damon_del_scheme(struct damos *s) +{ + list_del(&s->list); +} + +static void damon_free_scheme(struct damos *s) +{ + kfree(s); +} + +static void damon_destroy_scheme(struct damos *s) +{ + damon_del_scheme(s); + damon_free_scheme(s); +} + /* * Returns number of monitoring target tasks */ @@ -642,6 +672,87 @@ static void kdamond_count_age(struct damon_ctx *c, unsigned int threshold) } } +static int damos_do_action(struct damon_task *task, struct damon_region *r, + enum damos_action action) +{ + struct task_struct *t; + struct mm_struct *mm; + int madv_action; + int ret = -EINVAL; + + switch (action) { + case DAMOS_WILLNEED: + madv_action = MADV_WILLNEED; + break; + case DAMOS_COLD: + madv_action = MADV_COLD; + break; + case DAMOS_PAGEOUT: + madv_action = MADV_PAGEOUT; + break; + case DAMOS_HUGEPAGE: + madv_action = MADV_HUGEPAGE; + break; + case DAMOS_NOHUGEPAGE: + madv_action = MADV_NOHUGEPAGE; + break; + default: + pr_warn("Wrong action %d\n", action); + goto out; + } + + t = damon_get_task_struct(task); + if (!t) + goto out; + mm = damon_get_mm(task); + if (!mm) + goto put_task_out; + + ret = madvise_common(t, mm, PAGE_ALIGN(r->vm_start), + PAGE_ALIGN(r->vm_end - r->vm_start), madv_action); + + mmput(mm); +put_task_out: + put_task_struct(t); +out: + return ret; +} + +static void damon_do_apply_schemes(struct damon_ctx *c, struct damon_task *t, + struct damon_region *r) +{ + struct damos *s; + unsigned long sz; + + damon_for_each_schemes(c, s) { + sz = r->vm_end - r->vm_start; + if ((s->min_sz_region && sz < s->min_sz_region) || + (s->max_sz_region && s->max_sz_region < sz)) + continue; + if ((s->min_nr_accesses && r->nr_accesses < s->min_nr_accesses) + || (s->max_nr_accesses && + s->max_nr_accesses < r->nr_accesses)) + continue; + if ((s->min_age_region && r->age < s->min_age_region) || + (s->max_age_region && + s->max_age_region < r->age)) + continue; + damos_do_action(t, r, s->action); + r->age = 0; + } +} + +static void kdamond_apply_schemes(struct damon_ctx *c) +{ + struct damon_task *t; + struct damon_region *r; + + damon_for_each_task(c, t) { + damon_for_each_region(r, t) + damon_do_apply_schemes(c, t, r); + } +} + #define sz_damon_region(r) (r->vm_end - r->vm_start) /* @@ -966,6 +1077,7 @@ static int kdamond_fn(void *data) kdamond_count_age(ctx, max_nr_accesses / 10); if (ctx->aggregate_cb) ctx->aggregate_cb(ctx); + kdamond_apply_schemes(ctx); kdamond_flush_aggregated(ctx); kdamond_split_regions(ctx); } @@ -1040,6 +1152,27 @@ int damon_stop(struct damon_ctx *ctx) return damon_turn_kdamond(ctx, false); } +/* + * Set the data access monitoring oriented schemes + * + * NOTE: This function should not be called while the kdamond of the context is + * running. + * + * Returns 0 if success, or negative error code otherwise. + */ +int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, + ssize_t nr_schemes) +{ + struct damos *s, *next; + ssize_t i; + + damon_for_each_schemes_safe(ctx, s, next) + damon_destroy_scheme(s); + for (i = 0; i < nr_schemes; i++) + damon_add_scheme(ctx, schemes[i]); + return 0; +} + /* * This function should not be called while the kdamond is running. */ @@ -1485,6 +1618,7 @@ static int __init damon_init_user_ctx(void) prandom_seed_state(&ctx->rndseed, 42); INIT_LIST_HEAD(&ctx->tasks_list); + INIT_LIST_HEAD(&ctx->schemes_list); ctx->sample_cb = NULL; ctx->aggregate_cb = NULL; -- 2.17.1