From nobody Mon Apr 6 10:45:07 2026 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 199A8254B18 for ; Thu, 19 Mar 2026 18:31:23 +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=1773945085; cv=none; b=NAKOWaTwLb3kVNPVZ3dZRvs7ArdtH22dtOeUrpLdrcp5RlCEw0gdTIilo70xCsiTCNk0AR5AXT3yPhv6qjihwnFDoUMJFgQy5FqLWoygb1g5a28dK2vSu2wPh5TXAPa3RquzpegJ4TTzThiPdSzlCALC/2z60JT1jqLzmvgVBK8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773945085; c=relaxed/simple; bh=mGGbC/AaH2Nw4nAbmk3HBXTk7XDsPqvns4V+yqIbHBo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T5TE8nLpDY+i3HXcjXd0ibAyJPew3lmqHc6i0iPtUzu/Hn3Xd9kN74QDaNv5DQBDKJKUK2JaEXS3BmxHoJC9zsEyW5t/Ljnk/swjBeA2qa64hVq61ahuyzOAEcE4wbjaE/uuffwOtIF2KyTujYH2bVhGdJldsIhPnEV25pmgAzw= 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=1yLuBo1u; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=FNsKTwXF; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=1yLuBo1u; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=FNsKTwXF; 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="1yLuBo1u"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="FNsKTwXF"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="1yLuBo1u"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="FNsKTwXF" 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-out1.suse.de (Postfix) with ESMTPS id 5D4954D214; Thu, 19 Mar 2026 18:31:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1773945082; 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=I4EDCTugoDpsygL/2msKaEtTb3dy6BTNe9WE3o1d0jk=; b=1yLuBo1uPsF7z6th72+dt19GsYUozcSNQDwQVvNoEUIO8xlkCQjkp4RfxmrQrL6tkawFkZ AbNhIEKyfDz+iyh/bBt1sqYI6gIeH05abz4Fj/osp0e56aU6fU7rX7nOZA7IaaUy75NGLv WVuGhHCKjYx1IRqIB/h+KfVVMkRPQuo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1773945082; 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=I4EDCTugoDpsygL/2msKaEtTb3dy6BTNe9WE3o1d0jk=; b=FNsKTwXFm1HxfV7fsG9TwBtg5wzBro/64pwQoXbQJMxkCYqM2xBbjAlw8C1Sl35kwD1QpK rhazKeGrVL87ZmAQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1773945082; 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=I4EDCTugoDpsygL/2msKaEtTb3dy6BTNe9WE3o1d0jk=; b=1yLuBo1uPsF7z6th72+dt19GsYUozcSNQDwQVvNoEUIO8xlkCQjkp4RfxmrQrL6tkawFkZ AbNhIEKyfDz+iyh/bBt1sqYI6gIeH05abz4Fj/osp0e56aU6fU7rX7nOZA7IaaUy75NGLv WVuGhHCKjYx1IRqIB/h+KfVVMkRPQuo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1773945082; 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=I4EDCTugoDpsygL/2msKaEtTb3dy6BTNe9WE3o1d0jk=; b=FNsKTwXFm1HxfV7fsG9TwBtg5wzBro/64pwQoXbQJMxkCYqM2xBbjAlw8C1Sl35kwD1QpK rhazKeGrVL87ZmAQ== 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 7B5294273B; Thu, 19 Mar 2026 18:31:21 +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 6EKSGvlAvGmbdgAAD6G6ig (envelope-from ); Thu, 19 Mar 2026 18:31:21 +0000 From: Pedro Falcato To: Andrew Morton , "Liam R. Howlett" , Lorenzo Stoakes Cc: Pedro Falcato , Vlastimil Babka , Jann Horn , David Hildenbrand , Dev Jain , Luke Yang , jhladky@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] mm/mprotect: move softleaf code out of the main function Date: Thu, 19 Mar 2026 18:31:06 +0000 Message-ID: <20260319183108.1105090-3-pfalcato@suse.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260319183108.1105090-1-pfalcato@suse.de> References: <20260319183108.1105090-1-pfalcato@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 [-6.80 / 50.00]; REPLY(-4.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)[-0.997]; MIME_GOOD(-0.10)[text/plain]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:mid,suse.de:email]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCPT_COUNT_TWELVE(0.00)[12]; RCVD_VIA_SMTP_AUTH(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; R_RATELIMIT(0.00)[to_ip_from(RL8d8xedm6iu8o66torxsk6bwd)]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-Spam-Score: -6.80 X-Spam-Level: Content-Type: text/plain; charset="utf-8" Move softleaf change_pte_range code into a separate function. This makes the change_pte_range() function (or where it inlines) a good bit smaller. Plus it lessens cognitive load when reading through the function. Signed-off-by: Pedro Falcato Reviewed-by: Lorenzo Stoakes (Oracle) --- mm/mprotect.c | 128 +++++++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 60 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 1bd0d4aa07c2..8d4fa38a8a26 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -211,6 +211,73 @@ static void set_write_prot_commit_flush_ptes(struct vm= _area_struct *vma, commit_anon_folio_batch(vma, folio, page, addr, ptep, oldpte, ptent, nr_p= tes, tlb); } =20 +static noinline long change_pte_softleaf(struct vm_area_struct *vma, + unsigned long addr, pte_t *pte, pte_t oldpte, unsigned long cp_flags) +{ + bool uffd_wp =3D cp_flags & MM_CP_UFFD_WP; + bool uffd_wp_resolve =3D cp_flags & MM_CP_UFFD_WP_RESOLVE; + softleaf_t entry =3D softleaf_from_pte(oldpte); + pte_t newpte; + + if (softleaf_is_migration_write(entry)) { + const struct folio *folio =3D softleaf_to_folio(entry); + + /* + * A protection check is difficult so + * just be safe and disable write + */ + if (folio_test_anon(folio)) + entry =3D make_readable_exclusive_migration_entry( + swp_offset(entry)); + else + entry =3D make_readable_migration_entry(swp_offset(entry)); + newpte =3D swp_entry_to_pte(entry); + if (pte_swp_soft_dirty(oldpte)) + newpte =3D pte_swp_mksoft_dirty(newpte); + } else if (softleaf_is_device_private_write(entry)) { + /* + * We do not preserve soft-dirtiness. See + * copy_nonpresent_pte() for explanation. + */ + entry =3D make_readable_device_private_entry( + swp_offset(entry)); + newpte =3D swp_entry_to_pte(entry); + if (pte_swp_uffd_wp(oldpte)) + newpte =3D pte_swp_mkuffd_wp(newpte); + } else if (softleaf_is_marker(entry)) { + /* + * Ignore error swap entries unconditionally, + * because any access should sigbus/sigsegv + * anyway. + */ + if (softleaf_is_poison_marker(entry) || + softleaf_is_guard_marker(entry)) + return 0; + /* + * If this is uffd-wp pte marker and we'd like + * to unprotect it, drop it; the next page + * fault will trigger without uffd trapping. + */ + if (uffd_wp_resolve) { + pte_clear(vma->vm_mm, addr, pte); + return 1; + } + } else { + newpte =3D oldpte; + } + + if (uffd_wp) + newpte =3D pte_swp_mkuffd_wp(newpte); + else if (uffd_wp_resolve) + newpte =3D pte_swp_clear_uffd_wp(newpte); + + if (!pte_same(oldpte, newpte)) { + set_pte_at(vma->vm_mm, addr, pte, newpte); + return 1; + } + return 0; +} + static long change_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, pgprot_t newprot, unsigned long cp_flags) @@ -317,66 +384,7 @@ static long change_pte_range(struct mmu_gather *tlb, pages++; } } else { - softleaf_t entry =3D softleaf_from_pte(oldpte); - pte_t newpte; - - if (softleaf_is_migration_write(entry)) { - const struct folio *folio =3D softleaf_to_folio(entry); - - /* - * A protection check is difficult so - * just be safe and disable write - */ - if (folio_test_anon(folio)) - entry =3D make_readable_exclusive_migration_entry( - swp_offset(entry)); - else - entry =3D make_readable_migration_entry(swp_offset(entry)); - newpte =3D swp_entry_to_pte(entry); - if (pte_swp_soft_dirty(oldpte)) - newpte =3D pte_swp_mksoft_dirty(newpte); - } else if (softleaf_is_device_private_write(entry)) { - /* - * We do not preserve soft-dirtiness. See - * copy_nonpresent_pte() for explanation. - */ - entry =3D make_readable_device_private_entry( - swp_offset(entry)); - newpte =3D swp_entry_to_pte(entry); - if (pte_swp_uffd_wp(oldpte)) - newpte =3D pte_swp_mkuffd_wp(newpte); - } else if (softleaf_is_marker(entry)) { - /* - * Ignore error swap entries unconditionally, - * because any access should sigbus/sigsegv - * anyway. - */ - if (softleaf_is_poison_marker(entry) || - softleaf_is_guard_marker(entry)) - continue; - /* - * If this is uffd-wp pte marker and we'd like - * to unprotect it, drop it; the next page - * fault will trigger without uffd trapping. - */ - if (uffd_wp_resolve) { - pte_clear(vma->vm_mm, addr, pte); - pages++; - } - continue; - } else { - newpte =3D oldpte; - } - - if (uffd_wp) - newpte =3D pte_swp_mkuffd_wp(newpte); - else if (uffd_wp_resolve) - newpte =3D pte_swp_clear_uffd_wp(newpte); - - if (!pte_same(oldpte, newpte)) { - set_pte_at(vma->vm_mm, addr, pte, newpte); - pages++; - } + pages +=3D change_pte_softleaf(vma, addr, pte, oldpte, cp_flags); } } while (pte +=3D nr_ptes, addr +=3D nr_ptes * PAGE_SIZE, addr !=3D end); lazy_mmu_mode_disable(); --=20 2.53.0