Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1729pxu; Wed, 25 Nov 2020 11:29:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+N9pfQ0Ilu+zlbNuZoKqV58mWmxWUN21NHQoSV3GJut8l3oUWzCDQFz7pzb2tceGHV5i5 X-Received: by 2002:a17:906:358c:: with SMTP id o12mr4506163ejb.168.1606332576181; Wed, 25 Nov 2020 11:29:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606332576; cv=none; d=google.com; s=arc-20160816; b=MJgzgB81zL7xw8F2FJr7BNm+yXQ6PEFboZ99oh6/OpAPrAjmZezVOkZ4jJhB/tEg0Q suONNC9tA+tFw1vEowDMlT6QmLZG5KyRcujjgLHUtgVinUVMGCKI0iXbb8JE1vwy0Bm/ sWLLT5/kNpyTbmNF7r9zM9iPD5ByoZRlGY7AArVSyN4FIeyLYupHK1KYa1juLpK9jSgi S21rTwYaGAWGp7mxF0SRWmlSlyz+CSFH4IZU84NOaxvaLDhpyZdg78j+w/mgJdYbhwZF uAnsjRyTXlSsEr7EWzOuwYU7j7ii2MZ92zwbsmR8v5Nrrv0TbjUrhM/t4wYR0dPIvqeX KQDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=/dr1z8GLAHbqdUSLMfSORSWBf0JxpTH4/HuhobQFQYY=; b=IKjX89rR9Nz1Lyd9K/225rPr8IgsZCzCGlGMWrUJ/RSPy4TqLXcpcemmEuycBtsDUh 8pyngaTkzSUGet8/Ek3BEeszSmbro2F53JCrPDtFqt9kR6W37cBzbpf+hsHnceMGTvv7 wKOFD/D51QEK9GHCg3nksJSwwaddmCy7MQIVhtiSuTmFMABbzwcpzya6OMpwpe85AnTN Z1mXWFMtdnPKVBo7fWFMViWE2KXwcKfog+hmqBTmfJ9eUhpM9wB0ogW2JDnY8lIZv9wK pAh6P4/AytD+/I0VZ9uUfyohJjIQkOuxDqbKWe1L1l1rvyc6nUUyUo2ZTeyo3IkHIs1X UfxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Sm3hTkgo; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m7si2043087edq.548.2020.11.25.11.29.13; Wed, 25 Nov 2020 11:29:36 -0800 (PST) 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=@google.com header.s=20161025 header.b=Sm3hTkgo; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730137AbgKYPad (ORCPT + 99 others); Wed, 25 Nov 2020 10:30:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726060AbgKYPac (ORCPT ); Wed, 25 Nov 2020 10:30:32 -0500 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2175C0613D4 for ; Wed, 25 Nov 2020 07:30:31 -0800 (PST) Received: by mail-lj1-x243.google.com with SMTP id b17so2689963ljf.12 for ; Wed, 25 Nov 2020 07:30:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/dr1z8GLAHbqdUSLMfSORSWBf0JxpTH4/HuhobQFQYY=; b=Sm3hTkgoNxjF6bwnIj/sfSeKFf8MO0H8v/rfxzw1h1Ngqc6X0khN4eF2RWn+izdqMT INNAaBAMuJOsvgZkaMSZVfkSWgBmblHGDHaktgcfDAcHuLGegeAe0GJ+ZgqXbZfP2jbd M9HRZ5F3+kNriIYPfK6Xjh6GsYHBT9pricSaWlt7Kr98qP1Q9kPslG54wY2tHUX+6Yiz QoCyRIlWzYljs3mHJxDlTQmPCChKhZj/CJCfsnu2zaAFcJt/T6CmzpWaxD7je4BKu3qA D6XkVyA9pNzAkvMEP8YDY+KZtuTHH63Q45psPrcoDfKZPiqq6kQ3de8iu9sfbDhB3lz5 iPnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/dr1z8GLAHbqdUSLMfSORSWBf0JxpTH4/HuhobQFQYY=; b=khsKBA7B04Y6UoqgI18qGH8IvC++ruptv/RZtn5SK/ZRhTPnSACMxaM6G70owr7yUp 64i+7vBk0jj7GYbOWa+mrZXWz5D6tWtMZG1hyyGCIdGe56RU6jL10tKtOZEFi5/oah9N PzJ/p629SJvjWQG2vk+TlHnLv6Gkj7C50nwkzuuzp0KCDoIZEcrxj87bnLGdkyZessXn 2h6iKs876SGS52vU63If5ryxRgI3HZMaY6PukFUhJFCFrGaGp93BoOoyYohGMD6aUr19 2/4zg0PchT8uc8j+m17+DNb+XfT7ktMgo4g+Ebore41skBCZaATiOlcDeNLTcOU+jntq QfBQ== X-Gm-Message-State: AOAM530r3qbeyPj1jmyxZsN7+NamwR9EbXEW7eytHX4T+9tspI/YhiGK nWHerHkOxVFPeuwumoGPRHdXiOvJGLJ4Z6kkUQ0rIQ== X-Received: by 2002:a2e:9746:: with SMTP id f6mr1499698ljj.270.1606318229890; Wed, 25 Nov 2020 07:30:29 -0800 (PST) MIME-Version: 1.0 References: <20201020085940.13875-1-sjpark@amazon.com> <20201020085940.13875-7-sjpark@amazon.com> In-Reply-To: <20201020085940.13875-7-sjpark@amazon.com> From: Shakeel Butt Date: Wed, 25 Nov 2020 07:30:18 -0800 Message-ID: Subject: Re: [PATCH v22 06/18] mm/damon: Implement primitives for the virtual memory address spaces To: SeongJae Park Cc: Andrew Morton , SeongJae Park , Jonathan.Cameron@huawei.com, Andrea Arcangeli , acme@kernel.org, alexander.shishkin@linux.intel.com, amit@kernel.org, benh@kernel.crashing.org, brendan.d.gregg@gmail.com, Brendan Higgins , Qian Cai , Colin Ian King , Jonathan Corbet , David Hildenbrand , dwmw@amazon.com, Marco Elver , "Du, Fan" , foersleo@amazon.de, Greg Thelen , Ian Rogers , jolsa@redhat.com, "Kirill A. Shutemov" , Mark Rutland , Mel Gorman , Minchan Kim , Ingo Molnar , namhyung@kernel.org, "Peter Zijlstra (Intel)" , Randy Dunlap , Rik van Riel , David Rientjes , Steven Rostedt , Mike Rapoport , sblbir@amazon.com, Shuah Khan , sj38.park@gmail.com, snu@amazon.de, Vlastimil Babka , Vladimir Davydov , Yang Shi , Huang Ying , zgf574564920@gmail.com, linux-damon@amazon.com, Linux MM , linux-doc@vger.kernel.org, LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 20, 2020 at 2:06 AM SeongJae Park wrote: > > From: SeongJae Park > > This commit introduces a reference implementation of the address space > specific low level primitives for the virtual address space, so that > users of DAMON can easily monitor the data accesses on virtual address > spaces of specific processes by simply configuring the implementation to > be used by DAMON. > > The low level primitives for the fundamental access monitoring are > defined in two parts: > 1. Identification of the monitoring target address range for the address > space. > 2. Access check of specific address range in the target space. > > The reference implementation for the virtual address space provided by > this commit is designed as below. > > PTE Accessed-bit Based Access Check > ----------------------------------- > > The implementation uses PTE Accessed-bit for basic access checks. That > is, it clears the bit for next sampling target page and checks whether > it set again after one sampling period. This could disturb other kernel > subsystems using the Accessed bits, namely Idle page tracking and the > reclaim logic. To avoid such disturbances, DAMON makes it mutually > exclusive with Idle page tracking and uses ``PG_idle`` and ``PG_young`` > page flags to solve the conflict with the reclaim logics, as Idle page > tracking does. > > VMA-based Target Address Range Construction > ------------------------------------------- > > Only small parts in the super-huge virtual address space of the > processes are mapped to physical memory and accessed. Thus, tracking > the unmapped address regions is just wasteful. However, because DAMON > can deal with some level of noise using the adaptive regions adjustment > mechanism, tracking every mapping is not strictly required but could > even incur a high overhead in some cases. That said, too huge unmapped > areas inside the monitoring target should be removed to not take the > time for the adaptive mechanism. > > For the reason, this implementation converts the complex mappings to > three distinct regions that cover every mapped area of the address > space. Also, the two gaps between the three regions are the two biggest > unmapped areas in the given address space. The two biggest unmapped > areas would be the gap between the heap and the uppermost mmap()-ed > region, and the gap between the lowermost mmap()-ed region and the stack > in most of the cases. Because these gaps are exceptionally huge in > usual address spacees, excluding these will be sufficient to make a > reasonable trade-off. Below shows this in detail:: > > > > > (small mmap()-ed regions and munmap()-ed regions) > > > > > Signed-off-by: SeongJae Park > Reviewed-by: Leonard Foerster > --- > include/linux/damon.h | 14 + > mm/damon/Kconfig | 10 + > mm/damon/Makefile | 1 + > mm/damon/primitives.c | 582 ++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 607 insertions(+) > create mode 100644 mm/damon/primitives.c > > diff --git a/include/linux/damon.h b/include/linux/damon.h > index b8562814751e..70cc4b54212e 100644 > --- a/include/linux/damon.h > +++ b/include/linux/damon.h > @@ -238,4 +238,18 @@ int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); > > #endif /* CONFIG_DAMON */ > > +#ifdef CONFIG_DAMON_PRIMITIVES > + > +/* Reference callback implementations for virtual memory */ > +void damon_va_init_regions(struct damon_ctx *ctx); > +void damon_va_update_regions(struct damon_ctx *ctx); > +void damon_va_prepare_access_checks(struct damon_ctx *ctx); > +unsigned int damon_va_check_accesses(struct damon_ctx *ctx); > +bool damon_va_target_valid(struct damon_target *t); > +void damon_va_cleanup(struct damon_ctx *ctx); > +void damon_va_set_primitives(struct damon_ctx *ctx); > + > +#endif /* CONFIG_DAMON_PRIMITIVES */ > + > + > #endif > diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig > index d00e99ac1a15..0d2a18ddb9d8 100644 > --- a/mm/damon/Kconfig > +++ b/mm/damon/Kconfig > @@ -12,4 +12,14 @@ config DAMON > See https://damonitor.github.io/doc/html/latest-damon/index.html for > more information. > > +config DAMON_PRIMITIVES I would rather name this base on virtual address space monitoring maybe like DAMON_VMA_OPTIMIZED_MONITORING or something similar. PRIMITIVES does not seem like a good name for this. > + bool "Monitoring primitives for virtual address spaces monitoring" > + depends on DAMON && MMU && !IDLE_PAGE_TRACKING > + select PAGE_EXTENSION if !64BIT > + select PAGE_IDLE_FLAG > + help > + This builds the default data access monitoring primitives for DAMON. > + The primitives support only virtual address spaces. If this cannot > + cover your use case, you can implement and use your own primitives. > + > endmenu > diff --git a/mm/damon/Makefile b/mm/damon/Makefile > index 4fd2edb4becf..2f3235a52e5e 100644 > --- a/mm/damon/Makefile > +++ b/mm/damon/Makefile > @@ -1,3 +1,4 @@ > # SPDX-License-Identifier: GPL-2.0 > > obj-$(CONFIG_DAMON) := core.o > +obj-$(CONFIG_DAMON_PRIMITIVES) += primitives.o > diff --git a/mm/damon/primitives.c b/mm/damon/primitives.c > new file mode 100644 > index 000000000000..9b603ac0077c > --- /dev/null > +++ b/mm/damon/primitives.c Same with the filename. > @@ -0,0 +1,582 @@ > +// SPDX-License-Identifier: GPL-2.0 [snip] > +static bool damon_va_young(struct mm_struct *mm, unsigned long addr, > + unsigned long *page_sz) > +{ > + pte_t *pte = NULL; > + pmd_t *pmd = NULL; > + spinlock_t *ptl; > + bool young = false; > + > + if (follow_pte_pmd(mm, addr, NULL, &pte, &pmd, &ptl)) > + return false; > + > + *page_sz = PAGE_SIZE; > + if (pte) { > + young = pte_young(*pte); > + if (!young) > + young = !page_is_idle(pte_page(*pte)); > + pte_unmap_unlock(pte, ptl); > + return young; > + } > + > +#ifdef CONFIG_TRANSPARENT_HUGEPAGE > + young = pmd_young(*pmd); > + if (!young) > + young = !page_is_idle(pmd_page(*pmd)); > + spin_unlock(ptl); > + *page_sz = ((1UL) << HPAGE_PMD_SHIFT); > +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > + > + return young; Don't you need mmu_notifier_clear_young() here? I am still looking more into this patch