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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DAB0AC5475B for ; Wed, 6 Mar 2024 15:18:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70FDD6B0075; Wed, 6 Mar 2024 10:18:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C1186B007D; Wed, 6 Mar 2024 10:18:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5390A6B007E; Wed, 6 Mar 2024 10:18:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3B59F6B0075 for ; Wed, 6 Mar 2024 10:18:53 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0D0E4A1582 for ; Wed, 6 Mar 2024 15:18:53 +0000 (UTC) X-FDA: 81866971746.10.B851CB2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf05.hostedemail.com (Postfix) with ESMTP id CF7CB10000B for ; Wed, 6 Mar 2024 15:18:50 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GdP1Odyb; spf=pass (imf05.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709738331; h=from:from:sender: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:dkim-signature; bh=B3AgORaESFFvox9zpKLbd1ltP47eOPbrMICuVAI5M0s=; b=dCtgMvJuNCu8RxHtBxT35thKGXOkDNhi67tQnnD5DMTFN5hJv90kOE1rk/rZch+5oN0/Hd ljfxSPYsu5L7ok3zcG4++GGYj2R79K1+noHAsAZY/wTWG8BLJ6U4d/HR/iMvwVEn62blOs /9HYlQN45nuWMla8lPQK9uwlvGdLLSI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709738331; a=rsa-sha256; cv=none; b=t7a84wFmmFHdgsjm/BX3vwhn3pqCw7SR4SKANr/U3kAEBd7eRvanSxM3Yf4LX6OQBF95z7 dpZ4fQ88oJvohegFEvS5ZVemchrrpW5RLfh3KILzTjdOUv7vOyE5oJTj9XJcyj09mN5uYk 2d2KRMWR+XSwPtolxB/BL9dHrAL+yg0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GdP1Odyb; spf=pass (imf05.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709738330; 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=B3AgORaESFFvox9zpKLbd1ltP47eOPbrMICuVAI5M0s=; b=GdP1OdybrcT7d+uY/HHp6rIOwAzqQGlGOHr+5qVpMQBdTmTpOo9XB1OOKQWoxqjKEisrCh /sy7Aw3sao55DWTvcZYrI0U2+IWpFpshQqJAunQvCHyjoq1aO3F8g8Xjr8Gs8m0f+hVxdB 1/WFEMjvB5zjaAOE5lK92+EspBXBIew= 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-636-k6M7GwaBOVSfw4MscMILIA-1; Wed, 06 Mar 2024 10:18:48 -0500 X-MC-Unique: k6M7GwaBOVSfw4MscMILIA-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-33deb1f7afdso580632f8f.2 for ; Wed, 06 Mar 2024 07:18:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709738327; x=1710343127; 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=B3AgORaESFFvox9zpKLbd1ltP47eOPbrMICuVAI5M0s=; b=JiN5IKiVESUqiRC8Lr0msfP6a2VULd8IFmhjOtsk9JiPjH6/wvPBBU3AYv+igYSM0B 6ZfxT+nd3Sb5Qak4l03zwvANc1zanGPiaOXublElFahR/TDSat3ku84utQ0ykakAbWGC KNcop8A0IqE8BuiuXsEWGHal4JEgBOfCpuX6zo5N34W2WCrDqq10lLXC0UYzjxfp0xGe aKpbz6WZTM9svfyQuP1yHjWd4k1bkFufk4f53CC8/raJ0v71eVxoZbTCVDygpo51Rd9Z C3xNJGA5aAeEVeuDxO9mbZT1vdVtkbCRD+ia2uE4qd6CTWeSFtool1GgMglsjkKBZLas tG/g== X-Gm-Message-State: AOJu0YzNXYbNzFCorlqMDKtdblSpQuVDWzDrIudDuYbPJCHRSiSk3MN4 4lcoasEz7t8aMIz8XdFnFxYqCPiy5BUyi/rxjvlftnrOYGDfIxrWL2xqy3fyt/1GEh3++NtKAGt +jh13t4MXKXps8jj8n3tUpWBAcbLm69queeNwWNbEI0HCB5Nl X-Received: by 2002:a05:6000:108d:b0:33d:47c6:8f3e with SMTP id y13-20020a056000108d00b0033d47c68f3emr10114826wrw.41.1709738327361; Wed, 06 Mar 2024 07:18:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IFm3P7ItsL4hCJ0E/3bb6q7bBVguDSYnE1QJYe8mnPmGm7Ovz/lHKsav81WRtCFmno/X7G9qQ== X-Received: by 2002:a05:6000:108d:b0:33d:47c6:8f3e with SMTP id y13-20020a056000108d00b0033d47c68f3emr10114811wrw.41.1709738326846; Wed, 06 Mar 2024 07:18:46 -0800 (PST) Received: from ?IPV6:2003:cb:c744:e000:ec38:8ec6:b1c4:c844? (p200300cbc744e000ec388ec6b1c4c844.dip0.t-ipconnect.de. [2003:cb:c744:e000:ec38:8ec6:b1c4:c844]) by smtp.gmail.com with ESMTPSA id cc4-20020a5d5c04000000b0033e45e4f22bsm6097346wrb.73.2024.03.06.07.18.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Mar 2024 07:18:46 -0800 (PST) Message-ID: <52599fd8-76dc-4d8f-b9f2-78146fc7a518@redhat.com> Date: Wed, 6 Mar 2024 16:18:45 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/5] Remove some races around folio_test_hugetlb To: Matthew Wilcox Cc: linux-mm@kvack.org, Oscar Salvador References: <20240301214712.2853147-1-willy@infradead.org> 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/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v0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+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: 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-Rspamd-Queue-Id: CF7CB10000B X-Rspam-User: X-Stat-Signature: bqceobumagc4gy6733df8apwsy6w89dg X-Rspamd-Server: rspam03 X-HE-Tag: 1709738330-345800 X-HE-Meta: U2FsdGVkX1/kDrlKFgxcQ46TK2TcQXK8ilFzpVTOpbHtP8vvterrVpzMiG+YER5XQ3HHaMc+cte9UBbRNMc+So/TXYVD3I/EnR2Cy3y02F4U13DxNMn1ovCvKpz6tDuH+15wFF8FCXSjPNRquT8nyTZ+ZaxLPJpDeifYaObVxOT888KP2QMO97aNKK7eZISIV/YvY+W1S7G4sQAax6ef6vnjzrhJ0GZxPCHisMtjTFZnKJpn/Ax7xXgae8UxanPUrefTZzm4ZGPld1bvQRThVQlTTO7Lt0JWyWxJNC+iO7MbJoglvy3juEkTcFRCa//D5kaBnt9Qqs6Fxb0zoROH0ywPR6sGpC5+GjN7M2qX5oAjU+vRF7krgPWMsIdnwbUFPOG951rsVCLRYYprh/F5fsn47u9AtfhLyf8pzJzv7282/4KorW2cu8qXroQFRGQwQn/fW3crbb9+pImCV08nx4UhXXhcmgKl3DF+WapTbHwiKMoKLmyvVdKW5jznR92qI+YvRzJWmem6hmpA/Xzka3KRH5T/+ZkPohjJogAgwVxVokTgroEb15k13QJ5PLHumuJvxzd0Jb8xBPZFsulDnAwrs8nB4ABZ0IOxEfYNbK9I+sI8AZ4QglDZCznVHDDx4ab1XzTnDsWrZQnAVdwTkr/TvcjtGjtMuxEApNug/lNsUPJGoLoSmfZAR/iRhY1DTD3nJtwsfFW4mHVRtEFMpTqkr+t8mIVlljBjrZjEoN8kMsbEIvVthSEISTpBpjn7YwvIXlBvd/3V1b7ygFedUT2IPSPFfNHdO3ckNVF4ommsZXwK+CYRQOkF8i1dzkv8GoYkr0JrvTkHcXTQDLcOH4/kmjA1NPlOIn8ViRtuVNToE6EQ2JIGUM/leMiYm8d1k64fsTDak3IRQK+q5IRIM6l8qnnq7LvKFn7TvL9vd1+VMyTWbtTKJ356iojDIvKVlOOgdF6NRnG+kE6Thvp 9RjlPNnf hmqzhujp1HrkyM7iz0Iel1eQ/dV+Bi0fN/bomSZX6mSpP16FwHVGT40YtPW60wbQ3EStskTLjekBHvo0Thxy7zCmer2bxyf5k2k/lnqH0y20YcDotwjBoEuZHcA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 05.03.24 21:35, Matthew Wilcox wrote: > On Tue, Mar 05, 2024 at 10:10:08AM +0100, David Hildenbrand wrote: >>> The cost of this reliability is that we now consume the word I recently >>> freed in folio->page[1]. I think this is acceptable; we've still gained >>> a completely reliable folio_test_hugetlb() (which we didn't have before >>> I started messing around with the folio dtors). Non-hugetlb users >>> can use large_id as a pointer to something else entirely, or even as a >>> non-pointer, as long as they can guarantee it can't conflict (ie don't >>> use it as a bitfield). >> >> That probably means that we have to always set the lowest bit to use it for >> something else, or use another bit. > > Yes, that would work. > >> I was wondering if >> >> a) We could move that to another subpage. In hugetlb folios we have plenty >> of space for such things. I guess we'd have be able to detect the folio size >> without holding a reference, to make sure we can touch another subpage. > > Yes, that was my concern. I wanted to put it in page[2] with all the > other hugetlb goop, but I got to thinking about an order-1 compound > page allocated at the end of memmap and got scared. We could make > folio_test_hugetlb() look at ->flags for the head bit, then look at > ->flags_1 for the order and finally at ->hugetlb_id, but now we've looked > at three cachelines to answer a fairly frequent question. And then what > if the folio got split between looking at ->flags and ->flags_1 and we > get a bogus folio order that makes it look OK? We can't even look at > ->flags, ->flags_1 and recheck ->flags because it might have got split, > freed and reallocated in the meantime. > >> b) We could overload _nr_pages_mapped. We'd effectively have to steal one >> bit from _nr_pages_mapped to make this work. >> >> Maybe what works is using the existing mechanism (hugetlb flag), and then >> storing the pointer in __nr_pages_mapped. >> >> So depending on the hugetlb flag, we can interpret __nr_pages_mapped either >> as the pointer or as the old variant. >> >> Mostly only folio_large_is_mapped() would need care for now, to ignore >> _nr_pages_mapped if the hugetlb flag is set. > > I don't mind that at all. We wouldn't even need to steal a bit or use the > existing flag; we could just say that -2 means this is a hugetlb folio. > As long as it ends up at the same offset as page->mapping (because that's > always NULL or a pointer possibly with a low bit set so can't ever be a > number between -4095 and -1). Would hugetlb_id below be 32bit or 64bit on 64-bit? > > IOW: > > word page0 page1 > 0 flags flags > 1 lru.next head > 2 lru.prev entire_mapcount + gap > 3 mapping nr_pages_mapped + gap / hugetlb_id > 4 index pincount + nr_pages > 5 private unused > 6 mapcount+refcount mapcount+refcount(0) > 7 memcg_data - > > or on 32-bit > > word page0 page1 > 0 flags flags > 1 lru.next head > 2 lru.prev entire_mapcount > 3 mapping nr_pages_mapped / hugetlb_id ^ In the worst case, I think, nr_pages_mapped with a lot of entire mappings could end up matching hugetlb_id. We add a large value to nr_pages_mapped every time we add an entire mapping ... (not sure if that could currently be a problem with many entire mappings of a large folio) > 4 index pincount > 5 private unused > 6 mapcount mapcount > 7 refcount refcount > 8 memcg_data - > 9+ virtual? last_cpupid? whatever > > Does this fit with your plans? For the total mapcount this would do (and it would be better), but the layout gets a bit "sparse" on 64bit that way, which will end up being problematic for some other stuff I might want to put in there. Not that we have to resolve that now, just bringing it up, that maybe we can do better right away :) IIUC, we would not be able to reuse the "gap" in "nr_pages_mapped + gap / hugetlb_id", essentially consuming an additional 32bit compared to what we do now, correct? I was thinking of the following, assuming your example above indicated on64bit a hugetlb_id that is 64bit: hugetlb folios set * compound page * flag in subpage 1 (like we do) * nr_pages_mapped in subpage 1 to e.g., -2. So to check lockless if we have a hugetlb folio * Check if compund * Check if the flag in subpage 1 is set * Check if nr_pages_mapped matches Would that still be too unreliable? -- Cheers, David / dhildenb