Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1978618rdh; Sat, 25 Nov 2023 09:02:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEXFpMINkR1z17FU65H2rpAmjixy//elokcXzia7us90GRauc5CS4wVneu1hkmYhFVR/HBn X-Received: by 2002:a17:903:228a:b0:1cf:b146:8101 with SMTP id b10-20020a170903228a00b001cfb1468101mr3743846plh.16.1700931747903; Sat, 25 Nov 2023 09:02:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700931747; cv=none; d=google.com; s=arc-20160816; b=LP+ZHQr3EBnuww5sQDMkqTrrQSm1ywRBM7TEcLEGBVQQH+wq8H0Vr3R6pRRZjNGQnc DIChOPArQkVboT2Q6PMVo5k0QOdD6mPbYYsZ7TSzkWOokDVTKmi2yLfTSeqqtV2m8HMZ 1AZDUIcXfwzDUiFM07xdyaz4uXz/vrqOikJADGF7wiTqvel7YP56cSXjJixyuA2JEhSN XN4mj9jpTpKNL2Yz9zofs6TvF42XgCI3LjZlgm5ZPigt8pyLPhqCkdBEWqJiiozp3plg tLpODwdEoBdh7scXfBgtIeAmU3TPj7T3zyyk+m9ABfv9DTwR7HKV14UwKHT5y9upQHkn K9ng== 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 :organization:autocrypt:from:content-language:references:cc:to :subject:user-agent:mime-version:date:message-id:dkim-signature; bh=r2m8MsdEA6I0cTu0yk+FCxltGAW5Bh00EQieM20jSRA=; fh=Y8+5ArXkw5xbeP1nRoIgRBSweO0uEK97I+N1kPlOjGI=; b=Gmhe71hmtGBB9XSr85lBLXDYZr83EcwxLW8sUZbEEIgxR1YiZLEHkL59FGLvlwqcDP fJqgCBpxvWEsAJKB9PsCXfusk73V9unj0FoUvJAwKssC+esxVAaxkOxdJ3rjQVmM1aDk 1zXLgYxprals7enn9jAV9UgEg7kRmh8A0+qwrJ0u279vmIeEeGgcsWPbzrCXSzOAgoqj Ms7fry2g/pB+DkRTsJ8EwaXjr+qlBHgpcLIPW1xAAiaeBF3AG7VWiYRJAKz8PWdYfLav Blwp1mcKcAopYf/w9TlTknac1yIhV1B7Mlm/UCXDL3ApCsQdibw5l1rVfbnbqGMRC7nr kY5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ACiQPqGV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id x1-20020a170902ec8100b001bb0ba81053si3989191plg.50.2023.11.25.09.02.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 09:02:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ACiQPqGV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D86688081BDF; Sat, 25 Nov 2023 09:02:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231313AbjKYRCH (ORCPT + 99 others); Sat, 25 Nov 2023 12:02:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229697AbjKYRCF (ORCPT ); Sat, 25 Nov 2023 12:02:05 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68819106 for ; Sat, 25 Nov 2023 09:02:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700931730; 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=r2m8MsdEA6I0cTu0yk+FCxltGAW5Bh00EQieM20jSRA=; b=ACiQPqGVrQ5ubwx8Rfln7AYRUgKnUGMmdHL5IPrb9/R8vXPKhgnKhCqrxo/h/Oaw/UMVUz JkYvP/hPNf33tbf9jJJ5NEh/CeT+WyqnyCYorHsWhPQ1HFl47NOP4QzQ7nOHdOIVU2e/3d yOZH9Qy2Vo5hHi0hc8Peeqt/WjTZh7E= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-300-vzlNlMWiNzGCiNdU_DUEKg-1; Sat, 25 Nov 2023 12:02:08 -0500 X-MC-Unique: vzlNlMWiNzGCiNdU_DUEKg-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2c52cbb64c9so24767721fa.1 for ; Sat, 25 Nov 2023 09:02:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700931727; x=1701536527; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :content-language: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=r2m8MsdEA6I0cTu0yk+FCxltGAW5Bh00EQieM20jSRA=; b=FPHtAr12QyqMh7Tv1HBDkH2nTj0shj9/xqH2HhuQD6a9l7cQC4NvlX2YsYRAonvLyk yv/F9aBlnAMpPQrHQnUAZGnbzd03Pjm0JgC69gDg0w2QOXIHIflr/g6jPYvryuunXCKC G3cQ2T8hsDIB5i+kTGgEMphPO8JbwmgDyT8gXzmHGwsDuZweiNO+IBRpuvCJKxEeMXfg T3hoL4GMH4zPtIskFIZiX0BMnjGBuVAD2rKLiZ86FqIX0W0q1btCBwE6UaLQVbiRAZJW PKGqz5YwD+VXbwRutl46Re4EpvYVJ6SzDLUufjjy4MhZjA9nv1FosaEbdod8j0cQi76Q K7nw== X-Gm-Message-State: AOJu0Yw8AfrJz/YGfGpHxyFuGPF7mKFeyxfeGpQTkeOd6mA8u01ozMSy 8uaRB4rjgr6CBCn5EFj9X1kc5aXL8ERd7ijkSTYCjrku6/sp/rPV1YsX/MHy6SUMNLttJ1Lx0aO z6+y6PgjGNUiH30iM/pzO1bgFLViPBM4r X-Received: by 2002:a2e:874f:0:b0:2c5:22c0:582c with SMTP id q15-20020a2e874f000000b002c522c0582cmr4170872ljj.11.1700931727058; Sat, 25 Nov 2023 09:02:07 -0800 (PST) X-Received: by 2002:a2e:874f:0:b0:2c5:22c0:582c with SMTP id q15-20020a2e874f000000b002c522c0582cmr4170859ljj.11.1700931726505; Sat, 25 Nov 2023 09:02:06 -0800 (PST) Received: from ?IPV6:2003:cb:c72b:f000:d472:dd67:5dd2:10bd? (p200300cbc72bf000d472dd675dd210bd.dip0.t-ipconnect.de. [2003:cb:c72b:f000:d472:dd67:5dd2:10bd]) by smtp.gmail.com with ESMTPSA id c37-20020a05600c4a2500b003fee6e170f9sm8222933wmp.45.2023.11.25.09.02.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Nov 2023 09:02:06 -0800 (PST) Message-ID: Date: Sat, 25 Nov 2023 18:02:04 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH WIP v1 00/20] mm: precise "mapped shared" vs. "mapped exclusively" detection for PTE-mapped THP / partially-mappable folios To: Linus Torvalds Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Ryan Roberts , Matthew Wilcox , Hugh Dickins , Yin Fengwei , Yang Shi , Ying Huang , Zi Yan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , "Paul E. McKenney" References: <20231124132626.235350-1-david@redhat.com> Content-Language: en-US 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 ZW5icmFuZCA8ZGF2aWRA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xf8HFuE+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 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sat, 25 Nov 2023 09:02:25 -0800 (PST) On 24.11.23 21:55, Linus Torvalds wrote: > On Fri, 24 Nov 2023 at 05:26, David Hildenbrand wrote: >> >> Are you interested in some made-up math, new locking primitives and >> slightly unpleasant performance numbers on first sight? :) Hi Linus, first of all -- wow -- thanks for that blazing fast feedback! You really had to work through quite some text+code to understand what's happening. Thanks for prioritizing that over Black Friday shopping ;) > > Ugh. I'm not loving the "I have a proof, but it's too big to fit in > the margin" model of VM development. > > This does seem to be very subtle. Yes, compared to other kernel subsystems, this level of math in the VM is really new. The main reason I excluded the proof in this WIP series is not the size, though. I wanted to get the implementation out after talking about it (and optimizing it ...) for way too long and (a) proofs involving infinite sequences in pure ascii are just horrible to read; (b) I think the proof can be cleaned-up / simplified, especially after I came up with the "intuition" in the patch some days ago and decided to use that one instead for now. No questions asked, if this ever gets discussed for actual merging, only with a public, reviewed proof available. [most of the "magic" goes away once one simply uses one rmap value for each bit in the mm->mm_rmap_id; 22 bit -> 22 rmap values. Of course, 22 values are undesirable.] > > Also, please benchmark what your rmap changes do to just plain regular > pages - it *looks* like maybe all you did was to add some > VM_WARN_ON_FOLIO() for those cases, but I have this strong memory of > that > > if (likely(!compound)) { > > case being very critical on all the usual cases (and the cleanups by > Hugh last year were nice). Yes, indeed. I separated small vs. large folio handling cleanly, such that we always have a pattern like: + if (likely(!folio_test_large(folio))) + return atomic_add_negative(-1, &page->_mapcount); So, the fast default path is "small folio". As stated, I want to do much more benchmarking to better understand all performance impacts; especially, on top of Ryan's work of having THPs that are always PTE-mapped and we don't have to "artificially" force a PTE-mapped THP. > > I get the feeling that you are trying to optimize a particular case > that is special enough that some less complicated model might work. > > Just by looking at your benchmarks, I *think* the case you actually > want to optimize is "THP -> fork -> child exit/execve -> parent write > COW reuse" where the THP page was really never in more than two VM's, > and the second VM was an almost accidental temporary thing that is > just about the whole "fork->exec/exit" model. That's the most obvious/important case regarding COW reuse, agreed. And also where I originally started, because it looked like the low-hanging fruit (below). For the benchmarks I have so far, I focused mostly on the performance/harm of individual operations. Conceptually, with rmap IDs, there is no performance difference if you end up reusing a THP in the parent or the child, the performance is the same, so I didn't add it manually to the micro benchmarks. > > Which makes me really feel like your rmap_id is very over-engineered. > It seems to be designed to handle all the generic cases, but it seems > like the main cause for it is a very specific case that I _feel_ > should be something that could be tracked with *way* less information > (eg just have a "pointer to owner vma, and a simple counter of > non-owners"). That's precisely where I originally started [1], but quickly wondered (already in that mail): (a) How to cleanly and safely stabilize refcount vs. mapcount. Without playing tricks such that it's just "obvious" how it's just correct and race-free in the COW reuse path. (b) How to extend it to !anon folios, where we don't have a clean entry point like folio_add_new_anon_rmap(); primarily to get a sane replacement for folio_estimated_shares(), which I just dislike at this point. (c) If it's possibly to easily and cleanly change owners (creators in my mail), without involving locks. So I started thinking about the possibility of a precise and possibly more universal/cleaner way of handling it, that doesn't add too much runtime overhead. A way to just have what we have for small folios. I was surprised to find an approach that gives a precise answer and simply changes the owner implicitly, primarily just by adding/subtracting numbers. [I'll note that having a universal way to stabilize the mapcount vs. refcount could be quite valuable. But achieving that also for small folios would require e.g., having shared, hashed atomic seqcounts. And I'm not too interested in harming small-folio performance at this point :)] Now, if we want all that sooner, later, or maybe never, is a different question. This WIP version primarily tries to show what's possible, at which price, and what the limitations are. > > I dunno. I was cc'd, I looked at the patches, but I suspect I'm not > really the target audience. If Hugh is ok with this kind of Well, I really value your feedback, and you are always in my CC list when I'm messing with COW and mapcounts. Having that said, it's encouraging that you went over the patches (thanks again!) and nothing immediately jumped at you (well, besides the proof, but that will be fixed if this ever gets merged). > complexity, I bow to a higher authority. This *does* seem to add a lot > of conceptual complexity to something that is already complicated. I'll note that while it all sounds complicated, in the end it's "just" adding/subtracting numbers, and having a clean scheme to detect concurrent (un)mapping. Further, it's handled without any new rmap hooks. But yes, there sure is added code and complexity, and personally I dislike having to go from 3 to 6 rmap values to support arm64 with 512 MiB THP. If we could just squeeze it all into a single rmap value, it would all look much nicer: one total mapcount, one rmap value. Before this would get merged a lot more has to happen, and most rmap batching (and possibly including the exclusive atomic seqcount) could be beneficial even without the rmap ID handling, so it's natural to start with that independently. Thanks again! [1] https://lore.kernel.org/all/6cec6f68-248e-63b4-5615-9e0f3f819a0a@redhat.com/T/#u -- Cheers, David / dhildenb