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 8A1B11B4248 for ; Wed, 25 Jun 2025 12:09:12 +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=1750853354; cv=none; b=hrV0DF63XVS2vPCMeQPhSvSjGJGfpNhyuZVi65ORU9n2GvTRxVOYfVi0aqQJ2NlbdC59BE1pOZrSrpqYgd3dwleJNPIquVzfXpKONIHAW526/YFkHHj3ky21vEbHbhjGg7unVmvfAZGH5mer3YNcahjAawnMo/5OqHoi6pOMeZw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750853354; c=relaxed/simple; bh=nUwiQG+N6KT17n0amkcjD+CGUUJalvVpiZv42pwB1mc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Jo0R2dFSNe9RF/9AhBmv+1gxoqELCWyMUOYDjWLGNEmR0JiPYJqveah9T61u3ZfZyVTOTvCrqxu4kYvbpk6e6JR8suBjRgZcQQLOwoLe1GHh5Yh6+mWOZs4D65zWXWuU4PmAPuGrj0KWEQ6hbAima6J0Yqvfc2OrqVt+B6C3XoQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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=a78IoAxD; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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="a78IoAxD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750853351; 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=hXz4L9AjmUQwaNKAx39lxTXVJAFR2RsUPQZWFFE3k78=; b=a78IoAxDmWirQ1y8IhrSyGyJYtwwrcAtK4qBNbMAg6+QS8FCGxMpkjFhjDe4UkVyntL+qR Tl0N+cdzdBArI1xC8pglFFuait3QQQUmtoIUYLOPVmQyZ79wV1Y2nTyGRiEnXSub3Iub6E YcWysfYrByKA8LCSaVQ6VxstIXegdCk= 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-369-yWs0nsqwPvyysDIJpcuEqA-1; Wed, 25 Jun 2025 08:09:10 -0400 X-MC-Unique: yWs0nsqwPvyysDIJpcuEqA-1 X-Mimecast-MFC-AGG-ID: yWs0nsqwPvyysDIJpcuEqA_1750853349 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3a6da94a532so2482696f8f.2 for ; Wed, 25 Jun 2025 05:09:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750853349; x=1751458149; 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=hXz4L9AjmUQwaNKAx39lxTXVJAFR2RsUPQZWFFE3k78=; b=gVfwmIf1NbuxKH161rmNOQmCX/L+Euok30+PdptpYMmlAT5Kf3qBq/5qPI+S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6000:4815:b0:3a4:dc42:a0ac with SMTP id ffacd0b85a97d-3a6ed64e5ddmr2082403f8f.49.1750853348875; Wed, 25 Jun 2025 05:09:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH9Lr46v9Q28gB+WmOXi6ZuJCE56NnF+cZrNGYOXSV4lsNJpeXOrIPDeZxjqvKUmorwq1CGfA== X-Received: by 2002:a05:6000:4815:b0:3a4:dc42:a0ac with SMTP id ffacd0b85a97d-3a6ed64e5ddmr2082376f8f.49.1750853348346; Wed, 25 Jun 2025 05:09:08 -0700 (PDT) Received: from ?IPV6:2003:d8:2f12:1b00:5d6b:db26:e2b7:12? (p200300d82f121b005d6bdb26e2b70012.dip0.t-ipconnect.de. [2003:d8:2f12:1b00:5d6b:db26:e2b7:12]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-453823c57d7sm18992765e9.40.2025.06.25.05.09.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Jun 2025 05:09:06 -0700 (PDT) Message-ID: <5db6fb4c-079d-4237-80b3-637565457f39@redhat.com> Date: Wed, 25 Jun 2025 14:09:03 +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 v4 3/4] mm: Support batched unmap for lazyfree large folios during reclamation To: Barry Song <21cnbao@gmail.com> Cc: Lance Yang , akpm@linux-foundation.org, baolin.wang@linux.alibaba.com, chrisl@kernel.org, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, ying.huang@intel.com, zhengtangquan@oppo.com, Lance Yang References: <2c19a6cf-0b42-477b-a672-ed8c1edd4267@redhat.com> <20250624162503.78957-1-ioworker0@gmail.com> <27d174e0-c209-4851-825a-0baeb56df86f@redhat.com> <938c4726-b93e-46df-bceb-65c7574714a6@linux.dev> <5ba95609-302b-456a-a863-2bd5df51baf2@redhat.com> <6179dd30-5351-4a79-b0d6-f0e85650a926@redhat.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 UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4BAheAAhkBFiEEG9nKrXNcTDpGDfzKTd4Q9wD/g1oFAl8Ox4kFCRKpKXgACgkQTd4Q 9wD/g1oHcA//a6Tj7SBNjFNM1iNhWUo1lxAja0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kt WNyWQQ== Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 25.06.25 13:42, Barry Song wrote: > On Wed, Jun 25, 2025 at 11:27 PM David Hildenbrand wrote: >> >> On 25.06.25 13:15, Barry Song wrote: >>> On Wed, Jun 25, 2025 at 11:01 PM David Hildenbrand wrote: >>>> >>>> On 25.06.25 12:57, Barry Song wrote: >>>>>>> >>>>>>> Note that I don't quite understand why we have to batch the whole thing >>>>>>> or fallback to >>>>>>> individual pages. Why can't we perform other batches that span only some >>>>>>> PTEs? What's special >>>>>>> about 1 PTE vs. 2 PTEs vs. all PTEs? >>>>>> >>>>>> That's a good point about the "all-or-nothing" batching logic ;) >>>>>> >>>>>> It seems the "all-or-nothing" approach is specific to the lazyfree use >>>>>> case, which needs to unmap the entire folio for reclamation. If that's >>>>>> not possible, it falls back to the single-page slow path. >>>>> >>>>> Other cases advance the PTE themselves, while try_to_unmap_one() relies >>>>> on page_vma_mapped_walk() to advance the PTE. Unless we want to manually >>>>> modify pvmw.pte and pvmw.address outside of page_vma_mapped_walk(), which >>>>> to me seems like a violation of layers. :-) >>>> >>>> Please explain to me why the following is not clearer and better: >>> >>> This part is much clearer, but that doesn’t necessarily improve the overall >>> picture. The main challenge is how to exit the iteration of >>> while (page_vma_mapped_walk(&pvmw)). >> >> Okay, I get what you mean now. >> >>> >>> Right now, we have it laid out quite straightforwardly: >>> /* We have already batched the entire folio */ >>> if (nr_pages > 1) >>> goto walk_done; >> >> >> Given that the comment is completely confusing whens seeing the check ... :) >> >> /* >> * If we are sure that we batched the entire folio and cleared all PTEs, >> * we can just optimize and stop right here. >> */ >> if (nr_pages == folio_nr_pages(folio)) >> goto walk_done; >> >> would make the comment match. > > Yes, that clarifies it. > >> >>> >>> with any nr between 1 and folio_nr_pages(), we have to consider two issues: >>> 1. How to skip PTE checks inside page_vma_mapped_walk for entries that >>> were already handled in the previous batch; >> >> They are cleared if we reach that point. So the pte_none() checks will >> simply skip them? >> >>> 2. How to break the iteration when this batch has arrived at the end. >> >> page_vma_mapped_walk() should be doing that? > > It seems you might have missed the part in my reply that says: > "Of course, we could avoid both, but that would mean performing unnecessary > checks inside page_vma_mapped_walk()." > > That’s true for both. But I’m wondering why we’re still doing the check, > even when we’re fairly sure they’ve already been cleared or we’ve reached > the end :-) :) > > Somehow, I feel we could combine your cleanup code—which handles a batch > size of "nr" between 1 and nr_pages—with the > "if (nr_pages == folio_nr_pages(folio)) goto walk_done" check. Yeah, that's what I was suggesting. It would have to be part of the cleanup I think. I'm still wondering if there is a case where if (nr_pages == folio_nr_pages(folio)) goto walk_done; would be wrong when dealing with small folios. > In practice, this would let us skip almost all unnecessary checks, > except for a few rare corner cases. > > For those corner cases where "nr" truly falls between 1 and nr_pages, > we can just leave them as-is—performing the redundant check inside > page_vma_mapped_walk(). I mean, batching mapcount+refcount updates etc. is always a win. If we end up doing some unnecessary pte_none() checks, that might be suboptimal but mostly noise in contrast to the other stuff we will optimize out :) Agreed that if we can easily avoid these pte_none() checks, we should do that. Optimizing that for "nr_pages == folio_nr_pages(folio)" makes sense. -- Cheers, David / dhildenb