From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FE12374DD for ; Fri, 26 Apr 2024 21:33:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714167196; cv=none; b=hPAllq1SG2iskLYGdKGUQ7OLwUd6NB7uctpIuu1wBjBxBxpBAUPMxLh+R0lMiz/C6wF7RIzFfivJuUnN6Ej75ATKCGwFEgJpREuMGTDN+2BYT4Zk0/2u/MKUlYX4UaDDnalRk3j9LxMMFcxllDV4YpiLmgm8k7V2Ik86eT1k3p0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714167196; c=relaxed/simple; bh=kxnGaLzCx36ImG3jzFPNGZvnlVs5FtG14IIFMoRjSAM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=njRTSiTi9Sdp164MKalqU5LmHYm2TM7pzGa8U2W/TScPpvPw9QHzPPAInh4x5yHoMo5mcVESKFIUEt7cVUdL8OaYVUV5IwkhiGHifdVeKGUgp76LQnrSWk0OEzGiToVYHipPdHYivOQgdJQQEwlKdwFIBjjH1fNxeCP69BhDCkM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QUGKUOVT; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QUGKUOVT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714167194; 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:autocrypt:autocrypt; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=QUGKUOVT4BnqsqyZEcmrK9HvDUeUpy8/4BZ0r24rf+fZPiBU9SqccfCCAdsx3hVUDIq7c3 zo29VQDEYiOizBj1j5CYQbBczMqWRkh8UFEQoV1P0OJiiOaYBBL04PrDiAXYy2qdXoV/qq QD8X71SGRRJKQx2Sbw3sMykKTHYUy1w= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-231-e4CkGW0mNcWYhbONp_QUYQ-1; Fri, 26 Apr 2024 17:33:12 -0400 X-MC-Unique: e4CkGW0mNcWYhbONp_QUYQ-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-34b5bece899so1671029f8f.3 for ; Fri, 26 Apr 2024 14:33:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714167191; x=1714771991; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=QvA0kebL9p1cRnb/cxSS8Ajt4Ug4eg3w9fd2uQNKxeH3KkdLd6VVFdojAer4ouIGPP fmn1uMj+nFvOi/UK5hcTIiM1dRpfnyx1JUEWROdUvPcFTeM2RaPpf5yQow3+C2ZAwdKq sIOMjMqfYi8BWmF1PmyYttKbuPaybd+7qLFIbaeDxo2tHNgsrBGIrvsCfqUwboGuuE/R 07J0OtASpzKKbalUg2ReRRga2FqWE9XP659v5rlS7XDPRLY1x0IRtZF2tdzPjRFcNB0p sCAYzXzDv1QEuP+ieL7W2OuD+dtYTvkU16pAEVTyLVF0rlgVo2PoLVK6JYWWRxhGg3WA zWFw== X-Gm-Message-State: AOJu0YwP8ObbDGs2xH5ZRP1RCbO/GLYxK8MrilUwJ+GgRlEeBZLMSbCr uE36BnTob7FM9BYaTdUdO3u9v+HEqD5ByMD/pueac0QLd3U6/1cwbI8WfmqqrIYjhFLf7p20NsI /Ymvtrd27OmK4Kfq2hwnL1N8JTykutx9mpr0s+N8LELVH0VNSrqECow/d/bd+HA== X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554775wrm.60.1714167191491; Fri, 26 Apr 2024 14:33:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM1bRgkh0yb/MsAsyuNGZmpW4/Rz7FCY0YwSzgR+rZNpVvpkuS+p8nE7OJ5gTd+vbJaBdFtw== X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554751wrm.60.1714167190989; Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Received: from ?IPV6:2003:cb:c726:6100:20f2:6848:5b74:ca82? (p200300cbc726610020f268485b74ca82.dip0.t-ipconnect.de. [2003:cb:c726:6100:20f2:6848:5b74:ca82]) by smtp.gmail.com with ESMTPSA id e15-20020a05600c218f00b004146e58cc35sm35152816wme.46.2024.04.26.14.33.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Message-ID: Date: Fri, 26 Apr 2024 23:33:08 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/3] mm/gup: consistently name GUP-fast functions To: Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mike Rapoport , Jason Gunthorpe , John Hubbard , linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-riscv@lists.infradead.org, x86@kernel.org References: <20240402125516.223131-1-david@redhat.com> <20240402125516.223131-2-david@redhat.com> <8b42a24d-caf0-46ef-9e15-0f88d47d2f21@redhat.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH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ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit >> >>> Hmm, so when I enable 2M hugetlb I found ./cow is even failing on x86. >>> >>> # ./cow | grep -B1 "not ok" >>> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >>> not ok 161 No leak from parent into child >>> -- >>> # [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) >>> not ok 215 No leak from parent into child >>> -- >>> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 269 No leak from child into parent >>> -- >>> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 323 No leak from child into parent >>> >>> And it looks like it was always failing.. perhaps since the start? We >> >> Yes! >> >> commit 7dad331be7816103eba8c12caeb88fbd3599c0b9 >> Author: David Hildenbrand >> Date: Tue Sep 27 13:01:17 2022 +0200 >> >> selftests/vm: anon_cow: hugetlb tests >> Let's run all existing test cases with all hugetlb sizes we're able to >> detect. >> Note that some tests cases still fail. This will, for example, be fixed >> once vmsplice properly uses FOLL_PIN instead of FOLL_GET for pinning. >> With 2 MiB and 1 GiB hugetlb on x86_64, the expected failures are: >> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >> not ok 23 No leak from parent into child >> # [RUN] vmsplice() + unmap in child ... with hugetlb (1048576 kB) >> not ok 24 No leak from parent into child >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 35 No leak from child into parent >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 36 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 47 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 48 No leak from child into parent >> >> As it keeps confusing people (until somebody cares enough to fix vmsplice), I already >> thought about just disabling the test and adding a comment why it happens and >> why nobody cares. > > I think we should, and when doing so maybe add a rich comment in > hugetlb_wp() too explaining everything? Likely yes. Let me think of something. > >> >>> didn't do the same on hugetlb v.s. normal anon from that regard on the >>> vmsplice() fix. >>> >>> I drafted a patch to allow refcount>1 detection as the same, then all tests >>> pass for me, as below. >>> >>> David, I'd like to double check with you before I post anything: is that >>> your intention to do so when working on the R/O pinning or not? >> >> Here certainly the "if it's easy it would already have done" principle applies. :) >> >> The issue is the following: hugetlb pages are scarce resources that cannot usually >> be overcommitted. For ordinary memory, we don't care if we COW in some corner case >> because there is an unexpected reference. You temporarily consume an additional page >> that gets freed as soon as the unexpected reference is dropped. >> >> For hugetlb, it is problematic. Assume you have reserved a single 1 GiB hugetlb page >> and your process uses that in a MAP_PRIVATE mapping. Then it calls fork() and the >> child quits immediately. >> >> If you decide to COW, you would need a second hugetlb page, which we don't have, so >> you have to crash the program. >> >> And in hugetlb it's extremely easy to not get folio_ref_count() == 1: >> >> hugetlb_fault() will do a folio_get(folio) before calling hugetlb_wp()! >> >> ... so you essentially always copy. > > Hmm yes there's one extra refcount. I think this is all fine, we can simply > take all of them into account when making a CoW decision. However crashing > an userspace can be a problem for sure. Right, and a simple reference from page migration or some other PFN walker would be sufficient for that. I did not dare being responsible for that, even though races are rare :) The vmsplice leak is not worth that: hugetlb with MAP_PRIVATE to COW-share data between processes with different privilege levels is not really common. > >> >> >> At that point I walked away from that, letting vmsplice() be fixed at some point. Dave >> Howells was close at some point IIRC ... >> >> I had some ideas about retrying until the other reference is gone (which cannot be a >> longterm GUP pin), but as vmsplice essentially does without FOLL_PIN|FOLL_LONGTERM, >> it's quit hopeless to resolve that as long as vmsplice holds longterm references the wrong >> way. >> >> --- >> >> One could argue that fork() with hugetlb and MAP_PRIVATE is stupid and fragile: assume >> your child MM is torn down deferred, and will unmap the hugetlb page deferred. Or assume >> you access the page concurrently with fork(). You'd have to COW and crash the program. >> BUT, there is a horribly ugly hack in hugetlb COW code where you *steal* the page form >> the child program and crash your child. I'm not making that up, it's horrible. > > I didn't notice that code before; doesn't sound like a very responsible > parent.. > > Looks like either there come a hugetlb guru who can make a decision to > break hugetlb ABI at some point, knowing that nobody will really get > affected by it, or that's the uncharted area whoever needs to introduce > hugetlb v2. I raised this topic in the past, and IMHO we either (a) never should have added COW support; or (b) added COW support by using ordinary anonymous memory (hey, partial mappings of hugetlb pages! ;) ). After all, COW is an optimization to speed up fork and defer copying. It relies on memory overcommit, but that doesn't really apply to hugetlb, so we fake it ... One easy ABI break I had in mind was to simply *not* allow COW-sharing of anon hugetlb folios; for example, simply don't copy the page into the child. Chances are there are not really a lot of child processes that would fail ... but likely we would break *something*. So there is no easy way out :( -- Cheers, David / dhildenb 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 22625C4345F for ; Fri, 26 Apr 2024 21:33:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=u075jF5ZRum7bCDumvwuRoNbfwQxvzaLfumI/+9BX0Y=; b=RxD8uAb1iFYxSa 2roC0vIFI6c9ig40qE9J/SYui6gdRzY+vjWZIZrG8pueHXNfEocKrYKi0MgLUnNoqZIvI8aAMoeqR O1Us4wgFN6j3cZRZCwbyjtQzwEJYVS/zAxEgaQgXRY9oOIuMyOJ5y8zk5XgAyM6bugltluODtEKRB Y11pCsLZbb4puf1yDnEOZ8HOQyDNEN3dGwnC614nea75FMjSgkOZr9/DKygeWHQhs8vfgK+q93zYm DNweij/feq/vELM+QI4QXOokP1o5xWHg3bFbjFOgv7vgbwttADOWKWshLoafxIeMQRhv5APZPc8Cw d8dNBEmY7eUBWM8PfATA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0TC2-0000000E5NT-36qN; Fri, 26 Apr 2024 21:33:18 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0TBz-0000000E5MU-1BEj for linux-riscv@lists.infradead.org; Fri, 26 Apr 2024 21:33:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714167194; 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:autocrypt:autocrypt; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=QUGKUOVT4BnqsqyZEcmrK9HvDUeUpy8/4BZ0r24rf+fZPiBU9SqccfCCAdsx3hVUDIq7c3 zo29VQDEYiOizBj1j5CYQbBczMqWRkh8UFEQoV1P0OJiiOaYBBL04PrDiAXYy2qdXoV/qq QD8X71SGRRJKQx2Sbw3sMykKTHYUy1w= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-411-NClkw4MeNDaSgXn_jFmBvg-1; Fri, 26 Apr 2024 17:33:12 -0400 X-MC-Unique: NClkw4MeNDaSgXn_jFmBvg-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-343e54fc19bso1348610f8f.2 for ; Fri, 26 Apr 2024 14:33:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714167191; x=1714771991; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=OFtuoM8VuzfNgXTCEzh4x9xNArehFPmni93BtibO6eYCnK2Cv0tRsKiO5aza+iBAFu w5kjQEHPmzB7+5WkvhSFqymCQt+m771t9tX/kjnoBszAkC+oOuEdapZu3zge/B4nN8U8 bnHIXIFUGNY7iRySp/q0kgzUT5NFcG2Kk7FbWSUJuNuAKwWoO5l90H8jhh1qtpEsPPFR bOCbfNOdZxJNtJ5sBRX90oMGy7b7IeuhAQhbrQzQGm2zpvB+5XbywyvLnFhDQ0/40nLW dKJftWIAuobPh0IDFvQ2FGy5nGhTGqrwnvclKJai3ID/1rTxmudmz3rNBaOHKgRSWOc0 rU5w== X-Forwarded-Encrypted: i=1; AJvYcCUUs7hJAKXnZ8J7n0vOpC5oo6IGgrvYXlSIk++0BWF8JdEQou9saCN2tWmC1Et8NoZMJ84bqajQBWRh2RMk4M62Z/WnWjT7eLB6cL87O+fj X-Gm-Message-State: AOJu0YyxjS13hhHFBKZ96etUHs1cqcYW+C/AhFvyFJ1DB6sL0t3ykpz/ WbzFsxZfGeN3aV4V/vXyAk+ygDKEcjJ9RlWcuRBZXsuK6KkDSUQs1BmjqQ4Gy9JVn81GHbk/R6K CyuPaBQDFIOYQzIxQqIwfD9wXBdnbUsoUD+/YfcL8Atcl6P0+tcEe0YNl6CnFeLmn4Q== X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554778wrm.60.1714167191493; Fri, 26 Apr 2024 14:33:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM1bRgkh0yb/MsAsyuNGZmpW4/Rz7FCY0YwSzgR+rZNpVvpkuS+p8nE7OJ5gTd+vbJaBdFtw== X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554751wrm.60.1714167190989; Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Received: from ?IPV6:2003:cb:c726:6100:20f2:6848:5b74:ca82? (p200300cbc726610020f268485b74ca82.dip0.t-ipconnect.de. [2003:cb:c726:6100:20f2:6848:5b74:ca82]) by smtp.gmail.com with ESMTPSA id e15-20020a05600c218f00b004146e58cc35sm35152816wme.46.2024.04.26.14.33.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Message-ID: Date: Fri, 26 Apr 2024 23:33:08 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/3] mm/gup: consistently name GUP-fast functions To: Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mike Rapoport , Jason Gunthorpe , John Hubbard , linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-riscv@lists.infradead.org, x86@kernel.org References: <20240402125516.223131-1-david@redhat.com> <20240402125516.223131-2-david@redhat.com> <8b42a24d-caf0-46ef-9e15-0f88d47d2f21@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4BAheAAhkBFiEEG9nKrXNcTDpGDfzKTd4Q9wD/g1oFAl8Ox4kFCRKpKXgACgkQTd4Q 9wD/g1oHcA//a6Tj7SBNjFNM1iNhWUo1lxAja0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kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240426_143315_456357_C9AAC975 X-CRM114-Status: GOOD ( 29.55 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org >> >>> Hmm, so when I enable 2M hugetlb I found ./cow is even failing on x86. >>> >>> # ./cow | grep -B1 "not ok" >>> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >>> not ok 161 No leak from parent into child >>> -- >>> # [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) >>> not ok 215 No leak from parent into child >>> -- >>> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 269 No leak from child into parent >>> -- >>> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 323 No leak from child into parent >>> >>> And it looks like it was always failing.. perhaps since the start? We >> >> Yes! >> >> commit 7dad331be7816103eba8c12caeb88fbd3599c0b9 >> Author: David Hildenbrand >> Date: Tue Sep 27 13:01:17 2022 +0200 >> >> selftests/vm: anon_cow: hugetlb tests >> Let's run all existing test cases with all hugetlb sizes we're able to >> detect. >> Note that some tests cases still fail. This will, for example, be fixed >> once vmsplice properly uses FOLL_PIN instead of FOLL_GET for pinning. >> With 2 MiB and 1 GiB hugetlb on x86_64, the expected failures are: >> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >> not ok 23 No leak from parent into child >> # [RUN] vmsplice() + unmap in child ... with hugetlb (1048576 kB) >> not ok 24 No leak from parent into child >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 35 No leak from child into parent >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 36 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 47 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 48 No leak from child into parent >> >> As it keeps confusing people (until somebody cares enough to fix vmsplice), I already >> thought about just disabling the test and adding a comment why it happens and >> why nobody cares. > > I think we should, and when doing so maybe add a rich comment in > hugetlb_wp() too explaining everything? Likely yes. Let me think of something. > >> >>> didn't do the same on hugetlb v.s. normal anon from that regard on the >>> vmsplice() fix. >>> >>> I drafted a patch to allow refcount>1 detection as the same, then all tests >>> pass for me, as below. >>> >>> David, I'd like to double check with you before I post anything: is that >>> your intention to do so when working on the R/O pinning or not? >> >> Here certainly the "if it's easy it would already have done" principle applies. :) >> >> The issue is the following: hugetlb pages are scarce resources that cannot usually >> be overcommitted. For ordinary memory, we don't care if we COW in some corner case >> because there is an unexpected reference. You temporarily consume an additional page >> that gets freed as soon as the unexpected reference is dropped. >> >> For hugetlb, it is problematic. Assume you have reserved a single 1 GiB hugetlb page >> and your process uses that in a MAP_PRIVATE mapping. Then it calls fork() and the >> child quits immediately. >> >> If you decide to COW, you would need a second hugetlb page, which we don't have, so >> you have to crash the program. >> >> And in hugetlb it's extremely easy to not get folio_ref_count() == 1: >> >> hugetlb_fault() will do a folio_get(folio) before calling hugetlb_wp()! >> >> ... so you essentially always copy. > > Hmm yes there's one extra refcount. I think this is all fine, we can simply > take all of them into account when making a CoW decision. However crashing > an userspace can be a problem for sure. Right, and a simple reference from page migration or some other PFN walker would be sufficient for that. I did not dare being responsible for that, even though races are rare :) The vmsplice leak is not worth that: hugetlb with MAP_PRIVATE to COW-share data between processes with different privilege levels is not really common. > >> >> >> At that point I walked away from that, letting vmsplice() be fixed at some point. Dave >> Howells was close at some point IIRC ... >> >> I had some ideas about retrying until the other reference is gone (which cannot be a >> longterm GUP pin), but as vmsplice essentially does without FOLL_PIN|FOLL_LONGTERM, >> it's quit hopeless to resolve that as long as vmsplice holds longterm references the wrong >> way. >> >> --- >> >> One could argue that fork() with hugetlb and MAP_PRIVATE is stupid and fragile: assume >> your child MM is torn down deferred, and will unmap the hugetlb page deferred. Or assume >> you access the page concurrently with fork(). You'd have to COW and crash the program. >> BUT, there is a horribly ugly hack in hugetlb COW code where you *steal* the page form >> the child program and crash your child. I'm not making that up, it's horrible. > > I didn't notice that code before; doesn't sound like a very responsible > parent.. > > Looks like either there come a hugetlb guru who can make a decision to > break hugetlb ABI at some point, knowing that nobody will really get > affected by it, or that's the uncharted area whoever needs to introduce > hugetlb v2. I raised this topic in the past, and IMHO we either (a) never should have added COW support; or (b) added COW support by using ordinary anonymous memory (hey, partial mappings of hugetlb pages! ;) ). After all, COW is an optimization to speed up fork and defer copying. It relies on memory overcommit, but that doesn't really apply to hugetlb, so we fake it ... One easy ABI break I had in mind was to simply *not* allow COW-sharing of anon hugetlb folios; for example, simply don't copy the page into the child. Chances are there are not really a lot of child processes that would fail ... but likely we would break *something*. So there is no easy way out :( -- Cheers, David / dhildenb _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9E80EC4345F for ; Fri, 26 Apr 2024 21:33:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=aagBzg8Pe59JFbar5rH9R8qybzdRC/It/FoA5JYLSxY=; b=2Mn5kol2r4t5tp IoaU9Hp8lN/7k3Mc/Rt3oeL2XeJkOC9uvXD19DWXa8tfSutPA3vlUysAlxKlu0TDFah1o0EoYHflj GBdHuD3C/E+IKQuIjd6i4gZCY86CMM7MTUWM4V3DAK+NMUl6gkNEfGKx5dvHSJz3Ww2aCSlfDSUOs J22oLy6YMKHnfWLYzMLvYuiADNWi6kVqDkJzRtnaJMyWN5ED+lMRrjmvRASKwbTbNUqhbRdfwaIg3 qRYE1JG8XtN+867MQB6t8c38NGAgvDc6M8/QYPMN/h/+Xt5XYEgvrrymS+LLDK8Sb1Hb8rJqNRg7Y eL0DdO/TOp1HErJNGZZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0TC3-0000000E5Nm-1ynt; Fri, 26 Apr 2024 21:33:19 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0TBz-0000000E5MV-1BDb for linux-arm-kernel@lists.infradead.org; Fri, 26 Apr 2024 21:33:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714167194; 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:autocrypt:autocrypt; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=QUGKUOVT4BnqsqyZEcmrK9HvDUeUpy8/4BZ0r24rf+fZPiBU9SqccfCCAdsx3hVUDIq7c3 zo29VQDEYiOizBj1j5CYQbBczMqWRkh8UFEQoV1P0OJiiOaYBBL04PrDiAXYy2qdXoV/qq QD8X71SGRRJKQx2Sbw3sMykKTHYUy1w= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-411-yxxEMe8cOdCLgNRtvABHkg-1; Fri, 26 Apr 2024 17:33:12 -0400 X-MC-Unique: yxxEMe8cOdCLgNRtvABHkg-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-34b5bece899so1671025f8f.3 for ; Fri, 26 Apr 2024 14:33:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714167191; x=1714771991; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=jyK2rzywgRTaM1OAjmzCtj1rbmKJmj0WmosgAxqRDXCaIN6qISI4xpmd43wVLj9+zw WKkSH0Bu2igSXwRySwC+AfTglF4OYm7veaFmGT2yxs54wWjjjD64phtbgpm68HhtPG+P t/4D80Fo853wcKto5akJGYz9GwuOUlsssPhbd173bU9RfeF8chjuQsSO+noI0wC2pZ0v lPWRhv84Abkrg2bF0hAQ2nWk/0R6+e+E7EiS6eKidn8dQoeEaQkxTjMjL73jU9EXVja0 f4fEW/tyoOM7bAfEbOtShFm+qkvGtn2QQxG7UNu9gHedd4SkNuTIuNHfPALFo1Bpyxnt 4pkw== X-Forwarded-Encrypted: i=1; AJvYcCUraTgoaAaxbJAxU7F3cAznUk2FAt2kU1UpU7YmVgteF0UhM9Imd/M+taZ3ZHqhuSWPH/Xkz4P7kBLdQIn6XZ/++KMJ2oLgz9JUOJiOb+9NQLX7vRY= X-Gm-Message-State: AOJu0Ywz7hikNjB/RcBULKoBKqu6uMjiYjfzE7Yr7zoMbUZuSnIwO9DS gQvNEF7BkWrukahTD01sUfOlBJQc8UZdcNsuSz9mGMyegxFmcCH/3WXh81QIvyif9PdFZSxyQwi c9aKVR+9ZwWmvSQVda3rnDBMZ5JK8sV/rcAOWDxIspdqFivVWxtZk8TwCAa2UzQtm+5lecjP6 X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554767wrm.60.1714167191486; Fri, 26 Apr 2024 14:33:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM1bRgkh0yb/MsAsyuNGZmpW4/Rz7FCY0YwSzgR+rZNpVvpkuS+p8nE7OJ5gTd+vbJaBdFtw== X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554751wrm.60.1714167190989; Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Received: from ?IPV6:2003:cb:c726:6100:20f2:6848:5b74:ca82? (p200300cbc726610020f268485b74ca82.dip0.t-ipconnect.de. [2003:cb:c726:6100:20f2:6848:5b74:ca82]) by smtp.gmail.com with ESMTPSA id e15-20020a05600c218f00b004146e58cc35sm35152816wme.46.2024.04.26.14.33.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Message-ID: Date: Fri, 26 Apr 2024 23:33:08 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/3] mm/gup: consistently name GUP-fast functions To: Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mike Rapoport , Jason Gunthorpe , John Hubbard , linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-riscv@lists.infradead.org, x86@kernel.org References: <20240402125516.223131-1-david@redhat.com> <20240402125516.223131-2-david@redhat.com> <8b42a24d-caf0-46ef-9e15-0f88d47d2f21@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4BAheAAhkBFiEEG9nKr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e90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240426_143315_453683_4FBA4B63 X-CRM114-Status: GOOD ( 30.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org >> >>> Hmm, so when I enable 2M hugetlb I found ./cow is even failing on x86. >>> >>> # ./cow | grep -B1 "not ok" >>> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >>> not ok 161 No leak from parent into child >>> -- >>> # [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) >>> not ok 215 No leak from parent into child >>> -- >>> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 269 No leak from child into parent >>> -- >>> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 323 No leak from child into parent >>> >>> And it looks like it was always failing.. perhaps since the start? We >> >> Yes! >> >> commit 7dad331be7816103eba8c12caeb88fbd3599c0b9 >> Author: David Hildenbrand >> Date: Tue Sep 27 13:01:17 2022 +0200 >> >> selftests/vm: anon_cow: hugetlb tests >> Let's run all existing test cases with all hugetlb sizes we're able to >> detect. >> Note that some tests cases still fail. This will, for example, be fixed >> once vmsplice properly uses FOLL_PIN instead of FOLL_GET for pinning. >> With 2 MiB and 1 GiB hugetlb on x86_64, the expected failures are: >> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >> not ok 23 No leak from parent into child >> # [RUN] vmsplice() + unmap in child ... with hugetlb (1048576 kB) >> not ok 24 No leak from parent into child >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 35 No leak from child into parent >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 36 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 47 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 48 No leak from child into parent >> >> As it keeps confusing people (until somebody cares enough to fix vmsplice), I already >> thought about just disabling the test and adding a comment why it happens and >> why nobody cares. > > I think we should, and when doing so maybe add a rich comment in > hugetlb_wp() too explaining everything? Likely yes. Let me think of something. > >> >>> didn't do the same on hugetlb v.s. normal anon from that regard on the >>> vmsplice() fix. >>> >>> I drafted a patch to allow refcount>1 detection as the same, then all tests >>> pass for me, as below. >>> >>> David, I'd like to double check with you before I post anything: is that >>> your intention to do so when working on the R/O pinning or not? >> >> Here certainly the "if it's easy it would already have done" principle applies. :) >> >> The issue is the following: hugetlb pages are scarce resources that cannot usually >> be overcommitted. For ordinary memory, we don't care if we COW in some corner case >> because there is an unexpected reference. You temporarily consume an additional page >> that gets freed as soon as the unexpected reference is dropped. >> >> For hugetlb, it is problematic. Assume you have reserved a single 1 GiB hugetlb page >> and your process uses that in a MAP_PRIVATE mapping. Then it calls fork() and the >> child quits immediately. >> >> If you decide to COW, you would need a second hugetlb page, which we don't have, so >> you have to crash the program. >> >> And in hugetlb it's extremely easy to not get folio_ref_count() == 1: >> >> hugetlb_fault() will do a folio_get(folio) before calling hugetlb_wp()! >> >> ... so you essentially always copy. > > Hmm yes there's one extra refcount. I think this is all fine, we can simply > take all of them into account when making a CoW decision. However crashing > an userspace can be a problem for sure. Right, and a simple reference from page migration or some other PFN walker would be sufficient for that. I did not dare being responsible for that, even though races are rare :) The vmsplice leak is not worth that: hugetlb with MAP_PRIVATE to COW-share data between processes with different privilege levels is not really common. > >> >> >> At that point I walked away from that, letting vmsplice() be fixed at some point. Dave >> Howells was close at some point IIRC ... >> >> I had some ideas about retrying until the other reference is gone (which cannot be a >> longterm GUP pin), but as vmsplice essentially does without FOLL_PIN|FOLL_LONGTERM, >> it's quit hopeless to resolve that as long as vmsplice holds longterm references the wrong >> way. >> >> --- >> >> One could argue that fork() with hugetlb and MAP_PRIVATE is stupid and fragile: assume >> your child MM is torn down deferred, and will unmap the hugetlb page deferred. Or assume >> you access the page concurrently with fork(). You'd have to COW and crash the program. >> BUT, there is a horribly ugly hack in hugetlb COW code where you *steal* the page form >> the child program and crash your child. I'm not making that up, it's horrible. > > I didn't notice that code before; doesn't sound like a very responsible > parent.. > > Looks like either there come a hugetlb guru who can make a decision to > break hugetlb ABI at some point, knowing that nobody will really get > affected by it, or that's the uncharted area whoever needs to introduce > hugetlb v2. I raised this topic in the past, and IMHO we either (a) never should have added COW support; or (b) added COW support by using ordinary anonymous memory (hey, partial mappings of hugetlb pages! ;) ). After all, COW is an optimization to speed up fork and defer copying. It relies on memory overcommit, but that doesn't really apply to hugetlb, so we fake it ... One easy ABI break I had in mind was to simply *not* allow COW-sharing of anon hugetlb folios; for example, simply don't copy the page into the child. Chances are there are not really a lot of child processes that would fail ... but likely we would break *something*. So there is no easy way out :( -- Cheers, David / dhildenb _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 40A34C4345F for ; Fri, 26 Apr 2024 21:34:04 +0000 (UTC) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QUGKUOVT; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QUGKUOVT; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4VR5Zf6R4Wz3dX0 for ; Sat, 27 Apr 2024 07:34:02 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QUGKUOVT; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QUGKUOVT; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=redhat.com (client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=david@redhat.com; receiver=lists.ozlabs.org) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4VR5Yp5zTTz3cVr for ; Sat, 27 Apr 2024 07:33:17 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714167194; 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:autocrypt:autocrypt; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=QUGKUOVT4BnqsqyZEcmrK9HvDUeUpy8/4BZ0r24rf+fZPiBU9SqccfCCAdsx3hVUDIq7c3 zo29VQDEYiOizBj1j5CYQbBczMqWRkh8UFEQoV1P0OJiiOaYBBL04PrDiAXYy2qdXoV/qq QD8X71SGRRJKQx2Sbw3sMykKTHYUy1w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714167194; 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:autocrypt:autocrypt; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=QUGKUOVT4BnqsqyZEcmrK9HvDUeUpy8/4BZ0r24rf+fZPiBU9SqccfCCAdsx3hVUDIq7c3 zo29VQDEYiOizBj1j5CYQbBczMqWRkh8UFEQoV1P0OJiiOaYBBL04PrDiAXYy2qdXoV/qq QD8X71SGRRJKQx2Sbw3sMykKTHYUy1w= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-176-p2co4KJpOH6eU-d9rgldPw-1; Fri, 26 Apr 2024 17:33:12 -0400 X-MC-Unique: p2co4KJpOH6eU-d9rgldPw-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-343c6bb1f21so1536783f8f.1 for ; Fri, 26 Apr 2024 14:33:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714167191; x=1714771991; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=HTxgqruqHfJleI+BsiKGjG0w4odgnXdiSmb5CNOjsgc=; b=i9j/4OGZqGUylPjysFejqcpU55IALOMaLAGGuu+PG6vNe/e93OoZ+GM8M9vIGB7TBb VnqaOAnydjMFFceKdG+4ppvNBi7zty1v/lyNpTC8DvP5FQPp9fT4m8lDv/3+R5G+MNwI Txx1aAmOC+l45cG99LKdOuzQZhnWLtBinal7YCtcLxUcvJD/DiLBYZAwqiQxz59KdIrk L15cHsoZPF9mpDIm4MKzJTi9Zt2yWZvcziiP+IWoX0vcZJZGBAFnYIp/VztabtEXmrWD jD/EU1znSWLx+d9C5JSLv6uxCixQGkgt9yecoEWXd/S2lhqjCiDJ3sbjufIjrAJbCljL LtgA== X-Forwarded-Encrypted: i=1; AJvYcCXuZEtVRH15xRufcvuP3FAGt73DIWs9MEu08lejnDSNVm7mW1BaM135R8c/pFR1V1b+t22J0VhjwfJfNl+wxoyH72DZHci4QmpWOfp01A== X-Gm-Message-State: AOJu0YyWhNNp7/sPLIycsxGkqGCoqSUm4/MPQbILy+9RJW4xsujCD9JZ vzlOuWai6iv/665cJutawmCPvgRoGVQOmX3a0+dv7MAIebtY8PHuUsLMHv9F344UQMYWlvQ7qXK P6jBa5zUxn2Qa3hB82y5JtlA4SpG6ruZ1vEi1k2eF5rFheAJNxM6TNbGNiWf7wDs= X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554773wrm.60.1714167191490; Fri, 26 Apr 2024 14:33:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM1bRgkh0yb/MsAsyuNGZmpW4/Rz7FCY0YwSzgR+rZNpVvpkuS+p8nE7OJ5gTd+vbJaBdFtw== X-Received: by 2002:adf:e70b:0:b0:34c:5f6e:1720 with SMTP id c11-20020adfe70b000000b0034c5f6e1720mr554751wrm.60.1714167190989; Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Received: from ?IPV6:2003:cb:c726:6100:20f2:6848:5b74:ca82? (p200300cbc726610020f268485b74ca82.dip0.t-ipconnect.de. [2003:cb:c726:6100:20f2:6848:5b74:ca82]) by smtp.gmail.com with ESMTPSA id e15-20020a05600c218f00b004146e58cc35sm35152816wme.46.2024.04.26.14.33.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Apr 2024 14:33:10 -0700 (PDT) Message-ID: Date: Fri, 26 Apr 2024 23:33:08 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 1/3] mm/gup: consistently name GUP-fast functions To: Peter Xu References: <20240402125516.223131-1-david@redhat.com> <20240402125516.223131-2-david@redhat.com> <8b42a24d-caf0-46ef-9e15-0f88d47d2f21@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3Jf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+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-s390@vger.kernel.org, loongarch@lists.linux.dev, linux-sh@vger.kernel.org, John Hubbard , x86@kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Jason Gunthorpe , linux-fsdevel@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, Mike Rapoport Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" >> >>> Hmm, so when I enable 2M hugetlb I found ./cow is even failing on x86. >>> >>> # ./cow | grep -B1 "not ok" >>> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >>> not ok 161 No leak from parent into child >>> -- >>> # [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) >>> not ok 215 No leak from parent into child >>> -- >>> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 269 No leak from child into parent >>> -- >>> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >>> not ok 323 No leak from child into parent >>> >>> And it looks like it was always failing.. perhaps since the start? We >> >> Yes! >> >> commit 7dad331be7816103eba8c12caeb88fbd3599c0b9 >> Author: David Hildenbrand >> Date: Tue Sep 27 13:01:17 2022 +0200 >> >> selftests/vm: anon_cow: hugetlb tests >> Let's run all existing test cases with all hugetlb sizes we're able to >> detect. >> Note that some tests cases still fail. This will, for example, be fixed >> once vmsplice properly uses FOLL_PIN instead of FOLL_GET for pinning. >> With 2 MiB and 1 GiB hugetlb on x86_64, the expected failures are: >> # [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB) >> not ok 23 No leak from parent into child >> # [RUN] vmsplice() + unmap in child ... with hugetlb (1048576 kB) >> not ok 24 No leak from parent into child >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 35 No leak from child into parent >> # [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 36 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) >> not ok 47 No leak from child into parent >> # [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (1048576 kB) >> not ok 48 No leak from child into parent >> >> As it keeps confusing people (until somebody cares enough to fix vmsplice), I already >> thought about just disabling the test and adding a comment why it happens and >> why nobody cares. > > I think we should, and when doing so maybe add a rich comment in > hugetlb_wp() too explaining everything? Likely yes. Let me think of something. > >> >>> didn't do the same on hugetlb v.s. normal anon from that regard on the >>> vmsplice() fix. >>> >>> I drafted a patch to allow refcount>1 detection as the same, then all tests >>> pass for me, as below. >>> >>> David, I'd like to double check with you before I post anything: is that >>> your intention to do so when working on the R/O pinning or not? >> >> Here certainly the "if it's easy it would already have done" principle applies. :) >> >> The issue is the following: hugetlb pages are scarce resources that cannot usually >> be overcommitted. For ordinary memory, we don't care if we COW in some corner case >> because there is an unexpected reference. You temporarily consume an additional page >> that gets freed as soon as the unexpected reference is dropped. >> >> For hugetlb, it is problematic. Assume you have reserved a single 1 GiB hugetlb page >> and your process uses that in a MAP_PRIVATE mapping. Then it calls fork() and the >> child quits immediately. >> >> If you decide to COW, you would need a second hugetlb page, which we don't have, so >> you have to crash the program. >> >> And in hugetlb it's extremely easy to not get folio_ref_count() == 1: >> >> hugetlb_fault() will do a folio_get(folio) before calling hugetlb_wp()! >> >> ... so you essentially always copy. > > Hmm yes there's one extra refcount. I think this is all fine, we can simply > take all of them into account when making a CoW decision. However crashing > an userspace can be a problem for sure. Right, and a simple reference from page migration or some other PFN walker would be sufficient for that. I did not dare being responsible for that, even though races are rare :) The vmsplice leak is not worth that: hugetlb with MAP_PRIVATE to COW-share data between processes with different privilege levels is not really common. > >> >> >> At that point I walked away from that, letting vmsplice() be fixed at some point. Dave >> Howells was close at some point IIRC ... >> >> I had some ideas about retrying until the other reference is gone (which cannot be a >> longterm GUP pin), but as vmsplice essentially does without FOLL_PIN|FOLL_LONGTERM, >> it's quit hopeless to resolve that as long as vmsplice holds longterm references the wrong >> way. >> >> --- >> >> One could argue that fork() with hugetlb and MAP_PRIVATE is stupid and fragile: assume >> your child MM is torn down deferred, and will unmap the hugetlb page deferred. Or assume >> you access the page concurrently with fork(). You'd have to COW and crash the program. >> BUT, there is a horribly ugly hack in hugetlb COW code where you *steal* the page form >> the child program and crash your child. I'm not making that up, it's horrible. > > I didn't notice that code before; doesn't sound like a very responsible > parent.. > > Looks like either there come a hugetlb guru who can make a decision to > break hugetlb ABI at some point, knowing that nobody will really get > affected by it, or that's the uncharted area whoever needs to introduce > hugetlb v2. I raised this topic in the past, and IMHO we either (a) never should have added COW support; or (b) added COW support by using ordinary anonymous memory (hey, partial mappings of hugetlb pages! ;) ). After all, COW is an optimization to speed up fork and defer copying. It relies on memory overcommit, but that doesn't really apply to hugetlb, so we fake it ... One easy ABI break I had in mind was to simply *not* allow COW-sharing of anon hugetlb folios; for example, simply don't copy the page into the child. Chances are there are not really a lot of child processes that would fail ... but likely we would break *something*. So there is no easy way out :( -- Cheers, David / dhildenb