From nobody Sat Feb 7 09:48:01 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2248360723 for ; Wed, 28 Jan 2026 13:57:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769608632; cv=none; b=Njz4Dhxv5jua6jTLqgp5/CgQBgmiwQevfpMpNPxsjaieh+7Rc1KnO8TLiucjmth2tjSz6AVJvp/nKoMtPpLynlWduT5OqPpo7SMxlZ/8JpBFort3GPHJ4r/1yBrcx5i6H5kXffiieUjKlwgmL8GnRfQfzfbrT6K1Z5YuK8J8N+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769608632; c=relaxed/simple; bh=hHQLWMhahxCY/meKke54BXwjoj9QXEZAW+ce8KhMydU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=elTEvHSQdpwv4ySBB+emHWaVSrOOBQYnm+/GVEWrap4ZnuPYaOMfwY/iHdwTLUlxvj67zeWQ8BG2ELWPS6kpN+MiDawJg3OzoTNcLBFJKJzBz7fVG+49v06MczdO9sKUZtpveg0v3jwYyVcTw2pLgF7a6KMr2gKnR3/7/DocyXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cTZQ6R98; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cTZQ6R98" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AAA0C4AF09; Wed, 28 Jan 2026 13:57:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769608632; bh=hHQLWMhahxCY/meKke54BXwjoj9QXEZAW+ce8KhMydU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cTZQ6R983vdQ23cB680GoXRU6UtfvRZtA5nJ91sXhdrW2W07PXFBFkMNGrbDSGwQh 7FPOdS4mWFiijQTd5baeR/XRUZZHE2b86en9jJejM/AsJVuTsswcIfh+a8XMN/pYzt aua6hs79qeX8tbDWq8U+kIDtCkE7JL26HpWnpa6hdB2h2znOQgtOIV3a71P9C14JfR CtV4U0UeyULoxG6svBRDnv5PfU4rnKCh22hvtOkXzLHv+2IaH9THWgT4ThQQdXpBeF 4TYkCvAyoFD5vp1Y5Wp7EZfkpXRIOs3Fu9p7N4BrUPuIwov910Ddz7GpkZnTtI5R67 NdQ9QQNGQ2GQQ== Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id 4D1C8F40068; Wed, 28 Jan 2026 08:57:11 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Wed, 28 Jan 2026 08:57:11 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgdduieefheegucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepmfhirhihlhcu ufhhuhhtshgvmhgruhcuoehkrghssehkvghrnhgvlhdrohhrgheqnecuggftrfgrthhtvg hrnhephfdufeejhefhkedtuedvfeevjeffvdfhvedtudfgudffjeefieekleehvdetvdev necuvehluhhsthgvrhfuihiivgepgeenucfrrghrrghmpehmrghilhhfrhhomhepkhhirh hilhhlodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdduieduudeivdeiheeh qddvkeeggeegjedvkedqkhgrsheppehkvghrnhgvlhdrohhrghesshhhuhhtvghmohhvrd hnrghmvgdpnhgspghrtghpthhtohepvdekpdhmohguvgepshhmthhpohhuthdprhgtphht thhopegrkhhpmheslhhinhhugidqfhhouhhnuggrthhiohhnrdhorhhgpdhrtghpthhtoh epmhhutghhuhhnrdhsohhngheslhhinhhugidruggvvhdprhgtphhtthhopegurghvihgu sehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhllhihsehinhhfrhgruggvrggurd horhhgpdhrtghpthhtohepuhhsrghmrggrrhhifheigedvsehgmhgrihhlrdgtohhmpdhr tghpthhtohepfhhvughlsehgohhoghhlvgdrtghomhdprhgtphhtthhopehoshgrlhhvrg guohhrsehsuhhsvgdruggvpdhrtghpthhtoheprhhpphhtsehkvghrnhgvlhdrohhrghdp rhgtphhtthhopehvsggrsghkrgesshhushgvrdgtii X-ME-Proxy: Feedback-ID: i10464835:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 28 Jan 2026 08:57:09 -0500 (EST) From: Kiryl Shutsemau To: Andrew Morton , Muchun Song , David Hildenbrand , Matthew Wilcox , Usama Arif , Frank van der Linden Cc: Oscar Salvador , Mike Rapoport , Vlastimil Babka , Lorenzo Stoakes , Zi Yan , Baoquan He , Michal Hocko , Johannes Weiner , Jonathan Corbet , Huacai Chen , WANG Xuerui , Palmer Dabbelt , Paul Walmsley , Albert Ou , Alexandre Ghiti , kernel-team@meta.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, Kiryl Shutsemau Subject: [PATCHv5 15/17] mm: Remove the branch from compound_head() Date: Wed, 28 Jan 2026 13:54:56 +0000 Message-ID: <20260128135500.22121-16-kas@kernel.org> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260128135500.22121-1-kas@kernel.org> References: <20260128135500.22121-1-kas@kernel.org> 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 Content-Type: text/plain; charset="utf-8" The compound_head() function is a hot path. For example, the zap path calls it for every leaf page table entry. Rewrite the helper function in a branchless manner to eliminate the risk of CPU branch misprediction. Signed-off-by: Kiryl Shutsemau Reviewed-by: Muchun Song Reviewed-by: Zi Yan --- include/linux/page-flags.h | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 1aaa604f4b9b..16384cb6f962 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -224,25 +224,32 @@ static __always_inline bool compound_info_has_mask(vo= id) static __always_inline unsigned long _compound_head(const struct page *pag= e) { unsigned long info =3D READ_ONCE(page->compound_info); + unsigned long mask; + + if (!compound_info_has_mask()) { + /* Bit 0 encodes PageTail() */ + if (info & 1) + return info - 1; =20 - /* Bit 0 encodes PageTail() */ - if (!(info & 1)) return (unsigned long)page; - - /* - * If compound_info_has_mask() is false, the rest of compound_info is - * the pointer to the head page. - */ - if (!compound_info_has_mask()) - return info - 1; + } =20 /* * If compoun_info_has_mask() is true the rest of the info encodes * the mask that converts the address of the tail page to the head page. * * No need to clear bit 0 in the mask as 'page' always has it clear. + * + * Let's do it in a branchless manner. */ - return (unsigned long)page & info; + + /* Non-tail: -1UL, Tail: 0 */ + mask =3D (info & 1) - 1; + + /* Non-tail: -1UL, Tail: info */ + mask |=3D info; + + return (unsigned long)page & mask; } =20 #define compound_head(page) ((typeof(page))_compound_head(page)) --=20 2.51.2