From mboxrd@z Thu Jan 1 00:00:00 1970 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 16BC0C48297 for ; Mon, 12 Feb 2024 11:39:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C5D56B0071; Mon, 12 Feb 2024 06:39:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 475BC6B0072; Mon, 12 Feb 2024 06:39:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EE676B0074; Mon, 12 Feb 2024 06:39:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1DF016B0071 for ; Mon, 12 Feb 2024 06:39:26 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D57EE4074A for ; Mon, 12 Feb 2024 11:39:25 +0000 (UTC) X-FDA: 81782956290.30.B920411 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 685E01A000E for ; Mon, 12 Feb 2024 11:39:23 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Hi9jWNsM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.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=1707737963; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tnxlD2zZuM/MfG8rr4PtMPWvp6ek2FU8otdfOIHULks=; b=cdZeEv+LNszMxmNMjoBHzfxSQ5sgyeqnSRXcrRcxyAnh8O3L0xI+Dbqcx+tDWFgGhX++iG N2PwErShj8wMN+sacQVzC3s+uafg7HxiYlxT9aCnhUe6OZxVEatu7yDnelurP+5SEzHc8q 6VHxVRlNcH+cT6cohrTboMdSDeysOFQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Hi9jWNsM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.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=1707737963; a=rsa-sha256; cv=none; b=L3HF9Dnnw84usbAO+xO1k4aAJ4cLxhLMoTD0JUaadt2qLG9SFTng/WRMF1BE94PdrHRCKG 7wepYiI2NCjb/rGbN7Mhdr3ouGVlZQ/F90D6Du0FaIrwEBkAiLVR529LYaUUtQ5R5h/we9 wulNfJNiBbgKtCzeVkP1n//RivPg8Pg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707737962; 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=tnxlD2zZuM/MfG8rr4PtMPWvp6ek2FU8otdfOIHULks=; b=Hi9jWNsMXXUKC99gjHpUqjPAlZ0yvJv5JN6BiBnSVAlL3/QHP/K2FqFgZViYZifHnKDYbY xdWE51L+lIqb0R/9shav5dS6Eb9zamWF5CbEuDnSqKr65aLIppxIb+x+NUmcNl2aEH1fEO +OYhvl2HhNcorJf/6epOlGRbe/kMiNA= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-244-U7N9c4kIPTmcDSx5k9k5sg-1; Mon, 12 Feb 2024 06:39:21 -0500 X-MC-Unique: U7N9c4kIPTmcDSx5k9k5sg-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-33b1799e433so1515417f8f.2 for ; Mon, 12 Feb 2024 03:39:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707737960; x=1708342760; 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=tnxlD2zZuM/MfG8rr4PtMPWvp6ek2FU8otdfOIHULks=; b=HSvxn662aaDnMBcHXVC+vFq+NwVNDJU28N2GsZ1Kdjbxo5kbixMnZLnDWrBFCdFmiW 4QSbGghDnobVjKlb4kPwEbjmWUa7YgjBu1ByASXu9lxjxg/aNf4U59XPOrPD5//rgH1d SzWACiDBUHMFL2/nIrXlpYkgc2CR5W8g4G/OvvDeKazbHgBiMOLQc5CGVRXq217wF12H jchWOz6wswClLXZPoxG/AJqLdtDjEuKgI6uVqoPafQrRpHTa7TvbV73egxuW+s+lrNZ4 e7ZgK70FZycO1WVacFEHb0TK5jR1+rX18Z2/rgpUACDK0f/mF26C4K+WZ/TpZc+8Isf5 gJdw== X-Gm-Message-State: AOJu0Yz/Q/sW8Fty1YiraGwMGVlylaKjxWnvHo23Zp+klYSNxDPk9nJo 27amntpbF+p7Y/28vua2ODKBUKWBXo9lGobFZ7P4VSX/kQD/ds3La1cGmySz2F+nX353PchQK77 nLylyilmg7cTn203hSYI7BrKAU92OiTOji6hHlAxIROta1edC X-Received: by 2002:adf:f245:0:b0:33b:4908:9bbc with SMTP id b5-20020adff245000000b0033b49089bbcmr5699720wrp.40.1707737960572; Mon, 12 Feb 2024 03:39:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IGUioO4snca74CokLIda6VZmW9gaQHSjIGT6dyz21HSz8JeGKkqynO+oRpxdf1ri8H8EQD4hg== X-Received: by 2002:adf:f245:0:b0:33b:4908:9bbc with SMTP id b5-20020adff245000000b0033b49089bbcmr5699687wrp.40.1707737960095; Mon, 12 Feb 2024 03:39:20 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXiyxq86CcNpJpJsAACFBe1j8fZYXvupRStxAm/+jVhV5iBauROwZmNuDfXqNOf9q0vZDAHyC6Vl/kbqUmM4O0rgI817GGBGqc9xizNxNug0I7OfCp3LWkYJ1qW4FJz4Zplx0+heCfgjGSZa5pBCNeyKLWM1wet/IeVGyxnOG+fcJWh6k1vSejfKDvER62BvyCFj+K3PSyfNYo5vumCVZ3RARp3DqFudrU5Sh7HVoqKW6eRzvyITwymDfPiAFnDA32MQJNOJSiUHVR8rgHtHwRJXZziP1wULzTJoj87FoS9bVaiBoeDVvpl/cL1Ehm+W7OAl/ks2WcYAcJwlxuTLdNMzAbsDdB7NKBQGXuF8MF+1NR+/GTAT4tDu3CI5ysjmyYLyUSgmO6lg2hVF9b72l/vxVcalPZ9/15mY8auwJrWxU+2ktQUVZOAPXhRNTEHp44git663SnYQK49M11CxuxaFKiiAxwt1CgtfxPIH4xyoh6U1C0Y+84SSkH/4fEVKqtmtES+meG6P//7YI2jz7TAha46pBQKF7PcloXyY4frF/YRuhTzdv2scmk0alZMhBvmckQDQKCklT+LobWDSsJEnYt2P2c9+wxZbvnD3Q/pX56/24mM0TPMDM36WkMyjAqG7ZZD9GWF9h4SIZY3HhdIEdXPOfQAHWtuRFwjsCS+ZxknpqBaox+DSnYhFFcjpbJAVGd/utcn/rEMBO+qwuK54y0Ke0lIc0CSY0sH6ZYxPtN+4iw= Received: from ?IPV6:2003:cb:c730:2200:7229:83b1:524e:283a? (p200300cbc7302200722983b1524e283a.dip0.t-ipconnect.de. [2003:cb:c730:2200:7229:83b1:524e:283a]) by smtp.gmail.com with ESMTPSA id w13-20020a5d404d000000b0033b4dae972asm6535562wrp.37.2024.02.12.03.39.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Feb 2024 03:39:19 -0800 (PST) Message-ID: Date: Mon, 12 Feb 2024 12:39:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 09/10] mm/mmu_gather: improve cond_resched() handling with large folios and expensive page freeing To: Ryan Roberts , linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , 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 References: <20240209221509.585251-1-david@redhat.com> <20240209221509.585251-10-david@redhat.com> <6c66f7ca-4b14-4bbb-bf06-e81b3481b03f@redhat.com> <590946ad-a538-4c99-947f-93455c2d96c6@arm.com> <66ca6c58-1983-494f-b920-140be736f1d8@redhat.com> <398991e6-d09d-4f47-a110-4ff1e8356b6e@arm.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK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1trDQmPx2iP5+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: <398991e6-d09d-4f47-a110-4ff1e8356b6e@arm.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 685E01A000E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: spm6t6joagxqwg4mqjbai8jhbdeoy6ck X-HE-Tag: 1707737963-721960 X-HE-Meta: U2FsdGVkX1+U9BF1tooMWsiqu1EK2LuPnXjYMyyXh8OHwpO5PZj5mOtZoxbMvQ0VX6EsrJTNyEbbJv++HZwuxxnx+xK2AN3qz8b27reyC0Qji1aUQm5Fd+7ZvS3+hCTonzeKvCahxe4hnLmNFUOcx0+lBvcXqqhTB1alhyO6LULOype2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x3gEltHLEZoaVazUDOmOjyeQzofqn4zKTONI2xAfz3msQNZeGtAsxG3nFavJkBFdJ3g++RHlmmNulBIKRni9MCcnPUFSrswbmnzI4G6YGbBO/Sn1eJLt96PeSK6Gm0xPL4SFVqw88o24pbNheYCxe+EJabMXcd3YVwq91VA7H40mtA07aj26TINrJTxZYSuLkqq4DSW0fUqmBS+AaYf6tH6+kRMdrGqGc6lkZYPTePF4aUC5RGtnl1DYmyFJUr4TV9S3iFz9Mlp5qLW6sCpo2/tCPwsCknwctVWssJiE9CHE6xf6DcoweqhmY+3EUoLA0Azq1Hpo= 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: On 12.02.24 12:21, Ryan Roberts wrote: > On 12/02/2024 11:05, David Hildenbrand wrote: >> On 12.02.24 11:56, David Hildenbrand wrote: >>> On 12.02.24 11:32, Ryan Roberts wrote: >>>> On 12/02/2024 10:11, David Hildenbrand wrote: >>>>> Hi Ryan, >>>>> >>>>>>> -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()) { >>>>>> >>>>>> Is the performance win really worth 2 separate implementations keyed off this? >>>>>> It seems a bit fragile, in case any other operations get added to free >>>>>> which are >>>>>> proportional to size in future. Why not just always do the conservative >>>>>> version? >>>>> >>>>> I really don't want to iterate over all entries on the "sane" common case. We >>>>> already do that two times: >>>>> >>>>> a) free_pages_and_swap_cache() >>>>> >>>>> b) release_pages() >>>>> >>>>> Only the latter really is required, and I'm planning on removing the one in (a) >>>>> to move it into (b) as well. >>>>> >>>>> So I keep it separate to keep any unnecessary overhead to the setups that are >>>>> already terribly slow. >>>>> >>>>> No need to iterate a page full of entries if it can be easily avoided. >>>>> Especially, no need to degrade the common order-0 case. >>>> >>>> Yeah, I understand all that. But given this is all coming from an array, (so >>>> easy to prefetch?) and will presumably all fit in the cache for the common case, >>>> at least, so its hot for (a) and (b), does separating this out really make a >>>> measurable performance difference? If yes then absolutely this optimizaiton >>>> makes sense. But if not, I think its a bit questionable. >>> >>> I primarily added it because >>> >>> (a) we learned that each cycle counts during mmap() just like it does >>> during fork(). >>> >>> (b) Linus was similarly concerned about optimizing out another batching >>> walk in c47454823bd4 ("mm: mmu_gather: allow more than one batch of >>> delayed rmaps"): >>> >>> "it needs to walk that array of pages while still holding the page table >>> lock, and our mmu_gather infrastructure allows for batching quite a lot >>> of pages.  We may have thousands on pages queued up for freeing, and we >>> wanted to walk only the last batch if we then added a dirty page to the >>> queue." >>> >>> So if it matters enough for reducing the time we hold the page table >>> lock, it surely adds "some" overhead in general. >>> >>> >>>> >>>> You're the boss though, so if your experience tells you this is neccessary, then >>>> I'm ok with that. >>> >>> I did not do any measurements myself, I just did that intuitively as >>> above. After all, it's all pretty straight forward (keeping the existing >>> logic, we need a new one either way) and not that much code. >>> >>> So unless there are strong opinions, I'd just leave the common case as >>> it was, and the odd case be special. >> >> I think we can just reduce the code duplication easily: >> >> diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c >> index d175c0f1e2c8..99b3e9408aa0 100644 >> --- a/mm/mmu_gather.c >> +++ b/mm/mmu_gather.c >> @@ -91,18 +91,21 @@ void tlb_flush_rmaps(struct mmu_gather *tlb, struct >> vm_area_struct *vma) >>  } >>  #endif >> >> -static void tlb_batch_pages_flush(struct mmu_gather *tlb) >> -{ >> -    struct mmu_gather_batch *batch; >> +/* >> + * 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. >> + */ >> +#define MAX_NR_FOLIOS_PER_FREE        512 >> >> -    for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { >> -        struct encoded_page **pages = batch->encoded_pages; >> +static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch) >> +{ >> +    struct encoded_page **pages = batch->encoded_pages; >> +    unsigned int nr, nr_pages; >> >> -        while (batch->nr) { >> -            /* >> -             * limit free batch count when PAGE_SIZE > 4K >> -             */ >> -            unsigned int nr = min(512U, batch->nr); >> +    while (batch->nr) { >> +        if (!page_poisoning_enabled_static() && !want_init_on_free()) { >> +            nr = min(MAX_NR_FOLIOS_PER_FREE, batch->nr); >> >>              /* >>               * Make sure we cover page + nr_pages, and don't leave >> @@ -111,14 +114,39 @@ static void tlb_batch_pages_flush(struct mmu_gather *tlb) >>              if (unlikely(encoded_page_flags(pages[nr - 1]) & >>                       ENCODED_PAGE_BIT_NR_PAGES_NEXT)) >>                  nr++; >> +        } else { >> +            /* >> +             * 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. >> +             */ >> +            for (nr = 0, nr_pages = 0; >> +                 nr < batch->nr && nr_pages < MAX_NR_FOLIOS_PER_FREE; >> +                 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; >> +        free_pages_and_swap_cache(pages, nr); >> +        pages += nr; >> +        batch->nr -= nr; >> >> -            cond_resched(); >> -        } >> +        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; >>  } >> > > Yes this is much cleaner IMHO! I don't think putting the poison and init_on_free > checks inside the while loops should make a whole lot of difference - you're > only going round that loop once in the common (4K pages) case. Exactly. > > Reviewed-by: Ryan Roberts Thanks, this is the full patch, including the extended patch description: From 5518fb32b950154794380d029eef8751af8c9804 Mon Sep 17 00:00:00 2001 From: David Hildenbrand Date: Fri, 9 Feb 2024 18:43:11 +0100 Subject: [PATCH] mm/mmu_gather: improve cond_resched() handling with large folios and expensive page freeing In tlb_batch_pages_flush(), we can end up freeing up to 512 pages or now up to 256 folio fragments that span more than one page, before we conditionally reschedule. 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 complicated 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. Avoid yet another loop over all encoded pages in the common case by handling that separately. Note that with page poisoning/zeroing, we might now 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. Freeing a PMD-mapped THP would similarly cause trouble. In theory, we might even free 511 order-0 pages + a single MAX_ORDER page, effectively having to zero out 8703 pages on arm64 with 64k, translating to ~544 MiB of memory: however, if 512 MiB doesn't result in soft lockups, 544 MiB is unlikely to result in soft lockups, so we won't care about that for the time being. 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. Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand --- mm/mmu_gather.c | 58 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c index d175c0f1e2c8..99b3e9408aa0 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -91,18 +91,21 @@ void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma) } #endif -static void tlb_batch_pages_flush(struct mmu_gather *tlb) -{ - struct mmu_gather_batch *batch; +/* + * 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. + */ +#define MAX_NR_FOLIOS_PER_FREE 512 - for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { - struct encoded_page **pages = batch->encoded_pages; +static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch) +{ + struct encoded_page **pages = batch->encoded_pages; + unsigned int nr, nr_pages; - while (batch->nr) { - /* - * limit free batch count when PAGE_SIZE > 4K - */ - unsigned int nr = min(512U, batch->nr); + while (batch->nr) { + if (!page_poisoning_enabled_static() && !want_init_on_free()) { + nr = min(MAX_NR_FOLIOS_PER_FREE, batch->nr); /* * Make sure we cover page + nr_pages, and don't leave @@ -111,14 +114,39 @@ static void tlb_batch_pages_flush(struct mmu_gather *tlb) if (unlikely(encoded_page_flags(pages[nr - 1]) & ENCODED_PAGE_BIT_NR_PAGES_NEXT)) nr++; + } else { + /* + * 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. + */ + for (nr = 0, nr_pages = 0; + nr < batch->nr && nr_pages < MAX_NR_FOLIOS_PER_FREE; + 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; + free_pages_and_swap_cache(pages, nr); + pages += nr; + batch->nr -= nr; - cond_resched(); - } + 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; } -- 2.43.0 -- Cheers, David / dhildenb