From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AC7536AB5B for ; Mon, 11 May 2026 02:54:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778468068; cv=none; b=t0/MDkw7fc05f7rrJk6HSMKnq4Bk4fzPo732rmkut+HnGn0dvyopiXatqlHbkY8uo2f+s9eo8xnzONIrbxffE4nRbXxCRyewAVx16yIDf3L2In8LnWhrcYtQhSMbyIBk+FGbBB/fv717+UaZQSGbjh3RtSj6mcinsAe2PAoY6jk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778468068; c=relaxed/simple; bh=KxDa7xFsw34MnhCKVM+nhJ4XMfdb59vM6cyGZizkFsw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=b0MDrRTeD5mgLw5sGEJkXO7UDrflAQH1C5Rwoaol420T3WIop6gpdAlL80pqM72kJ/yGDZZ/9EWa7HlT6f/nPczOV+aUdwQQenpjrxyOSj8sZHrn+0L9HBYTxmy7uEVFy/jtEJD1LNY9d2Ozr1Yf6ewUr6oLRMlbOSY+cxpMeAc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=OtrDo2C1; arc=none smtp.client-ip=91.218.175.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="OtrDo2C1" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1778468064; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=rwo92ed4cIHmGejveTVccOrdOc0h+7Q6Jv7ulCR5WYw=; b=OtrDo2C1hr1DFmyUCYslmNu0/aYQfTspdAMdgG4Ug2PC7PIUiwL4mHtgQX0aqWAqXxYhan mSVxg86fuYskf+fg9lVosAKDG2RTirSi2SSwOKKGcZ5L1QkPqiSOCVO1r9R5+4XkW0TfzW YleSf9VQqoxoql5twpmr5w7N07957m8= From: Ye Liu To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Xin Hao Cc: Ye Liu , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order Date: Mon, 11 May 2026 10:54:07 +0800 Message-ID: <20260511025408.54035-1-ye.liu@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: Ye Liu __khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped. Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures. Fixes: 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"") Suggested-by: David Hildenbrand Signed-off-by: Ye Liu --- Changes since v2: - Reorder to allocate mm_slot first, free it when flag already set, as suggested by David, Dev Jain and Lance Yang - Update the subject line to better match the patch. - Link: https://lore.kernel.org/all/20260506012130.9306-1-ye.liu@linux.dev/ Changes since v1: - Add Fixes tag as suggested by Dev Jain and Lance Yang - Link: https://lore.kernel.org/all/20260501075708.327217-1-ye.liu@linux.dev/ mm/khugepaged.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 5f4e009593e0..78735f34250a 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -437,13 +437,16 @@ void __khugepaged_enter(struct mm_struct *mm) /* __khugepaged_exit() must not run from under us */ VM_BUG_ON_MM(collapse_test_exit(mm), mm); - if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) - return; slot = mm_slot_alloc(mm_slot_cache); if (!slot) return; + if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) { + mm_slot_free(mm_slot_cache, slot); + return; + } + spin_lock(&khugepaged_mm_lock); mm_slot_insert(mm_slots_hash, mm, slot); /* -- 2.43.0