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.133.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 00CD016A37C for ; Wed, 28 Aug 2024 10:49:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724842142; cv=none; b=YLFNfhf+ZpL0xjUsnT+BiwCx7OwQVtecWNCpMQmNk1qHlu7yyRrqSoEXvjD48gkF2XFppFwzqDO3ahfG0T2N7A5/FjMdWKAgrrvADqwXm7v7z4L17YyYQT+zMHUYq09ajuc/owV7HlYSxDY1F2ZZzqrkWheCmvffoM6VYguhMvM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724842142; c=relaxed/simple; bh=I7NwsmOWQF9SQ+vAkMVgevHVS6irMTE3H5Pe8w98qi4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Fwe8GcYb40K4jJ6lnAwY1OyXK0XbxplRbO/O2++5vjRJQ7SNYy2GS7NEOF5mAHR2yPQg8d0idTAr2F8YUTfR/I+LFBUqLa1Jk76aX2EKYu9M1l96ohETAZeVHcxIzgSzQN45ZU4jLagDIM8iXt2OX1jFM8rKcepIlwgxCUCpq90= 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=ES2Uth4R; arc=none smtp.client-ip=170.10.133.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="ES2Uth4R" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724842140; 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=wvYsNaL9w1gkGPGglv5zQzpSgbJlfB/etLX3d1fPOrc=; b=ES2Uth4Rs8NuVA8cMsBXkZglBgiPIZ1VGOPvExWTdw0O77dhSZxXNNKWAR8PSSgZXZ4gWJ g0EcZGkbweUsPhwN5PpZ5GSc9L/KkJwBdJsC/usVSgUeIWukBeKJbuz/QwJhhEb4B6yUnH ASxzzWUmP+sBP5xlghB5QyxoJtKl/Ww= 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-246-TFKLgKn3Po2xWcm-X5VQRA-1; Wed, 28 Aug 2024 06:48:58 -0400 X-MC-Unique: TFKLgKn3Po2xWcm-X5VQRA-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3718c1637caso4001988f8f.0 for ; Wed, 28 Aug 2024 03:48:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724842138; x=1725446938; 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=wvYsNaL9w1gkGPGglv5zQzpSgbJlfB/etLX3d1fPOrc=; b=RicqOUw6KudGlLlG48znxkGlKpiJ97Jh98J6sr5RTm1ivVex8Dm/KXPGbYHNAQFCM5 2tCQMZWCkeRppdbFKWhgkqtcInyBmmKGFT3gk3u3kNbP9keCzkkNCO+feCs2nbZQgBHx d+7LLGB2DQ0sdYJfJY5ANaDMQc1YrKqM9e7WzE5R+gGvUJ94LNbah9ExYSMwi+31vru5 kdUXtsLylTwig2faIFy+wfaKQoAVHmkHZEPatRptDKUlG0L38SN7fdvk9+y6XW88cHBc W5zKsZUlqC2p5TLTB88+Wmc8H7tNQ6Fn3ZQqcbZIrHK6SedWcDMTwCbZ5E1jMFyu6ZjS jPqQ== X-Forwarded-Encrypted: i=1; AJvYcCUwiVC8UQ/5QIe9Ca+ud0qv9WC4fxkTrhxTnqDdhIdeA9e6UgSoc7oSRvAejaV4AoLdaF5jIhD4Jr5bfJw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxx6BuyyxThEornXq/Clf2QOwnneg/q3NO47fifgUMUe5LB9ipf Unp4DJ1hLdiU/QiHJ/3rzLWgwi0rgPPN+sattiYN2bW8XdUafxgpqmFMST2NX/R3n7PKDIrtkvs FCs8+CaaSKjMvXfP0zJEgAYzLidL2KIxIVzaNQUxGa6cDAtrP7PWQS0gtVf76YQ== X-Received: by 2002:a5d:63c8:0:b0:371:86bc:6c05 with SMTP id ffacd0b85a97d-37311840242mr10099959f8f.10.1724842137588; Wed, 28 Aug 2024 03:48:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF7EFzajU9R0cIfi0WpVWAwu9xU6E8957dEvkMylSwWZUSBfj7al0N2kiYw185R28Bx93YG8g== X-Received: by 2002:a5d:63c8:0:b0:371:86bc:6c05 with SMTP id ffacd0b85a97d-37311840242mr10099930f8f.10.1724842136774; Wed, 28 Aug 2024 03:48:56 -0700 (PDT) Received: from ?IPV6:2003:cb:c706:1700:6b81:27cb:c9e2:a09a? (p200300cbc70617006b8127cbc9e2a09a.dip0.t-ipconnect.de. [2003:cb:c706:1700:6b81:27cb:c9e2:a09a]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42ba61c0666sm18312685e9.0.2024.08.28.03.48.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Aug 2024 03:48:56 -0700 (PDT) Message-ID: <42aba316-2b01-4fdb-9aff-9e670aac4c6e@redhat.com> Date: Wed, 28 Aug 2024 12:48:55 +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 v2 01/14] mm: pgtable: introduce pte_offset_map_{ro|rw}_nolock() To: Qi Zheng Cc: hughd@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, akpm@linux-foundation.org, rppt@kernel.org, vishal.moola@gmail.com, peterx@redhat.com, ryan.roberts@arm.com, christophe.leroy2@cs-soprasteria.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org References: <4481a0e4-a7a5-4223-a8ab-d1215d7c6352@bytedance.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 UucZa4OT27U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vSJOx/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: <4481a0e4-a7a5-4223-a8ab-d1215d7c6352@bytedance.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 27.08.24 06:33, Qi Zheng wrote: > Hi David, > > On 2024/8/26 23:21, David Hildenbrand wrote: >> On 22.08.24 09:13, Qi Zheng wrote: >>> Currently, the usage of pte_offset_map_nolock() can be divided into the >>> following two cases: >>> >>> 1) After acquiring PTL, only read-only operations are performed on the >>> PTE >>>     page. In this case, the RCU lock in pte_offset_map_nolock() will >>> ensure >>>     that the PTE page will not be freed, and there is no need to worry >>>     about whether the pmd entry is modified. >> >> There is also the usage where we don't grab the PTL at all, and only do >> a racy (read-only) lookup. > > IIUC, pte_offset_map() should be used instead of pte_offset_map_nolock() > in this case. Yes, but the filemap.c thingy conditionally wants to lock later. But I agree that pte_offset_map() is better when not even wanting to lock. [...] >>> accessor functions: >>>    - pte_offset_map_nolock() >>>       maps PTE, returns pointer to PTE with pointer to its PTE table >>>       lock (not taken), or returns NULL if no PTE table; >> >> What will happen to pte_offset_map_nolock() after this series? Does it >> still exist or will it become an internal helper? > > I choose to remove it completely in [PATCH v2 13/14]. > Ah, great. [...] >> If someone thinks not requiring a non-NULL pointer is better, please >> speak up, I'm not married to that idea :) >> >>> +    pte = __pte_offset_map(pmd, addr, &pmdval); >>> +    if (likely(pte)) >>> +        *ptlp = pte_lockptr(mm, &pmdval); >>> +    *pmdvalp = pmdval; >>> +    return pte; >>> +} >>> + >>>   /* >>>    * pte_offset_map_lock(mm, pmd, addr, ptlp), and its internal >>> implementation >>>    * __pte_offset_map_lock() below, is usually called with the pmd >>> pointer for >>> @@ -356,6 +383,22 @@ pte_t *pte_offset_map_nolock(struct mm_struct >>> *mm, pmd_t *pmd, >>>    * recheck *pmd once the lock is taken; in practice, no callsite >>> needs that - >>>    * either the mmap_lock for write, or pte_same() check on contents, >>> is enough. >>>    * >>> + * pte_offset_map_ro_nolock(mm, pmd, addr, ptlp), above, is like >>> + * pte_offset_map(); but when successful, it also outputs a pointer >>> to the >>> + * spinlock in ptlp - as pte_offset_map_lock() does, but in this case >>> without >>> + * locking it.  This helps the caller to avoid a later >>> pte_lockptr(mm, *pmd), >>> + * which might by that time act on a changed *pmd: >>> pte_offset_map_ro_nolock() >>> + * provides the correct spinlock pointer for the page table that it >>> returns. >>> + * For readonly case, the caller does not need to recheck *pmd after >>> the lock is >>> + * taken, because the RCU lock will ensure that the PTE page will not >>> be freed. > + * >>> + * pte_offset_map_rw_nolock(mm, pmd, addr, pmdvalp, ptlp), above, is >>> like >>> + * pte_offset_map_ro_nolock(); but when successful, it also outputs the >>> + * pdmval. For cases where pte or pmd entries may be modified, that >>> is, maywrite >>> + * case, this can help the caller recheck *pmd once the lock is >>> taken. In some >>> + * cases, that is, either the mmap_lock for write, or pte_same() >>> check on >>> + * contents, is also enough to ensure that the pmd entry is stable. >>> + * >>>    * Note that free_pgtables(), used after unmapping detached vmas, or >>> when >>>    * exiting the whole mm, does not take page table lock before >>> freeing a page >>>    * table, and may not use RCU at all: "outsiders" like khugepaged >>> should avoid >> >> In general to me a step into the right direction. Likely the >> documentation could be further clarified in some aspects: >> >> Like that the use of pte_offset_map_ro_nolock() does not allow to easily >> identify if the page table was replaced in the meantime. Even after >> grabbing the PTL, we might be looking either at a page table that is >> still mapped or one that was unmapped and is about to get freed. But for >> R/O access this is usually sufficient AFAIUK. >> >> Or that "RO" / "RW" expresses the intended semantics, not that the >> *kmap* will be RO/RW protected. > > How about the following: > > pte_offset_map_ro_nolock(mm, pmd, addr, ptlp), above, is like > pte_offset_map(); but when successful, it also outputs a pointer to the > spinlock in ptlp - as pte_offset_map_lock() does, but in this case > without locking it. This helps the caller to avoid a later > pte_lockptr(mm, *pmd), which might by that time act on a changed *pmd: > pte_offset_map_ro_nolock() provides the correct spinlock pointer for the > page table that it returns. Even after grabbing the spinlock, we might > be looking either at a page table that is still mapped or one that was > unmapped and is about to get freed. But for R/O access this is usually > sufficient AFAIUK. Drop the "AFAIUK" :) "For R/O access this is sufficient." > > pte_offset_map_rw_nolock(mm, pmd, addr, pmdvalp, ptlp), above, is like > pte_offset_map_ro_nolock(); but when successful, it also outputs the > pdmval. For R/W access, the callers can not accept that the page table > it sees has been unmapped and is about to get freed. The pmdval can help > callers to recheck pmd_same() to identify this case once the spinlock is > taken. For some cases where exclusivity is already guaranteed, such as > holding the write lock of mmap_lock, or in cases where checking is > sufficient, such as a !pte_none() pte will be rechecked after the > spinlock is taken, there is no need to recheck pdmval. Right, using pte_same() one can achieve a similar result, assuming that the freed page table gets all ptes set to pte_none(). page_table_check_pte_clear_range() before pte_free_defer() in retract_page_tables/collapse_pte_mapped_thp() sanity checks that I think. In collapse_huge_page() that is not the case. But here, we also currently grab all heavily locks, to prevent any concurrent page table walker. > > Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* > will be RO/RW protected. Good. Please also incorporate the feedback from Muchun. -- Cheers, David / dhildenb