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 0C693C433F5 for ; Fri, 17 Dec 2021 22:18:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229980AbhLQWSb (ORCPT ); Fri, 17 Dec 2021 17:18:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbhLQWSa (ORCPT ); Fri, 17 Dec 2021 17:18:30 -0500 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59F02C061574 for ; Fri, 17 Dec 2021 14:18:29 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id x15so13252319edv.1 for ; Fri, 17 Dec 2021 14:18:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=5dEiolkPpvvhLwx2+Ioc9tJdHpFervZFA65QfTIT8vY=; b=UNpQI26wi57RyV1YxwSTRqGoF8pfqkrpBA7VJxkw1ZxwY2/GrWansyaJMvj0VZ+zMR 214ZaRKvpcMDVsjVSqCzfpGWodtPb3EbTzuTia7DZiYsm/Guy70EmsRf0kdKbipswVN4 hCq+6iDJno8XBTK4ljnI1AIK0LnvRqZevuKic= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=5dEiolkPpvvhLwx2+Ioc9tJdHpFervZFA65QfTIT8vY=; b=sdzI4BdChoGQ2PPdRdLHdXQ3oFhaDO0nd8xqL/Jj9nMvxuffrD1W4cR09P8itLZ0Hh V9ga0wfesRHHrRgYz1NdH5YhNELzghAElPJ9Jk6cl3P6FRz332fcG/Dxr2IIVO4v7jBN KfqF/LIWK8zkrgnOkITiAPXymgJeQ3J1mA5JgH54KwFM5weE+mAhEHfPWLQeb6sZxbw0 zuGWP8S5diUvdQc3pZZV8uRlNLPF9YaDDQbKtTreef0Z3cbY9VK/vO30iAfRmsJ8rbCH wu348lVNNWAizNqAPdQiV1zZ1065KBBAE5Cgi3t2o3l8TwlmhChXlM+XAiUOKkUDVTTd 6I8Q== X-Gm-Message-State: AOAM53125StevTnUjAmOFcskfwOMJGF99w5c9QFypzMveOJ32HfV8NYE ss+I6z1Tvw9yDnDBwpcP34drM/vSsM+a6HGieq8= X-Google-Smtp-Source: ABdhPJx1pGIfeiuXXmEYJE2laCaPaBSoXPrjw68p37zPZTU9dMtYT3LI1+PMW0atiexGufelJ+sw/Q== X-Received: by 2002:a05:6402:2756:: with SMTP id z22mr4716493edd.200.1639779507745; Fri, 17 Dec 2021 14:18:27 -0800 (PST) Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com. [209.85.221.45]) by smtp.gmail.com with ESMTPSA id nd22sm3205235ejc.91.2021.12.17.14.18.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Dec 2021 14:18:26 -0800 (PST) Received: by mail-wr1-f45.google.com with SMTP id s1so6643371wrg.1 for ; Fri, 17 Dec 2021 14:18:26 -0800 (PST) X-Received: by 2002:adf:f54e:: with SMTP id j14mr4057992wrp.442.1639779505799; Fri, 17 Dec 2021 14:18:25 -0800 (PST) MIME-Version: 1.0 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> In-Reply-To: <02cf4dcf-74e8-9cbd-ffbf-8888f18a9e8a@redhat.com> From: Linus Torvalds Date: Fri, 17 Dec 2021 14:18:09 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 06/11] mm: support GUP-triggered unsharing via FAULT_FLAG_UNSHARE (!hugetlb) To: David Hildenbrand 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" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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). 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. 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". 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. 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. Linus