From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C69A33AD83 for ; Fri, 19 Dec 2025 21:38:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766180291; cv=none; b=R2dbsTdvmpMZRhVdNP8rPZAUpcAsUb1BS0L/lHppfxBbJuSxxM27L3AU7CvRvm7XaYlw3qrQ6QAQ0LJVPeNSvQm07JlZogMubCocvZukTmtrYWTzfBJR5iiae9jjsAtBI3qCvonyMgjMxkPU4AR57msf5bZKsjxyoiImE7SH7dg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766180291; c=relaxed/simple; bh=ZM7HFDiERUcQNinAOST8oE0V9vxab3YGWH4CoJcMNYA=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=DMZy2GUAzmeDTzr3w2S0ZLJnoMj96tkWmeWSsUAFcFI2vpOTDYOxPI/PlpdGfhZI5FYOADb963j+JHqEqcwU1sJiPDtI0TSPfdU+GwNrZs6cyoOLx0F/r6Na480jAEvuFPcuYad3/zjBUeMasER3tIU2xKR/bKWw1DPUNzZfEe0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hQB2hV//; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hQB2hV//" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-42e2d02a3c9so1420228f8f.3 for ; Fri, 19 Dec 2025 13:38:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766180285; x=1766785085; darn=vger.kernel.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7qwBOZjXZNgWuokEpvkuCyDN4qULKX2eOsVZKGx+BRc=; b=hQB2hV//Q5VTBnLMvOiXLniSjt8KBaDoyxwEFPH51GgzFMhrNTZgdfOcsM/KyNTILt R6RLyu/TPdkQ67WMPAAMWxGR+E+pOcm5UFLK1B01G7ItwPSwew9/J9KVb8jQTFUwD24y LXc+YOIqizJlacP0XNGSKo5LUbrTYjDFDbJjJ1pVoQu3SNaXPTQBOyEkJzHxz7d/U0J0 Ka6cMQ5RvA0OMJRwiVjLf8z1FZP3oGTzT6Pp+wQJa2X5DrBhBwOEL9ZPqZZ0g3C/GLCg fS+QxOZzelrVeKDYiNYVB94Q2MO0yV4kBBGTs37ixHORpiGw6W1uSOYaEsFeW23H18sH ucZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766180285; x=1766785085; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=7qwBOZjXZNgWuokEpvkuCyDN4qULKX2eOsVZKGx+BRc=; b=QI2DrnVirr8Xj4r81UDKfqeMZHG87gVp2qcH7e65XjXRpkDQpAnNRXyYKJ01MiU21L /JRGh6YeF2z5sBlvjycsF8/SLJxwWkNrw/73052lLIcjyVZkBhGi9WyN+/Vjlz+m/OBl T96MSahmkJTHbYkHPFtSj8pYVBvaBbxCKvHnW9HMPSaXnbw4Y/Z0a08Hot3qnwVpgA5N JNUpLQChciE5MrLPqtsbcyERsnWxfsLYifRLjCbNMMhSmUT9LLqRglIWitSUbZ9ue0Q/ fULV2QEmjw7P/tdH2FyDK3sQopdAJnrZ22nsqiTDSGxm2+BXT9Zol0k5+xbi589YAOSM nWPQ== X-Forwarded-Encrypted: i=1; AJvYcCVpIFLXm9lplTkRQBNFNL7TiS7tNnJZa6hiexy0IXSHCfAtQwyIkO2KrnFhIa+Nukwy+Os/vYmO56m/eEM=@vger.kernel.org X-Gm-Message-State: AOJu0YwtMMtot847CR8dbI+rxCUyJusnkwziadByVWhvBA/BZ0ne9g7d n7FKeqjqU1EcQJlkKby23PGbfdql23Wc/OwuVDZcEvpRF7XbqzUuRa+y X-Gm-Gg: AY/fxX4bbjqJCOT/NVGY3wyJCmbgL9Vt7zGvLlBGg6C/1uoVuNtr0q6hVK0rGODbBM8 9d/3K+rHTZrOCGnmoGQJLrrvh51SUFXw/kbXX/D+WdWEScCokEEFuPb8rruaRLnkfe2D7PtEBPF Kfv2gr98WFImbC54Yn3QylmLYkznVKuijBUm25+Wv04fHgPm1hu1S8PMzphllXtGHuUDz6W9Q4/ lXbSmzG9ViZO6J7oZonhOGHp08/skU/4oORd0pbOYSUIaxajNeoLywDl5A8TrG8MTAHW7BH+u3u bYm/g0lhN74iGqMXLo+XHw/rB3Wssq+Q9SSd4J1y6siDSW04qpN79TuJwU9AZohyTV19H5UgSB1 zSjAw97V+0dcylNK+DUmvBtpVob+5h5TcaSLJetnKNS7XwiX/XmCbxkvTFXOWry1ncxqG75vk12 eTlFybFPiqGQmfsfzDOBm9mdJpWg7mVlk= X-Google-Smtp-Source: AGHT+IGyru8SGJqlxv8zJOxc9PglQsmvyTXqM/wZEemUdJ3eOipdnMijVxzSqXLtGy838wDBtvYe1A== X-Received: by 2002:a05:6000:178c:b0:431:918:501d with SMTP id ffacd0b85a97d-4324e702853mr4655672f8f.60.1766180285071; Fri, 19 Dec 2025 13:38:05 -0800 (PST) Received: from smtpclient.apple ([212.59.64.34]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4324ea1b1b1sm6922431f8f.3.2025.12.19.13.38.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Dec 2025 13:38:04 -0800 (PST) Content-Type: text/plain; charset=utf-8 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.300.41.1.7\)) Subject: Re: [PATCH v2 2/4] mm/hugetlb: fix two comments related to huge_pmd_unshare() From: Nadav Amit In-Reply-To: <2bbe1e49-95ba-42ea-b6af-5eeb61d68c4c@kernel.org> Date: Fri, 19 Dec 2025 23:37:50 +0200 Cc: Harry Yoo , Linux Kernel Mailing List , linux-arch@vger.kernel.org, "open list:MEMORY MANAGEMENT" , Will Deacon , "Aneesh Kumar K.V" , Andrew Morton , Nick Piggin , Peter Zijlstra , Arnd Bergmann , Muchun Song , Oscar Salvador , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pedro Falcato , Rik van Riel , Laurence Oberman , Prakash Sangappa , Liu Shixin Content-Transfer-Encoding: quoted-printable Message-Id: <3D7F0E85-2509-4925-BDD5-149E9E8D5C1F@gmail.com> References: <20251212071019.471146-1-david@kernel.org> <20251212071019.471146-3-david@kernel.org> <506fef86-5c3b-490e-94f9-2eb6c9c47834@kernel.org> <2bbe1e49-95ba-42ea-b6af-5eeb61d68c4c@kernel.org> To: "David Hildenbrand (Red Hat)" X-Mailer: Apple Mail (2.3864.300.41.1.7) > On 19 Dec 2025, at 16:13, David Hildenbrand (Red Hat) = wrote: >=20 > On 12/19/25 12:20, Harry Yoo wrote: >> On Fri, Dec 19, 2025 at 07:11:00AM +0100, David Hildenbrand (Red Hat) = wrote: >>> On 12/19/25 05:44, Harry Yoo wrote: >>>> On Fri, Dec 12, 2025 at 08:10:17AM +0100, David Hildenbrand (Red = Hat) wrote: >>>>> Ever since we stopped using the page count to detect shared PMD >>>>> page tables, these comments are outdated. >>>>>=20 >>>>> The only reason we have to flush the TLB early is because once we = drop >>>>> the i_mmap_rwsem, the previously shared page table could get freed = (to >>>>> then get reallocated and used for other purpose). So we really = have to >>>>> flush the TLB before that could happen. >>>>>=20 >>>>> So let's simplify the comments a bit. >>>>>=20 >>>>> The "If we unshared PMDs, the TLB flush was not recorded in = mmu_gather." >>>>> part introduced as in commit a4a118f2eead ("hugetlbfs: flush TLBs >>>>> correctly after huge_pmd_unshare") was confusing: sure it is = recorded >>>>> in the mmu_gather, otherwise tlb_flush_mmu_tlbonly() wouldn't do >>>>> anything. So let's drop that comment while at it as well. >>>>>=20 >>>>> We'll centralize these comments in a single helper as we rework = the code >>>>> next. >>>>>=20 >>>>> Fixes: 59d9094df3d7 ("mm: hugetlb: independent PMD page table = shared count") >>>>> Reviewed-by: Rik van Riel >>>>> Tested-by: Laurence Oberman >>>>> Reviewed-by: Lorenzo Stoakes >>>>> Acked-by: Oscar Salvador >>>>> Cc: Liu Shixin >>>>> Signed-off-by: David Hildenbrand (Red Hat) >>>>> --- >>>>=20 >>>> Looks good to me, >>>> Reviewed-by: Harry Yoo >>>>=20 >>>> with a question below. >>>=20 >>> Hi Harry, >>>=20 >>> thanks for the review! >> No problem! >> I would love to review more, as long as my time & ability allows ;) >>>>> mm/hugetlb.c | 24 ++++++++---------------- >>>>> 1 file changed, 8 insertions(+), 16 deletions(-) >>>>>=20 >>>>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >>>>> index 51273baec9e5d..3c77cdef12a32 100644 >>>>> --- a/mm/hugetlb.c >>>>> +++ b/mm/hugetlb.c >>>>> @@ -5304,17 +5304,10 @@ void __unmap_hugepage_range(struct = mmu_gather *tlb, struct vm_area_struct *vma, >>>>> tlb_end_vma(tlb, vma); >>>>> /* >>>>> - * If we unshared PMDs, the TLB flush was not recorded in = mmu_gather. We >>>>> - * could defer the flush until now, since by holding = i_mmap_rwsem we >>>>> - * guaranteed that the last reference would not be dropped. But = we must >>>>> - * do the flushing before we return, as otherwise i_mmap_rwsem = will be >>>>> - * dropped and the last reference to the shared PMDs page might = be >>>>> - * dropped as well. >>>>> - * >>>>> - * In theory we could defer the freeing of the PMD pages as = well, but >>>>> - * huge_pmd_unshare() relies on the exact page_count for the PMD = page to >>>>> - * detect sharing, so we cannot defer the release of the page = either. >>>>> - * Instead, do flush now. >>>>=20 >>>> Does this mean we can now try defer-freeing of these page tables, >>>> and if so, would it be worth it? >>>=20 >>> There is one very tricky thing: >>>=20 >>> Whoever is the last owner of a (previously) shared page table must = unmap any >>> contained pages (adjust mapcount/ref, sync a/d bit, ...). >> Right. >>> So it's not just a matter of deferring the freeing, because these = page tables >>> will still contain content. >> I was (and maybe still) bit confused while reading the old comment as >> it implied (or maybe I just misread) that by deferring freeing of = page tables >> we don't have to flush TLB in __unmap_hugepage_range() and can flush = later >> instead. >=20 > Yeah, I am also confused by the old comment. I think the idea there = was to drop the reference only later and thereby deferred-free the page. My bad. I looked again, and the comment indeed doesn=E2=80=99t make much = sense. Thanks for fixing it.