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 305121E0B74 for ; Thu, 30 Jan 2025 15:43:16 +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=1738251798; cv=none; b=Vu5wpYU/4hFvmmABvoaO0xj8gk5rBHzMIdjolaDbyiLXJYYDu6AskvlS0esc3cLKkUiNCxcB/n1Sq7C3OObsMtf9d4uz6rAKZLxIYARdTFFWYJTw9toQ12QvS2ZFYAMry+9WON/YL2qfMbBy1JeiXl2TEow1a5hAcZ3DFDk9eSE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738251798; c=relaxed/simple; bh=22A1Y4gI6DxYYwteH4vtrnvqyKs64d/Rt5SgtQwCGf4=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=PG4Noxdc+ZU0Lw2HzdjrEtLI/yxUn1VBKk1aXCAwHj4rGt9TUzJYpcuR40x+S19SW6uEXwCDRdu0wZevmh82HmkkgAI20bH0s6u6uZKeVN244asx2CmmI/O3RiNeu5XFpEBkgFiKeoTNGd9JQLOpsqV/MHo+favmkw1WtyXRS3I= 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=XKqH5qiR; 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="XKqH5qiR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738251796; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to: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=E8VqjgOdCdCLYF29XdRz8twWYIg6gPk2sCF7nSD+9Lc=; b=XKqH5qiR3C0Q5NsMslZJzYh2k/f1wczPWbRr3xgIDQttFp8OxCAaEkxSwTUzwrpvpXjw4o rrOHXf9R4iPdFPOOGx1qjXwwfcgiohYGC0hDNbbpi0mEugfAAScttx/oEFdbcJ1+lMmDbZ aa91FiDiZP5PUbYDnLNMV6/btiJx+e0= 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-119-u2ocutDTPIq6URhoqFkCaw-1; Thu, 30 Jan 2025 10:43:12 -0500 X-MC-Unique: u2ocutDTPIq6URhoqFkCaw-1 X-Mimecast-MFC-AGG-ID: u2ocutDTPIq6URhoqFkCaw Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-38bf4913669so473185f8f.2 for ; Thu, 30 Jan 2025 07:43:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738251791; x=1738856591; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=E8VqjgOdCdCLYF29XdRz8twWYIg6gPk2sCF7nSD+9Lc=; b=oFCpIbLAiqJChqPNWzdeDafAIuiDbmlMxN2n53K0LQAq+ahpkL9MgPfxgt9w6eQXj2 iej43yKTk+pJ72uiiPDrv2sOtzp2ssIt2img4xJXU3+rQbcjD5paNR/2UjLp450EIbdJ rYQJhFKLxZlVNdtIiWRZsMhDtxR21CxfNZaBJYxZN9q7adXknnHusk0uGFbQ06Qq7gXG AqvmCMfXrngxUSnUxG5s1I5LhDrHzip6Paf/SJY5iLaU522tIaAzJDIWcuT6rChRskb5 RlxbYe77sjKuMboqJZpNDmE6DeKrges3WrSIIJRbEtVE6lsnIkh5ieYMlScr7kBoFNcv kYXg== X-Gm-Message-State: AOJu0Ywap44ibQnjHv1APyDSb8ZAH4IOZ/SNUDfGnzH6BDX95nu/Xtij nRL+0H52Ny0EdzSyU+xfMPuM607x1ImncZWF72zqJDO0SB9Nw5Ufa4PML/RvsNYoEGVzRzatUC8 h2M/cLy6tD7SnTSnuyPseVoNotCGMpPxekBEVI6NO7L/sWnHoo8NHyyTQjobduG5lPAqkOfLEZD pVTpq3bLOFTQGLaA+QFnZXeB5RDV/+O2uLDcYqe/JEE3Tq X-Gm-Gg: ASbGncttqUEyzfJ4owg4gOI76QmcXRuGB9O/YJITnMC5iGGaB5QYvPf4QPiDDIpoGgq R4nDq0tj/7THBb1uDufnAQDzNcUwNjcmQkQaLetDS9VSlyyDpWQ2LkaoycRV9jiAmvcYpCaxifr bofnJEjpxRVAYeEmPAXj1K+FmvxgDOlgYsCOvyEwrmewodzoRjqAaYF3bEX6reKUnIRtJtOIFqi hQ7MMgUa9Xk81K3dpbVLkjhdJDyXy3RzvCU5Lpeu25Fp30cOPp1i97ZG4O0oby3CddZPtkOn6Wf DBEkmvL3jvtuWEoEY+SEHmEodTBzEroqu0m5uOwip/xUo0sfXeFl9XzLerDq9BmqEiA4+ZNNCyV 9af8py/Z2hjn8M0m3f3NhnQg7bWDEwzpI X-Received: by 2002:a5d:64e4:0:b0:38a:8d4c:aad3 with SMTP id ffacd0b85a97d-38c5194b957mr6843512f8f.18.1738251791572; Thu, 30 Jan 2025 07:43:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLO59HpypkIWrcJ/WRB/dj0EyzKgw1ZdKghuaXSMYdKrXN8wI/cUA3y+0uHXVGa3XTW9zA9g== X-Received: by 2002:a5d:64e4:0:b0:38a:8d4c:aad3 with SMTP id ffacd0b85a97d-38c5194b957mr6843463f8f.18.1738251790992; Thu, 30 Jan 2025 07:43:10 -0800 (PST) Received: from ?IPV6:2003:cb:c713:3b00:16ce:8f1c:dd50:90fb? (p200300cbc7133b0016ce8f1cdd5090fb.dip0.t-ipconnect.de. [2003:cb:c713:3b00:16ce:8f1c:dd50:90fb]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c5c1cf560sm2281427f8f.89.2025.01.30.07.43.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jan 2025 07:43:10 -0800 (PST) Message-ID: Date: Thu, 30 Jan 2025 16:43:08 +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 v1 12/12] mm/rmap: keep mapcount untouched for device-exclusive entries To: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, Andrew Morton , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe References: <20250129115411.2077152-1-david@redhat.com> <20250129115411.2077152-13-david@redhat.com> <887df26d-b8bb-48df-af2f-21b220ef22e6@redhat.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ d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HyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+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: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit >> Assume you have a THP (or any mTHP today). You can easily trigger the >> scenario that folio_mapcount() != 0 with active device-exclusive entries, >> and you start doing rmap walks and stumble over these device-exclusive >> entries and *not* handle them properly. Note that more and more systems are >> configured to just give you THP unless you explicitly opted-out using >> MADV_NOHUGEPAGE early. >> >> Note that b756a3b5e7ea added that hunk that still walks these >> device-exclusive entries in rmap code, but didn't actually update the rmap >> walkers: >> >> @@ -102,7 +104,8 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw) >> >> /* Handle un-addressable ZONE_DEVICE memory */ >> entry = pte_to_swp_entry(*pvmw->pte); >> - if (!is_device_private_entry(entry)) >> + if (!is_device_private_entry(entry) && >> + !is_device_exclusive_entry(entry)) >> return false; >> >> pfn = swp_offset(entry); >> >> That was the right thing to do, because they resemble PROT_NONE entries and >> not migration entries or anything else that doesn't hold a folio reference). > > Yeah I got that part. What I meant is that doubling down on this needs a > full audit and cannot rely on "we already have device private entries > going through these paths for much longer", which was the impression I > got. I guess it worked, thanks for doing that below :-) I know I know, I shouldn't have touched it ... :) So yeah, I'll spend some extra work on sorting out the other cases. > > And at least from my very rough understanding of mm, at least around all > this gpu stuff, tracking device exclusive mappings like real cpu mappings > makes sense, they do indeed act like PROT_NONE with some magic to restore > access on fault. > > I do wonder a bit though what else is all not properly tracked because > they should be like prot_none except arent. I guess we'll find those as we > hit them :-/ Likely a lot of stuff. But more in a "entry gets ignored -- functionality not implemented, move along" way, because all page table walkers have to care about !pte_present() already; it's just RMAP code that so far never required it. [...] > >> If thp constantly reassembles a pmd entry because hey all the >>> memory is contig and userspace allocated a chunk of memory to place >>> atomics that alternate between cpu and gpu nicely separated by 4k pages, >>> then we'll thrash around invalidating ptes to no end. So might be more >>> fallout here. >> >> khugepaged will back off once it sees an exclusive entry, so collapsing >> could only happen once everything is non-exclusive. See >> __collapse_huge_page_isolate() as an example. > > Ah ok. I think might be good to add that to the commit message, so that > people who don't understand mm deeply (like me) aren't worried when they > stumble over this change in the future again when digging around. Will do, thanks for raising that concern! > >> It's really only page_vma_mapped_walk() callers that are affected by this >> change, not any other page table walkers. > > I guess my mm understanding is just not up to that, but I couldn't figure > out why just looking at page_vma_mapped_walk() only is good enough? See above: these never had to handle !page_present() before -- in contrast to the other page table walkers. So nothing bad happens when these page table walkers traverse these PTEs, it's just that the functionality will usually be implemented. Take MADV_PAGEOUT as an example: madvise_cold_or_pageout_pte_range() will simply skip "!pte_present()", because it wouldn't know what to do in that case. Of course, there could be page table walkers that check all cases and bail out if they find something unexpected: do_swap_page() cannot make forward progress and will inject a VM_FAULT_SIGBUS if it doesn't recognize the entry. But these are rather rare. We could enlighten selected page table walkers to handle device-exclusive where it really makes sense later. -- Cheers, David / dhildenb