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 EFE8C3EB812 for ; Thu, 2 Apr 2026 14:16:48 +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=1775139410; cv=none; b=pBru0o/gTHGMWGyGytM2LqAtXl4adW86yVEcZmKcYQJD1zbtT7GBt+7/SWmVOqbpX2kPpS8FYRNE/3240/dru32LR5Jo+tTx077A2llHAOneJSkbwMAUp2EEWxpa3rFgonX9B5fH46lvWWmiGje9kgcwcEE/Y/ski4t2hb3WDUY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775139410; c=relaxed/simple; bh=2Vk3c7BG6KfQH4Y9nqmnjkgn5QkA2P2rZYyS1623oPM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=eXR3JfHS25UeFQeVz/ukwibT85C20KjmFJwXflBVfji5q2RMlivTy+cCHksc/jyNn18ZA8nPp3lJq31RPUqf26MiGAQY2tdjTihmZQzRsKbTFHOnMA5db2wytLrUrA5SVf8/Uhb27ddmzdMZOY80xxNH/tZOnlluM1jxlSrTLjc= 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=Up9f0WZi; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=HknQF3Mg; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Up9f0WZi; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=HknQF3Mg; 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="Up9f0WZi"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="HknQF3Mg"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Up9f0WZi"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="HknQF3Mg" 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 0CD164D343; Thu, 2 Apr 2026 14:16:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1775139407; 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; bh=lHVLkHx+G9AsEmcMTWgCDVpEt0qDNniMJG3ECVycNqs=; b=Up9f0WZiI3gHZ8dp9MBHnQOCMzVN1+h2CKw6gR3zG870b81TYNxP9Su/AQ2nC9HpLbpRT/ WMwYKrEiD37v6RIe6/hereAXib8imy8UQOJrKMiQQU5OebWhWTbuQoeT+uoci2ZvcYx4rR Cs4Oy/v27AY47m8yP4KS6Fcw27Rzleo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1775139407; 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; bh=lHVLkHx+G9AsEmcMTWgCDVpEt0qDNniMJG3ECVycNqs=; b=HknQF3MgIvLV7zW68xhdn5QsluwcttaFwUQPqNxPki7pbpXvcUbBXaOuh71L0OcIMMGh6n cYIqQ8PP90asX7DA== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1775139407; 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; bh=lHVLkHx+G9AsEmcMTWgCDVpEt0qDNniMJG3ECVycNqs=; b=Up9f0WZiI3gHZ8dp9MBHnQOCMzVN1+h2CKw6gR3zG870b81TYNxP9Su/AQ2nC9HpLbpRT/ WMwYKrEiD37v6RIe6/hereAXib8imy8UQOJrKMiQQU5OebWhWTbuQoeT+uoci2ZvcYx4rR Cs4Oy/v27AY47m8yP4KS6Fcw27Rzleo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1775139407; 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; bh=lHVLkHx+G9AsEmcMTWgCDVpEt0qDNniMJG3ECVycNqs=; b=HknQF3MgIvLV7zW68xhdn5QsluwcttaFwUQPqNxPki7pbpXvcUbBXaOuh71L0OcIMMGh6n cYIqQ8PP90asX7DA== 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 337164A0B0; Thu, 2 Apr 2026 14:16:46 +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 NTMuCU56zmlQDQAAD6G6ig (envelope-from ); Thu, 02 Apr 2026 14:16:46 +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 v3 0/2] mm/mprotect: micro-optimization work Date: Thu, 2 Apr 2026 15:16:26 +0100 Message-ID: <20260402141628.3367596-1-pfalcato@suse.de> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Result: default: False [-2.80 / 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]; MIME_GOOD(-0.10)[text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_TWELVE(0.00)[12]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,imap1.dmz-prg2.suse.org:helo]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FUZZY_RATELIMITED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-Spam-Score: -2.80 X-Spam-Level: Micro-optimize the change_protection functionality and the change_pte_range() routine. This set of functions works in an incredibly tight loop, and even small inefficiencies are incredibly evident when spun hundreds, thousands or hundreds of thousands of times. There was an attempt to keep the batching functionality as much as possible, which introduced some part of the slowness, but not all of it. Removing it for !arm64 architectures would speed mprotect() up even further, but could easily pessimize cases where large folios are mapped (which is not as rare as it seems, particularly when it comes to the page cache these days). The micro-benchmark used for the tests was [0] (usable using google/benchmark and g++ -O2 -lbenchmark repro.cpp) This resulted in the following (first entry is baseline): --------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------- mprotect_bench 85967 ns 85967 ns 6935 mprotect_bench 70684 ns 70684 ns 9887 After the patchset we can observe an ~18% speedup in mprotect. Wonderful for the elusive mprotect-based workloads! Testing & more ideas welcome. I suspect there is plenty of improvement possible but it would require more time than what I have on my hands right now. The entire inlined function (which inlines into change_protection()) is gigantic - I'm not surprised this is so finnicky. Note: per my profiling, the next _big_ bottleneck here is modify_prot_start_ptes, exactly on the xchg() done by x86. ptep_get_and_clear() is _expensive_. I don't think there's a properly safe way to go about it since we do depend on the D bit quite a lot. This might not be such an issue on other architectures. Luke Yang reported [1]: : On average, we see improvements ranging from a minimum of 5% to a : maximum of 55%, with most improvements showing around a 25% speed up in : the libmicro/mprot_tw4m micro benchmark. Link: https://lore.kernel.org/all/aY8-XuFZ7zCvXulB@luyang-thinkpadp1gen7.toromso.csb/ Link: https://gist.github.com/heatd/1450d273005aba91fa5744f44dfcd933 [0] Link: https://lkml.kernel.org/r/CAL2CeBxT4jtJ+LxYb6=BNxNMGinpgD_HYH5gGxOP-45Q2OncqQ@mail.gmail.com [1] Cc: Vlastimil Babka Cc: Jann Horn Cc: David Hildenbrand Cc: Dev Jain Cc: Luke Yang Cc: jhladky@redhat.com Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org v3: - Collapse a few lines into a single line in patch 1 (David) - Bring the inlining to a higher level (David) - Pick up David's patch 1 ACK (thank you!) - Pick up Luke Yang's Tested-by (thank you!) - Add Luke's results and akpmify the Links: a bit (cover letter) v2: - Addressed Sashiko's concerns - Picked up Lorenzo's R-b's (thank you!) - Squashed patch 1 and 4 into a single one (David) - Renamed the softleaf leaf function (David) - Dropped controversial noinlines & patch 3 (Lorenzo & David) v1: https://lore.kernel.org/linux-mm/20260319183108.1105090-1-pfalcato@suse.de/ Pedro Falcato (2): mm/mprotect: move softleaf code out of the main function mm/mprotect: special-case small folios when applying write permissions mm/mprotect.c | 218 ++++++++++++++++++++++++++++---------------------- 1 file changed, 124 insertions(+), 94 deletions(-) -- 2.53.0