Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7267597rwr; Tue, 2 May 2023 11:58:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Vt2PRYskgMTsNOM1W0utGMMZvBETNWjkU/lLlNyHAAqwH5Z6rl68evOdOD593l3nwVz3q X-Received: by 2002:a05:6a21:9989:b0:ef:4922:a70a with SMTP id ve9-20020a056a21998900b000ef4922a70amr23788657pzb.30.1683053902003; Tue, 02 May 2023 11:58:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683053901; cv=none; d=google.com; s=arc-20160816; b=wlFTgKbP9o+t3tlnntQA2VuQFj0NDjJC4h0XgWAYZSLdiN0UW6tu9PKwq+u0w129KF DnoOmB+/GGUEBuFTGxHM0dhyXc2r38vD2bi31CtMZa8UWv9RikH8pKtk16QsY/QnkdIs Ck9U/3cu8mM77yWqpg/oPRTbHzDay5AbOrpgzhJx9g52/Fw9TFYMoM4JLyFR1S+IKwNM SW9xihZmOnJ8WEnglQAY+L7o5cF1Q0Lc+n8i6pq1I6UvUWQPmoysZePnKmPND7HThBCw X90qsVM+yQ9d0d53k5xEVixF9ASvw+FOqrdZDLn1T0LElcinUT7XJUyo1XyTwnNNo73t 9DFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=TKK6aVpjuVU0zocvvzx2NbMr8G38Leo7ZXygkcERGR4=; b=w/1CRsrn+zjA05n+tuzFZjm5MOSsUDEYuz0hwNX5Ekt72omERAVcft+t7WMENux6Sk F0C73YBPi6Y0U1JIzM25Xgx2Ro+IaGkmjwxc3KjkwEhZwHK7yd3KphJO1kmgOCcyc01q TOFIlL3y+urVaVY2clntquzFgcv2PuKoRor1J1jZnYaus/1ei8Y/RvC1LAP0LNw1rJ5Y 8PVCmE3OftElIhvPENbHgmwqg9/XrlnfCapkqIE21CB5M6E5I5obpKA1DZbFLPU+BEe/ 8pwzB2sJIPYUKbYT3jWsw+Z0cULVaPPJDtKOqnEPR7op7tmKRvCan2DYrtjU+JplOU9Y I3Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=jT2Ze1BF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s206-20020a632cd7000000b0051389efe297si12045358pgs.265.2023.05.02.11.58.05; Tue, 02 May 2023 11:58:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=jT2Ze1BF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229556AbjEBSxh (ORCPT + 99 others); Tue, 2 May 2023 14:53:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjEBSxe (ORCPT ); Tue, 2 May 2023 14:53:34 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D7D6E78; Tue, 2 May 2023 11:53:33 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3f178da21b5so27542825e9.3; Tue, 02 May 2023 11:53:33 -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=hLXjBniekhV5CFSmcgrX3kpfmkTbdSekZ8RPLv7GyrhavZ437ifPmNI6tj6+Hkpcug EJQFh4FiatIi9Bb+BhSe6AEF1JM/RA0XF3YoQwtYBOgm3uFa19lWAO40Z33wKl+7McJd nPIaH8FBjRcgu2/3NmLRJ1t/siGzCGFcE096WEmbg2/pg4JVAokCSTD1CDpiz/tDoMEi 5qwBMlg5gLKK+7aZzepvOCpHLKE9rR5oCYztoJYL1XmBhtET5LhbAl1x7Gesti7nU1zI 59mZ9WjXO0/Tsz9nc0/7m25WOT2AMZFpqAYTAxKrav9rHyKAnjOLHtO54hZNX/0Qj19p pQuQ== X-Gm-Message-State: AC+VfDy+9UQRxAj5pfOJNuXuo329B5oz2/+rh1UuM6LYt0wMcSgmq9h3 XvJnTl0JN+1FSW/LPy42qzI= 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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.