From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) (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 7327B500940 for ; Thu, 22 Jan 2026 02:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.186 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769048231; cv=none; b=MDU5JYxKwdcXoEzA4aMJCbMPCS8G/5dKTLmA3r+rLT1nyu8LtQUROtbatva8/3Eti5CWswRZ3zST71Yx0IEW+EXf+ghpjFRGxzV3/CQf5Y8jLAAoe7EeI+jl0l0t6WL/f2Ph16jI2q2RhSFbjS4oaSdTeIjkIO+YIk89cOeGzwE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769048231; c=relaxed/simple; bh=vbD2ZWwQMsJ06TsrWRUrQk4b8QvIrVOS2i159M77Rhc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=oU5zuTzzZQh5G/mJil2qIGTvFVxM4zd+4M9pqcUDPYI56tgbWLTb/dWCDr6ql5zVZF6RmzEG5obwy8fgIWQUHQEQvrJZFTlrkF9WIC8bnWEVJP5+d8UbVidD9Fbf/WOzMIHvpm1UQ6CI98J9RQshFNZVcoEGeBM1HX7NAwfdY10= 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=vxVaWm4o; arc=none smtp.client-ip=95.215.58.186 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="vxVaWm4o" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769048224; 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=f+C6AtOWHzYIGqfm/rwPb/eSNwaxZ0ijqjDnRreQERY=; b=vxVaWm4oPNUgRXy4kjIxipzVZgD6lz2Zb7uQ7ZKeIROuiWh9Vu1mqypGiE2r/sJnSwmpak T/4pNtt2hjKXREXc37HmPzmnd4B++kFCf/zyct0CHyOVMlgk4sdB/q3BsgTf4E1pcIGgGg 4vZCql5IkHvgdufTZmrQGhCF/hxpUs0= Date: Thu, 22 Jan 2026 10:16:36 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH] mm/huge_memory: Fix iterator variable usage after swap() To: "David Hildenbrand (Red Hat)" , zenghongling , Qi Zheng , Muchun Song Cc: linux-mm@kvack.org, dev.jain@arm.com, akpm@linux-foundation.org, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, npache@redhat.com, linux-kernel@vger.kernel.org, baohua@kernel.org, Liam.Howlett@oracle.com, zhongling0719@126.com, ziy@nvidia.com, lorenzo.stoakes@oracle.com References: <20260121081343.713715-1-zenghongling@kylinos.cn> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On 2026/1/21 20:28, David Hildenbrand (Red Hat) wrote: > On 1/21/26 10:25, Lance Yang wrote: >> >> >> On 2026/1/21 16:13, zenghongling wrote: >>> The iterator variable 'folio' is swapped with 'prev' in the else >>> branch. Using 'folio' after swap() checks the potentially NULL >>> 'prev' value, not the original iterator value. >>> >>> Fix by moving folio_put() call before the swap operation in the >>> path where swap() occurs. >>> >>> Found by: >>> ./huge_memory.c:4225:6-11: ERROR: iterator variable bound on line >>> 4178 cannot be NULL >> > > Which tool did find that? A compiler? Looks like a false positive from coccinelle? The pattern is tricky to follow statically :) > >> Good catch! >> >> But which tree is your patch based on? >> >> Seems like that was already fixed in commit 776bde7caf80[1]. The >> whole thing deferred_split_scan() was refactored using folio_batch, >> so the buggy code with swap(folio, prev) is gone ... >> >> Ccing Muchun and Qi who fixed that. >> >> [1] >> https://lore.kernel.org/ >> all/59cb6b6fb5ffcff9d23b81890b252960139ad8e7.1762762324.git.zhengqi.arch@bytedance.com/ > > Right, in > > commit 776bde7caf80f6af72b087cafe7d9f607b14716d > Author: Muchun Song > Date:   Mon Nov 10 16:17:57 2025 +0800 > >     mm: thp: use folio_batch to handle THP splitting in > deferred_split_scan() > > > Which raises the question whether we would want to backport that patch > to stable kernels > if there was indeed a problem? > > > But: I don't immediately see the problem. > > If pref is NULL (and folio obviously !+NULL), we'll end up with >     * pref != NULL >     * folio == NULL > >     The "if (folio)" check will do nothing, because we defer the > freeing to the > >         if (prev) >             folio_put(prev); > >     later > > If pref is != NULL (and folio obviously !+NULL), we'll end up with >     * pref = NULL >     * folio = NULL > >     The if (folio) and if (prev) handling will care of it all. > > > So ... this pretty much looks like working as expected? Right! After reading through the old code, the swap() design is correct and working as intended, IIUC.