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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AC8FC433F5 for ; Fri, 17 Dec 2021 22:44:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231264AbhLQWox (ORCPT ); Fri, 17 Dec 2021 17:44:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:52041 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230461AbhLQWow (ORCPT ); Fri, 17 Dec 2021 17:44:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639781091; 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=2l3tv70qw5U/DFH1AeqC7MLWadTCO2kcG/3tbtkmi6M=; b=htUrNS2cPU8QirVkWB2F3h21GXkJP8bTkj4VQHRxkuIEoLLZsqU4ka3aEVBmJ4s3tyyuDD 2I4ICoUxTHfacRiIA7pR0vB84a/jDmHC6GlCR62LPJkbmD7q+iBTSXZ11nNx6u3GtFcQ31 4i3C4SatbLrDSM9Y2lOnO2vj04M61mk= 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-2-eG2hoCyeOPq43d22SjNKGQ-1; Fri, 17 Dec 2021 17:43:47 -0500 X-MC-Unique: eG2hoCyeOPq43d22SjNKGQ-1 Received: by mail-wm1-f72.google.com with SMTP id 69-20020a1c0148000000b0033214e5b021so1283357wmb.3 for ; Fri, 17 Dec 2021 14:43:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=2l3tv70qw5U/DFH1AeqC7MLWadTCO2kcG/3tbtkmi6M=; b=NyAroIGmm0zhvFr+LWu6rHTSsJUt1V3aGDkqi/VlxgL6pmgEH/sfDqCEEgpU6rT/yH DYF+wJ4/I82rIbQGb8ql2pwFx84BImTpTIY++RmkDxUB/+99dNe9AQJUiCVHQIm9r0Pe VmyWjkm/K2GTBjLbrCZJ+aiE39vi4VFNVoLCj7GBjC6nNNu4BisSmuBs58km1w7y4SBa /nQiGrb848UNh4VqRkSR3w//RbIz/57eR2GgROeAwX2XTRoo9EwcM601/SN41QMS92gC p+ZoOuF1sbWBgkoQ6Qf1tD+Bn1er6VNnQG7kmhQ5IhCz3JZyc4OUavbE7jmI1y/krL84 Ld5g== X-Gm-Message-State: AOAM532w0lFDRjrg8jziLh1BnjVeP5g1Iwi0Wyv/1M67YH8lKoaBksm6 x4jH+DDix/lstu73AL0VrRM/RS7tysNwXr5xh+KG/+M2Z58M4d8aPmUfQU9Wi2OLuImZN5cc6e8 OW73m+x8u7kPOtGOGoq+3bVGV X-Received: by 2002:adf:f48e:: with SMTP id l14mr4202085wro.88.1639781026622; Fri, 17 Dec 2021 14:43:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlsG6viaLqvbuV/wNUm4ZwQ7VL/P5e2xT8/kUZ9SE40GD4CaG/NiGfqmJpgjssvi9LApgTBw== X-Received: by 2002:adf:f48e:: with SMTP id l14mr4202074wro.88.1639781026429; Fri, 17 Dec 2021 14:43:46 -0800 (PST) Received: from [192.168.3.132] (p4ff234b8.dip0.t-ipconnect.de. [79.242.52.184]) by smtp.gmail.com with ESMTPSA id p5sm8945149wrd.13.2021.12.17.14.43.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Dec 2021 14:43:46 -0800 (PST) Message-ID: Date: Fri, 17 Dec 2021 23:43:44 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Content-Language: en-US To: Linus Torvalds Cc: Linux Kernel Mailing List , Andrew Morton , Hugh Dickins , David Rientjes , Shakeel Butt , John Hubbard , Jason Gunthorpe , Mike Kravetz , Mike Rapoport , Yang Shi , "Kirill A . Shutemov" , Matthew Wilcox , Vlastimil Babka , Jann Horn , Michal Hocko , Nadav Amit , Rik van Riel , Roman Gushchin , Andrea Arcangeli , Peter Xu , Donald Dutile , Christoph Hellwig , Oleg Nesterov , Jan Kara , Linux-MM , "open list:KERNEL SELFTEST FRAMEWORK" , "open list:DOCUMENTATION" References: <20211217113049.23850-1-david@redhat.com> <20211217113049.23850-7-david@redhat.com> <9c3ba92e-9e36-75a9-9572-a08694048c1d@redhat.com> <02cf4dcf-74e8-9cbd-ffbf-8888f18a9e8a@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 06/11] mm: support GUP-triggered unsharing via FAULT_FLAG_UNSHARE (!hugetlb) In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 17.12.21 23:18, Linus Torvalds wrote: > On Fri, Dec 17, 2021 at 1:47 PM David Hildenbrand wrote: >> >> For now I have not heard a compelling argument why the mapcount is >> dubious, I repeat: >> >> * mapcount can only increase due to fork() >> * mapcount can decrease due to unmap / zap > > And to answer the "why is this dubious", let' sjust look at your > actual code that I reacted to: > > + vmf->page = vm_normal_page(vmf->vma, vmf->address, vmf->orig_pte); > + if (vmf->page && PageAnon(vmf->page) && !PageKsm(vmf->page) && > + page_mapcount(vmf->page) > 1) { > > Note how you don't just check page_mapcount(). Why not? Because > mapcount is completely immaterial if it's not a PageAnon page, so you > test for that. > > So even when you do the mapcount read as one atomic thing, it's one > atomic thing that depends on _other_ things, and all these checks are > not atomic. > > But a PageAnon() page can actually become a swap-backed page, and as > far as I can tell, your code doesn't have any locking to protect > against that. The pages stay PageAnon(). swap-backed pages simply set a bit IIRC. mapcount still applies. > > So now you need not only the mmap_sem (to protect against fork), you > also need the page lock (to protect against rmap changing the type of > page). No, I don't think so. But I'm happy to be proven wrong because I might just be missing something important. > > I don't see you taking the page lock anywhere. Maybe the page table > lock ends up serializing sufficiently with the rmap code that it ends > up working > > In the do_wp_page() path, we currently do those kinds of racy checks > too, but then we do a trylock_page, and re-do them. And at any time > there is any question about things, we fall back to copying - because > a copy is always safe. Yes, I studied that code in detail as well. > > Well, it's always safe if we have the rule that "once we've pinned > things, we don't cause them to be COW again". We should also be handling FOLL_GET, but that's a completely different discussion. > > But that "it's safe if" was exactly my (b) case. > > That's why I much prefer the model I'm trying to push - it's > conceptually quite simple. I can literally explain mine at a > conceptual level with that "break pre-existing COW, make sure no > future COW" model. :) We really might be talking about the same thing just that my point is that the mapcount is the right thing to use for making the discussion whether to break COW -> triger unsharing. > > In contrast, I look at your page_mapcount() code, and I go "there is > no conceptual rules here, and the actual implementation details look > dodgy". > > I personally like having clear conceptual rules - as opposed to random > implementation details. Oh, don't get me wrong, me to. But for me it just all makes perfect. What we document is: "The fault is an unsharing request to unshare a shared anonymous page (-> mapped R/O). Does not apply to KSM." And the code checks for exactly that. And in that context the mapcount just expresses exactly what we want. Again, unless I am missing something important that you raise above. Anyhow, it's late in Germany. thanks for the discussion Linus! -- Thanks, David / dhildenb