From nobody Fri Apr 3 14:41:01 2026 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 ECCBE1D9663 for ; Tue, 24 Mar 2026 15:44:07 +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=1774367050; cv=none; b=qi+sLZFJ8haJsrhre7eps3nvfPSGRM8kIgTW3xnBuhyimX25RUNvinv/lxNrv4zJ8ckryxFjy4uwZXAkOUhsUaNLkEsDpTtorq2E/molTTgvp4S7A/9e8kFicpmwr1ofDXv1nUlUvHTe/XYxLN5WHp9VUy06dj2F25LLTTZ84Wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774367050; c=relaxed/simple; bh=Fp/RFrrCaJtccnXbosMpqP0P4NMnh02H60zYO9oo7CQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tMtZ/g4anC2X73m+xHJdYeqTZfcAxwda/gU6N50zSt64/24dXprrq8fUEuOpr0T/MirGKaYK1cs5ZyegF6rENV4dkJiowvQEAKp+7sHJVva6MXGTAHXCVkVxMOSz0hvqL6gmi9GUN9QhfXQDJwdoedhZcSmeNo+kUV0kDycJ1UY= 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=V1WhEkWc; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=JFbqxGSe; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=NE0HPANI; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=hKLOksps; 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="V1WhEkWc"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="JFbqxGSe"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="NE0HPANI"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="hKLOksps" 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 2F3D55BD53; Tue, 24 Mar 2026 15:43:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1774367037; 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=hXvQifaGZb+FBFXUSPZxP9DOLUBRewc3OEREji2KlYo=; b=V1WhEkWcz8mKBQSQ0Vncn1mFQeVv5C+RqWsIzMSenal6c76cnS1AApeMxswpoP1vgni8Vo ydOTNiP10BGRx+6moRpjUF+GWAzgFtHHOn/rP81KQccsKfu0PZKV8ut3I1cS53AqPacZf3 w0sF2uqH72PFRYihdnXSI4GN4qElNII= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1774367037; 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=hXvQifaGZb+FBFXUSPZxP9DOLUBRewc3OEREji2KlYo=; b=JFbqxGSeuAJyjoHvWr4/NBqbCfq6eODPya7wohNBpS8I/3ZekRrvoDB2tZUDMzmAFaZje1 RQPpx4j5SZD5gZDg== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1774367036; 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=hXvQifaGZb+FBFXUSPZxP9DOLUBRewc3OEREji2KlYo=; b=NE0HPANIaJZz+ZWdbmFPMMnqRr2wz91rgEFEVe7D9MP6qypP0hNPtS0fwY0sg5inBBhn36 B2EoTaYDCryHyRO4A+IDWTpyfGMIAMC+6kpEcYW8/nWMeyxfl9n71Z+uJLTM5Bape/jVSt Zm0xBSI4h2TyVuXJHPHxn7PA2nG9le4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1774367036; 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=hXvQifaGZb+FBFXUSPZxP9DOLUBRewc3OEREji2KlYo=; b=hKLOkspsi0DrbIpnk9LaF1jaOJtwTHvDvAf0/I3QvJs2bBj8hnydR9oFcEaibzqvYpsuNG oAfF5ptICRWTleCw== 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 5DBEA43EF5; Tue, 24 Mar 2026 15:43:55 +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 UNSaEzuxwmnUNgAAD6G6ig (envelope-from ); Tue, 24 Mar 2026 15:43:55 +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 v2 2/2] mm/mprotect: special-case small folios when applying write permissions Date: Tue, 24 Mar 2026 15:43:42 +0000 Message-ID: <20260324154342.156640-3-pfalcato@suse.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260324154342.156640-1-pfalcato@suse.de> References: <20260324154342.156640-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 [-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)[-0.999]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_TWO(0.00)[2]; FUZZY_RATELIMITED(0.00)[rspamd.com]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_TWELVE(0.00)[12]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; URIBL_BLOCKED(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:mid,suse.de:email]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; TO_DN_SOME(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:mid,suse.de:email] X-Spam-Flag: NO X-Spam-Score: -2.80 X-Spam-Level: Content-Type: text/plain; charset="utf-8" The common order-0 case is important enough to want its own branch, and avoids the hairy, large loop logic that the CPU does not seem to handle particularly well. While at it, encourage the compiler to inline batch PTE logic and resolve constant branches by adding __always_inline strategically. Reviewed-by: Lorenzo Stoakes (Oracle) Signed-off-by: Pedro Falcato --- mm/mprotect.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 2eaf862e5734..2fda26107066 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -103,7 +103,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma= , unsigned long addr, return can_change_shared_pte_writable(vma, pte); } =20 -static int mprotect_folio_pte_batch(struct folio *folio, pte_t *ptep, +static __always_inline int mprotect_folio_pte_batch(struct folio *folio, p= te_t *ptep, pte_t pte, int max_nr_ptes, fpb_t flags) { /* No underlying folio, so cannot batch */ @@ -117,9 +117,9 @@ static int mprotect_folio_pte_batch(struct folio *folio= , pte_t *ptep, } =20 /* Set nr_ptes number of ptes, starting from idx */ -static void prot_commit_flush_ptes(struct vm_area_struct *vma, unsigned lo= ng addr, - pte_t *ptep, pte_t oldpte, pte_t ptent, int nr_ptes, - int idx, bool set_write, struct mmu_gather *tlb) +static __always_inline void prot_commit_flush_ptes(struct vm_area_struct *= vma, + unsigned long addr, pte_t *ptep, pte_t oldpte, pte_t ptent, + int nr_ptes, int idx, bool set_write, struct mmu_gather *tlb) { /* * Advance the position in the batch by idx; note that if idx > 0, @@ -169,7 +169,7 @@ static int page_anon_exclusive_sub_batch(int start_idx,= int max_len, * pte of the batch. Therefore, we must individually check all pages and * retrieve sub-batches. */ -static void commit_anon_folio_batch(struct vm_area_struct *vma, +static __always_inline void commit_anon_folio_batch(struct vm_area_struct = *vma, struct folio *folio, struct page *first_page, unsigned long addr, pte_t = *ptep, pte_t oldpte, pte_t ptent, int nr_ptes, struct mmu_gather *tlb) { @@ -177,6 +177,13 @@ static void commit_anon_folio_batch(struct vm_area_str= uct *vma, int sub_batch_idx =3D 0; int len; =20 + /* Optimize for the common order-0 case. */ + if (likely(nr_ptes =3D=3D 1)) { + prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, 1, + 0, PageAnonExclusive(first_page), tlb); + return; + } + while (nr_ptes) { expected_anon_exclusive =3D PageAnonExclusive(first_page + sub_batch_idx= ); len =3D page_anon_exclusive_sub_batch(sub_batch_idx, nr_ptes, --=20 2.53.0