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 E2771ECAAD1 for ; Fri, 26 Aug 2022 00:56:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F59E6B0075; Thu, 25 Aug 2022 20:56:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A5606B0078; Thu, 25 Aug 2022 20:56:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56FA96B007B; Thu, 25 Aug 2022 20:56:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4738A6B0075 for ; Thu, 25 Aug 2022 20:56:17 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 21D1B1C6049 for ; Fri, 26 Aug 2022 00:56:17 +0000 (UTC) X-FDA: 79839927594.24.2DA9999 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf27.hostedemail.com (Postfix) with ESMTP id C40F94000E for ; Fri, 26 Aug 2022 00:56:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661475375; x=1693011375; h=from:to:cc:subject:references:date:in-reply-to: message-id:mime-version; bh=eHgZdT1se+5eBqgsr/sRIR9P3jAvQggbXOvU+BKI2EI=; b=badqe3n6l6JulGINjcg/FOoOEeoH2xUrrnm5RzKJnmA0FJNAvPOTeHD1 glmZLpzeSCGxyvaR6W/7U9Bcq8chlr1MpdYvzla5ek0Jg1AXJEiYQ5I75 gFkCLqO025AhQn7EPMQh4fxKHzNnY6wotgZAM5tUYEXJnZql8ejuBIkVr rYlywUayCwgk1jSEIppqH16bbTvjW7v7yJZgfDY33oHux9dhqnUzhET34 wmWNnCuU7cCpci9Y7rUf08bCFdco6IUY5Bh8DffH8OeDNYfvi9Vau/cK6 ehidPx71PHvyIgq6dgtcxeTnEZ0oWEfpexS3hYPKlvSU/AM1jXcVFy+ii A==; X-IronPort-AV: E=McAfee;i="6500,9779,10450"; a="380683856" X-IronPort-AV: E=Sophos;i="5.93,264,1654585200"; d="scan'208";a="380683856" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2022 17:56:11 -0700 X-IronPort-AV: E=Sophos;i="5.93,264,1654585200"; d="scan'208";a="587107455" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2022 17:56:06 -0700 From: "Huang, Ying" To: Alistair Popple Cc: , , Peter Xu , Nadav Amit , huang ying , LKML , "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , David Hildenbrand , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , , , Subject: Re: [PATCH v3 1/3] mm/migrate_device.c: Flush TLB while holding PTL References: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> <87sfll2jfc.fsf@yhuang6-desk2.ccr.corp.intel.com> <87y1vcdjzs.fsf@nvdebian.thelocal> Date: Fri, 26 Aug 2022 08:56:04 +0800 In-Reply-To: <87y1vcdjzs.fsf@nvdebian.thelocal> (Alistair Popple's message of "Fri, 26 Aug 2022 08:35:17 +1000") Message-ID: <87fshj3jrv.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=badqe3n6; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661475376; a=rsa-sha256; cv=none; b=t1kTGoYfbqKdU50QMnBL/s3FtTp8WK43qMI7YE/B8FcPJagMY9wTO+V4H1RUbVUFU8wNse QdMnQf1GmBlumRrOLZ7XeQiUPDHHYfyl29kDgw11uvcGp+pnvGnKAXU05fGAY70MKNrKL7 yWRg0+MzvNE47wCitH8XOiPwJZYQUFk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661475376; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3/Q0ihXo98z2VzSDQQy7WnGGEWY4tckWa1nQVijyvzg=; b=oV9GK5BQiQXCrx5sT6X8p9NnQNiJhiWycd/BBFXRns1QBX8QvTxyuOED14l8ird1S8Qq5F yvLZwsmXxR6jiZsAZScXH8W8QGuGSzE0sepEBRpn1+UH1ch0HY+61lE9azclxBZSNfPzdk 26Bvg2EeK3+lrYYmtaZFdYIaip6UdK4= X-Stat-Signature: 5pmitg73aaan8an769sfnwuxxr9gojys Authentication-Results: imf27.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=badqe3n6; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf27.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=ying.huang@intel.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C40F94000E X-Rspam-User: X-HE-Tag: 1661475375-977973 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: Alistair Popple writes: > "Huang, Ying" writes: > >> Alistair Popple writes: >> >>> When clearing a PTE the TLB should be flushed whilst still holding the >>> PTL to avoid a potential race with madvise/munmap/etc. For example >>> consider the following sequence: >>> >>> CPU0 CPU1 >>> ---- ---- >>> >>> migrate_vma_collect_pmd() >>> pte_unmap_unlock() >>> madvise(MADV_DONTNEED) >>> -> zap_pte_range() >>> pte_offset_map_lock() >>> [ PTE not present, TLB not flushed ] >>> pte_unmap_unlock() >>> [ page is still accessible via stale TLB ] >>> flush_tlb_range() >>> >>> In this case the page may still be accessed via the stale TLB entry >>> after madvise returns. Fix this by flushing the TLB while holding the >>> PTL. >>> >>> Signed-off-by: Alistair Popple >>> Reported-by: Nadav Amit >>> Fixes: 8c3328f1f36a ("mm/migrate: migrate_vma() unmap page from vma while collecting pages") >>> Cc: stable@vger.kernel.org >>> >>> --- >>> >>> Changes for v3: >>> >>> - New for v3 >>> --- >>> mm/migrate_device.c | 5 +++-- >>> 1 file changed, 3 insertions(+), 2 deletions(-) >>> >>> diff --git a/mm/migrate_device.c b/mm/migrate_device.c >>> index 27fb37d..6a5ef9f 100644 >>> --- a/mm/migrate_device.c >>> +++ b/mm/migrate_device.c >>> @@ -254,13 +254,14 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, >>> migrate->dst[migrate->npages] = 0; >>> migrate->src[migrate->npages++] = mpfn; >>> } >>> - arch_leave_lazy_mmu_mode(); >>> - pte_unmap_unlock(ptep - 1, ptl); >>> >>> /* Only flush the TLB if we actually modified any entries */ >>> if (unmapped) >>> flush_tlb_range(walk->vma, start, end); >> >> It appears that we can increase "unmapped" only if ptep_get_and_clear() >> is used? > > In other words you mean we only need to increase unmapped if pte_present > && !anon_exclusive? > > Agree, that's a good optimisation to make. However I'm just trying to > solve a data corruption issue (not dirtying the page) here, so will post > that as a separate optimisation patch. Thanks. OK. Then the patch looks good to me. Feel free to add my, Reviewed-by: "Huang, Ying" Best Regards, Huang, Ying >> >>> + arch_leave_lazy_mmu_mode(); >>> + pte_unmap_unlock(ptep - 1, ptl); >>> + >>> return 0; >>> } >>> >>> >>> base-commit: ffcf9c5700e49c0aee42dcba9a12ba21338e8136