From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 338271E231E for ; Mon, 12 Jan 2026 00:49:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768178969; cv=none; b=YaY9XXZ4bxkm4Iq0aY6ItvXkaffL/PUPOgd1X4GHZri4VoAJvL1HJHlQjNAVhilBoHKmHoDqeL7cKxbqKb86CGO7MuxfuBSoakwpw+7h1wSrs259NxBcWj8JWb+hlSRoleP83CjBI8JZNJ8FeqzPW2GfZolh38NmaUZAhwaD984= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768178969; c=relaxed/simple; bh=1JRpkgrFNhCWre7qoDBkLUqjqxSpODxtrL3BSTWY1As=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=MyGMewu9SkF1TZU4AM6UUguxF3kBHw9452BEk0EuyAOk9tM2A4GBItRF7KxfEPnNNEX1Tg9OwKKH2FBMvC+AXk8QzyElRK3YMolBnLfwYQP34IYxA+PHQpphP0mGDqFMGE49aZj0jE1w+R79AV+78T+S7KYYjR7bzslK2ZeQ3lY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hN+B3dim; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hN+B3dim" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34c314af2d4so4851864a91.3 for ; Sun, 11 Jan 2026 16:49:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768178967; x=1768783767; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=EqrYwOA/SKK2L1uqGSnc6LgyrcML9QFnO9Y/0CjSe2k=; b=hN+B3dimUlZWP7zpPmx7KGopyfBA0i33Lde8X2655Ahdsv/1hMx4VNd0GmI+4FfsMY Ys2HQczfG3/awsROYlY15g/bjYw3e48TLW6eLeaV8YUpibVMub4Jbrz1WSa6dJQB1ML6 r3aTsnwAyfFEp26W4NRze7RWMEhm93FqN9zK/1hGFoKKuhUniHirPjN+6JjNJeBoCzyp /EEktRoxH/v1Ze8llrq6NSb2fao7hWKCSS7BdI00bSLp9shZuE2+aOfQ/RUbAPRqUcZ2 mcBF01RgHkzuviBi6SRiQjaf5Za0Mo+3NBIGKpiHgPUc5ozCFCVr6N/SCo/TbgVpMH13 ONUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768178967; x=1768783767; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=EqrYwOA/SKK2L1uqGSnc6LgyrcML9QFnO9Y/0CjSe2k=; b=cD1Oce96/WCoxSH2QmlwYNMsmmSJ/l5HqAaeJPFteJpSQtRyCHX7QMdCDaYgIEf6So AhqjSr0K2KrtFgV/r+Hm8Sfb2reSUEHntk9kxtYrDMBqacu8Dlqdk44lr8t1Krp1gCAm shFYlo+wsly6+BXL/5inJVnawk2dXsKDRIX+4tCE61EW0XZ4xPG7Tu6EAOUyo4za6eIn h3UIX8OQhaR0UDuBRKqkoMIXqeIHd3N8ydmkdrVbVCaAckFl287quglhSB2x1bUv+f5B OK70NEz1aNRE/2vt45N+gWU1d1jCy+H+tyhTWkeXKw8jvCG4klHCL+zh3aHfRyZxRL9s 9HYg== X-Forwarded-Encrypted: i=1; AJvYcCXTy8zlbZJaqhgDJ1AE2h7MgnuWoJik9ftB86Js6mRuEEyS7sJ1aIwnU8QxLI1PZlybODzhgRRwfwXj/tY=@vger.kernel.org X-Gm-Message-State: AOJu0Yxm9UqCyptEQiLZP+KNIyEF1rXu+sJDvhlUcEgpza5Q6Cvuak6K 6XlXSHjTYJiwC3yv22lsauq4mZTfWJw4XIbLBNHZcBhuGnuozeUErfwQ+DDMgHsFGswR0ruy4zT sq/ocPp5nOsRKjw== X-Google-Smtp-Source: AGHT+IGAM5qyyxs1jxTgJvo0rmapMT3NR06icHbsNvYUhtDHrVEA7OLP9hcDkSNhOPUiw2WN2JX91JXWLGdtsQ== X-Received: from pjxx14.prod.google.com ([2002:a17:90b:58ce:b0:340:6b70:821e]) (user=jiaqiyan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1e0a:b0:34c:e5fc:faec with SMTP id 98e67ed59e1d1-34f68c3367fmr14390811a91.2.1768178967400; Sun, 11 Jan 2026 16:49:27 -0800 (PST) Date: Mon, 12 Jan 2026 00:49:20 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260112004923.888429-1-jiaqiyan@google.com> Subject: [PATCH v3 0/3] Only free healthy pages in high-order has_hwpoisoned folio From: Jiaqi Yan To: jackmanb@google.com, hannes@cmpxchg.org, linmiaohe@huawei.com, ziy@nvidia.com, harry.yoo@oracle.com, willy@infradead.org Cc: nao.horiguchi@gmail.com, david@redhat.com, lorenzo.stoakes@oracle.com, william.roche@oracle.com, tony.luck@intel.com, wangkefeng.wang@huawei.com, jane.chu@oracle.com, akpm@linux-foundation.org, osalvador@suse.de, muchun.song@linux.dev, rientjes@google.com, duenwen@google.com, jthoughton@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, rppt@kernel.org, surenb@google.com, mhocko@suse.com, Jiaqi Yan Content-Type: text/plain; charset="UTF-8" At the end of dissolve_free_hugetlb_folio() that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage. This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace. Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio(). However, there is always a time window between dissolve_free_hugetlb_folio() frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator. One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3]. Introduce free_has_hwpoisoned() to "salvage" the healthy pages and excludes the HWPoison ones in the high-order folio. free_has_hwpoisoned() happens after free_pages_prepare(), which already deals with both decomposing the original compound page, updating page metadata like alloc tag and page owner. Its idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block is freed via free_one_page() directly. free_has_hwpoisoned has linear time complexity wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity. I tested with some test-only code [4] and hugetlb-mfr [5], by checking the status of pcplist and freelist immediately after dissolve_free_hugetlb_folio() a free 2M or 1G hugetlb page that contains 1~8 HWPoison raw pages: - HWPoison pages are excluded by free_has_hwpoisoned(). - Some healthy pages can be in zone->per_cpu_pageset (pcplist) because pcp_count is not high enough. Many healthy pages are in some order's zone->free_area[order].free_list (freelist). - In rare cases, some healthy pages are in neither pcplist nor freelist. My best guest is they are allocated before the test checks. To illustrate the latency free_has_hwpoisoned() added to the freeing memory path, I tested its time cost with 8 HWPoison pages with instrument code in [4] for 20 sample runs: - Has HWPoison path: mean=2.02ms, stdev=0.14ms - No HWPoison path: mean=66us, stdev=6us free_has_hwpoisoned() is around 30x the baseline. It is far from triggering soft lockup, and the cost is fair for handling exceptional hardware memory errors. Given this nontrivial overhead, checking PG_has_hwpoisoned, doing normal free_pages_prepare(), and doing free_has_hwpoisoned() when necessary are wrapped in free_pages_prepare_has_hwpoisoned(), which replaces free_pages_prepare() calls in free_frozen_pages(). With free_has_hwpoisoned() ensuring HWPoison pages never made into buddy allocator, MFR don't need to take_page_off_buddy() anymore after disovling HWPoison hugepages. So refactor page_handle_poison to remove take_page_off_buddy() in case of hugepage, but still take_page_off_buddy() in case of free buddy page. Based on commit ccd1cdca5cd4 ("Merge tag 'nfsd-6.19-1' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux") Changelog v2 [7] -> v3: - Address comments from Mathew Wilcox, Harry Hoo, Miaohe Lin. - Let free_has_hwpoisoned() happen after free_pages_prepare(), which help to deal with decomposing the original compound page, and with page metadata like alloc tag and page owner. - Tested with "page_owner=on" and CONFIG_MEM_ALLOC_PROFILING*=y. - Wrap checking PG_has_hwpoisoned and free_has_hwpoisoned() into free_pages_prepare_has_hwpoisoned(), which replaces free_pages_prepare() calls in free_frozen_pages(). - Rename free_has_hwpoison_page() to free_has_hwpoisoned(). - Measure latency added by free_has_hwpoisoned(). - Ensure struct page *end is only used for pointer arithmetic, instead of accessed as page. - Refactor page_handl_poison instead of just __page_handle_poison. v1 [6] -> v2: - Total reimplementation based on discussions with Mathew Wilcox, Harry Hoo, Zi Yan etc - hugetlb_free_hwpoison_folio => free_has_hwpoison_pages. - Utilize has_hwpoisoned flag to tell buddy allocator a high-order folio contains HWPoison. - Simplify __page_handle_poison given that the HWPoison page(s) won't be freed within high-order folio. [1] https://lore.kernel.org/linux-mm/1460711275-1130-15-git-send-email-mgorman@techsingularity.net [2] https://lore.kernel.org/linux-mm/1460711275-1130-16-git-send-email-mgorman@techsingularity.net [3] https://lore.kernel.org/all/20230216095131.17336-1-vbabka@suse.cz [4] https://drive.google.com/file/d/1CzJn1Cc4wCCm183Y77h244fyZIkTLzCt/view?usp=sharing [5] https://lore.kernel.org/linux-mm/20251116013223.1557158-3-jiaqiyan@google.com [6] https://lore.kernel.org/linux-mm/20251116014721.1561456-1-jiaqiyan@google.com [7] https://lore.kernel.org/linux-mm/20251219183346.3627510-1-jiaqiyan@google.com Jiaqi Yan (3): mm/memory-failure: set has_hwpoisoned flags on HugeTLB folio mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio mm/memory-failure: refactor page_handle_poison() include/linux/page-flags.h | 2 +- mm/memory-failure.c | 85 ++++++++++---------- mm/page_alloc.c | 157 ++++++++++++++++++++++++++++++++++++- 3 files changed, 197 insertions(+), 47 deletions(-) -- 2.52.0.457.g6b5491de43-goog