Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1563460rdb; Wed, 31 Jan 2024 02:27:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEgHCj1xq4KFZZPDV1CUk7OhTvWABynXGI16sycMOOBcnWrbieZCy+sSRHH8Gv4hMTexb+E X-Received: by 2002:a05:620a:3183:b0:783:f683:ee26 with SMTP id bi3-20020a05620a318300b00783f683ee26mr1128651qkb.31.1706696872682; Wed, 31 Jan 2024 02:27:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706696872; cv=pass; d=google.com; s=arc-20160816; b=T1Q+mu/TzoZObWYiCZgFXJVAxThslen48JgUBFSroLao52oLJecQR/0MgYp0+NpJlr uEsv0AeQGLwSPZQTBDqpdqMibee10PV7sWasUICsEnHSyvT/TMySKEBp5xfLrRWQRNqv PvKmzYWFL+ymrn9ZZ8NKounVUG8UWV/Nm6QBjT9F/zNGZzweVGDxv2jViIw3rDBPFpfh iPK4C56fx0Ua4HTA8yf2mVXQD5HEMe/UGkCnn8nyDccv/MbzXjXp1Qt1AHf59AIrPPem NSBdIjLwI8CxXBuwBbI14+Q1+eZIA4ZN/ZO6FG3d0obz1VFilyP8o9oZMCKfVdgpQJGT 1CNg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=LmAV2l61ilUvFjcYEuKpa4PqXIDLxUje4g/USlfxWTo=; fh=dxoAg/IF9edelPoNiDOpdyrTt7Wjjxyjq6Uf4spDvuU=; b=En25JefR7H8RxXFCS4mO57xTrQ3z4ME/3+Puu37IJJq7WXG1phCmTapB+3V3FiKAgh SC1+FX0o6OZLb4B/qTYqdSsGQ/BQWIaJQmdbs51RU+qveanQdGgSxwyQFHWtGZIdIqxh qyfMswbkFQ0Iccrp8xOsSu6vDJt04W9ADlyBcm7C9Mxr63iBXa9HBsAamyWzHTipY48+ G/jDaGu663mMG7znpQEI7VnvVEc084J3+RYNJxD3H+A9s8IZ8riseFrk47FcrugJmfNP 5HuVEyW4n1fG3t5cwkHC5hjbpgZ42r9B6471fUvLJFVHkUVIu9tCNkiQcHekY1feR0Te q4BQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-46228-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46228-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=1; AJvYcCW1Zp3h7O+hRc457JyputhJQKBM+ky0uRWgWgfoPHT6Wg06m5rBhBmupFmrr/Jc9PW9Hilv0CgqtjKeHOnBMg7hTdyE5OY6Qyz+mTBDjA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u7-20020a05620a454700b007840b2928e5si3658470qkp.236.2024.01.31.02.27.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 02:27:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46228-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-46228-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46228-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 63FA61C29C4C for ; Wed, 31 Jan 2024 10:27:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 30BB469D26; Wed, 31 Jan 2024 10:26:27 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E901769964; Wed, 31 Jan 2024 10:26:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706696786; cv=none; b=iEV7pBWkixJGHrTzRzoQg7PoJLuwdPsm6ejXzYWRtkgJUlTZZYb27ZaTkkgFeGfTuD3tmPfoseD+SIptWsVN29LmCweunpfPRnKt98eStEjJNQfpDJSiF0Ogddnkla6Mvc686ZvZxkwmqrvSXEFY+hQrEvz5ho/5gPT+UibEYmU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706696786; c=relaxed/simple; bh=6k5DByJTFjsHHVF679JwFY8xzfBlwG6FkcsPcj4eTlI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=G80HiY6r6A7moUBav60uLKuWKVqQ1h2YjWhG7GQv4YUDRB2/SEIuw2/PgSxWO7fQ0NnlwokUTORToTkO+Vy3Rd4dRQPKZGJYdP8WeF2AE7QCFfUKSfG92ughHBK3LA5OlWkkJK4CchwtKy6PSahqnlukwShpVnHRkdfnOW7Q8iI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9F4F4DA7; Wed, 31 Jan 2024 02:27:06 -0800 (PST) Received: from [10.57.79.60] (unknown [10.57.79.60]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 994A23F5A1; Wed, 31 Jan 2024 02:26:15 -0800 (PST) Message-ID: <1fd26a83-8e6f-4b96-9d27-dd46de9488cc@arm.com> Date: Wed, 31 Jan 2024 10:26:13 +0000 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 v1 0/9] mm/memory: optimize unmap/zap with PTE-mapped THP To: David Hildenbrand , Yin Fengwei , linux-kernel@vger.kernel.org, Linus Torvalds , Michal Hocko Cc: linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , Catalin Marinas , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Peter Zijlstra , Michael Ellerman , Christophe Leroy , "Naveen N. Rao" , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Arnd Bergmann , linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, "Huang, Ying" References: <20240129143221.263763-1-david@redhat.com> <4ef64fd1-f605-4ddf-82e6-74b5e2c43892@intel.com> Content-Language: en-GB From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 31/01/2024 10:16, David Hildenbrand wrote: > On 31.01.24 03:20, Yin Fengwei wrote: >> On 1/29/24 22:32, David Hildenbrand wrote: >>> This series is based on [1] and must be applied on top of it. >>> Similar to what we did with fork(), let's implement PTE batching >>> during unmap/zap when processing PTE-mapped THPs. >>> >>> We collect consecutive PTEs that map consecutive pages of the same large >>> folio, making sure that the other PTE bits are compatible, and (a) adjust >>> the refcount only once per batch, (b) call rmap handling functions only >>> once per batch, (c) perform batch PTE setting/updates and (d) perform TLB >>> entry removal once per batch. >>> >>> Ryan was previously working on this in the context of cont-pte for >>> arm64, int latest iteration [2] with a focus on arm6 with cont-pte only. >>> This series implements the optimization for all architectures, independent >>> of such PTE bits, teaches MMU gather/TLB code to be fully aware of such >>> large-folio-pages batches as well, and amkes use of our new rmap batching >>> function when removing the rmap. >>> >>> To achieve that, we have to enlighten MMU gather / page freeing code >>> (i.e., everything that consumes encoded_page) to process unmapping >>> of consecutive pages that all belong to the same large folio. I'm being >>> very careful to not degrade order-0 performance, and it looks like I >>> managed to achieve that. >> > > Let's CC Linus and Michal to make sure I'm not daydreaming. > > Relevant patch: >   https://lkml.kernel.org/r/20240129143221.263763-8-david@redhat.com > > Context: I'm adjusting MMU gather code to support batching of consecutive pages > that belong to the same large folio, when unmapping/zapping PTEs. > > For small folios, there is no (relevant) change. > > Imagine we have a PTE-mapped THP (2M folio -> 512 pages) and zap all 512 PTEs: > Instead of adding 512 individual encoded_page entries, we add a combined entry > that expresses "page+nr_pages". That allows for "easily" adding various other > per-folio batching (refcount, rmap, swap freeing). > > The implication is, that we can now batch effective more pages with large > folios, exceeding the old 10000 limit. The number of involved *folios* does not > increase, though. > >> One possible scenario: >> If all the folio is 2M size folio, then one full batch could hold 510M memory. >> Is it too much regarding one full batch before just can hold (2M - 4096 * 2) >> memory? > > Excellent point, I think there are three parts to it: > > (1) Batch pages / folio fragments per batch page > > Before this change (and with 4k folios) we have exactly one page (4k) per > encoded_page entry in the batch. Now, we can have (with 2M folios), 512 pages > for every two encoded_page entries (page+nr_pages) in a batch page. So an > average ~256 pages per encoded_page entry. > > So one batch page can now store in the worst case ~256 times the number of > pages, but the number of folio fragments ("pages+nr_pages") would not increase. > > The time it takes to perform the actual page freeing of a batch will not be 256 > times higher -- the time is expected to be much closer to the old time (i.e., > not freeing more folios). IIRC there is an option to zero memory when it is freed back to the buddy? So that could be a place where time is proportional to size rather than proportional to folio count? But I think that option is intended for debug only? So perhaps not a problem in practice? > > (2) Delayed rmap handling > > We limit batching early (see tlb_next_batch()) when we have delayed rmap > pending. Reason being, that we don't want to check for many entries if they > require delayed rmap handling, while still holding the page table lock (see > tlb_flush_rmaps()), because we have to remove the rmap before dropping the PTL. > > Note that we perform the check whether we need delayed rmap handling per > page+nr_pages entry, not per page. So we won't perform more such checks. > > Once we set tlb->delayed_rmap (because we add one entry that requires it), we > already force a flush before dropping the PT lock. So once we get a single > delayed rmap entry in there, we will not batch more than we could have in the > same page table: so not more than 512 entries (x86-64) in the worst case. So it > will still be bounded, and not significantly more than what we had before. > > So regarding delayed rmap handling I think this should be fine. > > (3) Total patched pages > > MAX_GATHER_BATCH_COUNT effectively limits the number of pages we allocate (full > batches), and thereby limits the number of pages we were able to batch. > > The old limit was ~10000 pages, now we could batch ~5000 folio fragments > (page+nr_pages), resulting int the "times 256" increase in the worst case on > x86-64 as you point out. > > This 10000 pages limit was introduced in 53a59fc67f97 ("mm: limit mmu_gather > batching to fix soft lockups on !CONFIG_PREEMPT") where we wanted to handle > soft-lockups. > > As the number of effective folios we are freeing does not increase, I *think* > this should be fine. > > > If any of that is a problem, we would have to keep track of the total number of > pages in our batch, and stop as soon as we hit our 10000 limit -- independent of > page vs. folio fragment. Something I would like to avoid of possible. >