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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2D05C77B73 for ; Tue, 2 May 2023 16:43:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234225AbjEBQnh (ORCPT ); Tue, 2 May 2023 12:43:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233196AbjEBQng (ORCPT ); Tue, 2 May 2023 12:43:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83B0C1BE5 for ; Tue, 2 May 2023 09:42:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683045769; 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=dpO0j0fuAlZuSPU9fXnXLLOnBHWx4/eBeZNb665FeGo=; b=LpwUvUKTn6dkHShQyY7CiX9tkiBkYksUj0U+DwRg27ewzArZLSv2N+tapdZKPGHI662Hi9 roT0K2ZmStl8UmprQ/zM7MLD2LmzrTeJBDPhx2bG9yw5ZOgTgQ9Q222Ms1v9EMatMUDZXn I9JzbGjVfPQBNcZeyuVt5EzApPrhUfo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-121-amMEgilTNUa7cSsHXWjm4A-1; Tue, 02 May 2023 12:42:48 -0400 X-MC-Unique: amMEgilTNUa7cSsHXWjm4A-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-3f3157128b4so110163665e9.0 for ; Tue, 02 May 2023 09:42:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683045767; x=1685637767; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dpO0j0fuAlZuSPU9fXnXLLOnBHWx4/eBeZNb665FeGo=; b=WBqmH9phg1DnvJFxVIx2y9V77xhdYd/h5cNOXSIANU8ZUAlm66IW1BdgAylZbXFA9x JNpKpNhpsQFYo2zRdFUJTdkTi/Ze6bpVWfvAwfvRWNroJ5qGV/OpcG6xCQMrzyL+ddOL cZn1/bks489dPYVhVdNc11RqOZyck+rtNcfQV2vypGFpHFL2FWUw/T1Hal6LeE8oEvEb V9YjcjAE6RQhJq9gs8ZDxMe1LxkheJWSeb/wHnIAhXg1vVp4GeWIBd6RpyzAF0gg4Uwv eEjnLZc8IvZoldZi9ck2wGTAmoHrBiTgRyUvhiuhohoEKzZzaHCael1SWsTazTTv5Ybx cHEg== X-Gm-Message-State: AC+VfDzKlVT2oB65w6zNiLPbVtStcO0XTIPkmZFXMtMBSIqq8HtM49iH 9YDEsKkcmq5TIOa0gO5a2QpUyXnIWQecpJMBGIz2Dc/1FNHGalUHJcf/ui4pzwamP+kuDM94FG+ hj8Olu10ekB5mMFZlAhnBVdV4SUcbtw== X-Received: by 2002:a5d:68c5:0:b0:306:2c20:c4fb with SMTP id p5-20020a5d68c5000000b003062c20c4fbmr5554479wrw.2.1683045767269; Tue, 02 May 2023 09:42:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ78lwNkqI+m8OE8j/YlVV4qi/BTpTIcVJ+8rzfRbqfEcCWatqDcWBDzUV8LMYWKUuettICYWg== X-Received: by 2002:a5d:68c5:0:b0:306:2c20:c4fb with SMTP id p5-20020a5d68c5000000b003062c20c4fbmr5554450wrw.2.1683045766894; Tue, 02 May 2023 09:42:46 -0700 (PDT) Received: from ?IPV6:2003:cb:c700:2400:6b79:2aa:9602:7016? (p200300cbc70024006b7902aa96027016.dip0.t-ipconnect.de. [2003:cb:c700:2400:6b79:2aa:9602:7016]) by smtp.gmail.com with ESMTPSA id u12-20020adfdd4c000000b0030635735a57sm2817098wrm.60.2023.05.02.09.42.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 May 2023 09:42:45 -0700 (PDT) Message-ID: Date: Tue, 2 May 2023 18:42:42 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v7 2/3] mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings Content-Language: en-US To: Lorenzo Stoakes , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: 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 , Dave Chinner , Theodore Ts'o , Peter Xu , Matthew Rosato , "Paul E . McKenney" , Christian Borntraeger References: From: David Hildenbrand Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-perf-users@vger.kernel.org On 02.05.23 18:34, 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 results in both data being written to a folio without writenotify, and > the folio being dirtied unexpectedly (if the caller decides to do so). > > This issue was first reported by Jan Kara [1] in 2018, where the problem > resulted in file system crashes. > > This is only relevant when the mappings are file-backed and the underlying > file system requires folio dirty tracking. File systems which do not, such > as shmem or hugetlb, are not at risk and therefore can be written to > without issue. > > Unfortunately this limitation of GUP has been present for some time and > requires future rework of the GUP API in order to provide correct write > access to such mappings. > > However, for the time being we introduce this check to prevent the most > egregious case of this occurring, use of the FOLL_LONGTERM pin. > > These mappings are considerably more likely to be written to after > folios are cleaned and thus simply must not be permitted to do so. > > This patch changes only the slow-path GUP functions, a following patch > adapts the GUP-fast path along similar lines. > > [1]:https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz/ > > Suggested-by: Jason Gunthorpe > Signed-off-by: Lorenzo Stoakes > Reviewed-by: John Hubbard > Reviewed-by: Mika Penttilä > Reviewed-by: Jan Kara > Reviewed-by: Jason Gunthorpe > --- > mm/gup.c | 43 ++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 42 insertions(+), 1 deletion(-) > > diff --git a/mm/gup.c b/mm/gup.c > index ff689c88a357..6e209ca10967 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -959,16 +959,53 @@ static int faultin_page(struct vm_area_struct *vma, > return 0; > } > > +/* > + * 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. > + * > + * 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 results in both data being written to a folio without writenotify, and > + * the folio being dirtied unexpectedly (if the caller decides to do so). > + */ > +static bool writeable_file_mapping_allowed(struct vm_area_struct *vma, > + unsigned long gup_flags) > +{ > + /* > + * If we aren't pinning then no problematic write can occur. A long term > + * pin is the most egregious case so this is the case we disallow. > + */ > + if (!(gup_flags & (FOLL_PIN | FOLL_LONGTERM))) > + return true; If you really want to keep FOLL_PIN here ... this has to be if ((gup_flags & (FOLL_PIN | FOLL_LONGTERM)) != (FOLL_PIN | FOLL_LONGTERM)) or two separate checks. Otherwise you'd also proceed if only FOLL_PIN is set. Unless my tired eyes betrayed me. -- Thanks, David / dhildenb