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 E8C90331A75; Wed, 3 Dec 2025 15:52:48 +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=1764777169; cv=none; b=L66/Wo8X0CEZuhNXenuQXLq2rfGKOW6GHn4cQ/nBzxv4d8BAb3DxFlhLsVejDlVqrYJHdV98yjx3Rvua7B9f+3lXuxg8eOQWqQk6DjkJEZb/yn/6ZC8+HCns4W+tRs/YaWcT9mDAuHpDhUWnXRdcVxfPETgIDCH+3r65BXOCACQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764777169; c=relaxed/simple; bh=Ju8E948+/9IMus4le2ww/Bvjy61OqHSK9/Ahh/Gty/g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e8mR8RphvIVWxbKN9Swvtzq5X8bPCbJAEXKWoMdcq09UPVcSWpkiip+lKY970zAFyuY7tn+CtqSMsdnnpUdaEiNd7pB9aovSvqsn6QQcZxDhQvmXMH8ng9i7rgLMAchP4JdE2sMhmsve5mE7a1/crj7Vgg56clb11DhNkeEA1iM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=UayClvui; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="UayClvui" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7514EC116B1; Wed, 3 Dec 2025 15:52:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1764777168; bh=Ju8E948+/9IMus4le2ww/Bvjy61OqHSK9/Ahh/Gty/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UayClvuilpowZISRnAUrxwDwmZ5Z6ffw6xeKZqq/dPC7o/yNxXVso/eWm+4cooZOt C5omgpIjwu8dit50PF0R6Yo2FtCxiLO+h4DHMRLF1URCDRSzUG/DCO6iF+I+Jf+YYS jRak5AUVFKNpJANRy1LodYVZuUqB/aP6SZMoLaXU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Wei Yang , Zi Yan , "David Hildenbrand (Red Hat)" , Baolin Wang , Andrew Morton Subject: [PATCH 6.17 102/146] mm/huge_memory: fix NULL pointer deference when splitting folio Date: Wed, 3 Dec 2025 16:28:00 +0100 Message-ID: <20251203152350.192985024@linuxfoundation.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251203152346.456176474@linuxfoundation.org> References: <20251203152346.456176474@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.17-stable review patch. If anyone has any objections, please let me know. ------------------ From: Wei Yang commit cff47b9e39a6abf03dde5f4f156f841b0c54bba0 upstream. Commit c010d47f107f ("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order via mapping->flags before proceeding with the split work. This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing mapping->flags in this state leads directly to a NULL pointer dereference. This commit fixes the issue by moving the check for mapping != NULL before any attempt to access mapping->flags. Link: https://lkml.kernel.org/r/20251119235302.24773-1-richard.weiyang@gmail.com Fixes: c010d47f107f ("mm: thp: split huge page to any lower order pages") Signed-off-by: Wei Yang Reviewed-by: Zi Yan Acked-by: David Hildenbrand (Red Hat) Reviewed-by: Baolin Wang Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/huge_memory.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3626,6 +3626,16 @@ static int __folio_split(struct folio *f if (folio != page_folio(split_at) || folio != page_folio(lock_at)) return -EINVAL; + /* + * Folios that just got truncated cannot get split. Signal to the + * caller that there was a race. + * + * TODO: this will also currently refuse shmem folios that are in the + * swapcache. + */ + if (!is_anon && !folio->mapping) + return -EBUSY; + if (new_order >= folio_order(folio)) return -EINVAL; @@ -3666,18 +3676,6 @@ static int __folio_split(struct folio *f gfp_t gfp; mapping = folio->mapping; - - /* Truncated ? */ - /* - * TODO: add support for large shmem folio in swap cache. - * When shmem is in swap cache, mapping is NULL and - * folio_test_swapcache() is true. - */ - if (!mapping) { - ret = -EBUSY; - goto out; - } - min_order = mapping_min_folio_order(folio->mapping); if (new_order < min_order) { ret = -EINVAL;