From nobody Fri Oct 10 20:53:44 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31243248166 for ; Thu, 12 Jun 2025 13:47:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736041; cv=none; b=pSfMy5NBQuWOeE4+pp5c5fv+1/1JJAnjex2OEAEtbLxFhm4fcv6V+Fx6KWwvUyfAREwjzvxDswQj8+tTa5iIM6RRS+PTWzHP6LT+uurGIaJjHgYIi3MGiXKhDfg+CBLGKdyjhzZNDOWhN1SGuMTOxV5OHCtyLkptEDLjpLNFPLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736041; c=relaxed/simple; bh=pGRn1TP2eU18JEUiY+hmUCTpQTOjWGDuEKiPNWef71Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jl0Xr/iyC3SHqTlHdTNB+TwXwap8N1YW4X1o5HDqsrAw2uTx/rjeQaDvjyoNaMvJLpCNI1aQJmkrjyJOTBVnmFz3S+g0+DXc/7bE2dhMUHDI+FDAO3s1twvmBNXfnfIexpJ3i62a5h+I9GmG+9HlMWOA5aDEguj4pyPBqKl/vyc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=kC0uQpKM; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=9g0fOro2; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=kC0uQpKM; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=9g0fOro2; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="kC0uQpKM"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="9g0fOro2"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="kC0uQpKM"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="9g0fOro2" 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-out2.suse.de (Postfix) with ESMTPS id 41D511F7EC; Thu, 12 Jun 2025 13:47:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ffla1ABZDA8XX25IjpFUNbW65FCfMdMde7v3HHIzjDA=; b=kC0uQpKMEVQBhikBxZJDNMBrdoDYTzP7MLQyDDZkS3UqVYs6/DJb5snZREdZPq4r4gmDuy eOr6uk3K10OYBkcNc9YSXFeivaEJl/ndgGm81bfHRGYFhZaR9BnG7kpUrr7bm67QYsX9lr kIweGsxnovHzNGvQLskJHfl6UwXmsFA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ffla1ABZDA8XX25IjpFUNbW65FCfMdMde7v3HHIzjDA=; b=9g0fOro2POLxYr07HxyAsbPzR6Y+4gtGRd8iB5VdgY2NmplY+EPLuKzC17fxM2qu3WBBhn D8ACwkiDrOfdwyCg== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=kC0uQpKM; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=9g0fOro2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ffla1ABZDA8XX25IjpFUNbW65FCfMdMde7v3HHIzjDA=; b=kC0uQpKMEVQBhikBxZJDNMBrdoDYTzP7MLQyDDZkS3UqVYs6/DJb5snZREdZPq4r4gmDuy eOr6uk3K10OYBkcNc9YSXFeivaEJl/ndgGm81bfHRGYFhZaR9BnG7kpUrr7bm67QYsX9lr kIweGsxnovHzNGvQLskJHfl6UwXmsFA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ffla1ABZDA8XX25IjpFUNbW65FCfMdMde7v3HHIzjDA=; b=9g0fOro2POLxYr07HxyAsbPzR6Y+4gtGRd8iB5VdgY2NmplY+EPLuKzC17fxM2qu3WBBhn D8ACwkiDrOfdwyCg== 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 B773413A6D; Thu, 12 Jun 2025 13:47:16 +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 4K8nKmTaSmhJEAAAD6G6ig (envelope-from ); Thu, 12 Jun 2025 13:47:16 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , James Houghton , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 1/5] mm,hugetlb: Change mechanism to detect a COW on private mapping Date: Thu, 12 Jun 2025 15:46:57 +0200 Message-ID: <20250612134701.377855-2-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612134701.377855-1-osalvador@suse.de> References: <20250612134701.377855-1-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: 41D511F7EC X-Rspamd-Action: no action X-Spam-Flag: NO X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_COUNT_TWO(0.00)[2]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[9]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,suse.de:mid,suse.de:dkim,suse.de:email]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.de:+] X-Spam-Score: -3.01 X-Spam-Level: Content-Type: text/plain; charset="utf-8" hugetlb_wp() checks whether the process is trying to COW on a private mappi= ng in order to know whether the reservation for that address was already consu= med or not. If it was consumed and we are the ownner of the mapping, the folio will hav= e to be unmapped from the other processes. Currently, that check is done by looking up the folio in the pagecache and compare it to the folio which is mapped in our pagetables. If it differs, it means we already mapped it privately before, consuming a reservation on the way. All we are interested in is whether the mapped folio is anonymous, so we can simplify and check for that instead. Also, we transition from a trylock to a folio_lock, since the former was on= ly needed when hugetlb_fault() had to lock both folios, in order to avoid dead= lock. Closes: https://lore.kernel.org/lkml/20250513093448.592150-1-gavinguo@igali= a.com/ Signed-off-by: Oscar Salvador Suggested-by: Peter Xu Acked-by: David Hildenbrand --- mm/hugetlb.c | 70 +++++++++++++--------------------------------------- 1 file changed, 17 insertions(+), 53 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8746ed2fec13..175edafeec67 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6152,8 +6152,7 @@ static void unmap_ref_private(struct mm_struct *mm, s= truct vm_area_struct *vma, * cannot race with other handlers or page migration. * Keep the pte_same checks anyway to make transition from the mutex easie= r. */ -static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, - struct vm_fault *vmf) +static vm_fault_t hugetlb_wp(struct vm_fault *vmf) { struct vm_area_struct *vma =3D vmf->vma; struct mm_struct *mm =3D vma->vm_mm; @@ -6215,16 +6214,17 @@ static vm_fault_t hugetlb_wp(struct folio *pagecach= e_folio, PageAnonExclusive(&old_folio->page), &old_folio->page); =20 /* - * If the process that created a MAP_PRIVATE mapping is about to - * perform a COW due to a shared page count, attempt to satisfy - * the allocation without using the existing reserves. The pagecache - * page is used to determine if the reserve at this address was - * consumed or not. If reserves were used, a partial faulted mapping - * at the time of fork() could consume its reserves on COW instead - * of the full address range. + * If the process that created a MAP_PRIVATE mapping is about to perform + * a COW due to a shared page count, attempt to satisfy the allocation + * without using the existing reserves. + * In order to determine where this is a COW on a MAP_PRIVATE mapping it + * is enough to check whether the old_folio is anonymous. This means that + * the reserve for this address was consumed. If reserves were used, a + * partial faulted mapping at the fime of fork() could consume its reserv= es + * on COW instead of the full address range. */ if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && - old_folio !=3D pagecache_folio) + folio_test_anon(old_folio)) cow_from_owner =3D true; =20 folio_get(old_folio); @@ -6603,7 +6603,7 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, hugetlb_count_add(pages_per_huge_page(h), mm); if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { /* Optimization, do the COW without a second fault */ - ret =3D hugetlb_wp(folio, vmf); + ret =3D hugetlb_wp(vmf); } =20 spin_unlock(vmf->ptl); @@ -6670,11 +6670,9 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, { vm_fault_t ret; u32 hash; - struct folio *folio =3D NULL; - struct folio *pagecache_folio =3D NULL; + struct folio *folio; struct hstate *h =3D hstate_vma(vma); struct address_space *mapping; - int need_wait_lock =3D 0; struct vm_fault vmf =3D { .vma =3D vma, .address =3D address & huge_page_mask(h), @@ -6769,8 +6767,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct= vm_area_struct *vma, * If we are going to COW/unshare the mapping later, we examine the * pending reservations for this page now. This will ensure that any * allocations necessary to record that reservation occur outside the - * spinlock. Also lookup the pagecache page now as it is used to - * determine if a reservation has been consumed. + * spinlock. */ if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(vmf.orig_pte)) { @@ -6780,11 +6777,6 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, } /* Just decrements count, does not deallocate */ vma_end_reservation(h, vma, vmf.address); - - pagecache_folio =3D filemap_lock_hugetlb_folio(h, mapping, - vmf.pgoff); - if (IS_ERR(pagecache_folio)) - pagecache_folio =3D NULL; } =20 vmf.ptl =3D huge_pte_lock(h, mm, vmf.pte); @@ -6798,10 +6790,6 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, (flags & FAULT_FLAG_WRITE) && !huge_pte_write(vmf.orig_pte)) { if (!userfaultfd_wp_async(vma)) { spin_unlock(vmf.ptl); - if (pagecache_folio) { - folio_unlock(pagecache_folio); - folio_put(pagecache_folio); - } hugetlb_vma_unlock_read(vma); mutex_unlock(&hugetlb_fault_mutex_table[hash]); return handle_userfault(&vmf, VM_UFFD_WP); @@ -6813,23 +6801,14 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, /* Fallthrough to CoW */ } =20 - /* - * hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) and - * pagecache_folio, so here we need take the former one - * when folio !=3D pagecache_folio or !pagecache_folio. - */ + /* hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) */ folio =3D page_folio(pte_page(vmf.orig_pte)); - if (folio !=3D pagecache_folio) - if (!folio_trylock(folio)) { - need_wait_lock =3D 1; - goto out_ptl; - } - + folio_lock(folio); folio_get(folio); =20 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { if (!huge_pte_write(vmf.orig_pte)) { - ret =3D hugetlb_wp(pagecache_folio, &vmf); + ret =3D hugetlb_wp(&vmf); goto out_put_page; } else if (likely(flags & FAULT_FLAG_WRITE)) { vmf.orig_pte =3D huge_pte_mkdirty(vmf.orig_pte); @@ -6840,16 +6819,10 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, flags & FAULT_FLAG_WRITE)) update_mmu_cache(vma, vmf.address, vmf.pte); out_put_page: - if (folio !=3D pagecache_folio) - folio_unlock(folio); + folio_unlock(folio); folio_put(folio); out_ptl: spin_unlock(vmf.ptl); - - if (pagecache_folio) { - folio_unlock(pagecache_folio); - folio_put(pagecache_folio); - } out_mutex: hugetlb_vma_unlock_read(vma); =20 @@ -6861,15 +6834,6 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, vma_end_read(vma); =20 mutex_unlock(&hugetlb_fault_mutex_table[hash]); - /* - * Generally it's safe to hold refcount during waiting page lock. But - * here we just wait to defer the next page fault to avoid busy loop and - * the page is not used after unlocked before returning from the current - * page fault. So we are safe from accessing freed page, even if we wait - * here without taking refcount. - */ - if (need_wait_lock) - folio_wait_locked(folio); return ret; } =20 --=20 2.49.0 From nobody Fri Oct 10 20:53:44 2025 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0BB2253F1D for ; Thu, 12 Jun 2025 13:47:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736046; cv=none; b=P0KZatQmr7f5Mjt7J4zU09P31pX/SznpIjMwS7LAwgpYlL/4Mjy+EGKYmk7rjw46lmYbnDtOywIVrW0FgzvR2gm8HX7zEiQNjiPGtlNz8UW0lhjY8YCydZbDfEkOpULyb52maanT7pnTkOq77+3yWLwwgYpTqHOUzw0Vf+NGVD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736046; c=relaxed/simple; bh=mrnYwXze3IEA6YWuxw+bXXG/61PFWANBDnS+e2mHLug=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I/Xgy+TEa5eIN6x1zhqUY+iXFhxTQnoMXUPD+AkjdJgnN7p/8UNZTnSrVZE6/EK1+DMjTezI/urAh4tLI1BJnejOEL7DkgWvGGZtA/vxz4McRR1lscdzH5LVFvaLWdJBIqbfa+TErpgNgTUQrVLzbb2EWMBP1WqeO0iVgz+Nzmg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=gGMahn7h; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=q/cz1jQg; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=gGMahn7h; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=q/cz1jQg; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="gGMahn7h"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="q/cz1jQg"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="gGMahn7h"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="q/cz1jQg" 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 D1A5621977; Thu, 12 Jun 2025 13:47:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EaFjV+YYVmHkh8fxJDeIMLJgp7WZCofGq73lz5YTZdk=; b=gGMahn7h2NFCR1mcl3kx3ny/sXV9xL8tq61+MYwsP/AWMz5M3AxsRV20QVUU6aZ2jPVgwq FWlc6Jt2/neLj902ixoGf54o8eQcvkVsrigF2G4vXmyYO5PsuqIOjM/4adi4InRQjRPaTs +IXPAZaFheDfmXVaqCtNG0PS78iLzXY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EaFjV+YYVmHkh8fxJDeIMLJgp7WZCofGq73lz5YTZdk=; b=q/cz1jQgd5bjEPf/ecu/5YPTQir0e1oZjgTTV22SsKFy8JHwy1qyBsEy47zKUB3/8wrPeB 7vdvd5pOGrs1evBg== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=gGMahn7h; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="q/cz1jQg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EaFjV+YYVmHkh8fxJDeIMLJgp7WZCofGq73lz5YTZdk=; b=gGMahn7h2NFCR1mcl3kx3ny/sXV9xL8tq61+MYwsP/AWMz5M3AxsRV20QVUU6aZ2jPVgwq FWlc6Jt2/neLj902ixoGf54o8eQcvkVsrigF2G4vXmyYO5PsuqIOjM/4adi4InRQjRPaTs +IXPAZaFheDfmXVaqCtNG0PS78iLzXY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736037; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EaFjV+YYVmHkh8fxJDeIMLJgp7WZCofGq73lz5YTZdk=; b=q/cz1jQgd5bjEPf/ecu/5YPTQir0e1oZjgTTV22SsKFy8JHwy1qyBsEy47zKUB3/8wrPeB 7vdvd5pOGrs1evBg== 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 5237A132D8; Thu, 12 Jun 2025 13:47:17 +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 WFNhEWXaSmhJEAAAD6G6ig (envelope-from ); Thu, 12 Jun 2025 13:47:17 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , James Houghton , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 2/5] mm,hugetlb: Document the reason to lock the folio in the faulting path Date: Thu, 12 Jun 2025 15:46:58 +0200 Message-ID: <20250612134701.377855-3-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612134701.377855-1-osalvador@suse.de> References: <20250612134701.377855-1-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCPT_COUNT_SEVEN(0.00)[9]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; R_RATELIMIT(0.00)[to_ip_from(RL9xiirkf5437rzygec8i7198k)]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.de:dkim,suse.de:mid,suse.de:email] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: D1A5621977 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 Content-Type: text/plain; charset="utf-8" Recent conversations showed that there was a misunderstanding about why we were locking the folio prior to calling hugetlb_wp(). Document explicitly why we need to take the lock, explaining on the way that although the timespan for the locking of anonymous and file folios is diffe= rent, it would require a major surgery to represent that difference with the curr= ent code. Signed-off-by: Oscar Salvador --- mm/hugetlb.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 175edafeec67..dfa09fc3b2c6 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6537,6 +6537,10 @@ static vm_fault_t hugetlb_no_page(struct address_spa= ce *mapping, } new_pagecache_folio =3D true; } else { + /* + * hugetlb_wp() expects the folio to be locked in order to + * check whether we can re-use this page exclusively for us. + */ folio_lock(folio); anon_rmap =3D 1; } @@ -6801,7 +6805,19 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, /* Fallthrough to CoW */ } =20 - /* hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) */ + /* + * We need to lock the folio before calling hugetlb_wp(). + * Either the folio is in the pagecache and we need to copy it over + * to another file, so it must remain stable throughout the operation, + * or the folio is anonymous and we need to lock it in order to check + * whether we can re-use it and mark it exclusive for this process. + * The timespan for the lock differs depending on the type, since + * anonymous folios only need to hold the lock while checking whether we + * can re-use it, while we need to hold it throughout the copy in case + * we are dealing with a folio from a pagecache. + * Representing this difference would be tricky with the current code, + * so just hold the lock for the duration of hugetlb_wp(). + */ folio =3D page_folio(pte_page(vmf.orig_pte)); folio_lock(folio); folio_get(folio); --=20 2.49.0 From nobody Fri Oct 10 20:53:44 2025 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D095325A2D2 for ; Thu, 12 Jun 2025 13:47:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736052; cv=none; b=OSaRqRDESVk9HjVmllk8OyvarFCCGOZz3JgSlrIGFmzFOEBBz4xsfQ9lJk+Pyb0feamXgqTK0f/IeLXKQ2F2Uv+LtGUw/TonAVVD0g9bLcPgh+qlw6deqyBOpUJmkXRQnAeY5FpWV8z0jg+1kCLxnbpmkqMnj44m9NOCpSt9tA0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736052; c=relaxed/simple; bh=+sSAp8qXn6cQTtZrbNBGZmqBEN4KINGiCBzMWx3bKEs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e6gCuXIoM9NnJyghOaqQW/dd8ks6+jlkRkOcSxinKvWtNQtviZMwVlCvywZTFj/hEYWi7qBtNvr/sSgHlWS/HCWAISno+0mKG5JPQUoBHUNhFQ3w3t+me6XYj+9lJHmoH3FBzoBq3MYn1c5zkhnh5D6RXz1o2ekVGUVebI0wS7w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=dCemQDOT; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=9Bf/jW9p; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=dCemQDOT; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=9Bf/jW9p; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="dCemQDOT"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="9Bf/jW9p"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="dCemQDOT"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="9Bf/jW9p" 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 6AC5521985; Thu, 12 Jun 2025 13:47:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736038; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vt/8GylTs/EQrkcPezsjpSOQ7ldzUYCaWmy2QFxBUFY=; b=dCemQDOT8NRxm/2HdvF5olIZ2zAth0e4UmZM7G0eFE6WgVAqneoYqcOhIXEsVfQ++7Dw5Y +92zLK+n0ZShK4qoicbKJkmPD1vwNKtdYimE6KuLK3ufO7+AJwQBRYwUab2TqWmVlIIAES YoDif25FpIwJ+ms7G+dB+4scRmcvm/I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736038; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vt/8GylTs/EQrkcPezsjpSOQ7ldzUYCaWmy2QFxBUFY=; b=9Bf/jW9pjaBJNqh+O7dnGyYRYZmqcUaXSPmNbbCpFxvJedgFktFzm6mHrIw98vsKdsW2vN qjegRpSUZ5CWGuCA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=dCemQDOT; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="9Bf/jW9p" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736038; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vt/8GylTs/EQrkcPezsjpSOQ7ldzUYCaWmy2QFxBUFY=; b=dCemQDOT8NRxm/2HdvF5olIZ2zAth0e4UmZM7G0eFE6WgVAqneoYqcOhIXEsVfQ++7Dw5Y +92zLK+n0ZShK4qoicbKJkmPD1vwNKtdYimE6KuLK3ufO7+AJwQBRYwUab2TqWmVlIIAES YoDif25FpIwJ+ms7G+dB+4scRmcvm/I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736038; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vt/8GylTs/EQrkcPezsjpSOQ7ldzUYCaWmy2QFxBUFY=; b=9Bf/jW9pjaBJNqh+O7dnGyYRYZmqcUaXSPmNbbCpFxvJedgFktFzm6mHrIw98vsKdsW2vN qjegRpSUZ5CWGuCA== 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 E27D313A6D; Thu, 12 Jun 2025 13:47:17 +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 AA2lNGXaSmhJEAAAD6G6ig (envelope-from ); Thu, 12 Jun 2025 13:47:17 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , James Houghton , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 3/5] mm,hugetlb: Conver anon_rmap into boolean Date: Thu, 12 Jun 2025 15:46:59 +0200 Message-ID: <20250612134701.377855-4-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612134701.377855-1-osalvador@suse.de> References: <20250612134701.377855-1-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCPT_COUNT_SEVEN(0.00)[9]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; R_RATELIMIT(0.00)[to_ip_from(RL9xiirkf5437rzygec8i7198k)]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.de:dkim,suse.de:mid,suse.de:email] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 6AC5521985 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 Content-Type: text/plain; charset="utf-8" We use anon_rmap as a boolean, so declare it as that. Signed-off-by: Oscar Salvador Reviewed-by: David Hildenbrand --- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index dfa09fc3b2c6..62bc3808f99e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6431,7 +6431,7 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, struct mm_struct *mm =3D vma->vm_mm; struct hstate *h =3D hstate_vma(vma); vm_fault_t ret =3D VM_FAULT_SIGBUS; - int anon_rmap =3D 0; + bool anon_rmap =3D false; unsigned long size; struct folio *folio; pte_t new_pte; @@ -6542,7 +6542,7 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, * check whether we can re-use this page exclusively for us. */ folio_lock(folio); - anon_rmap =3D 1; + anon_rmap =3D true; } } else { /* --=20 2.49.0 From nobody Fri Oct 10 20:53:44 2025 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7110E24DCE2 for ; Thu, 12 Jun 2025 13:47:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736059; cv=none; b=Uz43E6xCYZw0w2Z67rvehBVcZzW5QrcsWkOoOHR73Zgq2NnQUhEQYQl9oIIlPD7uMZSnuT8CUch19m/ZIdNzf0pTVXJs2SXV5dZzywjFsZtSrtIfN2dXBITzTa8OqlisFr8GmeTJTHWVjsKgD9ghTNBau6gZo/b09xLztVeanrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736059; c=relaxed/simple; bh=Dx4F4kRLv6WKbLr8ZOEav4L6SeKUI6GtppIcl6bTmWI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IQiDLdQo9NsnJO/16uqH3PIoGtBboRJnjZD/ev2GkObOWKd/jM334DfVa5OPKljIFzvpykd8r7N6XZn4UQ5d+BaAgnJEl7egPVwhli9HFqN0Yuk20Sg0wovdhErR6WxpLfHUXRm9nUH8xaLNb7qWE/qZx8OnJ4OiU4Gs6M3AO9s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=NDU1ewJH; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Ldl6zGXA; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=NDU1ewJH; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Ldl6zGXA; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="NDU1ewJH"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Ldl6zGXA"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="NDU1ewJH"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Ldl6zGXA" 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 0418321986; Thu, 12 Jun 2025 13:47:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N8oNzaGUW5qLujz6KN4jF5cE4+hRczq1P7iZs6uKvYo=; b=NDU1ewJHuBxYRllt+TaN9nMUngGutNhrt90DaRyImP86NuxFgs9h1gouiXhfE4kzn6SV4x EzF/7BPM4VP6Z0Uwsnti6mCZwHxMmCMaDHmSYlTmvUfnUuO5Q3W0ve0vtzRqV9RK8Dy66X ZdXJDUkmCv67B5qQsQYNXEHlzOHgWkA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N8oNzaGUW5qLujz6KN4jF5cE4+hRczq1P7iZs6uKvYo=; b=Ldl6zGXAuCxWWfrJeMN/RWS+oyQKET0q4r8Fmi32QBKRYrPJ0VqDbRg4NcFFAa5aLQQIMx WGPnyaHqB+A91+CA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=NDU1ewJH; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=Ldl6zGXA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N8oNzaGUW5qLujz6KN4jF5cE4+hRczq1P7iZs6uKvYo=; b=NDU1ewJHuBxYRllt+TaN9nMUngGutNhrt90DaRyImP86NuxFgs9h1gouiXhfE4kzn6SV4x EzF/7BPM4VP6Z0Uwsnti6mCZwHxMmCMaDHmSYlTmvUfnUuO5Q3W0ve0vtzRqV9RK8Dy66X ZdXJDUkmCv67B5qQsQYNXEHlzOHgWkA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N8oNzaGUW5qLujz6KN4jF5cE4+hRczq1P7iZs6uKvYo=; b=Ldl6zGXAuCxWWfrJeMN/RWS+oyQKET0q4r8Fmi32QBKRYrPJ0VqDbRg4NcFFAa5aLQQIMx WGPnyaHqB+A91+CA== 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 7B66D132D8; Thu, 12 Jun 2025 13:47:18 +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 GACBG2baSmhJEAAAD6G6ig (envelope-from ); Thu, 12 Jun 2025 13:47:18 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , James Houghton , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 4/5] mm,hugetlb: Drop obsolete comment about non-present pte and second faults Date: Thu, 12 Jun 2025 15:47:00 +0200 Message-ID: <20250612134701.377855-5-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612134701.377855-1-osalvador@suse.de> References: <20250612134701.377855-1-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCPT_COUNT_SEVEN(0.00)[9]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; R_RATELIMIT(0.00)[to_ip_from(RL9xiirkf5437rzygec8i7198k)]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim,suse.de:mid,suse.de:email,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 0418321986 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 Content-Type: text/plain; charset="utf-8" There is a comment in hugetlb_fault() that does not hold anymore. This one: /* * vmf.orig_pte could be a migration/hwpoison vmf.orig_pte at this * point, so this check prevents the kernel from going below assuming * that we have an active hugepage in pagecache. This goto expects * the 2nd page fault, and is_hugetlb_entry_(migration|hwpoisoned) * check will properly handle it. */ This was written because back in the day we used to do: hugetlb_fault () { ptep =3D huge_pte_offset(...) if (ptep) { entry =3D huge_ptep_get(ptep) if (unlikely(is_hugetlb_entry_migration(entry)) ... else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) ... } ... ... /* * entry could be a migration/hwpoison entry at this point, so this * check prevents the kernel from going below assuming that we have * a active hugepage in pagecache. This goto expects the 2nd page fault, * and is_hugetlb_entry_(migration|hwpoisoned) check will properly * handle it. */ if (!pte_present(entry)) goto out_mutex; ... } The code was designed to check for hwpoisoned/migration entries upfront, and then bail out if further down the pte was not present anymore, relying on the second fault to properly handle migration/hwpoison entries t= hat time around. The way we handle this is different nowadays, so drop the misleading commen= t. Signed-off-by: Oscar Salvador --- mm/hugetlb.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 62bc3808f99e..ad377e24b7d0 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6742,13 +6742,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, =20 ret =3D 0; =20 - /* - * vmf.orig_pte could be a migration/hwpoison vmf.orig_pte at this - * point, so this check prevents the kernel from going below assuming - * that we have an active hugepage in pagecache. This goto expects - * the 2nd page fault, and is_hugetlb_entry_(migration|hwpoisoned) - * check will properly handle it. - */ + /* Not present, either a migration or a hwpoisoned entry */ if (!pte_present(vmf.orig_pte)) { if (unlikely(is_hugetlb_entry_migration(vmf.orig_pte))) { /* --=20 2.49.0 From nobody Fri Oct 10 20:53:44 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBF6725486A for ; Thu, 12 Jun 2025 13:47:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736047; cv=none; b=WsqfvAM5KMbwUWb0C8oa+ZETia4iOW3giZtt0SuJXBhfzu1w8mf4xeIMMVSN6txoY685w7iFC1ysdU2pkjo+jlXWwo5mfDKH5dUpILkTH4dn+kLRn5INwRfeCUgfMl91yDaXNeWwB5URv9yF12LwP0Ho8NPM1tLHlKkeyW3qosQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749736047; c=relaxed/simple; bh=6VxsUa9pp7zu1n0zdDNUqNqWXpP5qoBim9VfuOOJWnY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HaDTmdb+o78x0ucDusF63csG7J+8cMh0FyBMsaNFxQTfT4oQ+t37tJfNvnX3NNzMLRc7+Evc6XyfCYhTme/VInCyJJxnqqWGrYp2T7eBGQbThWXw95lcUUempk1yAfDNKC9vKM1UG72rK//hbFeym4aA6i7eQjpz4NgPFmomF8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=NPzjCHIN; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=RMFn6t//; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=NPzjCHIN; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=RMFn6t//; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="NPzjCHIN"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="RMFn6t//"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="NPzjCHIN"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="RMFn6t//" 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-out2.suse.de (Postfix) with ESMTPS id 934BA1F850; Thu, 12 Jun 2025 13:47:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e+huMrOQB10g9Wu0c3leQPZ840vTa2RdWXsWQX3/xvQ=; b=NPzjCHINtoCkkgooLDQVx7y7JRILmxtXH0n2Vc16NPF0Rjd48K9rSJu7Twud7rtT69tKYp IKsMNEcR+bWpbB42qppy62Wg8Ft4mJSqqz4wFS9l186aS84Yj0khSxeQNm4QJbadimS5MQ HPqRnveuLhhXgAubXWbhrm7ObYNWz0I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e+huMrOQB10g9Wu0c3leQPZ840vTa2RdWXsWQX3/xvQ=; b=RMFn6t///l87pVFne+2oETPz0Qr2en1Z2yDDezyc87wmWG+KFh6KR5YXkXA5YS6f2GnpZ0 0OLCV7Od1RiJQpCw== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=NPzjCHIN; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="RMFn6t//" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e+huMrOQB10g9Wu0c3leQPZ840vTa2RdWXsWQX3/xvQ=; b=NPzjCHINtoCkkgooLDQVx7y7JRILmxtXH0n2Vc16NPF0Rjd48K9rSJu7Twud7rtT69tKYp IKsMNEcR+bWpbB42qppy62Wg8Ft4mJSqqz4wFS9l186aS84Yj0khSxeQNm4QJbadimS5MQ HPqRnveuLhhXgAubXWbhrm7ObYNWz0I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749736039; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e+huMrOQB10g9Wu0c3leQPZ840vTa2RdWXsWQX3/xvQ=; b=RMFn6t///l87pVFne+2oETPz0Qr2en1Z2yDDezyc87wmWG+KFh6KR5YXkXA5YS6f2GnpZ0 0OLCV7Od1RiJQpCw== 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 1613613A6D; Thu, 12 Jun 2025 13:47:19 +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 sCDIAmfaSmhJEAAAD6G6ig (envelope-from ); Thu, 12 Jun 2025 13:47:19 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , James Houghton , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 5/5] mm,hugetlb: Drop unlikelys from hugetlb_fault Date: Thu, 12 Jun 2025 15:47:01 +0200 Message-ID: <20250612134701.377855-6-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612134701.377855-1-osalvador@suse.de> References: <20250612134701.377855-1-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: 934BA1F850 X-Rspamd-Action: no action X-Spam-Flag: NO X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCPT_COUNT_SEVEN(0.00)[9]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; R_RATELIMIT(0.00)[to_ip_from(RL9xiirkf5437rzygec8i7198k)]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,suse.de:mid,suse.de:dkim,suse.de:email] X-Spam-Score: -3.01 X-Spam-Level: Content-Type: text/plain; charset="utf-8" The unlikely predates an era where we were checking for hwpoisoned/migration entries prior to checking whether the pte was present. Currently, we check for the pte to be a migration/hwpoison entry after we have checked that is not present, so it must be either one or the other. Signed-off-by: Oscar Salvador --- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ad377e24b7d0..d6b0f2b68beb 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6744,7 +6744,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct= vm_area_struct *vma, =20 /* Not present, either a migration or a hwpoisoned entry */ if (!pte_present(vmf.orig_pte)) { - if (unlikely(is_hugetlb_entry_migration(vmf.orig_pte))) { + if (is_hugetlb_entry_migration(vmf.orig_pte)) { /* * Release the hugetlb fault lock now, but retain * the vma lock, because it is needed to guard the @@ -6755,7 +6755,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct= vm_area_struct *vma, mutex_unlock(&hugetlb_fault_mutex_table[hash]); migration_entry_wait_huge(vma, vmf.address, vmf.pte); return 0; - } else if (unlikely(is_hugetlb_entry_hwpoisoned(vmf.orig_pte))) + } else if (is_hugetlb_entry_hwpoisoned(vmf.orig_pte)) ret =3D VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h)); goto out_mutex; --=20 2.49.0