From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (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 45FBA25A62F for ; Fri, 17 Jan 2025 01:30:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.99 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737077448; cv=none; b=W3NFCLwMSjX0Fty5PCc9MEGsi9YbfGTMzDWFQ0kXGNQLCIzNuMvUkiPmtusp6+d4IpnIHadO4Px2sEt9A6Oa8O/OM7Nm52eJPG0mtV7sVO7rGvNFksuYN/AIy5nQNryNPHDmQD78J2ZQSN1DMYISIYAfd67DI+H1+u1Nx8pmTZQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737077448; c=relaxed/simple; bh=13eqMavSgvXRA3LyTxXkP1+osL496TBJGw43SpXZgaI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=TK8tucmGyWX5vbS8gqD6zUpjvsCBy+qdkCFsLdxhHmhbKppDxTeWNjS1HsebpWz+N9334bs5hPwLzo5oi2aw7Uf+hzbtvS2Q8ItVR85fWW2UsltYvYyT+GJBAlcfpxyI++SC87QCidD16JUEveRgnKfwsfAfL3LHJJomGtFo3lA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=O/4NdAuN; arc=none smtp.client-ip=115.124.30.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="O/4NdAuN" DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1737077437; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=TsqFKuMiLwMIWqbIsmre8PDwSTy200zBboCltX5otHE=; b=O/4NdAuNjmCTQjGis439DpIGg31ScpzSD+w4c4JkxwIQHC5Y59poAcuzjzM2eStwScMKvbHk6uaPk5ZBiRDLXXVBFwY4z3Idx5lrlPl1xJFEMueeJbJBAxaLNgaSjfQ+COkEr5fUAA0jFeaH0zWkiAMB5DXG7GjNeUUZuH2Kh8A= Received: from 30.74.144.128(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WNmu3tq_1737077435 cluster:ay36) by smtp.aliyun-inc.com; Fri, 17 Jan 2025 09:30:36 +0800 Message-ID: Date: Fri, 17 Jan 2025 09:30:33 +0800 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 v3 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap To: Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: chrisl@kernel.org, david@redhat.com, ioworker0@gmail.com, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.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 References: <20250115033808.40641-1-21cnbao@gmail.com> <20250115033808.40641-5-21cnbao@gmail.com> From: Baolin Wang In-Reply-To: <20250115033808.40641-5-21cnbao@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2025/1/15 11:38, Barry Song wrote: > From: Barry Song > > The try_to_unmap_one() function currently handles PMD-mapped THPs > inefficiently. It first splits the PMD into PTEs, copies the dirty > state from the PMD to the PTEs, iterates over the PTEs to locate > the dirty state, and then marks the THP as swap-backed. This process > involves unnecessary PMD splitting and redundant iteration. Instead, > this functionality can be efficiently managed in > __discard_anon_folio_pmd_locked(), avoiding the extra steps and > improving performance. > > The following microbenchmark redirties folios after invoking MADV_FREE, > then measures the time taken to perform memory reclamation (actually > set those folios swapbacked again) on the redirtied folios. > > #include > #include > #include > #include > > #define SIZE 128*1024*1024 // 128 MB > > int main(int argc, char *argv[]) > { > while(1) { > volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE, > MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > > memset((void *)p, 1, SIZE); > madvise((void *)p, SIZE, MADV_FREE); > /* redirty after MADV_FREE */ > memset((void *)p, 1, SIZE); > > clock_t start_time = clock(); > madvise((void *)p, SIZE, MADV_PAGEOUT); > clock_t end_time = clock(); > > double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; > printf("Time taken by reclamation: %f seconds\n", elapsed_time); > > munmap((void *)p, SIZE); > } > return 0; > } > > Testing results are as below, > w/o patch: > ~ # ./a.out > Time taken by reclamation: 0.007300 seconds > Time taken by reclamation: 0.007226 seconds > Time taken by reclamation: 0.007295 seconds > Time taken by reclamation: 0.007731 seconds > Time taken by reclamation: 0.007134 seconds > Time taken by reclamation: 0.007285 seconds > Time taken by reclamation: 0.007720 seconds > Time taken by reclamation: 0.007128 seconds > Time taken by reclamation: 0.007710 seconds > Time taken by reclamation: 0.007712 seconds > Time taken by reclamation: 0.007236 seconds > Time taken by reclamation: 0.007690 seconds > Time taken by reclamation: 0.007174 seconds > Time taken by reclamation: 0.007670 seconds > Time taken by reclamation: 0.007169 seconds > Time taken by reclamation: 0.007305 seconds > Time taken by reclamation: 0.007432 seconds > Time taken by reclamation: 0.007158 seconds > Time taken by reclamation: 0.007133 seconds > … > > w/ patch > > ~ # ./a.out > Time taken by reclamation: 0.002124 seconds > Time taken by reclamation: 0.002116 seconds > Time taken by reclamation: 0.002150 seconds > Time taken by reclamation: 0.002261 seconds > Time taken by reclamation: 0.002137 seconds > Time taken by reclamation: 0.002173 seconds > Time taken by reclamation: 0.002063 seconds > Time taken by reclamation: 0.002088 seconds > Time taken by reclamation: 0.002169 seconds > Time taken by reclamation: 0.002124 seconds > Time taken by reclamation: 0.002111 seconds > Time taken by reclamation: 0.002224 seconds > Time taken by reclamation: 0.002297 seconds > Time taken by reclamation: 0.002260 seconds > Time taken by reclamation: 0.002246 seconds > Time taken by reclamation: 0.002272 seconds > Time taken by reclamation: 0.002277 seconds > Time taken by reclamation: 0.002462 seconds Good result. > This patch significantly speeds up try_to_unmap_one() by allowing it > to skip redirtied THPs without splitting the PMD. > > Suggested-by: Baolin Wang > Suggested-by: Lance Yang > Signed-off-by: Barry Song LGTM. Reviewed-by: Baolin Wang