From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (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 81EC04499A9 for ; Thu, 9 Jul 2026 18:54:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783623304; cv=none; b=dvflBglg1nzBTmTRnA+whw6IIWDZFaPhrNUaTeQU+8c04ccaXE/h8v1N3B/IMUAe8mkd2yAmr3EVSJbqU8pSRr4y/5XCvVSDRw3Gga3R+6LZiu23iJxLCSZWSp+4J/kXZv6TsVC+K6P3ltZ30IczHbFojhYWiYFfgzr6Az36ZyY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783623304; c=relaxed/simple; bh=yEH1Th0SMln4nRxcxBbel/RZvr2c6M5YqAH272qGUfs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n1U46kzWYf5IzF2wH1cW1in+yGEe//zfaA4qa3fGeYo8j+TrkgbsZbFJrpq4B7LkRtWBU00BEaBRLqJPebNpHLnTPk88wAoIZSRk1svbF7qBX40gknEo3Qlc+ox7IW4EZ+KPt2bzt01DKuh+opV+eSEw6sqRcV5QrA6vPN12cW8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=i8X0I0Uo; arc=none smtp.client-ip=209.85.210.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="i8X0I0Uo" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-7e9f69eeb6cso136852a34.1 for ; Thu, 09 Jul 2026 11:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783623298; x=1784228098; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Wf2mCygj2V3MtxPOIjhT5RGqf+/9JXlGVheo76S1fao=; b=i8X0I0UoR4MJs8iyzeTDh+RnR9NxndvVSYo9xZUgeZY1zBMl8W/HuesVO/cmXMip6P hd73orXixvA+vVLz0ySGONIayPxuIBAwCxWLdHofmBv8xlloHyLe+lNrzL1kS2prQZGP CI6ywlH27qEkr0a5yuVMF21w4t/JtrguLEXhh8EKr26tH3Ljk8H/5pclbTKXTM/+XhuM 56bUMM8XYvS466LZ+fb99hdTrGQMjAGQ6JuVsRW0VSucMj10DFS9fu73Lp8bzyi8Tue+ nLLevn6hqvLV1iWgWQxnQNmecbFKmQJyWlQBATTgopJpFV8H7sm4vjUAWwA0AJBKwUj8 gTXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783623298; x=1784228098; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Wf2mCygj2V3MtxPOIjhT5RGqf+/9JXlGVheo76S1fao=; b=OXsDeZ+PSuYiP2qQTPblIdh1e4LA7BA87B+CRgV0O/N7zvzLJm5DdiZ7UXekfV5fzJ h6aUBTBhOqmEuWb9rPdG2RvVyoesAZupMu+sZbjcGwtRLjEBskiMRDQoc73iOqIw7RPJ vRYAW/5T4i3uZjd3FdGIjVzNl4NUqoFcHCcjgYfdzfNAEN6Ibr2xTnxV+2hvp05Y4EJs /FRJiKsHZ/FN43Ii4Mzxoc1Oy7ih6dfyrZvn67TGQHSqEGkQ5HWNxKcqFFM4mlNU49fL jamXngYQOZdc5hzxHvBjZ5K2pIzBEGRZrfjF2TQ5uuHTaSsiPwnTNY6WmSiVrMw+I6PX dj1w== X-Forwarded-Encrypted: i=1; AFNElJ9o04REYd/4nmSJzT1fKJCMvGzV7o/ZEtUvtVa6aQgYiBgstLaVpGMI7YG/sMrRDBuPI5h4L+o5kWIuSOc=@vger.kernel.org X-Gm-Message-State: AOJu0YwMK6lTCBbFwug2X/IQqW5vS24gb62fRP/0RBnvunOZssG7hbvl hH9vvx4C/Dto+J7fhknv3GQUGxNkTyZgb5FFTIXptbnxwf65lCCwgWbY X-Gm-Gg: AfdE7clkAnlec6smGDUnqa332Y71HJfyi89RGRI9V+BurJ1/sAlsLlHyQwXLjiJg53k DGbOkMet51nKyLYf0IJ2qIyk6JaHPWSexxoT91rASeJ5fZWsmNXCOdbw5+59uRJ/3GnfclzfClW XHyVpCcx64fugx+Tqr6SwSLl673YR43B3c/207dNdZo7h3C+WwUP2TfOqSUXPsF130zZn3b2vpB 6vl7JyhcAzfYY5RwTiLPwzqlFPLMFPKKSPiarJytfDPF96Z4dNzB+0GMMvWXkCTs67RPAMyn4Ob cOjw78RdsGG9EsGHH3lbf5DkkREB6UQl4Oh848qwkjEXA3qGcLmEUpm4IWEIKPF2sTw3W25ivNi Bp5Q8NRRT4iWy/CVAvCbw8sus3GYMUXm/gMI8/ptzmTpmcuaax9V85Q8PR4RmJrMabZGFKEDXmG OIxzZnSP8eOU0Zl5z5CZtWOjm3C7Ub8FU9bMxpc+34j8Q= X-Received: by 2002:a05:6830:2b08:b0:7e9:b537:102e with SMTP id 46e09a7af769-7ebcff9e14cmr5905427a34.27.1783623298042; Thu, 09 Jul 2026 11:54:58 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:40::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7ebcb2630casm5108633a34.18.2026.07.09.11.54.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 11:54:57 -0700 (PDT) From: Joshua Hahn To: Ackerley Tng via B4 Relay Cc: Muchun Song , Oscar Salvador , David Hildenbrand , Shakeel Butt , Nhat Pham , Andrew Morton , Peter Xu , Wupeng Ma , fvdl@google.com, rientjes@google.com, jthoughton@google.com, vannapurve@google.com, erdemaktas@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ackerley Tng , stable@vger.kernel.org Subject: Re: [PATCH v2 3/5] mm: hugetlb: Fix folio refcount mismatch on memcg charge failure Date: Thu, 9 Jul 2026 11:54:55 -0700 Message-ID: <20260709185456.1854151-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260708-hugetlb-alloc-failure-fixes-v2-3-c7f27cbb462b@google.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Wed, 08 Jul 2026 15:12:51 -0700 Ackerley Tng via B4 Relay wrote: Hi Ackerley, Thanks again for the fix. Thank you also for adding the reproducers, they made testing this really easy for me! : -D > From: Ackerley Tng > > When mem_cgroup_charge_hugetlb(folio, gfp) returns -ENOMEM, the folio has > its refcount set to 1 via folio_ref_unfreeze(folio, 1). > > The error path calls free_huge_folio(folio) directly, which expects a > refcount of 0. Hence, VM_BUG_ON_FOLIO(folio_ref_count(folio), folio) is > triggered. So yeah, I built mm-new (with your hugetlb open code series) and ran reproducer #3, and got the following output: ... [ 23.855813] Call Trace: [ 23.855838] [ 23.855877] hugetlb_alloc_folio+0x190/0x360 [ 23.855930] alloc_hugetlb_folio+0xf9/0x310 [ 23.855972] hugetlb_no_page+0x590/0xa00 [ 23.856016] hugetlb_fault+0x194/0x770 [ 23.856060] handle_mm_fault+0x29b/0x2c0 [ 23.856103] do_user_addr_fault+0x208/0x6e0 [ 23.856146] exc_page_fault+0x67/0x140 [ 23.856191] asm_exc_page_fault+0x22/0x30 [ 23.856234] RIP: 0033:0x401734 ... Running it with your fix, I don't get a kernel crash. So this change looks good to me, please feel free to add: Tested-by: Joshua Hahn Reviewed-by: Joshua Hahn But..... It seems like fixing this bug actually surfaces a different bug. I don't see a kernel crash anymore, but my kernel gets stuck in a different loop: ... [ 42.526726] pagefault_out_of_memory: 120780 callbacks suppressed [ 42.526732] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF [ 42.526955] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF [ 42.527084] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF [ 42.527192] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF [ 42.527288] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF [ 42.527382] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF [ 42.527476] Huh VM_FAULT_OOM leaked out to the #PF handler. Retrying PF ... mem_cgroup_charge_hugetlb() fails --> hugetlb_alloc_folio() returns ERR_PTR(-ENOMEM) --> propagated to alloc_hugetlb_folio() --> hugetlb_no_page converts it to vmf_error(PTR_ERROR(folio)) --> propagates up to the pagefault handler.. But there's no OOM handler to resolve, I think. So it just keeps retrying the fault and cycling over and over and over again... I think we need to be returning ENOSPC instead of ENOMEM like the other failure paths in alloc_hugetlb_folio (or hugetlb_alloc_folio... I think we should change this name, it's a bit confusing). diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 288535838a48b..05214b1cd491a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2903,7 +2903,7 @@ struct folio *hugetlb_alloc_folio(struct hstate *h, * were committed to the folio and freeing the folio * would have cleared those up. */ - return ERR_PTR(ret); + return ERR_PTR(-ENOSPC); } return folio; This gives me the following result from your reproducer, which I think is the expected behavior: attempting to remount cgroup2 with memory_hugetlb_accounting... Successfully enabled memory_hugetlb_accounting Child: Attempting to allocate and touch 2MB hugepage... Child: mmap succeeded at 0x7f0023600000, touching it now (should trigger fault)... Parent: Child exited. Cleaning up. Parent: Child killed by signal 7 (Bus error) Parent: Child got SIGBUS as expected (if kernel didn't crash). What do you think? Happy to send it out as a separate fix or feel free to fold it into your series / fix, whatever you prefer. Thanks Ackerley! > Even with CONFIG_DEBUG_VM disabled, returning a folio with refcount 1 to > the freelist can corrupt allocator state later. > > Use folio_put(folio) instead of free_huge_folio(folio) to properly drop the > reference before freeing it. > Fixes: 991135774c0e0 ("memcg/hugetlb: introduce mem_cgroup_charge_hugetlb") > Cc: stable@vger.kernel.org > Signed-off-by: Ackerley Tng > Reviewed-by: Muchun Song > --- > mm/hugetlb.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 4093c1c0a4a1d..1f3f4b964b153 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -2990,7 +2990,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, > lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); > > if (ret == -ENOMEM) { > - free_huge_folio(folio); > + folio_put(folio); > return ERR_PTR(-ENOMEM); > } > > > -- > 2.55.0.795.g602f6c329a-goog