Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp971139rwr; Fri, 5 May 2023 07:29:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5HESM8xDtahBXG/hRgc+b2w493cYDb0TId/tUN7Ou9+kYhkMJU6G6h6Ufl3ZvohcX3h/B2 X-Received: by 2002:a05:6a00:15c6:b0:635:d9e1:8e1e with SMTP id o6-20020a056a0015c600b00635d9e18e1emr7458157pfu.11.1683296982031; Fri, 05 May 2023 07:29:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683296981; cv=none; d=google.com; s=arc-20160816; b=csYzk8tRPgc9xQpOSyhBYSkT9DD8ZBIItbbm0OEh3dnyUlBhYocyQ/1x7tarv9w+xg qqtLEzSRr8/F0xSgXAzM6lvRUpifNgGCGoTKHo6T8Rx5cgkkzQtAg5vmacX0574vft1a odizTJJ7E5uSdi8CPNaVqx0yovDMCeUI29z3Res2no5pqi+0Uug/n01D4RY4ePPCOrRg kNmKSFUBG+pRoqxXWZ+C6l5Mqt+LqiQSm8O2hq4xCssh/ax/wkVrXcns+K2fSg6o2WMK TO0R4+2buv3qy0lZW2TX1LDhJ5T24sCgWohGY2Q2OZIBdia7J3VxmhOC3DT44/Qywaox 5LVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:content-language:references:cc:to:user-agent :mime-version:date:message-id:dkim-signature; bh=P1oCIf84RG7+6O7buoOZWskEuD34mndm17BNvxGm8Ys=; b=hCEvLqp/+5urozcOxQF/qzUtzjmWFELTGeYCTf55N2xqfNQy6jhl//huF8RqxM0jZ3 0eCLYgFu5R1L4AfcuBJ3BxL4zWCLQSRRCSoDQI3dPBFPfmnlKC289Px8t+LgRwWNj5BC BFHXxAgurQSSQqadMdTYKvtB+e2Wna3zrriLco9sCSBlg7OXlwonPkuEotNMCF8QNVjv GSJlIinQ5J+wjf+yHdu1auRSUct7+rwTNJaKG7M3U/x75okIRzHQ8amZlus+WhkPPgH4 jLqbTksLaXxixerydvOIq9kJmaCRrl86BMaXQUXb12FeSeQiDpqDV4DGnewGkR7pgY5E SYbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="AbSn3/Md"; 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=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 126-20020a620584000000b0063b7341250esi2085891pff.277.2023.05.05.07.29.27; Fri, 05 May 2023 07:29:41 -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=@redhat.com header.s=mimecast20190719 header.b="AbSn3/Md"; 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=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232797AbjEEOSe (ORCPT + 99 others); Fri, 5 May 2023 10:18:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231977AbjEEOSb (ORCPT ); Fri, 5 May 2023 10:18:31 -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 8E217150C5 for ; Fri, 5 May 2023 07:17:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683296264; 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=P1oCIf84RG7+6O7buoOZWskEuD34mndm17BNvxGm8Ys=; b=AbSn3/Mdmk/ufWf/dgHzwFUUzYt++QyQXerOcfC5/Rfs1zEj3JsDLG/oZFq5Ja4R12oJPd /EldlX+UgB5Agle58mEZ7e8rgYBjtND29RSpQeiAK5tiOP0qQhAXovNvWcDqQ/S4WITEnh T/4+LWt2INVCZBjHvE1x/K4zjSpENc8= 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-639-Ud39Eg1DOD21qD3FpO-OKw-1; Fri, 05 May 2023 10:17:43 -0400 X-MC-Unique: Ud39Eg1DOD21qD3FpO-OKw-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-3f321e60feaso7422655e9.0 for ; Fri, 05 May 2023 07:17:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683296262; x=1685888262; h=content-transfer-encoding:in-reply-to:subject:organization:from :content-language:references:cc:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=P1oCIf84RG7+6O7buoOZWskEuD34mndm17BNvxGm8Ys=; b=V8zOCP0dyp40eO/Mq3YO1fs9EsPVMELnvqYD4pfF2aYYpeFex0626JiKFAIjlKARL8 F+kVeOSZok464ro5ZM95si5QVLkLJvcDwZS7zmKB8Yek99lCDXLEyCMZKU8HkEjzhuy6 RqrQt7TEA4z7jUC3X3gz2NU3ETY0KxiHvsbfPEf5hHdTkJVXjY18apx5ZHqEaereI2xn qgw8t8ZuhVWUEX5h51ndM6d5YZ7+H1jYf70u7dw9LTZEloYtsn2jcScg7cZArbwORa8T gkdz4Xex95hQI30yE50yKus91AF4P9N/P+JC66eCZ7H6PG8FULkABSo+8X8hkBBQlOgb 0+Sg== X-Gm-Message-State: AC+VfDyPn3qNSMSJsWa+gVzBD+cZe47MBbYtOuNmgC8Vt7d+nxraghlv jWmyzIfFjSF7Jfxeq3xXecFUi4yD9eeV5AcIGbTh7pkcvHKq9gJ30S1L5n92NMNskmE2uAcrSoN aFt5AqUch0R2tsUdxhzG7xH9yaxCllpz4 X-Received: by 2002:adf:e948:0:b0:2fb:87f7:3812 with SMTP id m8-20020adfe948000000b002fb87f73812mr1224228wrn.1.1683296261867; Fri, 05 May 2023 07:17:41 -0700 (PDT) X-Received: by 2002:adf:e948:0:b0:2fb:87f7:3812 with SMTP id m8-20020adfe948000000b002fb87f73812mr1224169wrn.1.1683296261426; Fri, 05 May 2023 07:17:41 -0700 (PDT) Received: from ?IPV6:2003:cb:c71f:6900:2b25:fc69:599e:3986? (p200300cbc71f69002b25fc69599e3986.dip0.t-ipconnect.de. [2003:cb:c71f:6900:2b25:fc69:599e:3986]) by smtp.gmail.com with ESMTPSA id h14-20020a5d6e0e000000b0030631dcbea6sm2548971wrz.77.2023.05.05.07.17.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 05 May 2023 07:17:40 -0700 (PDT) Message-ID: Date: Fri, 5 May 2023 16:17:38 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 To: Lorenzo Stoakes 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 , Dave Chinner , Theodore Ts'o , Peter Xu , Matthew Rosato , "Paul E . McKenney" , Christian Borntraeger References: Content-Language: en-US From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v8 3/3] mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, 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 >> And there is nothing wrong about pinning an anon page that's still in the >> swapcache. The following folio_test_anon() check will allow them. >> >> The check made sense in page_mapping(), but here it's not required. > > Waaaaaaaaaait a second, you were saying before:- > > "Folios in the swap cache return the swap mapping" -- you might disallow > pinning anonymous pages that are in the swap cache. > > I recall that there are corner cases where we can end up with an anon > page that's mapped writable but still in the swap cache ... so you'd > fallback to the GUP slow path (acceptable for these corner cases, I > guess), however especially the comment is a bit misleading then. > > So are we allowing or disallowing pinning anon swap cache pages? :P If we have an exclusive anon page that's still in the swap cache, sure! :) I think there are ways that can be done, and nothing would actually break. (I even wrote selftests in the cow selftests for that to amke sure it works as expected) > > I mean slow path would allow them if they are just marked anon so I'm inclined > to allow them. Exactly my reasoning. The less checks the better (especially if ordinary GUP just allows for pinning it) :) > >> >> I do agree regarding folio_test_slab(), though. Should we WARN in case we >> would have one? >> >> if (WARN_ON_ONCE(folio_test_slab(folio))) >> return false; >> > > God help us if we have a slab page at this point, so agreed worth doing, it > would surely have to arise from some dreadful bug/memory corruption. > Or some nasty race condition that we managed to ignore with rechecking if the PTEs/PMDs changed :) >>> + if (unlikely(folio_test_slab(folio) || folio_test_swapcache(folio))) >>> + return false; >>> + >>> + /* hugetlb mappings do not require dirty-tracking. */ >>> + if (folio_test_hugetlb(folio)) >>> + return true; >>> + >>> + /* >>> + * GUP-fast disables IRQs. When IRQS are disabled, RCU grace periods >>> + * cannot proceed, which means no actions performed under RCU can >>> + * proceed either. >>> + * >>> + * inodes and thus their mappings are freed under RCU, which means the >>> + * mapping cannot be freed beneath us and thus we can safely dereference >>> + * it. >>> + */ >>> + lockdep_assert_irqs_disabled(); >>> + >>> + /* >>> + * However, there may be operations which _alter_ the mapping, so ensure >>> + * we read it once and only once. >>> + */ >>> + mapping = READ_ONCE(folio->mapping); >>> + >>> + /* >>> + * The mapping may have been truncated, in any case we cannot determine >>> + * if this mapping is safe - fall back to slow path to determine how to >>> + * proceed. >>> + */ >>> + if (!mapping) >>> + return false; >>> + >>> + /* Anonymous folios are fine, other non-file backed cases are not. */ >>> + mapping_flags = (unsigned long)mapping & PAGE_MAPPING_FLAGS; >>> + if (mapping_flags) >>> + return mapping_flags == PAGE_MAPPING_ANON; >> >> KSM pages are also (shared) anonymous folios, and that check would fail -- >> which is ok (the following unsharing checks rejects long-term pinning them), >> but a bit inconstent with your comment and folio_test_anon(). >> >> It would be more consistent (with your comment and also the folio_test_anon >> implementation) to have here: >> >> return mapping_flags & PAGE_MAPPING_ANON; >> > > I explicitly excluded KSM out of fear that could be some breakage given they're > wrprotect'd + expected to CoW though? But I guess you mean they'd get picked up > by the unshare and so it doesn't matter + we wouldn't want to exclude an > PG_anon_exclusive case? Yes, unsharing handles that in the ordinary GUP and GUP-fast case. And unsharing is neither GUP-fast nor even longterm specific (for anon pages). Reason I'm brining this up is that I think it's best if we let folio_fast_pin_allowed() just check for what's absolutely GUP-fast specific. > > I'll make the change in any case given the unshare check! > > I notice the gup_huge_pgd() doesn't do an unshare but I mean, a PGD-sized huge > page probably isn't going to be CoW'd :P I spotted exactly the same thing and wondered about that (after all I added all that unsharing logic ... so I should know). I'm sure there must be a reason I didn't add it ;) ... probably we should just add it even though it might essentially be dead code for now (at least the cow selftests would try with each and every hugetlb size and eventually reveal the problem on whatever arch ends up using that code ... ). Do you want to send a patch to add unsharing to gup_huge_pgd() as well? -- Thanks, David / dhildenb