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 62D09C433EF for ; Fri, 17 Dec 2021 21:47:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E526E6B0071; Fri, 17 Dec 2021 16:47:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E016C6B0073; Fri, 17 Dec 2021 16:47:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7BF56B0074; Fri, 17 Dec 2021 16:47:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0200.hostedemail.com [216.40.44.200]) by kanga.kvack.org (Postfix) with ESMTP id B5B7A6B0071 for ; Fri, 17 Dec 2021 16:47:35 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 7CE658911A for ; Fri, 17 Dec 2021 21:47:25 +0000 (UTC) X-FDA: 78928622850.07.04B71B0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id CF6A4A0035 for ; Fri, 17 Dec 2021 21:47:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639777644; 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=WvXlhuvoyqQ/nEvi2sE+ZfIFkcm6bbvZ5PcvxRE9QYk=; b=Qrjv6qDESi1Iqaftjz0kXcaoElfVy8IUnz6sl38/zVanEeqyC6ZLXtiy0ow7bfGarwLz8b /gqEoCZBvQ4YdCqaDphgL5WSRIOBgRGTuU6Gsx0t7qnfli2p0UZ9tkJ3Sk+sUmNuE6kZAz SH3T8hvDwY2IrpYplp21SJBlzFTfsNU= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-637-9H_YHiNXNLmLdOKAflcItQ-1; Fri, 17 Dec 2021 16:47:23 -0500 X-MC-Unique: 9H_YHiNXNLmLdOKAflcItQ-1 Received: by mail-wm1-f71.google.com with SMTP id ay34-20020a05600c1e2200b00337fd217772so1633173wmb.4 for ; Fri, 17 Dec 2021 13:47:23 -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=WvXlhuvoyqQ/nEvi2sE+ZfIFkcm6bbvZ5PcvxRE9QYk=; b=Y/6M1gffzgEuumYcVWKKhzqXU7Xy9sHYZrjtIzZdnUG7/BXAJYY2W+lbS++bMk0rfm Mkp3i9HK4gQry5JGIUlsUWoRt5aYLx5fDpu7c5Y8ZTtejETlCTsZiKeVGHYz18mXMOg4 K5sBBFPxlWM2iv8TkHnUGT5f8mn3kGBOIqAXXWe/N8jw1yGeF13KSeA6GEcdq+IwQIkX RlAppsqTdTQqT+FQW4ImQTsOkSyg2F9pbRY3mVnm9b7DjQL4TMAWHUOIACu5a3F/GkwK 4JqM3ogi8OwSuGX1s4ERS1RQGOHlDHDrKGkubjYRuWFpBjohnFUxmk+Ysp2Ft658yRt7 VQtg== X-Gm-Message-State: AOAM532ijBJnw/OCJQ5Fh9sK000TIrybKk9z3pbF4P0hqq/dQLUY9wG5 gxOJxPx887qtJwCasJChlutzGSmpG1vUzibxeo5vSPmgrRruwtoty0JkoZF8jy+kWTjlNkbreTh wDdNIij3JPY0= X-Received: by 2002:a05:600c:1e1c:: with SMTP id ay28mr11017077wmb.131.1639777642079; Fri, 17 Dec 2021 13:47:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJxJedX+I/4u/x6GDRH9l8IWq6Pzbh4I8I+fvWisIgceyydoV9yQ3TNEm9r2dxERxeBN0DVJVA== X-Received: by 2002:a05:600c:1e1c:: with SMTP id ay28mr11017038wmb.131.1639777641748; Fri, 17 Dec 2021 13:47:21 -0800 (PST) Received: from [192.168.3.132] (p4ff234b8.dip0.t-ipconnect.de. [79.242.52.184]) by smtp.gmail.com with ESMTPSA id n24sm8313296wms.45.2021.12.17.13.47.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Dec 2021 13:47:21 -0800 (PST) Message-ID: <02cf4dcf-74e8-9cbd-ffbf-8888f18a9e8a@redhat.com> Date: Fri, 17 Dec 2021 22:47:20 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 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> 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: 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 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Qrjv6qDE; spf=none (imf25.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CF6A4A0035 X-Stat-Signature: kofwy4fy3sx8crb7q6bjjuj1hrkndpxs X-HE-Tag: 1639777638-230654 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 17.12.21 22:36, Linus Torvalds wrote: > On Fri, Dec 17, 2021 at 12:55 PM David Hildenbrand wrote: >> >> If we have a shared anonymous page we cannot have GUP references, not >> even R/O ones. Because GUP would have unshared and copied the page, >> resulting in a R/O mapped anonymous page. > > Doing a GUP on an actual shared page is wrong to begin with. > > You even know that, you try to use "page_mapcount() > 1" to disallow it. GUP is incomaptible with shared anonymous pages, therefore it has to trigger unsharing, correct. > > My point is that it's wrong regardless, and that "mapcount" is > dubious, and that COW cannot - and must not - use mapcount, and that I > think your shared case should strive to avoid it for the exact same > reason. 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 We can protect from the transtition == 1 -> >1 using the mmap_lock. For COW the mapcount is the only thing that matters *if we take GUP* out of the equation. And that's exactly what we OTOH, take a look which issues resulted from the page_count changes. That's what I call dubious, sorry to say. > > So, what I think should happen is: > > (a) GUP makes sure that it only ever looks up pages that can be > shared with this VM. This may in involve breaking COW early with any > past fork(). Is that unsharing as we propose it? > > (b) it marks such pages so that any future work will not cause them > to COW either Right, exactly. GUP before fork does not result in a page getting shared again. > > Note that (a) is not necessarily "always COW and have to allocate and > copy new page". In particular, if the page is already writable, you > know you already have exclusive access to it and don't need to COW. > > And if it isn't writable, then the other common case is "the cow has > only one user, and it's us" - that's the "refcount == 1" case. > > And (b) is what we do with that page_maybe_dma_pinned() logic for > fork(), but also for things like swap cache creation (eg see commit > feb889fb40fa: "mm: don't put pinned pages into the swap cache"). I fully agree with b). GUP before fork is a totally different set of problems than GUP after fork. > > Note that this code all already exists, and already works - even > without getting the (very expensive) mmap_sem. So it works with > fast-GUP and it can race with concurrent forking by another thread, > which is why we also have that seqcount thing. I know, I studied it intensively :) > > As far as I can tell, your "mapcount" logic fundamentally requires > mmap_sem for the fork() race avoidance, for example. Yes. Or any other more lightweight synchronization in the future. For now this is just perfect. > > So this is why I don't like the mapcount games - I think they are very > fragile, and not at all as logical as the two simple rules a/b above. I don't really see anything fragile, really. I'm happy to learn as always. > > I believe you can make mapcount games _work_ - we used to have > something like that. It was incredibly fragile, and it had its own set > of bugs, but with enough care it's doable. We made it work, and it was comparatively simple. -- Thanks, David / dhildenb