Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp578546ybj; Tue, 5 May 2020 04:12:34 -0700 (PDT) X-Google-Smtp-Source: APiQypLm90MCsjrABz0pQEoQFl2YbD1RdlADLnOe+FZ5QsxcJxlJRcPRphq17Kz2Fr7JNoXXvw9j X-Received: by 2002:aa7:d7d8:: with SMTP id e24mr2108001eds.215.1588677154080; Tue, 05 May 2020 04:12:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588677154; cv=none; d=google.com; s=arc-20160816; b=gikTRVsiFxdc8Sg99VhcvyMbNPQBeHjmM1wweP3PHJn2eKBXRtMvfsYsACtkFV4+AI RFpDpbS5Iddz8MgNAbFvAeAOB2WuU+xOLHcDV20T5eiR6CBhZv2PM3fkSlS37tBi3A35 LfDb9H1tFJ8oYHAiylmPSfQ5egAI52lek1cbgE3YpGWmu8JSsBxWuiMw3muYpCKaQQMS eIifmfsuY3TjlyfxlZc16cISOkzmHa8AdY3fsj689274Nh7hBOZ1wZbApUfPxwThUT8X n4YLT9Vk262atacxkwmH2zV1JK1D15NfQdK65kc3mmH46zCnWfbkf9SoKIHm9sMLn3gP GaPA== 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=GfwmAepOlGw0jc65vePXQyjyaX4cDEsLZQAUUm5+13E=; b=XtquB5KxbO7wmwt7rvvriZd0D2Oroq2CCd4eq0oHg/MkzIdxDRPbAEnNcLpjH5kSlk dI9Z5w0cV70znrAh8hqC6oqVVorMbXzTG0cG+wCt+Ldig20ueTsWYtheG9to2N7YQyDE IzpHPpv3AyUr7PgV4Ce/Heeu9+2CmoxHAU6dl4LPggD+Dz1PN4unqwOOrngFQlWWVQOs I5v4P2+c9pheXLkR3UWeHtuVn1nULFEGB6+SsDDYagfjI0gOOhK/0arnA6oIlFlfci36 KE4VOW4W+CbCofCC4wiW0slm9aAWd9BctneNr66L+00MP/LybnVvZt1xon46mmIOmqoz HGxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=LeodwwAu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id l15si1134832edv.74.2020.05.05.04.12.09; Tue, 05 May 2020 04:12:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=LeodwwAu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1728811AbgEELKd (ORCPT + 99 others); Tue, 5 May 2020 07:10:33 -0400 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:3509 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725766AbgEELKb (ORCPT ); Tue, 5 May 2020 07:10:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1588677030; x=1620213030; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=GfwmAepOlGw0jc65vePXQyjyaX4cDEsLZQAUUm5+13E=; b=LeodwwAugr4EdW2N6ILG6GYUgLq6xwsMg4MFmR5R+voM8mKbetBqRMaq cGu7Gj92xHAw8+KRhN+2aObq3g7Vp+GJRIoxPLJdjIg3RtYk8UAz+UCMu a2WopIQRk7/fVvywTnyXnaR5nsLL3TLtEPVKTh/gNaDuBSIYsU6ZVwhul k=; IronPort-SDR: U951sFxepCa+Qb7TXkybI8h2ll5BqH3pY9nkGPY6TclyfJcYUJSxV07bvxi+Tk3I+E4NWG6Nk2 ak9flGZeyIzQ== X-IronPort-AV: E=Sophos;i="5.73,354,1583193600"; d="scan'208";a="28930959" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2b-55156cd4.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 05 May 2020 11:10:14 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-55156cd4.us-west-2.amazon.com (Postfix) with ESMTPS id 75550A1BD4; Tue, 5 May 2020 11:10:11 +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.1497.2; Tue, 5 May 2020 11:10:10 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.160.92) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 5 May 2020 11:09:54 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v10 03/16] mm: Introduce Data Access MONitor (DAMON) Date: Tue, 5 May 2020 13:08:02 +0200 Message-ID: <20200505110815.10532-4-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200505110815.10532-1-sjpark@amazon.com> References: <20200505110815.10532-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.92] X-ClientProxiedBy: EX13D35UWB004.ant.amazon.com (10.43.161.230) 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 This commit introduces a kernel module named DAMON. Note that this commit is implementing only the stub for the module load/unload, basic data structures, and simple manipulation functions of the structures to keep the size of commit small. The core mechanisms of DAMON will be implemented one by one by following commits. Signed-off-by: SeongJae Park --- include/linux/damon.h | 36 +++++++++ mm/Kconfig | 12 +++ mm/Makefile | 1 + mm/damon.c | 173 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+) create mode 100644 include/linux/damon.h create mode 100644 mm/damon.c diff --git a/include/linux/damon.h b/include/linux/damon.h new file mode 100644 index 000000000000..19f411d36c0d --- /dev/null +++ b/include/linux/damon.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * DAMON api + * + * Copyright 2019-2020 Amazon.com, Inc. or its affiliates. + * + * Author: SeongJae Park + */ + +#ifndef _DAMON_H_ +#define _DAMON_H_ + +#include +#include + +/* Represents a monitoring target region of [vm_start, vm_end) */ +struct damon_region { + unsigned long vm_start; + unsigned long vm_end; + unsigned long sampling_addr; + unsigned int nr_accesses; + struct list_head list; +}; + +/* Represents a monitoring target task */ +struct damon_task { + int pid; + struct list_head regions_list; + struct list_head list; +}; + +struct damon_ctx { + struct list_head tasks_list; /* 'damon_task' objects */ +}; + +#endif diff --git a/mm/Kconfig b/mm/Kconfig index ab80933be65f..9ea49633a6df 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -739,4 +739,16 @@ config ARCH_HAS_HUGEPD config MAPPING_DIRTY_HELPERS bool +config DAMON + tristate "Data Access Monitor" + depends on MMU + help + Provides data access monitoring. + + DAMON is a kernel module that allows users to monitor the actual + memory access pattern of specific user-space processes. It aims to + be 1) accurate enough to be useful for performance-centric domains, + and 2) sufficiently light-weight so that it can be applied online. + If unsure, say N. + endmenu diff --git a/mm/Makefile b/mm/Makefile index 272e66039e70..5346314edee6 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -110,3 +110,4 @@ obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o +obj-$(CONFIG_DAMON) += damon.o diff --git a/mm/damon.c b/mm/damon.c new file mode 100644 index 000000000000..e319c1e19f3d --- /dev/null +++ b/mm/damon.c @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Data Access Monitor + * + * Copyright 2019-2020 Amazon.com, Inc. or its affiliates. + * + * Author: SeongJae Park + */ + +#define pr_fmt(fmt) "damon: " fmt + +#include +#include +#include +#include + +#define damon_get_task_struct(t) \ + (get_pid_task(find_vpid(t->pid), PIDTYPE_PID)) + +#define damon_next_region(r) \ + (container_of(r->list.next, struct damon_region, list)) + +#define damon_prev_region(r) \ + (container_of(r->list.prev, struct damon_region, list)) + +#define damon_for_each_region(r, t) \ + list_for_each_entry(r, &t->regions_list, list) + +#define damon_for_each_region_safe(r, next, t) \ + list_for_each_entry_safe(r, next, &t->regions_list, list) + +#define damon_for_each_task(ctx, t) \ + list_for_each_entry(t, &(ctx)->tasks_list, list) + +#define damon_for_each_task_safe(ctx, t, next) \ + list_for_each_entry_safe(t, next, &(ctx)->tasks_list, list) + +/* Get a random number in [l, r) */ +#define damon_rand(l, r) (l + prandom_u32() % (r - l)) + +/* + * Construct a damon_region struct + * + * Returns the pointer to the new struct if success, or NULL otherwise + */ +static struct damon_region *damon_new_region(struct damon_ctx *ctx, + unsigned long vm_start, unsigned long vm_end) +{ + struct damon_region *region; + + region = kmalloc(sizeof(*region), GFP_KERNEL); + if (!region) + return NULL; + + region->vm_start = vm_start; + region->vm_end = vm_end; + region->nr_accesses = 0; + INIT_LIST_HEAD(®ion->list); + + return region; +} + +/* + * Add a region between two other regions + */ +static inline void damon_insert_region(struct damon_region *r, + struct damon_region *prev, struct damon_region *next) +{ + __list_add(&r->list, &prev->list, &next->list); +} + +static void damon_add_region(struct damon_region *r, struct damon_task *t) +{ + list_add_tail(&r->list, &t->regions_list); +} + +static void damon_del_region(struct damon_region *r) +{ + list_del(&r->list); +} + +static void damon_free_region(struct damon_region *r) +{ + kfree(r); +} + +static void damon_destroy_region(struct damon_region *r) +{ + damon_del_region(r); + damon_free_region(r); +} + +/* + * Construct a damon_task struct + * + * Returns the pointer to the new struct if success, or NULL otherwise + */ +static struct damon_task *damon_new_task(int pid) +{ + struct damon_task *t; + + t = kmalloc(sizeof(*t), GFP_KERNEL); + if (!t) + return NULL; + + t->pid = pid; + INIT_LIST_HEAD(&t->regions_list); + + return t; +} + +static void damon_add_task(struct damon_ctx *ctx, struct damon_task *t) +{ + list_add_tail(&t->list, &ctx->tasks_list); +} + +static void damon_del_task(struct damon_task *t) +{ + list_del(&t->list); +} + +static void damon_free_task(struct damon_task *t) +{ + struct damon_region *r, *next; + + damon_for_each_region_safe(r, next, t) + damon_free_region(r); + kfree(t); +} + +static void damon_destroy_task(struct damon_task *t) +{ + damon_del_task(t); + damon_free_task(t); +} + +static unsigned int nr_damon_tasks(struct damon_ctx *ctx) +{ + struct damon_task *t; + unsigned int nr_tasks = 0; + + damon_for_each_task(ctx, t) + nr_tasks++; + + return nr_tasks; +} + +static unsigned int nr_damon_regions(struct damon_task *t) +{ + struct damon_region *r; + unsigned int nr_regions = 0; + + damon_for_each_region(r, t) + nr_regions++; + + return nr_regions; +} + +static int __init damon_init(void) +{ + return 0; +} + +static void __exit damon_exit(void) +{ +} + +module_init(damon_init); +module_exit(damon_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("SeongJae Park "); +MODULE_DESCRIPTION("DAMON: Data Access MONitor"); -- 2.17.1