Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp806901rdb; Fri, 26 Jan 2024 11:11:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IFcN2Y3ODd3uMAILmOvHI+NHRDjS+nzANLi6IgmL9IjxpgXZplK477xYZ9e5zHeMUEmKULX X-Received: by 2002:a05:6808:45:b0:3be:1cd:cc57 with SMTP id v5-20020a056808004500b003be01cdcc57mr225798oic.70.1706296294045; Fri, 26 Jan 2024 11:11:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706296294; cv=pass; d=google.com; s=arc-20160816; b=ZfEsV+60UsDG5DXTtJ5wwr3lFTPjnALrkwtOlKWtmFWFNNqm20IRg6TCeVdP6k/5D5 Epi2Bz22leYON38TCeY/S9HiZDQBNqZhLUG/0q3TAlqPfeWCjTzY6iYU0T70rNU9iLz1 PgEL5NNlfWGKXC5lk2w5dg0dZ2kUhZbrTr8YuVzSzUmFljyXmIKYHwEnU1Q6M2UEPvOC 5tuLME21KiVGJGSDNWUPjnnNmvrDhXTQTMirqkKaf1FFUpDRGWhcj726V0hZDj5uKtfZ IyzcK7Lqun14a1ciIGq8DyYhpgLrMaQ/s6BaK+IFlf7sc9oDBScRRbV7b2H/05lkYkfW cHmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=4g8K7l28tbM/mldWkR+pvjoSC5Y71b2XjVOt+98pFVU=; fh=+/vcVyQ/hyoQ4AAD3OnuMcpo6aZtoZ/p88DJ9kxrMt4=; b=KEs9LbfakFvOuAD92YFbQTU7ZZOKflyDfu5i+rV9r4BWuFLEuTlE8wwr+7Vie1cASj WzQczlqRIHWq2FC3ZWG7qA2irPts6B4L0/cxy5FNXJG3ANOxGP3x0VXM+mTR+kwkOOSg Otd4p3G/1ApoNlFHYhcj2OBLWkTDDzH/nsXkDNnyDO6aSNzuIJ2rcK03zfnSDJmJnJab PRefbx2ixzGIJbgnTAOT6M5JQBcvp3H8X+oP4dMSMeHnLrlPNiKTEOrpY/gGbC89NPFc Mm9Y+k8GVOfZWv0u3ffO/Isagvb8J2gQmK8gJb2A5EKcIcNEES6z6QYeTvSfwMuhqobV 8/4w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hkRALx7D; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-40545-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40545-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y6-20020a633206000000b005c5e24d4364si1512267pgy.428.2024.01.26.11.11.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 11:11:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40545-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hkRALx7D; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-40545-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40545-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A669828254C for ; Fri, 26 Jan 2024 19:11:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59656224C2; Fri, 26 Jan 2024 19:11:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hkRALx7D" 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 745B3224C7 for ; Fri, 26 Jan 2024 19:11:25 +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=1706296287; cv=none; b=WS0Z56vdA0Fui8fUNXQfwSyWVOI3icGBNEC/Za7Zw5TI6adAnsktH1IN8dmwxvTo0fDsCoC1CsScrC21W5ikY6FiETxHxn4V2ZcU+rYGfCeIuM09wM/6/zyPUsjC7krrXcgJ/Yf88N6Lwj7HzjRyZq3WkyU+PksrYrOK2yyWnrA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706296287; c=relaxed/simple; bh=taq9MRcHQZPGoD+oxR/jZ6ymaVRJzNqx14jm8Jqa3BY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ficF5RlMR4oQi/J0/xDZaPJJyN9s4lAxxWhlyavoiBgwvkgT+O9fL+jj7wbv8B5ek/qd4wSfEZfleEleHNug8xzuWiA2VLs7z+SnvhigllwF3dRrmoq/ito9ItCBjMC32YjZ/ux7Imabf8X91lGNq3kTC8ZOVw1Y8GwtnAzBvmA= 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=hkRALx7D; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706296284; 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=4g8K7l28tbM/mldWkR+pvjoSC5Y71b2XjVOt+98pFVU=; b=hkRALx7DRadyyFtZYHWweWw1ou80cjuK7z+H0EZFpEo40h7umGQK78zV2fgCv/D5n1XYFf AKHNbc2BMlg4I5marHix7kpmhdwpMlhFSCc4B5LWVBIJ13LM/oiQzUlyxokNzmRqtx65aI hN/1yxDmTWQcjt/FfTXqVKUVY0K4jE0= 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-60-0Y0Z_9_qPC-uqS8SWosYiw-1; Fri, 26 Jan 2024 14:11:23 -0500 X-MC-Unique: 0Y0Z_9_qPC-uqS8SWosYiw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40e4f70af99so11210725e9.2 for ; Fri, 26 Jan 2024 11:11:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706296282; x=1706901082; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=4g8K7l28tbM/mldWkR+pvjoSC5Y71b2XjVOt+98pFVU=; b=Gl2RbRnJN4ifcqJm493GfX2Z4cQzA3NxLqqayZiMPp/C51q6EwK1IK6DdInmW41Fd+ 0jD5pmSpU5iqYTdR2SZvofyR3Wp2VfLwLCgo+XCR7k5uDCWu0kRxw3ulFhLgTZjP/Xvq Xzuk6yAjBtiSjOgVvDcVoLMEma97FlrJJhtY3QUB3ykdeTnEPixs+uKKrBEIabmh/pCM zucTh7g3n0LSOobvSyq1+MYvuyJCLeM8yxlXClJl8yM4rPYnYXAw0rkP5yFScrXZaqxB SrCt1mZJZy+6WrZ7x9/iNBlxxXM3MN62l8NoKgIH2ENggXpXJhT/xWMYyRbH8547U6fb CUSw== X-Gm-Message-State: AOJu0YwikE2c+c9hPs3N1KoZQHl7KMC/GzZFhxxB70sfgOHcOKGTHH/N Sp4RsfzYo+uv1+Z3XMiPhKNiAYsa5e4wJywvt3DKRpEVpAdKCANxnZ8bSaPxjkQlMLe4nAxxKhL Sy2UwxVZQmcq3rW0KSrVjrFSBcJRqqjUifBB1CfbpyhBqk1WWf9uaBYkCXzYCEA1lfLWTCPAVPi 9LT1TjYf3TS+rkE62k5qN4y1ewaMCGz8dS8ZcYeg3Bfw== X-Received: by 2002:adf:e34e:0:b0:337:a81a:a92 with SMTP id n14-20020adfe34e000000b00337a81a0a92mr93428wrj.16.1706296281789; Fri, 26 Jan 2024 11:11:21 -0800 (PST) X-Received: by 2002:adf:e34e:0:b0:337:a81a:a92 with SMTP id n14-20020adfe34e000000b00337a81a0a92mr93386wrj.16.1706296281267; Fri, 26 Jan 2024 11:11:21 -0800 (PST) Received: from ?IPV6:2003:cb:c70a:5100:7e95:22ff:3f9b:1e92? (p200300cbc70a51007e9522ff3f9b1e92.dip0.t-ipconnect.de. [2003:cb:c70a:5100:7e95:22ff:3f9b:1e92]) by smtp.gmail.com with ESMTPSA id h5-20020adfe985000000b00337d67a85c1sm1811358wrm.62.2024.01.26.11.11.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Jan 2024 11:11:20 -0800 (PST) Message-ID: <5b5b2ceb-9099-4acc-a995-d979ad414c6a@redhat.com> Date: Fri, 26 Jan 2024 20:11:19 +0100 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 13/15] mm/memory: optimize fork() with PTE-mapped THP Content-Language: en-US To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , Ryan Roberts , Russell King , Catalin Marinas , Will Deacon , Dinh Nguyen , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , "David S. Miller" , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org References: <20240125193227.444072-1-david@redhat.com> <20240125193227.444072-14-david@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsg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32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/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: <20240125193227.444072-14-david@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 25.01.24 20:32, David Hildenbrand wrote: > Let's implement PTE batching when consecutive (present) PTEs map > consecutive pages of the same large folio, and all other PTE bits besides > the PFNs are equal. > > We will optimize folio_pte_batch() separately, to ignore selected > PTE bits. This patch is based on work by Ryan Roberts. > > Use __always_inline for __copy_present_ptes() and keep the handling for > single PTEs completely separate from the multi-PTE case: we really want > the compiler to optimize for the single-PTE case with small folios, to > not degrade performance. > > Note that PTE batching will never exceed a single page table and will > always stay within VMA boundaries. > > Further, processing PTE-mapped THP that maybe pinned and have > PageAnonExclusive set on at least one subpage should work as expected, > but there is room for improvement: We will repeatedly (1) detect a PTE > batch (2) detect that we have to copy a page (3) fall back and allocate a > single page to copy a single page. For now we won't care as pinned pages > are a corner case, and we should rather look into maintaining only a > single PageAnonExclusive bit for large folios. > > Signed-off-by: David Hildenbrand > --- > include/linux/pgtable.h | 31 +++++++++++ > mm/memory.c | 112 +++++++++++++++++++++++++++++++++------- > 2 files changed, 124 insertions(+), 19 deletions(-) > > diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h > index 351cd9dc7194f..891ed246978a4 100644 > --- a/include/linux/pgtable.h > +++ b/include/linux/pgtable.h > @@ -650,6 +650,37 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addres > } > #endif > > +#ifndef wrprotect_ptes > +/** > + * wrprotect_ptes - Write-protect consecutive pages that are mapped to a > + * contiguous range of addresses. > + * @mm: Address space to map the pages into. > + * @addr: Address the first page is mapped at. > + * @ptep: Page table pointer for the first entry. > + * @nr: Number of pages to write-protect. > + * > + * May be overridden by the architecture; otherwise, implemented as a simple > + * loop over ptep_set_wrprotect(). > + * > + * Note that PTE bits in the PTE range besides the PFN can differ. For example, > + * some PTEs might already be write-protected. > + * > + * Context: The caller holds the page table lock. The pages all belong > + * to the same folio. The PTEs are all in the same PMD. > + */ After writing documentation for another two such functions, I'll change this to: /** * wrprotect_ptes - Write-protect PTEs that map consecutive pages of the same * folio. * @mm: Address space the pages are mapped into. * @addr: Address the first page is mapped at. * @ptep: Page table pointer for the first entry. * @nr: Number of entries to write-protect. * * May be overridden by the architecture; otherwise, implemented as a simple * loop over ptep_set_wrprotect(). * * Note that PTE bits in the PTE range besides the PFN can differ. For example, * some PTEs might be write-protected. * * Context: The caller holds the page table lock. The PTEs map consecutive * pages that belong to the same folio. The PTEs are all in the same PMD. */ -- Cheers, David / dhildenb