From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 CDF0B3AF656 for ; Tue, 28 Apr 2026 11:30:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777375847; cv=none; b=a3gqH0hBrx00f7esDYTHH11L6ABDI+QIyeJYCr+tmXqcs7IiZJYK+CHT+Ck+GsOLEAsUFZUL+GQq+j5EShg1wl7UmF2uExA+HJlyGXitetbwningATzNX/04V6ImpmbY5JlSXJSoj2dT+LSVIUPvMQ2PrBI9AVIk85URDlYjTHE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777375847; c=relaxed/simple; bh=x6Uv0MthFJaHnLCh7NMFD66PD52OOBjgYxHvakLPsCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EuPEQ5a75S/acWJZDJpxQiOX6AlT7Rd0ih9eHiwsHX9jljMEggLyQHBGmHimZrgnXzf6O+rSvv4NRg/vWstniAtdB0HQaajMdFg/ry/JoJFVDQRoTntnhsjr7hOPMkwRjnvk9AjVp9XDQaJcTglaQNxz8YY4Hrbl/FOVxWDGbKA= 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=gwOKDpiq; arc=none smtp.client-ip=209.85.210.178 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="gwOKDpiq" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-82f431c0ab6so5209983b3a.0 for ; Tue, 28 Apr 2026 04:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777375845; x=1777980645; 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; bh=g1Ct8Le8BV/CkL7dz/X4QUOvjolhlSF4pzodVhQ1Ejs=; b=gwOKDpiqnfhnEBAqJ5EOR6rE1Yj7oKrUgCM41ITyn02vpvgjkLxtNd7/mlaovvOoTn xnEX9j5qJqQ+yWO+G662xyDnb/YXX855eC6Sc7OAlKtqnfZxKpwPmP73a7ttYqcpzNg+ hWJE5jBoTBxQts8jVi0uh6CblFHVGUOtcB75BC568NWef2wyV69fKRwwNF818HvvCtxv T9bec4fYgFAyQ5ueubuMpVX22F6m62iJyztPG/sfycUuL5tr8AtAehIoxOXkd591tC3l U29VAtr1h3IMCYM7WjbuxXRn/JlixWZAfCrrNFgqgWR1wqJaiRfuO4jdou/F/3EcYiTK mcIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777375845; x=1777980645; 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; bh=g1Ct8Le8BV/CkL7dz/X4QUOvjolhlSF4pzodVhQ1Ejs=; b=Oh1unkaAOr/5Y1d10aXQmop/crkRJMaUXT4A15hTfnqQ+QgHRQvv1XBeT05XHCnLO1 uNTCkswW2dU2iWDMnFCj9VPlMbgpiCAFXGrrWCedMuodd17T5bdjjE/fsgWBkg7XfcEG +WBi+M3l9UW3Hoe5VpnGIsIuMGK8Gh3dA93+ZxPPEJB98HSEcilCiQqohFbcYWX6HHGI VvbmsSsPZAcVeBQhoJgEN/no3m2fxuP7SXn7CT918npzIzjZ/O4Hvx3+rNrheEAo30kC 4eNUDaHXc/Jvb1t+XIRJJDnMAwmXjWFepO8fD3yHrMpI7VpIHABwAQjZ6LeogPLQlA+G VNiA== X-Forwarded-Encrypted: i=1; AFNElJ/yHEL2+lwbEWed7VvPcu27npbIwT4Wu2OUmnYdI88v92UHzYh+2Sr9hIH1EefMmpbHqe+w3B/tFxSDsq8=@vger.kernel.org X-Gm-Message-State: AOJu0YxL01E2afWLMe4mhnaoLULCxLm5H/hZpKiWUDViHvEpL+Hk4S0r vIoTJ+zOdvaVYfPN8ex0B3ujEB5DY0OVgCHbl9fxQNSvwzVAnSq/XLwn X-Gm-Gg: AeBDiet+QrZX+IWhHTne+kZ85K1aa4lSgdWsPq84f7IABGasVLZjIT+DfO4G5NwiT68 q5lOB6rneIClSfkFM6VW7g9P8ZilYugxwGvn/Fqj5KQdK1Izl2mS8JRzit0YIPIVA7ICr0ZtEiF 7SUXEMvrkLAxIV4W4dlXX8q3CUhDqStR2n9A8cL2Y/qIBD1jWw1hKYYs/KaTL/oLm5mdaUKcWu6 lSiRHq0L1wWkbrpEEb1vKq0HtE8D7PmZ19PcsXItsyKwyLSI6w+7zPaC+tJsV0gvhDC+QmciLO2 v1nhQLUvE4TOgg/UO+CX4SbWZ5OQU5pIOrjS74qIWHDquRTg76n47gg7TRTboEf+mu6fgnQKXHT GdAz6vTfWAc33e/lC4WESUduoLA2PIOZ4iuikRo0H+WEApbCYHPVwWasCbZ98xtMuxpnGGEU2n0 BhGHxmnWeTQBMdznJnY+nOU90dVViYk8PL1PB+5SxSCIbAFQgu/O4qklZystb9Z/u1+SrmD70Ue 41L X-Received: by 2002:a05:6a00:181b:b0:82f:4191:da10 with SMTP id d2e1a72fcca58-834ddae0572mr2816395b3a.6.1777375845004; Tue, 28 Apr 2026 04:30:45 -0700 (PDT) Received: from KRHW1CJW23.bytedance.net ([203.208.189.8]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-834daf6b111sm2455545b3a.52.2026.04.28.04.30.41 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 28 Apr 2026 04:30:44 -0700 (PDT) From: Zhao Li To: Andrew Morton Cc: mawupeng1@huawei.com, Zhao Li , Muchun Song , Oscar Salvador , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v3] mm/hugetlb: fix max-only subpool accounting on alloc_hugetlb_folio failure Date: Tue, 28 Apr 2026 19:30:38 +0800 Message-ID: <20260428113037.88766-2-enderaoelyther@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260428030712.66256-2-enderaoelyther@gmail.com> References: <20260427145247.84157-2-enderaoelyther@gmail.com> <20260428030712.66256-2-enderaoelyther@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit alloc_hugetlb_folio() calls hugepage_subpool_get_pages() when map_chg is set. For a subpool with max_hpages != -1, that bumps used_hpages regardless of whether it returns gbl_chg = 0 (rsv slot consumed) or gbl_chg > 0 (used_hpages slot only). If the allocation later fails before a folio is returned, the unwind must undo the used_hpages bump. The old cleanup only ran for !gbl_chg, leaking used_hpages on the gbl_chg > 0 path. For gbl_chg > 0 on max-only subpools (max_hpages != -1, min_hpages == -1), hugepage_subpool_get_pages() took only a speculative used_hpages slot. Drop that slot directly under spool->lock. In that configuration hugepage_subpool_put_pages() cannot restore rsv_hpages, so the direct decrement is the exact inverse and is race-free against concurrent puts. This matches the used_hpages-only part of hugetlb_reserve_pages()'s out_put_pages cleanup, but restricts it to the max-only case where no rsv_hpages restoration is possible. Mounts with min_hpages != -1 are left unchanged for now. v2's approach (hugepage_subpool_put_pages() + h->resv_huge_pages++ to back a restored rsv_hpages slot) double-counts global backing under concurrent free_huge_folio() and creates phantom reservations under concurrent hugetlb_unreserve_pages(). Safe cleanup of that quadrant needs a coordinated fix across multiple call sites. Reproduced on size=20M hugetlbfs with the faulting task in a hugetlb cgroup whose limit is exceeded. Vanilla leaks 6/8 hugepages of subpool quota; this patch leaks 0/8. Verified under QEMU. Fixes: a833a693a490 ("mm: hugetlb: fix incorrect fallback for subpool") Cc: stable@vger.kernel.org # v6.15+ Signed-off-by: Zhao Li --- Changes in v3: - Replace v2's hugepage_subpool_put_pages() + h->resv_huge_pages++ on the gbl_chg > 0 branch with a direct used_hpages-- under spool->lock. - Restrict the cleanup to (max_hpages != -1, min_hpages == -1) where the direct decrement is the exact inverse of the speculative bump. Changes in v2: - Skip the gbl_chg > 0 cleanup when max_hpages is unset. - Add hugepage_subpool_put_pages() + h->resv_huge_pages++ on the gbl_chg > 0 branch. mm/hugetlb.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f24bf49be047e..cfdeaf6394c5b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3025,13 +3025,24 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h), h_cg); out_subpool_put: - /* - * put page to subpool iff the quota of subpool's rsv_hpages is used - * during hugepage_subpool_get_pages. - */ - if (map_chg && !gbl_chg) { - gbl_reserve = hugepage_subpool_put_pages(spool, 1); - hugetlb_acct_memory(h, -gbl_reserve); + if (map_chg) { + if (!gbl_chg) { + /* Full inverse when subpool_get_pages() consumed rsv_hpages. */ + gbl_reserve = hugepage_subpool_put_pages(spool, 1); + hugetlb_acct_memory(h, -gbl_reserve); + } else if (gbl_chg > 0 && spool && spool->min_hpages == -1 && + spool->max_hpages != -1) { + unsigned long flags; + + /* + * For max-only subpools, subpool_get_pages() took only a + * speculative used_hpages slot. Drop that slot directly. + */ + spin_lock_irqsave(&spool->lock, flags); + if (spool->used_hpages > 0) + spool->used_hpages--; + unlock_or_release_subpool(spool, flags); + } } -- 2.50.1 (Apple Git-155)