Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C2A3C433E2 for ; Fri, 28 Aug 2020 11:40:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EE8C620872 for ; Fri, 28 Aug 2020 11:40:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="TF+4FLih" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EE8C620872 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 95FEF6B0007; Fri, 28 Aug 2020 07:40:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EA4D6B0008; Fri, 28 Aug 2020 07:40:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B0BC6B000A; Fri, 28 Aug 2020 07:40:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id 622106B0007 for ; Fri, 28 Aug 2020 07:40:11 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1F302824556B for ; Fri, 28 Aug 2020 11:40:11 +0000 (UTC) X-FDA: 77199783822.27.kick41_0f16e8e27075 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id DF6283D663 for ; Fri, 28 Aug 2020 11:40:10 +0000 (UTC) X-HE-Tag: kick41_0f16e8e27075 X-Filterd-Recvd-Size: 7799 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Fri, 28 Aug 2020 11:40:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1598614810; x=1630150810; h=from:to:cc:subject:date:message-id:in-reply-to: mime-version; bh=qpu0feKwOcymvYtWLTKyhyFtGGbT1hSlzhSdO90X9SQ=; b=TF+4FLih635jj8c9IZYaLJ87CG8NGd5Pfgt37C6koCwDCA+XoYtOVveO EfkufR8lDVxTkFGCPPgH/h+OGv2PGaO/EVG7muDw+xWhs2uH9i9LpxTCQ kgEG6Fc6NM29R2wJIpGmUS0UjJxWrfWb33f5RikftQtIHB0BV4fAzAt3W 4=; X-IronPort-AV: E=Sophos;i="5.76,363,1592870400"; d="scan'208";a="50525029" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1d-37fd6b3d.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 28 Aug 2020 11:40:09 +0000 Received: from EX13D31EUB001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-37fd6b3d.us-east-1.amazon.com (Postfix) with ESMTPS id EC561282E6F; Fri, 28 Aug 2020 11:39:57 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.161.244) by EX13D31EUB001.ant.amazon.com (10.43.166.210) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 28 Aug 2020 11:39:37 +0000 From: SeongJae Park To: Alkaid CC: , SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , SeongJae Park Subject: Re: [RFC v7 06/10] mm/damon: Implement callbacks for physical memory monitoring Date: Fri, 28 Aug 2020 13:39:21 +0200 Message-ID: <20200828113921.15279-1-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200828095315.27210-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.244] X-ClientProxiedBy: EX13D40UWC004.ant.amazon.com (10.43.162.175) To EX13D31EUB001.ant.amazon.com (10.43.166.210) X-Rspamd-Queue-Id: DF6283D663 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, 28 Aug 2020 11:53:15 +0200 SeongJae Park wrote: > On Fri, 28 Aug 2020 04:11:56 -0400 Alkaid wrote: > > > > > [-- Attachment #1: Type: text/plain, Size: 2677 bytes --] > > > > Hi SeongJae, > > > > I think there are potential memory leaks in the following execution paths > > Agreed, definitely memory leaks exists. Thank you for let me know this! I > will post a patch for this soon. And, below is the patch. The complete tree is available at: https://github.com/sjp38/linux/tree/damon/next Thanks, SeongJae Park ==================================== >8 ======================================= From 8f605d807c55b536ab5b0f87306ac78033dc4499 Mon Sep 17 00:00:00 2001 From: SeongJae Park Date: Fri, 28 Aug 2020 11:29:30 +0000 Subject: [PATCH] mm/damon/paddr: Add missed 'put_page()' calls Exceptional cases handlings in 'damon_phys_mkold()' and 'damon_phys_young()' doesn't properly put pages. This commit fixes the problem by adding the 'put_page()' call. Reported-by: Alkaid Signed-off-by: SeongJae Park --- mm/damon.c | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/mm/damon.c b/mm/damon.c index d0d55656553b..74a10ea54958 100644 --- a/mm/damon.c +++ b/mm/damon.c @@ -836,12 +836,16 @@ unsigned int kdamond_check_vm_accesses(struct damon_ctx *ctx) /* access check functions for physical address based regions */ /* - * Get a page by pfn if it is in the LRU list. Otherwise, returns NULL. + * Get a page by @pfn if it is in the LRU list and mapped. If the page needs + * locked, do the lock and save the result in @locked. Otherwise, returns + * NULL. * - * The body of this function is stollen from the 'page_idle_get_page()'. We - * steal rather than reuse it because the code is quite simple . + * The body of this function is mostly stollen from the 'page_idle_get_page()' + * and 'page_idle_clear_pte_refs()'. We steal rather than reuse it not because + * we are great artists but the code is quite simple and we need to unify parts + * of the two functions. */ -static struct page *damon_phys_get_page(unsigned long pfn) +static struct page *damon_phys_get_page(unsigned long pfn, bool *locked) { struct page *page = pfn_to_online_page(pfn); pg_data_t *pgdat; @@ -854,9 +858,22 @@ static struct page *damon_phys_get_page(unsigned long pfn) spin_lock_irq(&pgdat->lru_lock); if (unlikely(!PageLRU(page))) { put_page(page); - page = NULL; + spin_unlock_irq(&pgdat->lru_lock); + return NULL; } spin_unlock_irq(&pgdat->lru_lock); + + if (!page_mapped(page) || !page_rmapping(page)) { + put_page(page); + return NULL; + } + + *locked = !PageAnon(page) || PageKsm(page); + if (*locked && !trylock_page(page)) { + put_page(page); + return NULL; + } + return page; } @@ -869,26 +886,19 @@ static bool damon_page_mkold(struct page *page, struct vm_area_struct *vma, static void damon_phys_mkold(unsigned long paddr) { - struct page *page = damon_phys_get_page(PHYS_PFN(paddr)); + bool locked; + struct page *page = damon_phys_get_page(PHYS_PFN(paddr), &locked); struct rmap_walk_control rwc = { .rmap_one = damon_page_mkold, .anon_lock = page_lock_anon_vma_read, }; - bool need_lock; if (!page) return; - if (!page_mapped(page) || !page_rmapping(page)) - return; - - need_lock = !PageAnon(page) || PageKsm(page); - if (need_lock && !trylock_page(page)) - return; - rmap_walk(page, &rwc); - if (need_lock) + if (locked) unlock_page(page); put_page(page); } @@ -930,7 +940,8 @@ static bool damon_page_accessed(struct page *page, struct vm_area_struct *vma, static bool damon_phys_young(unsigned long paddr, unsigned long *page_sz) { - struct page *page = damon_phys_get_page(PHYS_PFN(paddr)); + bool locked; + struct page *page = damon_phys_get_page(PHYS_PFN(paddr), &locked); struct damon_phys_access_chk_result result = { .page_sz = PAGE_SIZE, .accessed = false, @@ -940,21 +951,13 @@ static bool damon_phys_young(unsigned long paddr, unsigned long *page_sz) .rmap_one = damon_page_accessed, .anon_lock = page_lock_anon_vma_read, }; - bool need_lock; if (!page) return false; - if (!page_mapped(page) || !page_rmapping(page)) - return false; - - need_lock = !PageAnon(page) || PageKsm(page); - if (need_lock && !trylock_page(page)) - return false; - rmap_walk(page, &rwc); - if (need_lock) + if (locked) unlock_page(page); put_page(page); -- 2.17.1