From nobody Wed Oct 8 08:20:19 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 ABD2A28C2C5 for ; Mon, 30 Jun 2025 14:42:26 +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=1751294548; cv=none; b=Plm8pTuxkBLf+EU6JmBH8he9RYXEAgwMoxMiroM6icnYADkoomWM9e6HzP9JU5AHBOH99ErNFK+f+dRqw8vst+OVO6zts5guuOI5nLwQKMGNAkPXfZhP9mHAaU35Qd3b070FS3cVn4vA2z8q0kxKG1xBPdWJkNrytC0fkM4cJz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751294548; c=relaxed/simple; bh=y0hbNfyG/rjNC/73E1ZP7TsUSimgrLut4xPmXwJh6EU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OrEkxmOpZA8FML92KQi5CqSQR+gtAZ2ALOMaaXUy6o3Mbx6ZvFn9j1cRps8CwDkzQsXK0rdXSoRRpciRpQ/BQoJqGKM2SeD2mgOhW219VKXcW/pwqe16CHys7zsyck6swPcSh+P6lsMkjm8luIHp2KJUqTamLIG79Fop1AYAZ+s= 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=v3SEP+35; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Wm0ha8iP; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=v3SEP+35; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Wm0ha8iP; 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="v3SEP+35"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Wm0ha8iP"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="v3SEP+35"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Wm0ha8iP" 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 0AE7F21165; Mon, 30 Jun 2025 14:42:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294537; 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=aoXI4BTUSdoTvkbJ+U0NIhJSkczrCgYuAhLAQMuINis=; b=v3SEP+358BahKnozexSGibwj68IhIV2tIfokbAz+C8xgncbLdXspeeFLgNgDBAwnhW5WO7 JrdqfzkBGIhOdG5XoGtX3xHnEf3dtKCeGbRyBhCPt/CSCW1RhcaUmGBF2a43pSifFlpBNQ mrqt2HUY3NfLFbDmrUhANagDcP2vWYE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294537; 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=aoXI4BTUSdoTvkbJ+U0NIhJSkczrCgYuAhLAQMuINis=; b=Wm0ha8iPgYdYJ5vR9bd0ymE5Y3bcRM9hA8z/Pd57NiLoSVoPv9OOvPbRyHV5KIploWMbuK 3Pob6s7jbgUuLOCg== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=v3SEP+35; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=Wm0ha8iP DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294537; 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=aoXI4BTUSdoTvkbJ+U0NIhJSkczrCgYuAhLAQMuINis=; b=v3SEP+358BahKnozexSGibwj68IhIV2tIfokbAz+C8xgncbLdXspeeFLgNgDBAwnhW5WO7 JrdqfzkBGIhOdG5XoGtX3xHnEf3dtKCeGbRyBhCPt/CSCW1RhcaUmGBF2a43pSifFlpBNQ mrqt2HUY3NfLFbDmrUhANagDcP2vWYE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294537; 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=aoXI4BTUSdoTvkbJ+U0NIhJSkczrCgYuAhLAQMuINis=; b=Wm0ha8iPgYdYJ5vR9bd0ymE5Y3bcRM9hA8z/Pd57NiLoSVoPv9OOvPbRyHV5KIploWMbuK 3Pob6s7jbgUuLOCg== 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 89F2213A24; Mon, 30 Jun 2025 14:42: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 4DjrHkiiYmjqdAAAD6G6ig (envelope-from ); Mon, 30 Jun 2025 14:42:16 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH v4 1/5] mm,hugetlb: change mechanism to detect a COW on private mapping Date: Mon, 30 Jun 2025 16:42:08 +0200 Message-ID: <20250630144212.156938-2-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250630144212.156938-1-osalvador@suse.de> References: <20250630144212.156938-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 [-5.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from,2a07:de40:b281:106:10:150:64:167:received]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_SEVEN(0.00)[8]; RCVD_COUNT_TWO(0.00)[2]; DKIM_TRACE(0.00)[suse.de:+]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_TLS_ALL(0.00)[]; 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,linux.dev:email] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 0AE7F21165 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -5.01 Content-Type: text/plain; charset="utf-8" hugetlb_wp() checks whether the process is trying to COW on a private mapping in order to know whether the reservation for that address was already consumed. If it was consumed and we are the ownner of the mapping, the folio will have 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. Link: https://lkml.kernel.org/r/20250627102904.107202-1-osalvador@suse.de Link: https://lkml.kernel.org/r/20250627102904.107202-2-osalvador@suse.de Link: https://lore.kernel.org/lkml/20250513093448.592150-1-gavinguo@igalia.= com/ [1] Fixes: 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchroniza= tion") Signed-off-by: Oscar Salvador Reported-by: Gavin Guo Closes: https://lore.kernel.org/lkml/20250513093448.592150-1-gavinguo@igali= a.com/ Suggested-by: Peter Xu Acked-by: David Hildenbrand Cc: Muchun Song Signed-off-by: Andrew Morton --- mm/hugetlb.c | 88 ++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 54 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index fa7faf38c99e..14274a02dd14 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6149,8 +6149,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; @@ -6212,16 +6211,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); @@ -6600,7 +6600,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); @@ -6668,10 +6668,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 hstate *h =3D hstate_vma(vma); struct address_space *mapping; - int need_wait_lock =3D 0; + bool need_wait_lock =3D false; struct vm_fault vmf =3D { .vma =3D vma, .address =3D address & huge_page_mask(h), @@ -6766,8 +6765,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)) { @@ -6777,11 +6775,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); @@ -6795,10 +6788,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); @@ -6810,24 +6799,19 @@ 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. - */ - 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_get(folio); - if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { if (!huge_pte_write(vmf.orig_pte)) { - ret =3D hugetlb_wp(pagecache_folio, &vmf); - goto out_put_page; + /* hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) */ + folio =3D page_folio(pte_page(vmf.orig_pte)); + if (!folio_trylock(folio)) { + need_wait_lock =3D true; + goto out_ptl; + } + folio_get(folio); + ret =3D hugetlb_wp(&vmf); + folio_unlock(folio); + folio_put(folio); + goto out_ptl; } else if (likely(flags & FAULT_FLAG_WRITE)) { vmf.orig_pte =3D huge_pte_mkdirty(vmf.orig_pte); } @@ -6836,17 +6820,8 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, if (huge_ptep_set_access_flags(vma, vmf.address, vmf.pte, vmf.orig_pte, flags & FAULT_FLAG_WRITE)) update_mmu_cache(vma, vmf.address, vmf.pte); -out_put_page: - if (folio !=3D pagecache_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 @@ -6859,11 +6834,16 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *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. + * hugetlb_wp drops all the locks, but the folio lock, before trying to + * unmap the folio from other processes. During that window, if another + * process mapping that folio faults in, it will take the mutex and then + * it will wait on folio_lock, causing an ABBA deadlock. + * Use trylock instead and bail out if we fail. + * + * Ideally, we should hold a refcount on the folio we wait for, but we do + * not want to use the folio after it becomes unlocked, but rather just + * wait for it to become unlocked, so hopefully next fault successes on + * the trylock. */ if (need_wait_lock) folio_wait_locked(folio); --=20 2.50.0 From nobody Wed Oct 8 08:20:19 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 23C4828DF3A for ; Mon, 30 Jun 2025 14:42:32 +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=1751294554; cv=none; b=qWyxEePiG6+ieRFyInWknxgfAqHVIV+xxV4IapCwVDCUjL0PzN+mOT3XCvenV19iDK6g46mseju04yVk4KPKw0R8gu31/eN46OUCYn+jFBMN1nY9aJ99bu5UXoz8MHolFIUaqEN4tRNV+N1f4mPx37tVTjXZqKloNxwVWlgLZLU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751294554; c=relaxed/simple; bh=3nimWEP4TU2hazlO9B3yM6uHoocXP+e13BqLuWR+t6U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BisDToRzKC4Kf+2lu/Wr/0KYkSprkoExsYiGFUXTdc3T4XArn2HnUr/H1krTP8H+PvgEfbR22PHKv8AU0yn65aCdsLOvpHPLM97u12FhMEbuJAcbApXDcVrEbdm1QYelqTQQG+7TScf15yFlMGFttVaUt13Z/3dh1K/r3J/tze4= 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=vT5nk7U2; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=pGILP/Nw; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=vT5nk7U2; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=pGILP/Nw; 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="vT5nk7U2"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="pGILP/Nw"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="vT5nk7U2"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="pGILP/Nw" 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 9D7F021166; Mon, 30 Jun 2025 14:42:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294537; 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=2K/Grp0pf+YGAmhL32U2P7mtv8DTunlS/1iRZeZXLDU=; b=vT5nk7U2x5sYGl1gHXXtdT1TZ/nzdJ/nN9pctW1RRt3saa9YMiBLhcPSNg2xCgcFGm7QL3 Cjv36Jg1cL/tyKTZlwQTjbVdFJ6zUNcTlj6aV0QL14TnppRVC5q4/tSFPbpy8aaL5NWSWB i69yoxhkaqV2NFH/pL/HugKoHzA/ER0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294537; 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=2K/Grp0pf+YGAmhL32U2P7mtv8DTunlS/1iRZeZXLDU=; b=pGILP/NwSeRJXhT9aK+GyG6qi3045fMfm8doOLh8QXNYLyYVDRM1rWFJpcsJsgGv4QtKrI cdJxjiFxsCJHFpBw== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=vT5nk7U2; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="pGILP/Nw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294537; 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=2K/Grp0pf+YGAmhL32U2P7mtv8DTunlS/1iRZeZXLDU=; b=vT5nk7U2x5sYGl1gHXXtdT1TZ/nzdJ/nN9pctW1RRt3saa9YMiBLhcPSNg2xCgcFGm7QL3 Cjv36Jg1cL/tyKTZlwQTjbVdFJ6zUNcTlj6aV0QL14TnppRVC5q4/tSFPbpy8aaL5NWSWB i69yoxhkaqV2NFH/pL/HugKoHzA/ER0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294537; 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=2K/Grp0pf+YGAmhL32U2P7mtv8DTunlS/1iRZeZXLDU=; b=pGILP/NwSeRJXhT9aK+GyG6qi3045fMfm8doOLh8QXNYLyYVDRM1rWFJpcsJsgGv4QtKrI cdJxjiFxsCJHFpBw== 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 1E6F41399F; Mon, 30 Jun 2025 14:42: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 MJCfBEmiYmjqdAAAD6G6ig (envelope-from ); Mon, 30 Jun 2025 14:42:17 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH v4 2/5] mm,hugetlb: sort out folio locking in the faulting path Date: Mon, 30 Jun 2025 16:42:09 +0200 Message-ID: <20250630144212.156938-3-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250630144212.156938-1-osalvador@suse.de> References: <20250630144212.156938-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 [-5.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; 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,linux.dev:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[8]; RCVD_TLS_ALL(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from,2a07:de40:b281:106:10:150:64:167:received]; DKIM_TRACE(0.00)[suse.de:+] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 9D7F021166 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -5.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 call in hugetlb_wp(). In fact, as soon as we have the folio mapped into the pagetables, we no longer need to hold it locked, because we know that no concurrent truncation could have happened. There is only one case where the folio needs to be locked, and that is when we are handling an anonymous folio, because hugetlb_wp() will check whether it can re-use it exclusively for the process that is faulting it in. So, pass the folio locked to hugetlb_wp() when that is the case. Link: https://lkml.kernel.org/r/20250627102904.107202-3-osalvador@suse.de Signed-off-by: Oscar Salvador Suggested-by: David Hildenbrand Cc: Gavin Guo Cc: Muchun Song Cc: Peter Xu Signed-off-by: Andrew Morton Acked-by: David Hildenbrand --- mm/hugetlb.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 14274a02dd14..31d39e2a0879 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6434,6 +6434,7 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, pte_t new_pte; bool new_folio, new_pagecache_folio =3D false; u32 hash =3D hugetlb_fault_mutex_hash(mapping, vmf->pgoff); + bool folio_locked =3D true; =20 /* * Currently, we are forced to kill the process in the event the @@ -6599,6 +6600,14 @@ static vm_fault_t hugetlb_no_page(struct address_spa= ce *mapping, =20 hugetlb_count_add(pages_per_huge_page(h), mm); if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { + /* + * No need to keep file folios locked. See comment in + * hugetlb_fault(). + */ + if (!anon_rmap) { + folio_locked =3D false; + folio_unlock(folio); + } /* Optimization, do the COW without a second fault */ ret =3D hugetlb_wp(vmf); } @@ -6613,7 +6622,8 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, if (new_folio) folio_set_hugetlb_migratable(folio); =20 - folio_unlock(folio); + if (folio_locked) + folio_unlock(folio); out: hugetlb_vma_unlock_read(vma); =20 @@ -6801,15 +6811,20 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, =20 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { if (!huge_pte_write(vmf.orig_pte)) { - /* hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) */ + /* + * Anonymous folios need to be lock since hugetlb_wp() + * checks whether we can re-use the folio exclusively + * for us in case we are the only user of it. + */ folio =3D page_folio(pte_page(vmf.orig_pte)); - if (!folio_trylock(folio)) { + if (folio_test_anon(folio) && !folio_trylock(folio)) { need_wait_lock =3D true; goto out_ptl; } folio_get(folio); ret =3D hugetlb_wp(&vmf); - folio_unlock(folio); + if (folio_test_anon(folio)) + folio_unlock(folio); folio_put(folio); goto out_ptl; } else if (likely(flags & FAULT_FLAG_WRITE)) { --=20 2.50.0 From nobody Wed Oct 8 08:20:19 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 35F9A28C5C1 for ; Mon, 30 Jun 2025 14:42:19 +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=1751294541; cv=none; b=R57hrrM3eaKgDcOdbd4ymE6sfY9NZFKO32JyAFPvugzYcXABcNo5czcGqoJ4Q2e9Ql9/FJKumlE7dPgBJWDCGUvSTHqM+18Ei/grZHebFJ4RDmafh5bvbFwW7SPcTxFNpKGdKBwadobuLazPS7fHHEMCeJaad4MImi+Bm2yeNLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751294541; c=relaxed/simple; bh=cEDBDTe2bFk8houKUMpD4iH7aUyOLvYuMbNzg7Pzd7s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tvYHrdpo6fPL+Qtzdirtl8KS0coOTxDUf6SzVdejWyHECvvCW7hqfYy0ihHrWxxNUeoE5Gag//DPuyTuP4x09a/P2rwLRmvVK+pH4f77QghUUBEeGWOQ0/vOT1G7GNTr66jic8v6rTmq8/h/wp1xlMVz/h9/KZv3XzAnkz1EN6I= 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=ZMJScKrT; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=QtkyIMBV; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=ZMJScKrT; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=QtkyIMBV; 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="ZMJScKrT"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="QtkyIMBV"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="ZMJScKrT"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="QtkyIMBV" 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 3486A1F397; Mon, 30 Jun 2025 14:42:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294538; 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=gsLGeQz2/SShAFkpABjfAB43POxq1/nuiPdGozgEwRs=; b=ZMJScKrT8yorsK3Vj9zd64X+typK2lYc7GQW8OLO7nYZhPyW2/wD2wi9tGeCXVdarZ2o1u PIVIcuoF9nQwXOJgkeevzLaCcrmP9+Nr3cvtxn31ws2l5r15NDkeDkozpggFTWzJUyOl7a yZNYpaDQNEHQvHBkZVhB4UvQiUT/XFg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294538; 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=gsLGeQz2/SShAFkpABjfAB43POxq1/nuiPdGozgEwRs=; b=QtkyIMBVLmCC4OsJ6FIpnFovcR/ThV82z9Wsq+vnqW5os+8C2p5B7/WDbKKVGlBW94ZPCi zYlnneRRBv5lDIBg== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=ZMJScKrT; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=QtkyIMBV DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294538; 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=gsLGeQz2/SShAFkpABjfAB43POxq1/nuiPdGozgEwRs=; b=ZMJScKrT8yorsK3Vj9zd64X+typK2lYc7GQW8OLO7nYZhPyW2/wD2wi9tGeCXVdarZ2o1u PIVIcuoF9nQwXOJgkeevzLaCcrmP9+Nr3cvtxn31ws2l5r15NDkeDkozpggFTWzJUyOl7a yZNYpaDQNEHQvHBkZVhB4UvQiUT/XFg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294538; 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=gsLGeQz2/SShAFkpABjfAB43POxq1/nuiPdGozgEwRs=; b=QtkyIMBVLmCC4OsJ6FIpnFovcR/ThV82z9Wsq+vnqW5os+8C2p5B7/WDbKKVGlBW94ZPCi zYlnneRRBv5lDIBg== 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 AFDCD13A24; Mon, 30 Jun 2025 14:42: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 4M0uKEmiYmjqdAAAD6G6ig (envelope-from ); Mon, 30 Jun 2025 14:42:17 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH v4 3/5] mm,hugetlb: rename anon_rmap to new_anon_folio and make it boolean Date: Mon, 30 Jun 2025 16:42:10 +0200 Message-ID: <20250630144212.156938-4-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250630144212.156938-1-osalvador@suse.de> References: <20250630144212.156938-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 [-5.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; 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)[]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; 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]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:106:10:150:64:167:received,2a07:de40:b281:104:10:150:64:97:from]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_SEVEN(0.00)[8]; R_RATELIMIT(0.00)[to_ip_from(RLfw9wkb71jz54yo3bnq4aib6i)]; 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,linux.dev:email] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 3486A1F397 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -5.01 Content-Type: text/plain; charset="utf-8" anon_rmap is used to determine whether the new allocated folio is anonymous. Rename it to something more meaningul like new_anon_folio and make it boolean, as we use it like that. While we are at it, drop 'new_pagecache_folio' as 'new_anon_folio' is enough to check whether we need to restore the consumed reservation. Link: https://lkml.kernel.org/r/20250627102904.107202-4-osalvador@suse.de Signed-off-by: Oscar Salvador Cc: David Hildenbrand Cc: Gavin Guo Cc: Muchun Song Cc: Peter Xu Signed-off-by: Andrew Morton Acked-by: David Hildenbrand --- mm/hugetlb.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 31d39e2a0879..67f3c9c16348 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6424,17 +6424,16 @@ static bool hugetlb_pte_stable(struct hstate *h, st= ruct mm_struct *mm, unsigned static vm_fault_t hugetlb_no_page(struct address_space *mapping, struct vm_fault *vmf) { + u32 hash =3D hugetlb_fault_mutex_hash(mapping, vmf->pgoff); + bool new_folio, new_anon_folio =3D false; struct vm_area_struct *vma =3D vmf->vma; 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; - unsigned long size; + bool folio_locked =3D true; struct folio *folio; + unsigned long size; pte_t new_pte; - bool new_folio, new_pagecache_folio =3D false; - u32 hash =3D hugetlb_fault_mutex_hash(mapping, vmf->pgoff); - bool folio_locked =3D true; =20 /* * Currently, we are forced to kill the process in the event the @@ -6533,10 +6532,9 @@ static vm_fault_t hugetlb_no_page(struct address_spa= ce *mapping, ret =3D VM_FAULT_SIGBUS; goto out; } - new_pagecache_folio =3D true; } else { + new_anon_folio =3D true; folio_lock(folio); - anon_rmap =3D 1; } } else { /* @@ -6585,7 +6583,7 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, if (!pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), vmf->orig_pte)) goto backout; =20 - if (anon_rmap) + if (new_anon_folio) hugetlb_add_new_anon_rmap(folio, vma, vmf->address); else hugetlb_add_file_rmap(folio); @@ -6604,7 +6602,7 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, * No need to keep file folios locked. See comment in * hugetlb_fault(). */ - if (!anon_rmap) { + if (!new_anon_folio) { folio_locked =3D false; folio_unlock(folio); } @@ -6640,7 +6638,8 @@ static vm_fault_t hugetlb_no_page(struct address_spac= e *mapping, backout: spin_unlock(vmf->ptl); backout_unlocked: - if (new_folio && !new_pagecache_folio) + /* We only need to restore reservations for private mappings */ + if (new_anon_folio) restore_reserve_on_error(h, vma, vmf->address, folio); =20 folio_unlock(folio); --=20 2.50.0 From nobody Wed Oct 8 08:20:19 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 BF31928DB58 for ; Mon, 30 Jun 2025 14:42:26 +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=1751294548; cv=none; b=icoNUKyhq882mMIb+uMu/zvrYPTBrx3EU2uO9rSRV9RPuHDChAIjE53i5E6cDqTaR2R89FEtO5zZq6LSf5svQLI6TJ0Ck7ZogUzipLogECii2ItVzhpVOKBR13LG33nDPWm8iHEiPiH84LTDZF8AwnjddQJ/pW5wpaYhjfMO3vA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751294548; c=relaxed/simple; bh=gxQmOOof0otsKlpt20PyNg5TbDDZsnanmCIdzr3GZmk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k01UfJH1ec5XK1KowvzyhEtfpyCqPEkhT5DnbDcwuZxpuGdJJxyvuLLN0l+56EKUl0JYZ5NhOnh1/ohHzvnJKAV2yOHppDdKD3utkSQmuAjCEXBdRLVxj0v5Q5Xa5tVitVK7OPnXCkjLBx2e9abcAyCQQFlH3TWf64kqJTbEivU= 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=fNw2Cazx; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=rdJ1dDrX; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=fNw2Cazx; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=rdJ1dDrX; 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="fNw2Cazx"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="rdJ1dDrX"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="fNw2Cazx"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="rdJ1dDrX" Received: from imap1.dmz-prg2.suse.org (unknown [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 BC94A1F393; Mon, 30 Jun 2025 14:42:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294538; 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=lswS/npHHg2FpDwDpRgpwsvZxZdHN4e8MQowvC+BDOQ=; b=fNw2CazxDou2e5Lf3A6ZpxG208/WtbJYxDUzNf/EmvLIOgHbGYNuPbMbFnGGoXzcc8KbqJ TCcwNkI1krvkJ0Y9wBqNCvhAEPZ57ed397g9O8o5IzrU5jPxFEgYuQ8Wirhicq+8ZOvGM6 waztCYLASagVbGBicUFfrf6loAKUWOw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294538; 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=lswS/npHHg2FpDwDpRgpwsvZxZdHN4e8MQowvC+BDOQ=; b=rdJ1dDrXteqCeY+23etpuh5UTIjzLFA3F0g9luhVSs3raT3CwR8xP9Ucfx5ctWcSh9UPhg n6GhkTkAZBXbbLAA== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294538; 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=lswS/npHHg2FpDwDpRgpwsvZxZdHN4e8MQowvC+BDOQ=; b=fNw2CazxDou2e5Lf3A6ZpxG208/WtbJYxDUzNf/EmvLIOgHbGYNuPbMbFnGGoXzcc8KbqJ TCcwNkI1krvkJ0Y9wBqNCvhAEPZ57ed397g9O8o5IzrU5jPxFEgYuQ8Wirhicq+8ZOvGM6 waztCYLASagVbGBicUFfrf6loAKUWOw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294538; 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=lswS/npHHg2FpDwDpRgpwsvZxZdHN4e8MQowvC+BDOQ=; b=rdJ1dDrXteqCeY+23etpuh5UTIjzLFA3F0g9luhVSs3raT3CwR8xP9Ucfx5ctWcSh9UPhg n6GhkTkAZBXbbLAA== 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 46BB11399F; Mon, 30 Jun 2025 14:42: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 2LmWDkqiYmjqdAAAD6G6ig (envelope-from ); Mon, 30 Jun 2025 14:42:18 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH v4 4/5] mm,hugetlb: drop obsolete comment about non-present pte and second faults Date: Mon, 30 Jun 2025 16:42:11 +0200 Message-ID: <20250630144212.156938-5-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250630144212.156938-1-osalvador@suse.de> References: <20250630144212.156938-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-Spam-Flag: NO X-Spam-Score: -2.80 X-Spamd-Result: default: False [-2.80 / 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]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_SEVEN(0.00)[8]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,suse.de:email,linux.dev:email,imap1.dmz-prg2.suse.org:helo]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: 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 that time around. The way we handle this is different nowadays, so drop the misleading comment. Link: https://lkml.kernel.org/r/20250627102904.107202-5-osalvador@suse.de Signed-off-by: Oscar Salvador Cc: David Hildenbrand Cc: Gavin Guo Cc: Muchun Song Cc: Peter Xu Signed-off-by: Andrew Morton Acked-by: David Hildenbrand --- mm/hugetlb.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 67f3c9c16348..ba078aa1cb96 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6745,13 +6745,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.50.0 From nobody Wed Oct 8 08:20:19 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 BF62B28CF76 for ; Mon, 30 Jun 2025 14:42:39 +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=1751294561; cv=none; b=JRZF60dq4o8IP038pZQSuLIkLrpLipU6cIDovqwIdSMmzg5JZNHvwqeD3ayZuuT7RA/drt5dcgbZR+C2jngbXyiUU7rSdz4GGn1r0DK+DAGsPvbkipHlxOg2YoBQvTHVxdlO42Dv1iTMPJRae3pokXfdHzJSvTEhp0TCUbnJ8KQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751294561; c=relaxed/simple; bh=wNc5/0oYy9n47qFvbw589KeGQi7wI1lepwH1WsSR3yU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vAoOcNW32e0ynlqGWvOdec+s5G70W6SnoIAvutrypUNQpNRq5niO8T6TNptlFfIAau0+fMfKuKOO61XrEz0fC3j4TGlaTk0OFSiTgX8R2dxXdNgQBqCfUzyCSyWgprE9Dl7sQz4AXL9uDcXQhl0QdfN2GaMhkBRuHzR4VeadR2Y= 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=ErG1j74Z; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=SVjaLSc+; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=ErG1j74Z; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=SVjaLSc+; 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="ErG1j74Z"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="SVjaLSc+"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="ErG1j74Z"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="SVjaLSc+" 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 5268521169; Mon, 30 Jun 2025 14:42:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294539; 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=I/0nrP3gEwMU8Fn4GtEZKqKPsnJhVzjLl9aZ9S6/OZg=; b=ErG1j74ZDfTCSF0jDtWMy7eMFoREIa+dH54jLRbAtEd/+HJYayYIXfTuI7aAyxoYhsLaIx /6KzhxVVr20gGd42bQQo+fP4BDYwolkMiBalHr2egN0762gaOk1TrmanYxMbkL/hGWWcP9 H9vIqrX3lLN1wpnoIILvVQJRqjtln24= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294539; 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=I/0nrP3gEwMU8Fn4GtEZKqKPsnJhVzjLl9aZ9S6/OZg=; b=SVjaLSc+Qk0F3x37ufNyImVLYi7z8sovgvTcJbk0HtoNj2VBgjqeFYVOpop0iRxXfZ/bKO aAqc18JLN+ZD5FBA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=ErG1j74Z; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=SVjaLSc+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1751294539; 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=I/0nrP3gEwMU8Fn4GtEZKqKPsnJhVzjLl9aZ9S6/OZg=; b=ErG1j74ZDfTCSF0jDtWMy7eMFoREIa+dH54jLRbAtEd/+HJYayYIXfTuI7aAyxoYhsLaIx /6KzhxVVr20gGd42bQQo+fP4BDYwolkMiBalHr2egN0762gaOk1TrmanYxMbkL/hGWWcP9 H9vIqrX3lLN1wpnoIILvVQJRqjtln24= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1751294539; 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=I/0nrP3gEwMU8Fn4GtEZKqKPsnJhVzjLl9aZ9S6/OZg=; b=SVjaLSc+Qk0F3x37ufNyImVLYi7z8sovgvTcJbk0HtoNj2VBgjqeFYVOpop0iRxXfZ/bKO aAqc18JLN+ZD5FBA== 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 CE49E13A24; Mon, 30 Jun 2025 14:42: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 2PyqL0qiYmjqdAAAD6G6ig (envelope-from ); Mon, 30 Jun 2025 14:42:18 +0000 From: Oscar Salvador To: Andrew Morton Cc: David Hildenbrand , Muchun Song , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH v4 5/5] mm,hugetlb: drop unlikelys from hugetlb_fault Date: Mon, 30 Jun 2025 16:42:12 +0200 Message-ID: <20250630144212.156938-6-osalvador@suse.de> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250630144212.156938-1-osalvador@suse.de> References: <20250630144212.156938-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: 5268521169 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)[]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_COUNT_TWO(0.00)[2]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[8]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,suse.de:dkim,suse.de:email,imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,linux.dev:email]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from,2a07:de40:b281:106:10:150:64:167:received]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; R_RATELIMIT(0.00)[to_ip_from(RLfw9wkb71jz54yo3bnq4aib6i)]; DKIM_TRACE(0.00)[suse.de:+] 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. Link: https://lkml.kernel.org/r/20250627102904.107202-6-osalvador@suse.de Signed-off-by: Oscar Salvador Cc: David Hildenbrand Cc: Gavin Guo Cc: Muchun Song Cc: Peter Xu Signed-off-by: Andrew Morton Acked-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 ba078aa1cb96..fda6b748e985 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6747,7 +6747,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 @@ -6758,7 +6758,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.50.0