From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 C6AB14D990F for ; Thu, 9 Jul 2026 17:38:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783618719; cv=none; b=QMGOSwzUfeHfPdmNl0RHmYe81VyteK5xMCiF6fhfoQVRBdvjEhkJZDO+YNFX13FmD4trKRPKmDrazb2kmzLjiFWC7VtdtPNeFPlldrTwP+YHUvr5ijiWruoS3AluOPUXhaYFedQvswuCRUg7kV7bUKMe66OAkRHwjJ+Pt62Nz0U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783618719; c=relaxed/simple; bh=ywuzmPiGdanF29EFXW/F1Md61ZwqlT51M4CCiOLBTIU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nqJboUp00LeEbgUepAJaXbmgHM4dCx8JW012y5XguxeaENrsbjMvfnJJDVbnuGmxL0t2gR78AJvuzKi2aVlB+RBV4ivoT5cYDlWbV7j0ojY3IGsYHBrHMHb9qXRMKOfa8nmOx2IGfmZlS0ekvVVXojJ+Q1g8tVVVCTSPgjnEtuU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AjrtR2kE; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AjrtR2kE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B6DF1F000E9; Thu, 9 Jul 2026 17:38:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783618717; bh=n+qAC8+pWCAwtA9QFanjjmN8UXpbT10T8+2PuoaEVOE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=AjrtR2kEG7ScPH0YTa21S14aCTavvagxpns2YPuJtlQr3BafqUdylg6Sk4gPQ25xq MnP3JR+UVB7zLV91etQkiBEVgqok5MlxQQqYWhkMPs0trKm3qkCTq6TbHCkBRN0e9L 9bRx/uivF57NcfiCuz2UbRVrO0FgYtG9djsNJxH7G0gr/FcCc0SPxuAeFFz0bChbAz 0UibzXHjI3ol3jTdRrQdimsDN/qpzZKtZEsJ8cCTkKzwESMXs2afr+k7x21vCU5fLg t9RnF4UAxWIyD6HI/oENnMiw9FdipSAEhUpIHeFjznnXXnnIKFQgNDPJuwKLAwe0WA NhpnttEbo7Isw== From: Pratyush Yadav To: Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Alexander Graf , Muchun Song , Oscar Salvador , David Hildenbrand , Andrew Morton , Jason Miu , Jork Loeser Cc: kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 02/21] kho: make radix max key width more obvious Date: Thu, 9 Jul 2026 19:37:51 +0200 Message-ID: <20260709173821.429921-3-pratyush@kernel.org> X-Mailer: git-send-email 2.55.0.141.g00534a21ce-goog In-Reply-To: <20260709173821.429921-1-pratyush@kernel.org> References: <20260709173821.429921-1-pratyush@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: 8bit From: "Pratyush Yadav (Google)" The KHO radix tree constants are somewhat hard to understand. The tree depth essentially comes from the max key width. The max key width comes from the need to store a 52-bit PFN plus one more bit for the order. All this is very obscure with the corrent code. The PFN width is defined as KHO_ORDER_0_LOG2, which makes very little sense to a new reader not already familiar with what the value means. Then the fact that an extra bit is needed is hidden in the KHO_TREE_MAX_DEPTH calculation. Simplify this by removing KHO_ORDER_0_LOG2 and replace it with KHO_RADIX_KEY_WIDTH. Update the comment to explain why this value is used. This moves the +1 from KHO_TREE_MAX_DEPTH to KHO_RADIX_KEY_WIDTH, making things clearer. Update kho_{encode,decode}_radix_key() to not use KHO_ORDER_0_LOG2. Instead, refactor the code and comments to make it clearer how the encoding and decoding is done. In kho_encode_radix_key(), add a new variable for the shift for phys. Use that in calculating where the order bit goes and in calculating the shifted PFN. Update comments to make this clearer. In kho_radix_decode_key(), turn order_bit to 0-indexed to simplify the eventual calculation for order. Touch up comments to make the computation clearer. Signed-off-by: Pratyush Yadav (Google) --- include/linux/kho/abi/kexec_handover.h | 9 +++------ kernel/liveupdate/kexec_handover.c | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/linux/kho/abi/kexec_handover.h b/include/linux/kho/abi/kexec_handover.h index 5e2eb8519bda..2f4fb9c63942 100644 --- a/include/linux/kho/abi/kexec_handover.h +++ b/include/linux/kho/abi/kexec_handover.h @@ -257,11 +257,8 @@ struct kho_vmalloc { * memory. These constants govern the indexing, sizing, and depth of the tree. */ enum kho_radix_consts { - /* - * The bit position of the order bit (and also the length of the - * shifted physical address) for an order-0 page. - */ - KHO_ORDER_0_LOG2 = 64 - PAGE_SHIFT, + /* Need to store the PFN, plus one bit for order. */ + KHO_RADIX_KEY_WIDTH = 64 - PAGE_SHIFT + 1, /* Size of the table in kho_radix_node, in log2 */ KHO_TABLE_SIZE_LOG2 = const_ilog2(PAGE_SIZE / sizeof(phys_addr_t)), @@ -274,7 +271,7 @@ enum kho_radix_consts { * and 1 bitmap level. */ KHO_TREE_MAX_DEPTH = - DIV_ROUND_UP(KHO_ORDER_0_LOG2 - KHO_BITMAP_SIZE_LOG2 + 1, + DIV_ROUND_UP(KHO_RADIX_KEY_WIDTH - KHO_BITMAP_SIZE_LOG2, KHO_TABLE_SIZE_LOG2) + 1, }; diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c index 7349cc82f6dc..ea24f23ce292 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -97,10 +97,12 @@ static struct kho_out kho_out = { */ static unsigned long kho_encode_radix_key(phys_addr_t phys, unsigned int order) { - /* Order bits part */ - unsigned long h = 1UL << (KHO_ORDER_0_LOG2 - order); - /* Shifted physical address part */ - unsigned long l = phys >> (PAGE_SHIFT + order); + /* The physical address is encoded by shifting the PFN by its order. */ + unsigned long shift = PAGE_SHIFT + order; + /* Order bit goes right before the shifted PFN. */ + unsigned long h = 1UL << (64 - shift); + /* Shifted PFN. */ + unsigned long l = phys >> shift; return h | l; } @@ -118,12 +120,13 @@ static unsigned long kho_encode_radix_key(phys_addr_t phys, unsigned int order) */ static phys_addr_t kho_decode_radix_key(unsigned long key, unsigned int *order) { - unsigned int order_bit = fls64(key); + /* fls64() indexes starting from 1. */ + unsigned int order_bit = fls64(key) - 1; phys_addr_t phys; - /* order_bit is numbered starting at 1 from fls64 */ - *order = KHO_ORDER_0_LOG2 - order_bit + 1; - /* The order is discarded by the shift */ + /* order bit goes right before the shifted PFN. */ + *order = 64 - (PAGE_SHIFT + order_bit); + /* The order bit is discarded by the shift */ phys = key << (PAGE_SHIFT + *order); return phys; -- 2.55.0.141.g00534a21ce-goog