From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC72E26ADC for ; Mon, 11 May 2026 05:45:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778478352; cv=none; b=B3qN96i6ixIJ91ARr4ivjnxvEMUFjy/GHSPq/nqGeyVE7+jX6gAD3RdlaBoV7U6Cbr56DWmOh8DrDzt8bV7Z1TKWsMfDqNJIQN9ajBfAmn8ClOi+hSEsNEwy6N12M3wOmJxTfvFSQlNEXoKtE6dZy5p9WpdZPNg72K6XhOIx/4s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778478352; c=relaxed/simple; bh=9EDhLSvWK/Jmn9/upRXosyoFua5uh6p4DHHLuigAScA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=mqu69Zfsl0nl7Faan+oxPmMjxnq5YtvgjYJNHLuvtkqvmTSH8uYX9T30i9IZ/aienDZD5DFwloHAb590bT8GpozJSCyMNWNf6hiGwcwqafYhC5LtAQk6/W4jZo/FBQ+rEt3uXE0/YeOszWz/pgRHpPjahhmLdEkaJhaTECDREc4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=TkERXVWi; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="TkERXVWi" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1E3131713; Sun, 10 May 2026 22:45:45 -0700 (PDT) Received: from [10.164.148.37] (MacBook-Pro.blr.arm.com [10.164.148.37]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 41F013F85F; Sun, 10 May 2026 22:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1778478350; bh=9EDhLSvWK/Jmn9/upRXosyoFua5uh6p4DHHLuigAScA=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=TkERXVWih2qdiEMBQxeV/tjpHsjfhPhlROD6OXYcvBB/l716tnqC3/KGEjTD1mto0 sIn/vBHwrocOYsnql/3RYxXQK+yOSbLja2CoCMKF/5gdctjf58Q9iSvhASiQV1SeMv glT2uv+w9g5wf/qrByl1Zxks5xFy+z5gwFz31SBs= Message-ID: Date: Mon, 11 May 2026 11:15:41 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order To: Ye Liu , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Xin Hao Cc: Ye Liu , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Barry Song , Lance Yang , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20260511025408.54035-1-ye.liu@linux.dev> Content-Language: en-US From: Dev Jain In-Reply-To: <20260511025408.54035-1-ye.liu@linux.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/05/26 8:24 am, 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, 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 > --- Reviewed-by: Dev Jain > 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); > /*