Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2222571pxa; Mon, 17 Aug 2020 04:31:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGyTHYGxwgGJQ5/AjxzyotEhikfoYKEOiRFvOU/tfJMQwc4kgophELjx6hQMSdoK2svgaz X-Received: by 2002:a05:6402:b32:: with SMTP id bo18mr14624657edb.201.1597663863438; Mon, 17 Aug 2020 04:31:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597663863; cv=none; d=google.com; s=arc-20160816; b=FwSpo8cgbqdeRhvIf3LlU9dGVt/rA70trp5wFbtV4s+B0e9ihxKncWJ+AW3gBeQmRZ D3toyZmbLqgRUvUTUyWKMWeetdbhvvQPt3O9JBHdsyKgvnNnl8Z2E4XeiET+LwcdULVO a3K7nPwlX5ZbT6GirC0nRdEq1pEbN4tzkfJVx4KTtdAqzFFvZ8n5Dc4GZX26PM9MMkyh R4P00XEssD172z6P3CHl/1WqfuYSPbw/zfAMkEHF39GIwalmcDIdM2aEq6rrWqQ96JEf gtmIhsOQoPYSvDX+NFY3dXajs4c7/AZ1zDtw5veAGvRq3dYFxX4yeNTKNQnj9SclAH/Y x7RQ== 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:dkim-signature; bh=EyPE9Guy6K8Ykgz/GFVPQxJeVx8vx3JyIj/anqhEN08=; b=KLVrbnk3p5ybT/36CR95vLEiqCEwbYjhQGECpGlmzzuvhn/LRA/kdAvM1WdHyHAUXw Amb7t1ly5MarXsN/zJcBM8z2L24z4uHqW8zWqXEAAkICA+t3vpirBPHz+cZMRK8NbR0l MOF9QTmBHPgM6zjDSMEp74CbYMRRwz1YsNGScC6sk4lxbY1uhKGmebLUy7TFrV9L0qfC GtsiV5wGwIioHWXJfX3I8wDveQcCpU0K+aeY+swagrQ6aeWYiHN3BngTDrmqw6kQx5dq 1KFgl4Ic9n77xDZa9c2gQohruyuGJt/3a7MUcUlX5Z5DLm06HKwfodeVZGlUUMv6H2MS Ar3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=Y0Z4W4ZN; 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 be10si10706087edb.574.2020.08.17.04.30.40; Mon, 17 Aug 2020 04:31:03 -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=Y0Z4W4ZN; 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 S1726829AbgHQKxp (ORCPT + 99 others); Mon, 17 Aug 2020 06:53:45 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:13633 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726171AbgHQKwu (ORCPT ); Mon, 17 Aug 2020 06:52:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1597661568; x=1629197568; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=EyPE9Guy6K8Ykgz/GFVPQxJeVx8vx3JyIj/anqhEN08=; b=Y0Z4W4ZNCXy3kCPCaimFYE9oXY6EMjhMjGDyWBwXdhHXJeErcNbCd6up YMEb6cuxglqdJ7Qi7Zrxke+HX4po4+riLrjF7wq3RPNhO4osaIkxGNRC7 L0pRtB1sJ+ybb7Bfy2svT6+bzWKIf/C4suehhClo+s1DsdW37Pjoe8/ie k=; X-IronPort-AV: E=Sophos;i="5.76,322,1592870400"; d="scan'208";a="67283530" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 17 Aug 2020 10:52:40 +0000 Received: from EX13MTAUEA001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2a-f14f4a47.us-west-2.amazon.com (Postfix) with ESMTPS id 0357DA18DF; Mon, 17 Aug 2020 10:52:37 +0000 (UTC) Received: from EX13D31EUA001.ant.amazon.com (10.43.165.15) by EX13MTAUEA001.ant.amazon.com (10.43.61.82) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 17 Aug 2020 10:52:37 +0000 Received: from u3f2cd687b01c55.ant.amazon.com (10.43.160.192) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 17 Aug 2020 10:52:20 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v20 01/15] mm: Introduce Data Access MONitor (DAMON) Date: Mon, 17 Aug 2020 12:51:23 +0200 Message-ID: <20200817105137.19296-2-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200817105137.19296-1-sjpark@amazon.com> References: <20200817105137.19296-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.192] X-ClientProxiedBy: EX13D02UWC004.ant.amazon.com (10.43.162.236) 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 DAMON is a data access monitoring framework subsystem for the Linux kernel. The core mechanisms of DAMON make it - accurate (the monitoring output is useful enough for DRAM level memory management; It might not appropriate for CPU Cache levels, though), - light-weight (the monitoring overhead is low enough to be applied online), and - scalable (the upper-bound of the overhead is in constant range regardless of the size of target workloads). Using this framework, therefore, the kernel's memory management mechanisms can make advanced decisions. Experimental memory management optimization works that incurring high data accesses monitoring overhead could implemented again. In user space, meanwhile, users who have some special workloads can write personalized applications for better understanding and optimizations of their workloads and systems. This commit is implementing only the stub for the initialization, basic data structures, and simple manipulation functions of the structures. The core mechanisms of DAMON will be implemented by following commits. Signed-off-by: SeongJae Park Reviewed-by: Leonard Foerster Reviewed-by: Varad Gautam --- include/linux/damon.h | 66 ++++++++++++++++ mm/Kconfig | 11 +++ mm/Makefile | 1 + mm/damon.c | 176 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 254 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..a6e839a236f4 --- /dev/null +++ b/include/linux/damon.h @@ -0,0 +1,66 @@ +/* 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 + +/** + * struct damon_addr_range - Represents an address region of [@start, @end). + * @start: Start address of the region (inclusive). + * @end: End address of the region (exclusive). + */ +struct damon_addr_range { + unsigned long start; + unsigned long end; +}; + +/** + * struct damon_region - Represents a monitoring target region. + * @ar: The address range of the region. + * @sampling_addr: Address of the sample for the next access check. + * @nr_accesses: Access frequency of this region. + * @list: List head for siblings. + */ +struct damon_region { + struct damon_addr_range ar; + unsigned long sampling_addr; + unsigned int nr_accesses; + struct list_head list; +}; + +/** + * struct damon_target - Represents a monitoring target. + * @id: Unique identifier for this target. + * @regions_list: Head of the monitoring target regions of this target. + * @list: List head for siblings. + * + * Each monitoring context could have multiple targets. For example, a context + * for virtual memory address spaces could have multiple target processes. The + * @id of each target should be unique among the targets of the context. For + * example, in the virtual address monitoring context, it could be a pidfd or + * an address of an mm_struct. + */ +struct damon_target { + unsigned long id; + struct list_head regions_list; + struct list_head list; +}; + +/** + * struct damon_ctx - Represents a context for each monitoring. + * @targets_list: Head of monitoring targets (&damon_target) list. + */ +struct damon_ctx { + struct list_head targets_list; /* 'damon_target' objects */ +}; + +#endif diff --git a/mm/Kconfig b/mm/Kconfig index f2104cc0d35c..a99d755d67d3 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -872,4 +872,15 @@ config ARCH_HAS_HUGEPD config MAPPING_DIRTY_HELPERS bool +config DAMON + bool "Data Access Monitor" + help + This feature allows to monitor access frequency of each memory + region. The information can be useful for performance-centric DRAM + level memory management. + + See https://damonitor.github.io/doc/html/latest-damon/index.html for + more information. + If unsure, say N. + endmenu diff --git a/mm/Makefile b/mm/Makefile index 6e9d46b2efc9..30c5dba52fb2 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -121,3 +121,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o +obj-$(CONFIG_DAMON) += damon.o diff --git a/mm/damon.c b/mm/damon.c new file mode 100644 index 000000000000..d446ba4bfb0a --- /dev/null +++ b/mm/damon.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Data Access Monitor + * + * Copyright 2019-2020 Amazon.com, Inc. or its affiliates. + * + * Author: SeongJae Park + * + * This file is constructed in below parts. + * + * - Functions and macros for DAMON data structures + * - Functions for the initialization + * + * The core parts are not implemented yet. + */ + +#define pr_fmt(fmt) "damon: " fmt + +#include +#include +#include +#include + +/* + * Functions and macros for DAMON data structures + */ + +#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_target(t, ctx) \ + list_for_each_entry(t, &(ctx)->targets_list, list) + +#define damon_for_each_target_safe(t, next, ctx) \ + list_for_each_entry_safe(t, next, &(ctx)->targets_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(unsigned long start, + unsigned long end) +{ + struct damon_region *region; + + region = kmalloc(sizeof(*region), GFP_KERNEL); + if (!region) + return NULL; + + region->ar.start = start; + region->ar.end = 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_target *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_target struct + * + * Returns the pointer to the new struct if success, or NULL otherwise + */ +static struct damon_target *damon_new_target(unsigned long id) +{ + struct damon_target *t; + + t = kmalloc(sizeof(*t), GFP_KERNEL); + if (!t) + return NULL; + + t->id = id; + INIT_LIST_HEAD(&t->regions_list); + + return t; +} + +static void damon_add_target(struct damon_ctx *ctx, struct damon_target *t) +{ + list_add_tail(&t->list, &ctx->targets_list); +} + +static void damon_del_target(struct damon_target *t) +{ + list_del(&t->list); +} + +static void damon_free_target(struct damon_target *t) +{ + struct damon_region *r, *next; + + damon_for_each_region_safe(r, next, t) + damon_free_region(r); + kfree(t); +} + +static void damon_destroy_target(struct damon_target *t) +{ + damon_del_target(t); + damon_free_target(t); +} + +static unsigned int nr_damon_targets(struct damon_ctx *ctx) +{ + struct damon_target *t; + unsigned int nr_targets = 0; + + damon_for_each_target(t, ctx) + nr_targets++; + + return nr_targets; +} + +static unsigned int nr_damon_regions(struct damon_target *t) +{ + struct damon_region *r; + unsigned int nr_regions = 0; + + damon_for_each_region(r, t) + nr_regions++; + + return nr_regions; +} + +/* + * Functions for the initialization + */ + +static int __init damon_init(void) +{ + return 0; +} + +module_init(damon_init); -- 2.17.1