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 BC28D1C3F32 for ; Tue, 6 Aug 2024 12:56:42 +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=1722949004; cv=none; b=iQ6Non/vEL+yFZkmU9+0zVZSQmm7CxlAd1C7MPatJtats2s+4XrjoAB2tw/eTN/afvZoSpvCCi5F8LLTjh4eji7GOM2zcelK0w8hN/Ah0TyMleLuH64Y4w0Q6t2PhFwaV9vEtEly0C4KHCHBrAUoSivhr0Xl/laHQ6ehS45wJ3o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722949004; c=relaxed/simple; bh=fLc1MYOd7G4PJElgJ09kwQDa07bqOByrdxppNzdUfT8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=s07IDdIC9hIVKG0+ECHQLoHKYHqbSsKwm/cquDDBLX2utKpD22ereMkJMehHukUFchqWpnnjHBe8GYaQZr6O/gCYDJGx1MJd8Z+CMPQHe7ftzsA2Aw3o9EirVAguiByhhRbdW5ZJn1HbzBvGgUYOzSdsIw669gj3+8olALttA6g= 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=dXlzhkKm; 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="dXlzhkKm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722949001; 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=BbVHeghCxcbavlpwPUmyKdqFJPLDIUOPTBi2eefPzmU=; b=dXlzhkKmjAEn3TCTmYHWPnYc2pqljveYLWLTlrdpyyYW1lRz+BjJvHlGrlMw2tb35kPrdp yZOEdwGmN9VPJ8KE5zFonx4/rxIhKiB/gRlvoeuhlK0SnLjGcaoyU/sTlfx93aCzdE7XpF vxIasItEcAQPOaXhFdVeP/LDRhmyvQc= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-237-JJocWgRuNbWZPOBq8NCLgA-1; Tue, 06 Aug 2024 08:56:40 -0400 X-MC-Unique: JJocWgRuNbWZPOBq8NCLgA-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42807a05413so5203615e9.2 for ; Tue, 06 Aug 2024 05:56:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722948999; x=1723553799; 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=BbVHeghCxcbavlpwPUmyKdqFJPLDIUOPTBi2eefPzmU=; b=fyZ2Pht6Jpf/qJXduofTdxqjTos83IxViHY4zzYNjp1Yhj07EEfl6EHFfRIZwV9KTr E+1mxlqUCqOPeGcFaNYcu+C/aCNoMUM/zFlSYSMhSwJBNl20VwKTQcdPa6rtk1F2sUFc 1iz5bGPREsi7vgKCI11dhun994LQYwFz5ICRdyopIxbgiJ5S8BE/fl9U8idEobgt/e7s gg7jDheBqJRTT70AxOLmG8LSh1bInBTpD1ViYn8ooDW9KAzkkmbsL+IZRRGS4qIAvAuc GTQ8GJRpcYObmbx6YbkheqCSiJA3ea0sDYa7SglFwwXGAckNfjZEwS8KHmF4cEA3Xw49 3nnQ== X-Gm-Message-State: AOJu0YxbIDcvEAC44ES1r3EFhAL1cD/hRcwhWBO0zC/WZky8ihkakAYw TQSq5KeOQVPVIzDVNeuhfwRZIIdxfOuR66VnhpIe6WpJ3pIusEUqtz15i6wRl4Q9+Wdu4E4dgNH /4ZHlC9sMPPVSJWP76MqE/4IVPhJEky11PbsKILxbAfEiycl/vunXaoh5emUNyA== X-Received: by 2002:a05:600c:1910:b0:426:6f31:5f5c with SMTP id 5b1f17b1804b1-428e6b05c80mr103269825e9.17.1722948999332; Tue, 06 Aug 2024 05:56:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IErghGnHAa8cyLzdPF1c+VPdsO4XjVa9+4FF2AWApKYF1zVoIVlBBFR4oazz8FbaqEwLOwzmw== X-Received: by 2002:a05:600c:1910:b0:426:6f31:5f5c with SMTP id 5b1f17b1804b1-428e6b05c80mr103269615e9.17.1722948998828; Tue, 06 Aug 2024 05:56:38 -0700 (PDT) Received: from ?IPV6:2003:cb:c73f:8500:f83c:3602:5300:88af? (p200300cbc73f8500f83c3602530088af.dip0.t-ipconnect.de. [2003:cb:c73f:8500:f83c:3602:5300:88af]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e6e9d887sm180338905e9.43.2024.08.06.05.56.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 06 Aug 2024 05:56:38 -0700 (PDT) Message-ID: <1e6bcaf2-7c48-4be0-b101-ec2ddc4098ce@redhat.com> Date: Tue, 6 Aug 2024 14:56:37 +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] mm: attempt to batch free swap entries for zap_pte_range() To: Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Chris Li , "Huang, Ying" , Hugh Dickins , Kalesh Singh , Ryan Roberts References: <20240806012409.61962-1-21cnbao@gmail.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 ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEIC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9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20240806012409.61962-1-21cnbao@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 06.08.24 03:24, Barry Song wrote: > From: Barry Song > > Zhiguo reported that swap release could be a serious bottleneck > during process exits[1]. With mTHP, we have the opportunity to > batch free swaps. > Thanks to the work of Chris and Kairui[2], I was able to achieve > this optimization with minimal code changes by building on their > efforts. > If swap_count is 1, which is likely true as most anon memory are > private, we can free all contiguous swap slots all together. > > Ran the below test program for measuring the bandwidth of munmap > using zRAM and 64KiB mTHP: > > #include > #include > #include > > unsigned long long tv_to_ms(struct timeval tv) > { > return tv.tv_sec * 1000 + tv.tv_usec / 1000; > } > > main() > { > struct timeval tv_b, tv_e; > int i; > #define SIZE 1024*1024*1024 > void *p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, > MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > if (!p) { > perror("fail to get memory"); > exit(-1); > } > > madvise(p, SIZE, MADV_HUGEPAGE); > memset(p, 0x11, SIZE); /* write to get mem */ > > madvise(p, SIZE, MADV_PAGEOUT); > > gettimeofday(&tv_b, NULL); > munmap(p, SIZE); > gettimeofday(&tv_e, NULL); > > printf("munmap in bandwidth: %ld bytes/ms\n", > SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b))); > } > > The result is as below (munmap bandwidth): > mm-unstable mm-unstable-with-patch > round1 21053761 63161283 > round2 21053761 63161283 > round3 21053761 63161283 > round4 20648881 67108864 > round5 20648881 67108864 > > munmap bandwidth becomes 3X faster. > > [1] https://lore.kernel.org/linux-mm/20240731133318.527-1-justinjiang@vivo.com/ > [2] https://lore.kernel.org/linux-mm/20240730-swap-allocator-v5-0-cb9c148b9297@kernel.org/ > > Cc: Kairui Song > Cc: Chris Li > Cc: "Huang, Ying" > Cc: Hugh Dickins > Cc: Kalesh Singh > Cc: Ryan Roberts > Cc: David Hildenbrand > Signed-off-by: Barry Song > --- > mm/swapfile.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 61 insertions(+) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index ea023fc25d08..ed872a186e81 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -156,6 +156,25 @@ static bool swap_is_has_cache(struct swap_info_struct *si, > return true; > } > > +static bool swap_is_last_map(struct swap_info_struct *si, > + unsigned long offset, int nr_pages, > + bool *has_cache) > +{ > + unsigned char *map = si->swap_map + offset; > + unsigned char *map_end = map + nr_pages; > + bool cached = false; > + > + do { > + if ((*map & ~SWAP_HAS_CACHE) != 1) > + return false; > + if (*map & SWAP_HAS_CACHE) > + cached = true; > + } while (++map < map_end); > + > + *has_cache = cached; > + return true; > +} > + > /* > * returns number of pages in the folio that backs the swap entry. If positive, > * the folio was reclaimed. If negative, the folio was not reclaimed. If 0, no > @@ -1469,6 +1488,39 @@ static unsigned char __swap_entry_free(struct swap_info_struct *p, > return usage; > } > > +static bool try_batch_swap_entries_free(struct swap_info_struct *p, Why call it "p" here and not "si" like in the other code you are touching? > + swp_entry_t entry, int nr, bool *any_only_cache) > +{ > + unsigned long offset = swp_offset(entry); > + struct swap_cluster_info *ci; > + bool has_cache = false; > + bool can_batch; > + int i; > + > + /* cross into another cluster */ > + if (nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER) > + return false; > + ci = lock_cluster_or_swap_info(p, offset); > + can_batch = swap_is_last_map(p, offset, nr, &has_cache); > + if (can_batch) { > + for (i = 0; i < nr; i++) > + WRITE_ONCE(p->swap_map[offset + i], SWAP_HAS_CACHE); > + } > + unlock_cluster_or_swap_info(p, ci); > + > + /* all swap_maps have count==1 and have no swapcache */ > + if (!can_batch) > + goto out; > + if (!has_cache) { > + spin_lock(&p->lock); > + swap_entry_range_free(p, entry, nr); > + spin_unlock(&p->lock); > + } > + *any_only_cache = has_cache; > +out: > + return can_batch; > +} > + > /* > * Drop the last HAS_CACHE flag of swap entries, caller have to > * ensure all entries belong to the same cgroup. > @@ -1797,6 +1849,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) > bool any_only_cache = false; > unsigned long offset; > unsigned char count; > + bool batched; > > if (non_swap_entry(entry)) > return; > @@ -1808,6 +1861,13 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) > if (WARN_ON(end_offset > si->max)) > goto out; > > + if (nr > 1 && swap_count(data_race(si->swap_map[start_offset]) == 1)) { > + batched = try_batch_swap_entries_free(si, entry, nr, > + &any_only_cache); > + if (batched) > + goto reclaim; > + } > + I'm wondering if we could find a way to clean this up to achieve here: if (WARN_ON(end_offset > si->max)) goto out; /* * First free all entries in the range.$ */ any_only_cache = __free_swap_entries(si, entry, nr); /* * Short-circuit the below loop if none of the entries had their * reference drop to zero. */ if (!any_only_cache) goto out; Whereby move the fallback loop in that new function static bool __free_swap_entries(struct swap_info_struct *si, swp_entry_t entry, int nr) { const unsigned long start_offset = swp_offset(entry); const unsigned long end_offset = start_offset + nr; bool any_only_cache = false; if (nr > 1 && swap_count(data_race(si->swap_map[start_offset]) == 1)) { [... what try_batch_swap_entries_free() would do ...] } fallback: for (offset = start_offset; offset < end_offset; offset++) { if (data_race(si->swap_map[offset])) { [... what the fallback code would do ...] } return any_only_cache; } > /* > * First free all entries in the range. > */ > @@ -1821,6 +1881,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) > } > } > > +reclaim: > /* > * Short-circuit the below loop if none of the entries had their > * reference drop to zero. -- Cheers, David / dhildenb