From patchwork Fri Dec 6 09:59:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13896801 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 B18B6E77173 for ; Fri, 6 Dec 2024 10:00:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40EBD8D0002; Fri, 6 Dec 2024 05:00:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B8EB6B0204; Fri, 6 Dec 2024 05:00:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 233E88D0002; Fri, 6 Dec 2024 05:00:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 04C796B0203 for ; Fri, 6 Dec 2024 05:00:03 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B82E842DEC for ; Fri, 6 Dec 2024 10:00:03 +0000 (UTC) X-FDA: 82864088160.14.0F88ABC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id BF7CA120015 for ; Fri, 6 Dec 2024 09:59:37 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XcPE27uN; spf=pass (imf29.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733479183; 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=OHFxPv9O3q7cC5DJx/fwSJeTT3u7bfbiSrofwZ2W0U4=; b=LQXzLEc8Dwq8GCnIx1k6KN1U3awaG6Vt9byvkhWwP47h8tJh7fg4yhMigxzM0rSHnD30gQ hB4vS1YJRiPRsZYsntTOaD5PwgtgixaDeS9hocwXTaZkX1WdHFJv94Q5a+QUPM1THCwYLh bxN6t7Xd+oNtGNNCvELBpGUHo9VVMfs= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XcPE27uN; spf=pass (imf29.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733479183; a=rsa-sha256; cv=none; b=Elo+81PZNgDjpCzs3Z8RF7mpT7ZvA1JhVemJoKSwMwJZpLv4Wgfm+Bpmw5XjoBZ6MBPIvc 5sKJ8YXAmgequqVKBe2t2RwshX0zxTrOFEsPub8D3oubXfIkPoAEG55ERacd9ZYzeSecEv Db+iFamTrobNON7talcxx1YYOxt2YsU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733479200; 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; bh=OHFxPv9O3q7cC5DJx/fwSJeTT3u7bfbiSrofwZ2W0U4=; b=XcPE27uNC4/b6DjJHmyeq4hEvbMpYnGtxcGjMZUABxalUIvhOkYHULgucZTm5z74LiXWQC uNVCKWY9GBRPVidCsvHlZOCLIuB8WfwZE2EFBO2KsT1CaBs2hkTPEnA1zkTXDhto+uZMYu LY5qdPIhzez/UjQLr/+R+xdbWbNe/Ew= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-569-qeN-yu8DOxCS6-u2x_pZqQ-1; Fri, 06 Dec 2024 04:59:59 -0500 X-MC-Unique: qeN-yu8DOxCS6-u2x_pZqQ-1 X-Mimecast-MFC-AGG-ID: qeN-yu8DOxCS6-u2x_pZqQ Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-434a9dce888so10526345e9.0 for ; Fri, 06 Dec 2024 01:59:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733479198; x=1734083998; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OHFxPv9O3q7cC5DJx/fwSJeTT3u7bfbiSrofwZ2W0U4=; b=jwXN28Ig9IQoFgkJ8Ig7xE0kfZxk/78Mia4xG7bCoLA9ELhMvS4yjQDSJ7ChZ4JQo8 PH4wV4yVkh9Uj4FtXakilI88vZKqBU//Yf7XFauw2iH5U8R4QuWknMOf6rf3qF5rQpN1 oTg7w89ywTvDcbWVn5b89O0aJPxxJOtD+uY+W7O/Bsp3KIEdgKRt3iv3/SJXEysWM9J4 BSA3XGzPKUWAYqFItHL5wt4uSqyLiGKSjkMxuis6lOgmTgbIO4gat3maZpj4RvBun9i2 GyS8jaUfegT0mpwOvuVb8PEyf9VNbJrPJ/zmgPlDUY5AyMNY1le94ZFWjo6GjUAQU7TH dLkw== X-Gm-Message-State: AOJu0YyGilsNJHTDyq0/+RQCfMrXbo/vxfLUU6bbXOZAQnoiOMShmRvW wTekgxKzfSsE4rtHxwo2AloSUClxhL/kihX7+Lg3mznyt2ty6KduPfcQPHFjua1p2uP74NrNUta U2sSPaahq3iAxz8hFWX5UHd9Uzvt2jCb3He93xpn7mgsM+ZHD X-Gm-Gg: ASbGncs6DkVtbya5YHTK7w5Ju0TB0kjO/3mM7cZxlM5Nsg2Msvca6t3C2xnipoEeZsr Pln78aA8AAbCLtWjGN6BzTYjB3mTAdHct8mas8NrGHnRa57Q1raa7GxQsyDJe0Lk6YhFcpwILQt wxYUUrmvEtfNpVqDnRw0pcSfQamdhZyOXCYIOLUTJuE+MUAk54pU9HVOaUVh9W8c7JodusQjUL+ /+SGDjWIEHvYcw7YHa3e1Hp3rapa4lXaDVdOC1PrGt1k4X2zWfavrKtV9Pvh1nx1QZ+94JwVduu rvoK7MqJHdNfEqtGFVOmJ3WQuObppkZYAbw= X-Received: by 2002:a05:600c:470b:b0:431:52cc:877e with SMTP id 5b1f17b1804b1-434ddecfed2mr18057145e9.20.1733479198199; Fri, 06 Dec 2024 01:59:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IFe5tufop43b05VWxmmjhji+VfNWJUbfs4BReG0q0hG+8wMTpPLAIMsOI9sLBE+lqFnBAZ94w== X-Received: by 2002:a05:600c:470b:b0:431:52cc:877e with SMTP id 5b1f17b1804b1-434ddecfed2mr18056965e9.20.1733479197845; Fri, 06 Dec 2024 01:59:57 -0800 (PST) Received: from localhost (p200300cbc71bd0001d1f238eaeafdbf7.dip0.t-ipconnect.de. [2003:cb:c71b:d000:1d1f:238e:aeaf:dbf7]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-434d5273199sm88616395e9.14.2024.12.06.01.59.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 06 Dec 2024 01:59:56 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Zi Yan , Vlastimil Babka , Yu Zhao Subject: [PATCH v1 1/2] mm/page_alloc: conditionally split > pageblock_order pages in free_one_page() and move_freepages_block_isolate() Date: Fri, 6 Dec 2024 10:59:50 +0100 Message-ID: <20241206095951.98007-2-david@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241206095951.98007-1-david@redhat.com> References: <20241206095951.98007-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 6HuT9xZ6LX1OVyPI168kNZ0nxeX-uJcg8YTLzfhPsZo_1733479198 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BF7CA120015 X-Rspam-User: X-Stat-Signature: oqbj7e1guxte6sn9xjuonabq69dghbtg X-HE-Tag: 1733479177-320964 X-HE-Meta: U2FsdGVkX18qGXFPmKNJ0xyfE3/cHLz50jBXAV2D9cck2S+PntWV6zuTSTr85BiPf5VX9c8lSsaWi0hXfbThVF4cgSKc9FY0Ss6/Nw1yjbG/Qm1hIbPeSrxAvewIrfNf4RDVfAya9XTxsFTZoGaVfheHarsqI8PSEO/uSOow3EU9C3apV0fZPgxq0h9wH5R+69dj8JJ4SQ1GujfgLNuBuL4sLHIpdWWiscitPQHBHHtz5rjm5t8yYA3+hcH7OEJHisTYxOqo3lLpkNepC1OwkWE9mA7D2RiI++XGKx+eh71ll0ybfE1q6nQ8AnZAbGH/dl7zEQSsdbofK2CH4dE3gxEzaikPh8V/1M9JEij2IDM3H1T8MNCcFYNMZ25+KBcCXtgiJdxPGKkrezO644VRLdKVOvE+BotFFKa51iUl5GSIw3lCsAD2ZFVL+TC9y6qbI12GW/YG3SBTk25tVaBe5+RHZ7YxJvKBpeXjwqL1eMC3+ZvsMwL994uEL0FrOXfDphoIHtlXh4V0aX63V+VmycfTSD+DtxtKu0rRIBt3QzzXz3f8RLbNtA4GM49Ma3A6GMTHUycnYNeIQoCW0nAtMj5J6qVdJatWF2x+W0TwSx35wIqFv0HFAqm7IqzfBKg+UVjlVbRUp5fUkaP9JYtglSe0NZDz7mfPfxaCwXspSSUJQnbSTQsprNUJHcgDlsMmiTiXwuujxOsmuCA1Q0TSA2lOGPmaEKeXUgZx1m/06QRNcqPmXHM94gC0tN0dASQ6Fg9E+YYu+hnwRBrVqhKhLf34F8shhARDvPkrOh9bG5g3UkH94h5q2/K1BO+3VOWBni21PsKVp/26xwGgWBDaWH5CUxMWSJ8AxaIpLe1c4l7zpW+JaKwN8cnYHz+Mso6A9EEngc5inuNJTiThL8+RL9BAJ+pJIhHZAb+yTBLQfPGFEfg10omuWdE72cvD8pWR6HB/udzxJNvorNxE6zC zKqoWYvB H76AyVFLw4IQ/Kuv4CVnpsotWCK53LWc9yGFC9rQfyRIIpQl4tDcQ4LTN0yyA9rYI9TbbosKU/gIerM1vMtaUkDLPeR3xMUiBa/yQthZ6rpVP/1vNWm288quTMtkq+cdVcPkygRsbLsWzADchr3E089x0XLne9yryshAZRcZP98/4PKPnuArlqfXYtCYBfA86sAn2e4/3B6qYzK4ICD83riR/LX2/PwuhVr0sfqucShzgy5U39dwxnXn2UovD+r704ADr88OvFwjQgjXahzfDtOOOoY5A5p7/oKDNCnfCR3P5dzCpsiWDwShmG6ZB6umGpHYYHdXbRRQEvABjxctPF6+7snAMuvUUy46sM1EDWPfHNMt4GOx1s5UEQ49EVTPX4X3oQah/sBTm3VhPMd6RPKZnXrhdxkWv6RxEzAmTp4VTdvu2EFfRugJu41QinIOO2sk2z33OF+AoMw4BcstfrYx7O14sap5cS/gW 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: Let's special-case for the common scenarios that: (a) We are freeing pages <= pageblock_order (b) We are freeing a page <= MAX_PAGE_ORDER and all pageblocks match (especially, no mixture of isolated and non-isolated pageblocks) When we encounter a > MAX_PAGE_ORDER page, it can only come from alloc_contig_range(), and we can process MAX_PAGE_ORDER chunks. When we encounter a >pageblock_order <= MAX_PAGE_ORDER page, check whether all pageblocks match, and if so (common case), don't split them up just for the buddy to merge them back. This makes sure that when we free MAX_PAGE_ORDER chunks to the buddy, for example during system startups, memory onlining, or when isolating consecutive pageblocks via alloc_contig_range()/memory offlining, that we don't unnecessarily split up what we'll immediately merge again, because the migratetypes match. Rename split_large_buddy() to __free_one_page_maybe_split(), to make it clearer what's happening, and handle in it only natural buddy orders, not the alloc_contig_range(__GFP_COMP) special case: handle that in free_one_page() only. Signed-off-by: David Hildenbrand Acked-by: Yu Zhao Acked-by: Vlastimil Babka lock, flags); } -/* Split a multi-block free page into its individual pageblocks. */ -static void split_large_buddy(struct zone *zone, struct page *page, - unsigned long pfn, int order, fpi_t fpi) +static bool pfnblock_migratetype_equal(unsigned long pfn, + unsigned long end_pfn, int mt) { - unsigned long end = pfn + (1 << order); + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | end_pfn, pageblock_nr_pages)); + while (pfn != end_pfn) { + struct page *page = pfn_to_page(pfn); + + if (unlikely(mt != get_pfnblock_migratetype(page, pfn))) + return false; + pfn += pageblock_nr_pages; + } + return true; +} + +static void __free_one_page_maybe_split(struct zone *zone, struct page *page, + unsigned long pfn, int order, fpi_t fpi_flags) +{ + const unsigned long end_pfn = pfn + (1 << order); + int mt = get_pfnblock_migratetype(page, pfn); + + VM_WARN_ON_ONCE(order > MAX_PAGE_ORDER); VM_WARN_ON_ONCE(!IS_ALIGNED(pfn, 1 << order)); /* Caller removed page from freelist, buddy info cleared! */ VM_WARN_ON_ONCE(PageBuddy(page)); - if (order > pageblock_order) - order = pageblock_order; - - while (pfn != end) { - int mt = get_pfnblock_migratetype(page, pfn); + /* + * With CONFIG_MEMORY_ISOLATION, we might be freeing MAX_ORDER_NR_PAGES + * pages that cover pageblocks with different migratetypes; for example + * only some migratetypes might be MIGRATE_ISOLATE. In that (unlikely) + * case, fallback to freeing individual pageblocks so they get put + * onto the right lists. + */ + if (!IS_ENABLED(CONFIG_MEMORY_ISOLATION) || + likely(order <= pageblock_order) || + pfnblock_migratetype_equal(pfn + pageblock_nr_pages, end_pfn, mt)) { + __free_one_page(page, pfn, zone, order, mt, fpi_flags); + return; + } - __free_one_page(page, pfn, zone, order, mt, fpi); - pfn += 1 << order; + while (pfn != end_pfn) { + mt = get_pfnblock_migratetype(page, pfn); + __free_one_page(page, pfn, zone, pageblock_order, mt, fpi_flags); + pfn += pageblock_nr_pages; page = pfn_to_page(pfn); } } @@ -1254,7 +1280,24 @@ static void free_one_page(struct zone *zone, struct page *page, unsigned long flags; spin_lock_irqsave(&zone->lock, flags); - split_large_buddy(zone, page, pfn, order, fpi_flags); + if (likely(order <= MAX_PAGE_ORDER)) { + __free_one_page_maybe_split(zone, page, pfn, order, fpi_flags); + } else if (IS_ENABLED(CONFIG_CONTIG_ALLOC)) { + const unsigned long end_pfn = pfn + (1 << order); + + /* + * The only way we can end up with order > MAX_PAGE_ORDER is + * through alloc_contig_range(__GFP_COMP). + */ + while (pfn != end_pfn) { + __free_one_page_maybe_split(zone, page, pfn, + MAX_PAGE_ORDER, fpi_flags); + pfn += MAX_ORDER_NR_PAGES; + page = pfn_to_page(pfn); + } + } else { + WARN_ON_ONCE(1); + } spin_unlock_irqrestore(&zone->lock, flags); __count_vm_events(PGFREE, 1 << order); @@ -1790,7 +1833,7 @@ bool move_freepages_block_isolate(struct zone *zone, struct page *page, del_page_from_free_list(buddy, zone, order, get_pfnblock_migratetype(buddy, pfn)); set_pageblock_migratetype(page, migratetype); - split_large_buddy(zone, buddy, pfn, order, FPI_NONE); + __free_one_page_maybe_split(zone, buddy, pfn, order, FPI_NONE); return true; } @@ -1801,7 +1844,7 @@ bool move_freepages_block_isolate(struct zone *zone, struct page *page, del_page_from_free_list(page, zone, order, get_pfnblock_migratetype(page, pfn)); set_pageblock_migratetype(page, migratetype); - split_large_buddy(zone, page, pfn, order, FPI_NONE); + __free_one_page_maybe_split(zone, page, pfn, order, FPI_NONE); return true; } move: