From patchwork Fri Feb 9 22:15:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13551948 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F409AC48297 for ; Fri, 9 Feb 2024 22:16:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91A6C6B0075; Fri, 9 Feb 2024 17:16:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A3AF6B009C; Fri, 9 Feb 2024 17:16:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F6866B009D; Fri, 9 Feb 2024 17:16:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 595DE6B0075 for ; Fri, 9 Feb 2024 17:16:11 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 007FB1C178E for ; Fri, 9 Feb 2024 22:16:11 +0000 (UTC) X-FDA: 81773674500.01.8CF26B3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 518BE180026 for ; Fri, 9 Feb 2024 22:16:09 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YutCGfZY; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707516969; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=m8W53GW4ZEWC3PprgXE/ukTwzuRzol5bFe9xhF3bvjg=; b=izEmKIf+XVP2uV0EOk6aVJ0AHzFXMSA2z2LTN+sO6MUaKVBJqhcsVmFOBXanvQrCgt1exU QqbYRLU39zlS30zIXiJgJ5ILXi6SzLCupo5Lm4cQC9lNa1HmyV+1U8TIvFR6T2bNN+o6Sz 9xI+vVyt043TrKroef6ncmN63yH+d88= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YutCGfZY; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707516969; a=rsa-sha256; cv=none; b=lLThJFefeizFa7Z0u4tW8vKP/XUGPRkztwwUu91YnPzAEDT+3+Cl8MAeEz4ZR7JOZBvLcc H2MjMpzc8iMsRSpfPTaTIylmDdWS3qDjZeuf2SNkpcgvAOxM28kelxUenEWZbIne6n5tlN zJxOQBifi3DeEmhRs9Dg1JqKjAJuy4Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707516968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m8W53GW4ZEWC3PprgXE/ukTwzuRzol5bFe9xhF3bvjg=; b=YutCGfZY7twT2p+Pny3wildQ0kF/bNFkMZCoQY3AEXc22+IbnlE55yQe8i24082LgS3/Lh dc8VjqIdi5lKC1bPr9fuuWa1tvbSGLQAjBM2LwdpQuUrZl3lHV65elz0uz9uWJpTUSsBLR ea9X9aMHHH/xSLPoMPcyAcATmNSiDdI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-60-t2V2UsG2PJSAXQJ2V6pbnw-1; Fri, 09 Feb 2024 17:16:04 -0500 X-MC-Unique: t2V2UsG2PJSAXQJ2V6pbnw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BE704837230; Fri, 9 Feb 2024 22:16:02 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.194.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id E26B01C14B04; Fri, 9 Feb 2024 22:15:56 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Ryan Roberts , Catalin Marinas , Yin Fengwei , Michal Hocko , 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 Subject: [PATCH v2 09/10] mm/mmu_gather: improve cond_resched() handling with large folios and expensive page freeing Date: Fri, 9 Feb 2024 23:15:08 +0100 Message-ID: <20240209221509.585251-10-david@redhat.com> In-Reply-To: <20240209221509.585251-1-david@redhat.com> References: <20240209221509.585251-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Rspamd-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TqkI+l8 K54gFsdE RQsOEzwAly5+m97pfmt2Gqczyiz7ofbPFNzCZXM5+y8cgc0YXgPaMnJ4SVmGWiNv+ajeEQVp41P4EiVTB74S76sfNSXazYasTl3RmIS4yealxehrH3pP5sUZqvjAW6bppZ0MYmJ/2h5LG2bSD55fudEGSpB3Ff9T1MaELyoz0vC1BP/OtSadvlG1XUoJ7Sn/vUSJV56vaAaI6DLKEE1IuZnXfivV5dWabeJJc0nehNBkHamrErmzd7iNr120QJoBc7r5p X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: It's a pain that we have to handle cond_resched() in tlb_batch_pages_flush() manually and cannot simply handle it in release_pages() -- release_pages() can be called from atomic context. Well, in a perfect world we wouldn't have to make our code more at all. With page poisoning and init_on_free, we might now run into soft lockups when we free a lot of rather large folio fragments, because page freeing time then depends on the actual memory size we are freeing instead of on the number of folios that are involved. In the absolute (unlikely) worst case, on arm64 with 64k we will be able to free up to 256 folio fragments that each span 512 MiB: zeroing out 128 GiB does sound like it might take a while. But instead of ignoring this unlikely case, let's just handle it. So, let's teach tlb_batch_pages_flush() that there are some configurations where page freeing is horribly slow, and let's reschedule more frequently -- similarly like we did for now before we had large folio fragments in there. Note that we might end up freeing only a single folio fragment at a time that might exceed the old 512 pages limit: but if we cannot even free a single MAX_ORDER page on a system without running into soft lockups, something else is already completely bogus. In the future, we might want to detect if handling cond_resched() is required at all, and just not do any of that with full preemption enabled. Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand --- mm/mmu_gather.c | 50 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c index d175c0f1e2c8..2774044b5790 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -91,18 +91,19 @@ void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma) } #endif -static void tlb_batch_pages_flush(struct mmu_gather *tlb) +static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch) { - struct mmu_gather_batch *batch; - - for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { - struct encoded_page **pages = batch->encoded_pages; + struct encoded_page **pages = batch->encoded_pages; + unsigned int nr, nr_pages; + /* + * We might end up freeing a lot of pages. Reschedule on a regular + * basis to avoid soft lockups in configurations without full + * preemption enabled. The magic number of 512 folios seems to work. + */ + if (!page_poisoning_enabled_static() && !want_init_on_free()) { while (batch->nr) { - /* - * limit free batch count when PAGE_SIZE > 4K - */ - unsigned int nr = min(512U, batch->nr); + nr = min(512, batch->nr); /* * Make sure we cover page + nr_pages, and don't leave @@ -119,6 +120,37 @@ static void tlb_batch_pages_flush(struct mmu_gather *tlb) cond_resched(); } } + + /* + * With page poisoning and init_on_free, the time it takes to free + * memory grows proportionally with the actual memory size. Therefore, + * limit based on the actual memory size and not the number of involved + * folios. + */ + while (batch->nr) { + for (nr = 0, nr_pages = 0; + nr < batch->nr && nr_pages < 512; nr++) { + if (unlikely(encoded_page_flags(pages[nr]) & + ENCODED_PAGE_BIT_NR_PAGES_NEXT)) + nr_pages += encoded_nr_pages(pages[++nr]); + else + nr_pages++; + } + + free_pages_and_swap_cache(pages, nr); + pages += nr; + batch->nr -= nr; + + cond_resched(); + } +} + +static void tlb_batch_pages_flush(struct mmu_gather *tlb) +{ + struct mmu_gather_batch *batch; + + for (batch = &tlb->local; batch && batch->nr; batch = batch->next) + __tlb_batch_free_encoded_pages(batch); tlb->active = &tlb->local; }