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 D90B340DFC5 for ; Fri, 1 May 2026 13:24:51 +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=1777641894; cv=none; b=guO1ktTaXfBxtnYg3t3lNxUzjZZAuGucMcA3sMnRMM/zJcGMVcytJzc3G9ev8CBo7gT45UXazs6ApAaIGXzZdh83brVykLfLyFXZs2761hOczACy2iEfyt4C4JMtMp+WZtUq4GaTEsMLAwLknRDjPN2zJRndnZQHWVE5e3DCOVU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777641894; c=relaxed/simple; bh=qxrMhdbkVfuu4bzTbgNeOTcKnlxXOpLPPjpNofdqV/I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=MTobC+2auY813QBXfLDY9TMc8WOMkFxrcHT6iT0Z/cV4kRc23QAL3Z9rvrjqvOgtJtoaUWqUODF1GWJX14b75MCDgDJXd3GEWw45xLpG0rmLprjetiFDhz4KaN20DkcNYhVwPaDJ3P8zgtpgLrWGu5nOG68g4yHLsDBaDtNNiNo= 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=QUs6hox7; 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="QUs6hox7" 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=1777641889; 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=xRO/682cR26d8aQNXMdR1KpAtOqpz21uS7mp51ZeARY=; b=QUs6hox7I6BFiUrgYrk85Xowcz9YDKt1u/CsA0LqcvGJ6VVBQ3/vyL31ZLPYsiot0Xclix WFxv8I/g158r5+CvFy3oDS1cLHwRFq5sctbgmu6K16EBtUco2fQ4s8A3VGcsAgMLx4uLcX g+UIv2rz2l7mmuT8fr7jkQVc5ghv8lg= From: Lance Yang To: dev.jain@arm.com, ye.liu@linux.dev Cc: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, liuye@kylinos.cn, ziy@nvidia.com, baolin.wang@linux.alibaba.com, liam@infradead.org, npache@redhat.com, ryan.roberts@arm.com, baohua@kernel.org, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/khugepaged: clear MMF_VM_HUGEPAGE on mm_slot_alloc() failure Date: Fri, 1 May 2026 21:24:38 +0800 Message-Id: <20260501132438.21298-1-lance.yang@linux.dev> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On Fri, May 01, 2026 at 04:10:58PM +0530, Dev Jain wrote: > > >On 01/05/26 1:27 pm, Ye Liu wrote: >> 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. >> >> This leaves the mm in an inconsistent state: it is marked as >> registered (MMF_VM_HUGEPAGE set), but will never be scanned by >> khugepaged. Future attempts to register the mm are skipped since >> khugepaged_enter_vma() checks the flag and returns early. >> >> Fix this by clearing MMF_VM_HUGEPAGE when mm_slot_alloc() fails, >> restoring the ability to retry registration later. >> >> Signed-off-by: Ye Liu >> --- >> mm/khugepaged.c | 4 +++- >> 1 file changed, 3 insertions(+), 1 deletion(-) >> >> diff --git a/mm/khugepaged.c b/mm/khugepaged.c >> index 7d48d4fbd5f3..60ab7c1b61dd 100644 >> --- a/mm/khugepaged.c >> +++ b/mm/khugepaged.c >> @@ -559,8 +559,10 @@ void __khugepaged_enter(struct mm_struct *mm) >> return; >> >> slot = mm_slot_alloc(mm_slot_cache); >> - if (!slot) >> + if (!slot) { >> + mm_flags_clear(MMF_VM_HUGEPAGE, mm); >> return; >> + } > >You could just move the test_and_set() after this no? Yep, that sounds better :) Just one small thing: if we move the test_and_set(), after mm_slot_alloc(), we need to free the slot when test_and_set() says that the flag was already set. Otherwise the racing caller that loses would leak the slot :) Something like: 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; } >So if slot allocation fails then MMF_VM_HUGEPAGE is never set. > >Fixes tag is probably required but not sure about Ccing stable. Yes, Fixes sounds good. khugepaged collapse is best-effort though, anyway :) So I'm also not sure about Ccing stable. >> >> spin_lock(&khugepaged_mm_lock); >> mm_slot_insert(mm_slots_hash, mm, slot); Cheers, Lance