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 A2555C77B78 for ; Tue, 2 May 2023 18:53:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35C586B0081; Tue, 2 May 2023 14:53:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 30B926B0082; Tue, 2 May 2023 14:53:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D3796B0083; Tue, 2 May 2023 14:53:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by kanga.kvack.org (Postfix) with ESMTP id BDBD96B0081 for ; Tue, 2 May 2023 14:53:32 -0400 (EDT) Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-3f178da21b5so27542835e9.3 for ; Tue, 02 May 2023 11:53:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683053612; x=1685645612; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=TKK6aVpjuVU0zocvvzx2NbMr8G38Leo7ZXygkcERGR4=; b=jT2Ze1BFSPANcjCx4YcSDsjU1FiI4f5SmDTIL5/oTgcpjUvLjtzuLvRH05Bo5BUlYA qVEKklhBVPjmFaFDEMRVR+ue7Gtso382k3buRqEG6lWw87eXFHlSWfUFyjMyUW9R0MWH 4xmD3hAU3EhxU5P6CIAoSu9SCbn+dxxiD6eq3TCe1ufBZhHEzauGYyXQJqZLvo6j6W80 ls4t0c6POelCUOF3mcMCzYXkWAf0cF1oKxZ5TO203hnDDNYN1Lcnqs4OdSnV5pG1yVks k+tMkFqvqzWwkX0nAqGkIZEQZ1fkoGmZi8S9B0ZmI1rfitvHnxspT7dy6bGjbUYqceuN GCVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683053612; x=1685645612; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=TKK6aVpjuVU0zocvvzx2NbMr8G38Leo7ZXygkcERGR4=; b=XFH3UymqDNiMQXWioge7shsX9mEt8dtrojB2OAhRKAjMBInJNGLAO8ZHb9KG4qAPf0 LTRnb0fkDRY3dghaMKnkyr3Dvcm/H4b2LPgDZ5VH1IgJkOaP7WUCWtr3p3YsG8h/HZU2 myjZam9voQjXT3K96L0leCjZFdQewOXCqSeR/uagR/gdviXWAkDgp5ZR/90WVDlpJ/Fx jWYIFzPaqfJ3eoFjmi1eB20yVXVNiROcfdHqhjwkxgL2Iwd8iee2DulqnT8w0M/L2q0K 7TuTPm+V5mGPlvzLEYcbRaB2RP6OYKhsmoizqMivcKzTNe/ZRaaeXTIzGm+vyBceTUKn 9ZNA== X-Gm-Message-State: AC+VfDyNW2aM5rLlFPdoC9DSglxVHN9q6iS+XnkZahjgBHp13n+KeW8b WkqE0VtwodcN7KNbqcjZ9QQ= X-Google-Smtp-Source: ACHHUZ7qBkKeVcJYF3HVFy70TfXWnSC+pmhIvAlKW7Ydykfbneuk4r/VXOqXv1LhyTw5KsdoKuYxAg== X-Received: by 2002:a05:6000:124b:b0:306:31b7:abe4 with SMTP id j11-20020a056000124b00b0030631b7abe4mr4532656wrx.14.1683053611747; Tue, 02 May 2023 11:53:31 -0700 (PDT) Received: from localhost (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.gmail.com with ESMTPSA id b5-20020a056000054500b002e5ff05765esm32028462wrf.73.2023.05.02.11.53.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 11:53:31 -0700 (PDT) Date: Tue, 2 May 2023 19:53:30 +0100 From: Lorenzo Stoakes To: Matthew Rosato Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jason Gunthorpe , Jens Axboe , Matthew Wilcox , Dennis Dalessandro , Leon Romanovsky , Christian Benvenuti , Nelson Escobar , Bernard Metzler , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Bjorn Topel , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Christian Brauner , Richard Cochran , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Oleg Nesterov , Jason Gunthorpe , John Hubbard , Jan Kara , "Kirill A . Shutemov" , Pavel Begunkov , Mika Penttila , David Hildenbrand , Dave Chinner , Theodore Ts'o , Peter Xu , "Paul E . McKenney" , Christian Borntraeger Subject: Re: [PATCH v7 0/3] mm/gup: disallow GUP writing to file-backed mappings by default Message-ID: <92fd5d71-ef9b-4971-944a-2a7bd74b5970@lucifer.local> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Tue, May 02, 2023 at 02:45:01PM -0400, Matthew Rosato wrote: > On 5/2/23 12:34 PM, Lorenzo Stoakes wrote: > > Writing to file-backed mappings which require folio dirty tracking using > > GUP is a fundamentally broken operation, as kernel write access to GUP > > mappings do not adhere to the semantics expected by a file system. > > > > A GUP caller uses the direct mapping to access the folio, which does not > > cause write notify to trigger, nor does it enforce that the caller marks > > the folio dirty. > > > > The problem arises when, after an initial write to the folio, writeback > > results in the folio being cleaned and then the caller, via the GUP > > interface, writes to the folio again. > > > > As a result of the use of this secondary, direct, mapping to the folio no > > write notify will occur, and if the caller does mark the folio dirty, this > > will be done so unexpectedly. > > > > For example, consider the following scenario:- > > > > 1. A folio is written to via GUP which write-faults the memory, notifying > > the file system and dirtying the folio. > > 2. Later, writeback is triggered, resulting in the folio being cleaned and > > the PTE being marked read-only. > > 3. The GUP caller writes to the folio, as it is mapped read/write via the > > direct mapping. > > 4. The GUP caller, now done with the page, unpins it and sets it dirty > > (though it does not have to). > > > > This change updates both the PUP FOLL_LONGTERM slow and fast APIs. As > > pin_user_pages_fast_only() does not exist, we can rely on a slightly > > imperfect whitelisting in the PUP-fast case and fall back to the slow case > > should this fail. > > > > v7: > > - Fixed very silly bug in writeable_file_mapping_allowed() inverting the > > logic. > > - Removed unnecessary RCU lock code and replaced with adaptation of Peter's > > idea. > > - Removed unnecessary open-coded folio_test_anon() in > > folio_longterm_write_pin_allowed() and restructured to generally permit > > NULL folio_mapping(). > > > > FWIW, I realize you are planning another respin, but I went and tried this version out on s390 -- Now when using a memory backend file and vfio-pci on s390 I see vfio_pin_pages_remote failing consistently. However, the pin_user_pages_fast(FOLL_WRITE | FOLL_LONGTERM) in kvm_s390_pci_aif_enable will still return positive. > Hey thanks very much for checking that :) This version will unconditionally apply the retriction to non-FOLL_LONGTERM by mistake (ugh) but vfio_pin_pages_remote() does seem to be setting FOLL_LONGTERM anyway so this seems a legitimate test. Interesting the _fast() variant succeeds... David, Jason et al. can speak more to the ins and outs of these virtualisation cases which I am not so familiar with, but I wonder if we do need a flag to provide an exception for VFIO.