Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp1129651pxa; Fri, 28 Aug 2020 04:42:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyGUXEF/ZY/ekKJmiJVPtDe8uoLcE6JB88A9Be7kJaIq9RduUEYmOlYl51mVZ3x08QdI6uR X-Received: by 2002:a17:906:3acb:: with SMTP id z11mr1271161ejd.530.1598614976344; Fri, 28 Aug 2020 04:42:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598614976; cv=none; d=google.com; s=arc-20160816; b=cxUuHKv3ygc4LewRpp0bANWWu6g2gRWVXlmjsVyU/bPdcZgpZW8k9puSl7VhOtS3VI /Ct7aqBOdYpw3TxDKUXYyt4U9NpBwwyTBd8pdgpipQ1eAZA0sLmqZiJWw+bo0Cxtz8Wy OyX+JtsdJlpPoWhwz1uBulisdXXernWGjAWjlmlfaE4U4Xyl4FZiW+KLnhpUGE5VXDD5 gv9TOUg0QjMX8MwnMywKXkLjwudSXgxxFRMd0ITgEDuyQn43T3qSYKvSSXTge/LNF4PK N+vyqxngo7Cjlf+23WIIhiXRbyzO4Kh+tVM8RzG9vpK15AXj+sBOKIfB6/liG1jQRGcJ T/qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=qpu0feKwOcymvYtWLTKyhyFtGGbT1hSlzhSdO90X9SQ=; b=IgdmKyMLDOdIxmWHDARl2GOdeH3vqHS/Hr19tcIifcbSR0+2sceYOsK+QrvFkCKsnH sNHS7qp9nezDvo/zvpHJQMOokyynBETluBgss6+SRkuERnATraFWGgTmMrfzuUSeRrzy Pu9OnLhNc+5Lt8BwvvmmdWVoW8dpS/FQ3UqUYp6QsX+v1g8TV04efTKiW/mWinkTMjjf qjqo1K9krd3E0XRxUJJ06lO6S4X/AsWvLw34FtJHzOWZ3aHUVT+pGIiVaCOMsOwou36v XeOwwYUzb/R5hXMDKLtvN+zsqZF3CIros3c2XREk5H6ch/oIWYkiInRQFVnNpsc3qx8C DdqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=O65CFHwL; 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 b4si408230edv.367.2020.08.28.04.42.33; Fri, 28 Aug 2020 04:42:56 -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=O65CFHwL; 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 S1729285AbgH1Lkn (ORCPT + 99 others); Fri, 28 Aug 2020 07:40:43 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:14312 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729280AbgH1Lk0 (ORCPT ); Fri, 28 Aug 2020 07:40:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1598614825; x=1630150825; h=from:to:cc:subject:date:message-id:in-reply-to: mime-version; bh=qpu0feKwOcymvYtWLTKyhyFtGGbT1hSlzhSdO90X9SQ=; b=O65CFHwLn+OpUEeTNuZX2XKH+XnL5SFCc+WgKZ7lJ0JDBgI9R8HmoZJa 8J6oCU5bUdTLdlmZ580u6va2+WGZFUNPoowU9PQb3310SEWznWa9pGAqn 10ra+ELqx5auRiN4JJRmzRLrHpzpLOUP9VlO6XzIhhx3aAO5AqxI15upJ 8=; 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) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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