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 E96471D144 for ; Mon, 18 Dec 2023 14:04:49 +0000 (UTC) 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="DD3BsfGa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702908288; 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=X9xufoJq3r2B0O7H1m+byYovL1TiHBedF6yf9/U8WPw=; b=DD3BsfGatJH0eNnB7zRIoBRbaLxvEXUqNaN31QnFKiVFRtyFQlgqa9rqiPpwZR5jNT2+Co 3clYrLmPwkqAG4qEBydhehmltE6zHbFc9/wIoJCr6P8Jw1kOialh7f0+rXwCNyGFSrMs3P NCFDeLul+91K5labKmEkeoEMsSK4Dec= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-605-FB8ZwiPyPcGrobnPdats_Q-1; Mon, 18 Dec 2023 09:04:47 -0500 X-MC-Unique: FB8ZwiPyPcGrobnPdats_Q-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40c299d1e36so25083685e9.1 for ; Mon, 18 Dec 2023 06:04:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702908286; x=1703513086; h=content-transfer-encoding:in-reply-to:organization:autocrypt: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=X9xufoJq3r2B0O7H1m+byYovL1TiHBedF6yf9/U8WPw=; b=ueqqhqQYd0G5WGd1F9ZQaOozgCTbA3ppx1oTO5dsus/+C7AWDnnbpOnIVjLQ9xifXS XZRYX4ez99i9mBDyORgnNx7AwaVPkOOePNtWRPt80p+XZcDObmcoqETTrwBBmOYbij9U 5iVGKtwSp3IEo11ySKPiXzP32EKZ96X5fj4+zFj+plfdchvoUytXJShtQ17Q+DgNY7/g LOCZYSGeWgJcydc8uBK0RZfrR2/QU+gVhVnS8zbGRXJdoBL03WCG4bPCeufjDJ07KjK7 TmVxy89EVWBLcB8C6uLNc8qvNDov0DzEH1qnSif80PDJvvnMgCATJj4n0h77q/nvNWnP xOLw== X-Gm-Message-State: AOJu0Ywayk1K9FeAb8nPUOVOmsik5XRMULIa+E1P8HH5UEPwqltiClgs GrHp8VSRTvhFT1BVgeF0lTsffJgBqp9ifYB6iVRt1cVBnPZdWtB/bC1bbKXHEedIH4fwEHjJSuq 8UMjRO/hAX48eTcn+YC03pS5uVKNylNNY X-Received: by 2002:a05:600c:1553:b0:40d:1778:c839 with SMTP id f19-20020a05600c155300b0040d1778c839mr800478wmg.141.1702908286035; Mon, 18 Dec 2023 06:04:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcEe4oxIKdoWGJpNNgct2Y7QUZNBaHPed36CrMgdRYdzYq01y0NjIDBP2vA8dNftPuSdKBGQ== X-Received: by 2002:a05:600c:1553:b0:40d:1778:c839 with SMTP id f19-20020a05600c155300b0040d1778c839mr800468wmg.141.1702908285537; Mon, 18 Dec 2023 06:04:45 -0800 (PST) Received: from ?IPV6:2003:cb:c72b:b500:b53e:6e32:1408:27ac? (p200300cbc72bb500b53e6e32140827ac.dip0.t-ipconnect.de. [2003:cb:c72b:b500:b53e:6e32:1408:27ac]) by smtp.gmail.com with ESMTPSA id ay35-20020a05600c1e2300b0040b2b38a1fasm42693165wmb.4.2023.12.18.06.04.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Dec 2023 06:04:45 -0800 (PST) Message-ID: <944d990f-3c98-4ade-8176-4e4b25eae0b8@redhat.com> Date: Mon, 18 Dec 2023 15:04:43 +0100 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 WIP v1 07/20] mm/rmap_id: track if one ore multiple MMs map a partially-mappable folio Content-Language: en-US To: Nadav Amit Cc: Linux Kernel Mailing List , linux-mm , Andrew Morton , Linus Torvalds , 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> <20231124132626.235350-8-david@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//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGj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HdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 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: 8bit On 17.12.23 20:13, Nadav Amit wrote: > >> On Nov 24, 2023, at 3:26 PM, David Hildenbrand wrote: >> >> 5. sub-IDs >> ========== >> >> To achieve (2), we generate sub-IDs that have the following property, >> assuming that our folio has P=folio_nr_pages() pages. >> "2 * sub-ID" cannot be represented by the sum of any other *2* sub-IDs >> "3 * sub-ID" cannot be represented by the sum of any other *3* sub-IDs >> "4 * sub-ID" cannot be represented by the sum of any other *4* sub-IDs >> ... >> "P * sub-ID" cannot be represented by the sum of any other *P* sub-IDs >> >> The sub-IDs are generated in generations, whereby >> (1) Generation #0 is the number 0 >> (2) Generation #N takes all numbers from generations #0..#N-1 and adds >> (P + 1)^(N - 1), effectively doubling the number of sub-IDs >> >> Consequently, the smallest number S in gen #N is: >> S[#N] = (P + 1)^(N - 1) >> >> The largest number L in gen #N is: >> L[#N] = (P + 1)^(N - 1) + (P + 1)^(N - 2) + ... (P + 1)^0 + 0. >> -> [geometric sum with "P + 1 != 1"] >> = (1 - (P + 1)^N) / (1 - (P + 1)) >> = (1 - (P + 1)^N) / (-P) >> = ((P + 1)^N - 1) / P > > David, as you know it took me a while to understand your impressive work. > Hi Nadav, thanks a bunch for digging into the details of the solution and trying to verify the correctness! And thanks a lot for highlighting the special case below that I intuitively used for the "intuition" :) > I think that part of what made it hard for me is the presentation and the > formulation of sub-IDs in arithmetic means instead of bit manipulations. Yes, that needs more work to make it all easier to understand. > > Basically, IIUC, you want for order-K pages to add K-1 “0” bits between > each rmap-id bits. > > In this case, in x86-64 (with BMI2) there is the PDEP instruction that can > generate these values rather easily with little overhead. Partially, yes. What you describe here is a special case that is easier to "grasp", likely a bit faster to calculate, but ends up being less-optimal in space consumption for some cases (especially, order-2 and order-9 folios). You are describing the special case where "P+1" is a power of two. Let me explain using the example (P = 3) from the "intuition" further down in this patch description, highlighting the mapping: RMAP-ID | |Subid | ----------------------------------- 0 | 0000 | 0 | 0000 0000 1 | 0001 | 1 | 0000 0001 2 | 0010 | 4 | 0000 0100 3 | 0011 | 5 | 0000 0101 4 | 0100 | 16 | 0001 0000 5 | 0101 | 17 | 0001 0001 6 | 0110 | 20 | 0001 0100 7 | 0111 | 21 | 0001 0101 8 | 1000 | 64 | 0100 0000 9 | 1001 | 65 | 0100 0001 10 | 1010 | 68 | 0100 0100 11 | 1011 | 69 | 0100 0101 12 | 1100 | 80 | 0101 0100 13 | 1101 | 81 | 0101 0001 14 | 1110 | 84 | 0101 0100 15 | 1111 | 85 | 0101 0101 So going from e.g., 11 -> 69 means adding one zero for each bit, just like you said. But adding 1 "0" bit is not sufficient for handling order-2 folios (P = 4), only for handling order-1 folios. So what the current approach does is the following (P = 4): RMAP-ID | | Subid | ----------------------------------- 0 | 0000 | 0 | 0000 0000 1 | 0001 | 1 | 0000 0001 2 | 0010 | 5 | 0000 0101 3 | 0011 | 6 | 0000 0110 4 | 0100 | 25 | 0001 1001 5 | 0101 | 26 | 0001 1010 6 | 0110 | 30 | 0001 1110 7 | 0111 | 31 | 0001 1111 8 | 1000 | 125 | 0111 1101 9 | 1001 | 126 | 0111 1110 10 | 1010 | 130 | 1000 0010 11 | 1011 | 131 | 1000 0011 12 | 1100 | 150 | 1001 0110 13 | 1101 | 151 | 1001 0111 14 | 1110 | 155 | 1001 1011 15 | 1111 | 156 | 1001 1100 Which is not just adding "0"s. To handle it using your simplification we'd have to add one more "0" bit to be able to use it for order-2 folios. So I'll refine your statement to: for order-K pages to add K “0” bits between each rmap-id bits. Then, it's easy to see how going from 24 RMAP-ID bits for an order-2 page would require with that simplification 3*24 = 72bit and would no longer fit into a single 64bit value. To summarize, with the optimized (currently implemented) scheme, we achieve: * == order-2: 1x 64bit rmap values * <= order-5: 2x 64bit rmap values * <= order-9: 3x 64bit rmap values * <= order-10: 4x 64bit rmap values * <= order-12: 5x 64bit rmap values * <= order-15: 6x 64bit rmap values * ... I think, going with the simplification above (to be double-checked), we'd achieve [essentially, subtracting 1 from all orders]: * == order-1: 1x 64bit rmap values * <= order-4: 2x 64bit rmap values * <= order-8: 3x 64bit rmap values * <= order-9: 4x 64bit rmap values * <= order-11: 5x 64bit rmap values * <= order-14: 6x 64bit rmap values * ... So especially for order-9 folios we would require 4 instead of 3 64bit values. Now, going with this simplification as first step makes absolute sense, because it's much more intuitive and eventually a bit easier to implement (although really not significantly IMHO). One can then easily add the optimization on top later. > > I think that besides the easier generation of sub-ids values in this > manner, discussing the matter without the “generations" also makes it > easier to understand the correctness (at least for me). Yes, that presentation certainly needs improvement. Generating the magic numbers recursively makes it easier to proof the correctness using induction, that's how I started to use that whole "generation" terminology. -- Cheers, David / dhildenb