From mboxrd@z Thu Jan 1 00:00:00 1970 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 4990E389108 for ; Tue, 24 Mar 2026 15:43:56 +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=1774367037; cv=none; b=Y2zWPkDx+2YAuZZ0vRjUPR9EGNvLCILHn0KZuim9OTmPOwZr4cN6u8iBSqz053kHRC5zl7S6oQZlyzcKmxFqgM/M2aiTZg58whtGwcOBQleskABfRba9xutjth3nbiJ3IwOHakHUgLFyMHkWBffnF0tJGmehorQJHtDtcEoLPEk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774367037; c=relaxed/simple; bh=v/m05uzzP9tZJITYS6dftD0cyMV+AqQbullJcA9jeHU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=aTzLJkf6H0BVuP1AYfYe+UVZdch89jYFAMnnoc9SP/1dP1MrmjTznC0/1YbE2nuPGmIdWzUJwOMvDfaByD4R2TeDf2UYKDwsnJTGvlrjnVLDcWJOme/yFEvdwcaVqYlgf87Ib0l5PfL+c4u3ILt0dxP4Q1Nrz5o777h4epbzJYc= 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=bW8IdZEm; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=ZrthvwxJ; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=bW8IdZEm; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=ZrthvwxJ; 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="bW8IdZEm"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ZrthvwxJ"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="bW8IdZEm"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ZrthvwxJ" 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 5B25B5BD47; Tue, 24 Mar 2026 15:43:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1774367034; 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=xKf0tFZ8m3RzqmDEZLBOwfFpYbl6YfWlvEft7j5ZCD4=; b=bW8IdZEmAZm9qHpTj+bzSvwiRXvmbs6ZMHwfxNS2vRQgPbaIBrenmu/NWCFdBFIPZgKasn qmp+dXeZLThvDaoi459H0HtOpGa+NCbmmWSeEP5IewluHORdiEa0KFT6XZ0m2yigzLvawh DfQnQZ/3XF18lbKryq5ZI7q5aDDzLUU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1774367034; 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=xKf0tFZ8m3RzqmDEZLBOwfFpYbl6YfWlvEft7j5ZCD4=; b=ZrthvwxJOPYDUm4ikbaOrMth0kzJvDc24rtMeohRAqUXAz4/P1KNdEWdN1sf3jxA6Kn9xA LQb5nWSjM0sLn0Aw== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=bW8IdZEm; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=ZrthvwxJ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1774367034; 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=xKf0tFZ8m3RzqmDEZLBOwfFpYbl6YfWlvEft7j5ZCD4=; b=bW8IdZEmAZm9qHpTj+bzSvwiRXvmbs6ZMHwfxNS2vRQgPbaIBrenmu/NWCFdBFIPZgKasn qmp+dXeZLThvDaoi459H0HtOpGa+NCbmmWSeEP5IewluHORdiEa0KFT6XZ0m2yigzLvawh DfQnQZ/3XF18lbKryq5ZI7q5aDDzLUU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1774367034; 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=xKf0tFZ8m3RzqmDEZLBOwfFpYbl6YfWlvEft7j5ZCD4=; b=ZrthvwxJOPYDUm4ikbaOrMth0kzJvDc24rtMeohRAqUXAz4/P1KNdEWdN1sf3jxA6Kn9xA LQb5nWSjM0sLn0Aw== 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 8252243EF5; Tue, 24 Mar 2026 15:43:53 +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 dHF2HDmxwmnUNgAAD6G6ig (envelope-from ); Tue, 24 Mar 2026 15:43:53 +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 0/2] mm/mprotect: micro-optimization work Date: Tue, 24 Mar 2026 15:43:40 +0000 Message-ID: <20260324154342.156640-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-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org 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)[]; 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_RATELIMITED(0.00)[rspamd.com]; 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]; ARC_NA(0.00)[]; RCPT_COUNT_TWELVE(0.00)[12]; MIME_TRACE(0.00)[0:+]; URIBL_BLOCKED(0.00)[suse.de:dkim,suse.de:mid,arm.com:email,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_TRACE(0.00)[suse.de:+]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; TO_DN_SOME(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim,suse.de:mid,arm.com:email,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns] X-Rspamd-Queue-Id: 5B25B5BD47 X-Spam-Flag: NO X-Spam-Score: -3.01 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 73374 ns 73373 ns 9602 After the patchset we can observe a 14% 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. [0]: https://gist.github.com/heatd/1450d273005aba91fa5744f44dfcd933 Link: https://lore.kernel.org/all/aY8-XuFZ7zCvXulB@luyang-thinkpadp1gen7.toromso.csb/ 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 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 | 146 ++++++++++++++++++++++++++++---------------------- 1 file changed, 81 insertions(+), 65 deletions(-) -- 2.53.0