From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5CDBC433FE for ; Thu, 3 Feb 2022 22:12:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D34536B0071; Thu, 3 Feb 2022 17:12:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CBC5A6B0072; Thu, 3 Feb 2022 17:12:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B37BC6B0074; Thu, 3 Feb 2022 17:12:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0202.hostedemail.com [216.40.44.202]) by kanga.kvack.org (Postfix) with ESMTP id A10FE6B0071 for ; Thu, 3 Feb 2022 17:12:30 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 66D9C95CBF for ; Thu, 3 Feb 2022 22:12:30 +0000 (UTC) X-FDA: 79102868460.18.5188376 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf24.hostedemail.com (Postfix) with ESMTP id D53F2180008 for ; Thu, 3 Feb 2022 22:12:29 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 19B29B835AD; Thu, 3 Feb 2022 22:12:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A46C8C340E8; Thu, 3 Feb 2022 22:12:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1643926346; bh=revw8UU7WFNKrOaFkz6WeTO23/dbXftr0dSdennufEU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=nsH11JP3yUbjj2MS/VR9+/Dq9JnXhVensr12oyT0HBBQDSJlQRFFcwcYNLD5wQUR3 ZPPo2yeT3VvLOa+lzwyRTsjjDVdeYbgQHp0MzwRRSZVqdSFmsOkl1FifJGRgPNIHlE oMVWanLVkSnb2E9aesTO468MS19Iy4YrEWdw3KTs= Date: Thu, 3 Feb 2022 14:12:26 -0800 From: Andrew Morton To: Yang Shi Cc: kirill.shutemov@linux.intel.com, jannh@google.com, willy@infradead.org, david@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [v4 PATCH] fs/proc: task_mmu.c: don't read mapcount for migration entry Message-Id: <20220203141226.d510a9fe3fb1f55fc75926e5@linux-foundation.org> In-Reply-To: <20220203182641.824731-1-shy828301@gmail.com> References: <20220203182641.824731-1-shy828301@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=nsH11JP3; spf=pass (imf24.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Rspam-User: nil X-Rspamd-Queue-Id: D53F2180008 X-Stat-Signature: bpkij9e8gynehsq58kbna9rkzsbpqqmp X-Rspamd-Server: rspam12 X-HE-Tag: 1643926349-79144 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 Thu, 3 Feb 2022 10:26:41 -0800 Yang Shi wrote: > v4: * s/Treated/Treat per David > * Collected acked-by tag from David > v3: * Fixed the fix tag, the one used by v2 was not accurate > * Added comment about the risk calling page_mapcount() per David > * Fix pagemap > v2: * Added proper fix tag per Jann Horn > * Rebased to the latest linus's tree The v2->v4 delta shows changes which aren't described above? --- a/fs/proc/task_mmu.c~fs-proc-task_mmuc-dont-read-mapcount-for-migration-entry-v4 +++ a/fs/proc/task_mmu.c @@ -469,9 +469,12 @@ static void smaps_account(struct mem_siz * If any subpage of the compound page mapped with PTE it would elevate * page_count(). * - * Treated regular migration entries as mapcount == 1 without reading - * mapcount since calling page_mapcount() for migration entries is - * racy against THP splitting. + * The page_mapcount() is called to get a snapshot of the mapcount. + * Without holding the page lock this snapshot can be slightly wrong as + * we cannot always read the mapcount atomically. It is not safe to + * call page_mapcount() even with PTL held if the page is not mapped, + * especially for migration entries. Treat regular migration entries + * as mapcount == 1. */ if ((page_count(page) == 1) || migration) { smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty, @@ -1393,6 +1396,7 @@ static pagemap_entry_t pte_to_pagemap_en { u64 frame = 0, flags = 0; struct page *page = NULL; + bool migration = false; if (pte_present(pte)) { if (pm->show_pfn) @@ -1414,13 +1418,14 @@ static pagemap_entry_t pte_to_pagemap_en frame = swp_type(entry) | (swp_offset(entry) << MAX_SWAPFILES_SHIFT); flags |= PM_SWAP; + migration = is_migration_entry(entry); if (is_pfn_swap_entry(entry)) page = pfn_swap_entry_to_page(entry); } if (page && !PageAnon(page)) flags |= PM_FILE; - if (page && page_mapcount(page) == 1) + if (page && !migration && page_mapcount(page) == 1) flags |= PM_MMAP_EXCLUSIVE; if (vma->vm_flags & VM_SOFTDIRTY) flags |= PM_SOFT_DIRTY; @@ -1436,6 +1441,7 @@ static int pagemap_pmd_range(pmd_t *pmdp spinlock_t *ptl; pte_t *pte, *orig_pte; int err = 0; + bool migration = false; #ifdef CONFIG_TRANSPARENT_HUGEPAGE ptl = pmd_trans_huge_lock(pmdp, vma); @@ -1476,11 +1482,12 @@ static int pagemap_pmd_range(pmd_t *pmdp if (pmd_swp_uffd_wp(pmd)) flags |= PM_UFFD_WP; VM_BUG_ON(!is_pmd_migration_entry(pmd)); + migration = is_migration_entry(entry); page = pfn_swap_entry_to_page(entry); } #endif - if (page && page_mapcount(page) == 1) + if (page && !migration && page_mapcount(page) == 1) flags |= PM_MMAP_EXCLUSIVE; for (; addr != end; addr += PAGE_SIZE) { _