From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 BE1283FBED1 for ; Mon, 11 May 2026 14:20:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778509220; cv=none; b=uYClnYgDCEpozg2jpem8NibT1f7BsvRgmk5LcZD/5/rTVkTJnJOmGPgZFHBLk4d1HiDqnRVUbSt127arw4O9VfYbSZYM4tXzTMqFvNOL2IcytmgzIutRWbopnbEnJD81t5xmR39r9kBQkBazdn+qnqFwXwi8KfgfRDmeYwo1LJU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778509220; c=relaxed/simple; bh=HcevkjlxcwTsli3Kr4LstbtESWEHpSWmJVTxiT0CtcI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hevEyOQIan6tOipwWMsq44vG6HlR2f+ExJYFlmNDqWmTG9uoBT/eVETrnsaVm5wT8JRsihEelTZP+dxiKp4TBpvQ/GBkCoUeiuRiyaeARUGjGy9v67WhWAgMpMGU5KtnM8R94DCW5b5YqMMoa2O25k2hYe/ab54AYNfGEVH9rC8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FK+0gWnz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FK+0gWnz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8224C2BCB0; Mon, 11 May 2026 14:20:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778509220; bh=HcevkjlxcwTsli3Kr4LstbtESWEHpSWmJVTxiT0CtcI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=FK+0gWnz8Ifm4i9roL53t7zmULLZRcjD3IWaqWRck4C2wro+hibkQheqLlXMGz5ga 0021BfOqwud2fxHpOkfOFi7Xv+4EoZgICCvyCOzQA/tqL+9wGicUbk/6mBtEya+SDS 5vKoXtV/nKXMHD9tg5xrS1PbLzBOLXHuYzvUYAAAWpFGkJiBFGLlHPeOhHqHkbrHbT iA6owS/6T2js71LUFiAju0OBatTqRJwO+aTDdMTHmxdvAcZA8d0GzMp9BNpZs1TtoN zuiTeFNFFKCAHFFtj3seDy8LI0QN+qb/4E+hu4w9lxadKZU3zkwEI6/WG9RF+USEAn uoWBXWHblOBIg== Date: Mon, 11 May 2026 15:20:14 +0100 From: Lorenzo Stoakes To: Ye Liu Cc: Andrew Morton , David Hildenbrand , Xin Hao , 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: Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order Message-ID: References: <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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260511025408.54035-1-ye.liu@linux.dev> On Mon, May 11, 2026 at 10:54:07AM +0800, 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 LGTM, so: Reviewed-by: Lorenzo Stoakes > --- > 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 >