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 53B08C0502C for ; Fri, 26 Aug 2022 22:19:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5EA46B0073; Fri, 26 Aug 2022 18:19:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E7196B0074; Fri, 26 Aug 2022 18:19:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83A46940007; Fri, 26 Aug 2022 18:19:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6D0666B0073 for ; Fri, 26 Aug 2022 18:19:58 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3A75A1602A8 for ; Fri, 26 Aug 2022 22:19:58 +0000 (UTC) X-FDA: 79843162476.03.043618C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id BA2D5160035 for ; Fri, 26 Aug 2022 22:19:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661552396; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q82AOj5mgWsqghXykEygItIBdPuSqpKfW9fkWnf7Xws=; b=F+7q+raUY60p4IviQDj23X09A98l6s1Rc503xZDJgnpfOsSsByVo47ZaH0B6W36ikrbd88 skOWbPZWaLUVW3OXG409qiCniEVU5isd2JA/WkbDxYhcRApwgljTDpTZIMHBRihT3C0N9e 7NnuoZKQnwhULjUdYdGhqli3Z3Y0Hj0= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-369-7foO30FaMXG8W_z3kOrfwA-1; Fri, 26 Aug 2022 18:19:53 -0400 X-MC-Unique: 7foO30FaMXG8W_z3kOrfwA-1 Received: by mail-wm1-f72.google.com with SMTP id i7-20020a1c3b07000000b003a534ec2570so4640008wma.7 for ; Fri, 26 Aug 2022 15:19:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=Q82AOj5mgWsqghXykEygItIBdPuSqpKfW9fkWnf7Xws=; b=BTPakdmmYuO8SXTo9DoOCeZ5fgHLfIuVlZtmb6j7lOEo0CDtH87lqq1sw4SY/4dZ8f DSmFCUyvp7dIWHm+LY6Gu5qFY2EyxG/emqG24CHsgyCvJeyWl2R43xI0eoI+rlNHF9nc 0qDodBA+Ocw+b7vsZMcxhhYYOgUiSeLQ9UyGGZ4Zhm0OkrXrqi+FBLGpb63ip5AMehZm zC9jWL8bIAOALheAJtkNo3F/XHvywfq/FepGZ8Tyntv8dcfr2i+7poUqOadWytwm9XfT gLRJLABHzk7SE+YnChiWYsESAn7u7BFhJBcjehOaMsp83ht+k8MN7vNutAK47x5B+z36 vA9w== X-Gm-Message-State: ACgBeo1iuKZDXZEXOgH83TPNJMeRJqvcwtG7DxaiGFwU4T/63NQyVEoz Ikq5XHF9YXMU3pjwZfomyxMpaTMISuL+6dXVS1Ay2lZJ3kFMBXDMQfjfGZzgm8AbexBwPRoy95c CqAR0KkuHinY= X-Received: by 2002:a5d:698e:0:b0:225:72d1:94c with SMTP id g14-20020a5d698e000000b0022572d1094cmr794608wru.381.1661552392049; Fri, 26 Aug 2022 15:19:52 -0700 (PDT) X-Google-Smtp-Source: AA6agR7uezQ+j0oPcMek2XYoB9iVNLxa90U2dcSyOQ9ZNnqcMKuPPCHXv0+i1qNpul+pt30Kva2P9A== X-Received: by 2002:a5d:698e:0:b0:225:72d1:94c with SMTP id g14-20020a5d698e000000b0022572d1094cmr794585wru.381.1661552391790; Fri, 26 Aug 2022 15:19:51 -0700 (PDT) Received: from ?IPV6:2003:cb:c70c:d200:fbf4:8c3c:56fa:173d? (p200300cbc70cd200fbf48c3c56fa173d.dip0.t-ipconnect.de. [2003:cb:c70c:d200:fbf4:8c3c:56fa:173d]) by smtp.gmail.com with ESMTPSA id j2-20020a056000124200b0022537d826f3sm824156wrx.23.2022.08.26.15.19.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Aug 2022 15:19:51 -0700 (PDT) Message-ID: Date: Sat, 27 Aug 2022 00:19:49 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 To: Peter Xu Cc: Alistair Popple , linux-mm@kvack.org, akpm@linux-foundation.org, Nadav Amit , huang ying , LKML , "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , paulus@ozlabs.org, linuxppc-dev@lists.ozlabs.org, stable@vger.kernel.org, Huang Ying References: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> <8735dkeyyg.fsf@nvdebian.thelocal> <8735dj7qwb.fsf@nvdebian.thelocal> <72146725-3d70-0427-50d4-165283a5a85d@redhat.com> <140e7688-b66d-2f6d-fed8-e39da5045420@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v3 2/3] mm/migrate_device.c: Copy pte dirty bit to page In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=F+7q+raU; spf=pass (imf08.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661552397; a=rsa-sha256; cv=none; b=hjjMscSBPGhyvRblqlTm7K4O1Mz5qUVT0i39Az4okSdbzrQZ+EE9ELLAIY6mj+TpCj483/ Si04enZBGwKHUjW3whgepL6gkrdNkDdCFckxS5gFx/ZvGsLzL0sget7fCsb1+vkTQgAlz+ 6rJIk8ip4tq8ZAec5e2a/ejxVc8jvyg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661552397; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Q82AOj5mgWsqghXykEygItIBdPuSqpKfW9fkWnf7Xws=; b=LL9ye89L7pnVwfmXspTr/uhYwnZncKw3+WxOEtw89BOnrV7oIKI0pXq4RV+wxtr6BGX4RA iOubnqL1ftBMR9XFHCWPAfC8rT+irU6Z/SE08mM05aKmdhzVHsJJi47u3J35JQ8cd1b9GA 4MzGLQhDPu8u98+xNLJLO7Th+92+xyA= X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=F+7q+raU; spf=pass (imf08.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam07 X-Stat-Signature: f5cdu87xm4wi6xcx8uaf7qrshudyjsyq X-Rspamd-Queue-Id: BA2D5160035 X-HE-Tag: 1661552396-590306 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 26.08.22 23:37, Peter Xu wrote: > On Fri, Aug 26, 2022 at 06:46:02PM +0200, David Hildenbrand wrote: >> On 26.08.22 17:55, Peter Xu wrote: >>> On Fri, Aug 26, 2022 at 04:47:22PM +0200, David Hildenbrand wrote: >>>>> To me anon exclusive only shows this mm exclusively owns this page. I >>>>> didn't quickly figure out why that requires different handling on tlb >>>>> flushs. Did I perhaps miss something? >>>> >>>> GUP-fast is the magic bit, we have to make sure that we won't see new >>>> GUP pins, thus the TLB flush. >>>> >>>> include/linux/mm.h:gup_must_unshare() contains documentation. >>> >>> Hmm.. Shouldn't ptep_get_and_clear() (e.g., xchg() on x86_64) already >>> guarantees that no other process/thread will see this pte anymore >>> afterwards? >> >> You could have a GUP-fast thread that just looked up the PTE and is >> going to pin the page afterwards, after the ptep_get_and_clear() >> returned. You'll have to wait until that thread finished. > Good that we're talking about it, very helpful! If that's actually not required -- good. What I learned how GUP-fast and TLB flushes interact is the following: GUP-fast disables local interrupts. A TLB flush will send an IPI and wait until it has been processed. This implies, that once the TLB flush succeeded, that the interrupt was handled and GUP-fast cannot be running anymore. BUT, there is the new RCU variant nowadays, and the TLB flush itself should not actually perform such a sync. They merely protect the page tables from getting freed and the THP from getting split IIUC. And you're correct that that wouldn't help. > IIUC the early tlb flush won't protect concurrent fast-gup from happening, > but I think it's safe because fast-gup will check pte after pinning, so > either: > > (1) fast-gup runs before ptep_get_and_clear(), then > page_try_share_anon_rmap() will fail properly, or, > > (2) fast-gup runs during or after ptep_get_and_clear(), then fast-gup > will see that either the pte is none or changed, then it'll fail the > fast-gup itself. I think you're right and I might have managed to confuse myself with the write_protect_page() comment. I placed the gup_must_unshare() check explicitly after the "pte changed" check for this reason. So once the PTE was cleared, GUP-fast would undo any pin performed on a now-stale PTE. > >> >> Another user that relies on this interaction between GUP-fast and TLB >> flushing is for example mm/ksm.c:write_protect_page() >> >> There is a comment in there explaining the interaction a bit more detailed. >> >> Maybe we'll be able to handle this differently in the future (maybe once >> this turns out to be an actual performance problem). Unfortunately, >> mm->write_protect_seq isn't easily usable because we'd need have to make >> sure we're the exclusive writer. >> >> >> For now, it's not too complicated. For PTEs: >> * try_to_migrate_one() already uses ptep_clear_flush(). >> * try_to_unmap_one() already conditionally used ptep_clear_flush(). >> * migrate_vma_collect_pmd() was the one case that didn't use it already >> (and I wonder why it's different than try_to_migrate_one()). > > I'm not sure whether I fully get the point, but here one major difference > is all the rest handles one page, so a tlb flush alongside with the pte > clear sounds reasonable. Even if so try_to_unmap_one() was modified to use > tlb batching, but then I see that anon exclusive made that batching > conditional. I also have question there on whether we can keep using the > tlb batching even with anon exclusive pages there. > > In general, I still don't see how stall tlb could affect anon exclusive > pages on racing with fast-gup, because the only side effect of a stall tlb > is unwanted page update iiuc, the problem is fast-gup doesn't even use tlb, > afaict.. I have the gut feeling that the comment in write_protect_page() is indeed stale, and that clearing PageAnonExclusive doesn't strictly need the TLB flush. I'll try to refresh my memory if there was any other case that I had to handle over the weekend. Thanks! -- Thanks, David / dhildenb