From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C38CC54E65 for ; Thu, 22 May 2025 19:33:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 989F66B007B; Thu, 22 May 2025 15:33:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 93A8F6B0083; Thu, 22 May 2025 15:33:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82B2B6B0085; Thu, 22 May 2025 15:33:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 645006B007B for ; Thu, 22 May 2025 15:33:03 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 03F481A054C for ; Thu, 22 May 2025 19:33:02 +0000 (UTC) X-FDA: 83471541846.05.2D31804 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf02.hostedemail.com (Postfix) with ESMTP id BF3818000F for ; Thu, 22 May 2025 19:33:00 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Axt7BNJR; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=FJaHp1pQ; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Axt7BNJR; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=FJaHp1pQ; spf=pass (imf02.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747942381; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XoGriKZH/KeIHMAtr2DMyXbRh64tZ6X3COoz6omsFrg=; b=hFgGIpnBipBPoBg0hupbi1i9eqC+MQVdFSCSayq6joTdwJ27jqvFsTY8yLq8s/AfS2cJDG hjlXHTzdLCmBOcxymN9juQYT7W5oW489FV3T/+f07RXn/nd5fzX/0kTsDtWcOCqM3hClMI VVwmbe0pQT0kts83ptQZ1K8+LDqItDI= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Axt7BNJR; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=FJaHp1pQ; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Axt7BNJR; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=FJaHp1pQ; spf=pass (imf02.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747942381; a=rsa-sha256; cv=none; b=Mxstz4G+09JXjwyQ9RDoGL5Bdd28LHNsu7mFbiV7ffgZ5tprBoS6H/HJYuSjAQcJbqkYQr RnRK7uxVC88OQEcxzjgddwaJe54rzZwpplGXvM7fpdvhKv7mSRxDpJbmr9zpMN0nCAUMNj MNQuz6gYHu4tqfxDHv34z/AO74qkALY= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 21CA921757; Thu, 22 May 2025 19:32:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1747942379; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XoGriKZH/KeIHMAtr2DMyXbRh64tZ6X3COoz6omsFrg=; b=Axt7BNJRz/6WYLTpf/+9XmUnNIYnbRDuHiuXK/p7w1iYNvLXKhs9Kpb9BK3HCluXopi+55 eRHGQyOWxUvi7iRxzqVjnJvqEOKJC+vvfCsoeMCAvDbQ8K7TX40Eca2JNrrIsnuNJg8rLI 4zgJOAcHKMXs8GIj5R+0Ku2HGstCPIU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1747942379; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XoGriKZH/KeIHMAtr2DMyXbRh64tZ6X3COoz6omsFrg=; b=FJaHp1pQ9ZFGJhQVY9TOIdfDRhcpaoLrDppHgkz8CqruLQD3rNM6OWWJGUeueYmtZx4Gg+ 7xIuflG7Rx4UKdAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1747942379; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XoGriKZH/KeIHMAtr2DMyXbRh64tZ6X3COoz6omsFrg=; b=Axt7BNJRz/6WYLTpf/+9XmUnNIYnbRDuHiuXK/p7w1iYNvLXKhs9Kpb9BK3HCluXopi+55 eRHGQyOWxUvi7iRxzqVjnJvqEOKJC+vvfCsoeMCAvDbQ8K7TX40Eca2JNrrIsnuNJg8rLI 4zgJOAcHKMXs8GIj5R+0Ku2HGstCPIU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1747942379; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XoGriKZH/KeIHMAtr2DMyXbRh64tZ6X3COoz6omsFrg=; b=FJaHp1pQ9ZFGJhQVY9TOIdfDRhcpaoLrDppHgkz8CqruLQD3rNM6OWWJGUeueYmtZx4Gg+ 7xIuflG7Rx4UKdAg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A0EA313433; Thu, 22 May 2025 19:32:56 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id qg3OGuh7L2j6awAAD6G6ig (envelope-from ); Thu, 22 May 2025 19:32:56 +0000 Date: Thu, 22 May 2025 21:32:48 +0200 From: Oscar Salvador To: Muchun Song Cc: Ge Yang , akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, 21cnbao@gmail.com, david@redhat.com, baolin.wang@linux.alibaba.com, liuzixing@hygon.cn Subject: Re: [PATCH] mm/hugetlb: fix kernel NULL pointer dereference when replacing free hugetlb folios Message-ID: References: <1747884137-26685-1-git-send-email-yangge1116@126.com> <644FF836-9DC7-42B4-BACE-C433E637B885@linux.dev> <3B8641A1-5345-44A5-B610-9BCBC980493D@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3B8641A1-5345-44A5-B610-9BCBC980493D@linux.dev> X-Rspamd-Action: no action X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: BF3818000F X-Stat-Signature: b1poaneoir1pqjgcxqrue6r3dor3b4ys X-Rspam-User: X-HE-Tag: 1747942380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OROS4GmmiLmVoQ9ucTwYb+njOKNR34eiYIWTmyaGd4kJ5RaJLZG5RypuHQFtiN+BX0T+oFn00dK96/wvD6L69c024IIcZNtBbRkCAR/vTwTbTB49NIqh7biHRK5JvnxqSZirmHr9+4sJ5f7I4sIU9VgQX42d3ruir3Qttny6aqvwhaG5gWblSxYhQ59SIltvgLHYzmGo1RE4vUs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, May 22, 2025 at 08:39:39PM +0800, Muchun Song wrote: > But I think we could use "folio_order() > MAX_PAGE_ORDER" to replace the check > of hstate_is_gigantic(), right? Then ee could remove the first parameter of hstate > from alloc_and_dissolve_hugetlb_folio() and obtain hstate in it. Yes, I think we can do that. So something like the following (compily-tested only) maybe? From d7199339e905f83b54d22849e8f21f631916ce94 Mon Sep 17 00:00:00 2001 From: Oscar Salvador Date: Thu, 22 May 2025 19:51:04 +0200 Subject: [PATCH] TMP --- mm/hugetlb.c | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index bd8971388236..20f08de9e37d 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2787,15 +2787,13 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, /* * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve * the old one - * @h: struct hstate old page belongs to * @old_folio: Old folio to dissolve * @list: List to isolate the page in case we need to * Returns 0 on success, otherwise negated error. */ -static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, - struct folio *old_folio, struct list_head *list) +static int alloc_and_dissolve_hugetlb_folio(struct folio *old_folio, struct list_head *list) { - gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; + struct hstate *h; int nid = folio_nid(old_folio); struct folio *new_folio = NULL; int ret = 0; @@ -2829,7 +2827,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, cond_resched(); goto retry; } else { + h = folio_hstate(old_folio); + if (!new_folio) { + gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; + spin_unlock_irq(&hugetlb_lock); new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); @@ -2874,35 +2876,20 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) { - struct hstate *h; int ret = -EBUSY; - /* - * The page might have been dissolved from under our feet, so make sure - * to carefully check the state under the lock. - * Return success when racing as if we dissolved the page ourselves. - */ - spin_lock_irq(&hugetlb_lock); - if (folio_test_hugetlb(folio)) { - h = folio_hstate(folio); - } else { - spin_unlock_irq(&hugetlb_lock); - return 0; - } - spin_unlock_irq(&hugetlb_lock); - /* * Fence off gigantic pages as there is a cyclic dependency between * alloc_contig_range and them. Return -ENOMEM as this has the effect * of bailing out right away without further retrying. */ - if (hstate_is_gigantic(h)) + if (folio_order(folio) > MAX_PAGE_ORDER) return -ENOMEM; if (folio_ref_count(folio) && folio_isolate_hugetlb(folio, list)) ret = 0; else if (!folio_ref_count(folio)) - ret = alloc_and_dissolve_hugetlb_folio(h, folio, list); + ret = alloc_and_dissolve_hugetlb_folio(folio, list); return ret; } @@ -2916,7 +2903,6 @@ int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) */ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) { - struct hstate *h; struct folio *folio; int ret = 0; @@ -2924,15 +2910,9 @@ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) while (start_pfn < end_pfn) { folio = pfn_folio(start_pfn); - if (folio_test_hugetlb(folio)) { - h = folio_hstate(folio); - } else { - start_pfn++; - continue; - } if (!folio_ref_count(folio)) { - ret = alloc_and_dissolve_hugetlb_folio(h, folio, + ret = alloc_and_dissolve_hugetlb_folio(folio, &isolate_list); if (ret) break; -- 2.49.0 -- Oscar Salvador SUSE Labs