Add support for boot-time caching
Subsystems (3)
cover Add support for boot-time caching
This patch series provides a simple key-value cache for storing boot-time configuration data. The goal is to allow kernel components and drivers to cache time-consuming-to-compute values during boot, enabling faster subsequent boots by avoiding redundant initialization work.
During kernel boot, many subsystems perform expensive operations like:
- Hardware detection and configuration
- Performance tuning calculations
- Resource allocation decisions
- Driver initialization parameters
These operations often produce the same results across boots on the same hardware, making them good candidates for caching.
---
Andrew Murray (1):
crypto: use bootcache to cache fastest algorithm
Marc Kelly (3):
base: bootcache: Add bootcache test backend
base: bootcache: Add bootcache memory backend
dt-bindings: bootcache: Add bindings for bootcache backend
acampanella-thegoodpenguin (2):
base: bootcache: initial commit
raid6: Add bootcache
.../bootcache/linux,bootcache-backend-memory.yaml | 67 +++++++ MAINTAINERS | 9 + crypto/xor.c | 29 ++- drivers/base/Kconfig | 32 +++ drivers/base/Makefile | 3 + drivers/base/bootcache.c | 179 +++++++++++++++++ drivers/base/bootcache_backend_memory.c | 220 +++++++++++++++++++++ drivers/base/bootcache_backend_test.c | 119 +++++++++++ include/linux/bootcache.h | 219 ++++++++++++++++++++ lib/raid6/algos.c | 21 ++ 10 files changed, 897 insertions(+), 1 deletion(-)
---
base-commit: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9
change-id: 20250915-bootcache-1bcf0ee0d7b0
Best regards,
Signature · 1 line
acampanella-thegoodpenguin <[email protected]>
RFC PATCH 1/6 base: bootcache: initial commit
bootcache provides boot-time key-value cache to help improve boot performance by allowing drivers to cache expensive computations.
Simple API are provided:
- bootcache_get_u16()/bootcache_set_u16() - retrieve/store u16 values
- bootcache_get_u32()/bootcache_set_u32() - retrieve/store u32 values
- bootcache_get_u64()/bootcache_set_u64() - retrieve/store u64 values
- bootcache_get_string()/bootcache_set_string() - retrieve/store strings
- bootcache_register_backend() - Backend registration
- bootcache_add_entry() - Add cache entry into framework
--- MAINTAINERS | 6 ++ drivers/base/Kconfig | 11 +++ drivers/base/Makefile | 1 + drivers/base/bootcache.c | 179 +++++++++++++++++++++++++++++++++++++ include/linux/bootcache.h | 219 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 416 insertions(+)
MAINTAINERS +6 -0
@@ -4418,6 +4418,12 @@ S: Maintained F: Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml F: drivers/iio/imu/bmi323/ +BOOT CACHE +M: Andrea Campanella <[email protected]> +S: Maintained +F: drivers/base/bootcache.c +F: include/linux/bootcache.h + BPF JIT for ARC M: Shahab Vahedi <[email protected]> L: [email protected]
drivers/base/Kconfig +11 -0
@@ -73,6 +73,17 @@ config DEVTMPFS_SAFE with the PROT_EXEC flag. This can break, for example, non-KMS video drivers. +config BOOTCACHE + bool "Boot-time cache for the kernel" + help + Enable a simple key-value cache subsystem for storing boot-time + configuration data. This allows drivers and kernel subsystems to + cache expensive computations during boot, potentially improving + boot performance on subsequent reboots by avoiding redundant + hardware detection and initialization work + + If unsure, say N. + config STANDALONE bool "Select only drivers that don't need compile-time external firmware" default y
drivers/base/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ topology.o container.o property.o cacheinfo.o \ swnode.o faux.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o +obj-$(CONFIG_BOOTCACHE) += bootcache.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.o
drivers/base/bootcache.c new file +179 -0
179 lines not on this page · raw message
include/linux/bootcache.h new file +219 -0
219 lines not on this page · raw message
Signature · 1 line
2.48.1
RE: [PATCH PREVIEW RFC 1/6] base: bootcache: initial commit
Quoted 355 lines
-----Original Message-----
From: acampanella-thegoodpenguin <[email protected]>bootcache provides boot-time key-value cache to help improve boot performance by allowing drivers to cache expensive computations.
Simple API are provided:
- bootcache_get_u16()/bootcache_set_u16() - retrieve/store u16 values
- bootcache_get_u32()/bootcache_set_u32() - retrieve/store u32 values
- bootcache_get_u64()/bootcache_set_u64() - retrieve/store u64 values
- bootcache_get_string()/bootcache_set_string() - retrieve/store strings
-bootcache_register_backend()- Backend registration
-bootcache_add_entry()- Add cache entry into frameworkSigned-off-by Marc Kelly <[email protected]>Signed-off-by Andrea Campanella <[email protected]>--- MAINTAINERS | 6 ++ drivers/base/Kconfig | 11 +++ drivers/base/Makefile | 1 + drivers/base/bootcache.c | 179 +++++++++++++++++++++++++++++++++++++ include/linux/bootcache.h | 219 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 416 insertions(+)MAINTAINERS +6
-0@@ -4418,6 +4418,12 @@ S: Maintained F: Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml F: drivers/iio/imu/bmi323/ +BOOT CACHE +M: Andrea Campanella <[email protected]> +S: Maintained +F: drivers/base/bootcache.c +F: include/linux/bootcache.h + BPF JIT for ARC M: Shahab Vahedi <[email protected]> L: [email protected]drivers/base/Kconfig +11
-0@@ -73,6 +73,17 @@ config DEVTMPFS_SAFE with the PROT_EXEC flag. This can break, for example, non-KMS video drivers. +config BOOTCACHE + bool "Boot-time cache for the kernel" + help + Enable a simple key-value cache subsystem for storing boot-time + configuration data. This allows drivers and kernel subsystems to + cache expensive computations during boot, potentially improving + boot performance on subsequent reboots by avoiding redundant + hardware detection and initialization work + + If unsure, say N. + config STANDALONE bool "Select only drivers that don't need compile-time external firmware" default ydrivers/base/Makefile +1
-0@@ -8,6 +8,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ topology.o container.o property.o cacheinfo.o \ swnode.o faux.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o +obj-$(CONFIG_BOOTCACHE) += bootcache.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.odrivers/base/bootcache.c new file +179
-0@@ -0,0 +1,179 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/hashtable.h> +#include <linux/string.h> +#include <linux/stringhash.h> +#include <linux/types.h> +#include <linux/spinlock.h> +#include <linux/errno.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> +#include <linux/bootcache.h> + +static DEFINE_HASHTABLE(bootcache_table, BOOTCACHE_HASH_BITS); +static DEFINE_SPINLOCK (bootcache_lock); +static struct kobject *bootcache_kobj; +static struct bootcache_info *bootcache_backend; +static bool bootcache_initilized; + +int bootcache_register_backend (struct bootcache_info *bci) +{ +int ret; + +if (!bci) +return -EINVAL ; +if (!bci->name) +return -EINVAL ; + +/* If we're not ready, tell backend to try again later */ +if (!bootcache_initilized) +return -EPROBE_DEFER ; + +if (bootcache_backend) { +pr_warn ("bootcache: Backend '%s' is already registered, cannot register '%s'\n" , + bootcache_backend->name, bci->name); +return -EBUSY ; + } +pr_info ("bootcache: Registering backend '%s'\n" , + bci->name); + +/* Have the backend load and populate the cache store */ + ret = bci->load_cache (); + +if (ret) + goto failed_initilize; + + bootcache_backend = bci; +return 0 ; + +failed_initilize: +return ret; +} +EXPORT_SYMBOL (bootcache_register_backend); + +int bootcache_get (const char *name,void *buf,size_t *len) +{ +struct bootcache_entry *entry; +u32 hash; +int ret = -ENOENT ; + +if (!name || !buf || !len) +return -EINVAL ; + + hash =full_name_hash (NULL , name,strlen (name)); + +spin_lock (&bootcache_lock); +hash_for_each_possible (bootcache_table, entry, node, hash) { +if (strcmp (entry->key, name) ==0 ) { +if (*len < entry->len) { + *len = entry->len; + ret = -ENOSPC ; + goto unlock; + } +memcpy (buf, entry->data, entry->len); + *len = entry->len; + ret =0 ; + goto unlock; + } + } + +unlock: +spin_unlock (&bootcache_lock); +return ret; +} +EXPORT_SYMBOL (bootcache_get); + +int bootcache_add_entry (struct bootcache_entry *entry) +{ +u32 hash; +struct bootcache_entry *existing_entry; +int ret =0 ; + + hash =full_name_hash (NULL , entry->key,strlen (entry->key)); + +spin_lock (&bootcache_lock); + +hash_for_each_possible (bootcache_table, existing_entry, node, hash) { +if (strcmp (existing_entry->key, entry->key) ==0 ) { + ret = -EEXIST ;// Key already exists + goto unlock; + } + } + +hash_add (bootcache_table, &entry->node, hash); + +unlock: +spin_unlock (&bootcache_lock); +return ret; +} +EXPORT_SYMBOL (bootcache_add_entry); + +int bootcache_set (const char *name,const void *data,size_t len) +{ +struct bootcache_entry *new_entry; +u32 hash; +int ret =0 ; + +if (!name || !data || !len) +return -EINVAL ; + + new_entry =kzalloc (sizeof (*new_entry),GFP_KERNEL ); +if (!new_entry) +return -ENOMEM ; + + new_entry->key =kstrdup (name,GFP_KERNEL ); +if (!new_entry->key) { + ret = -ENOMEM ; + goto free; + } + + new_entry->data =kmemdup (data, len,GFP_KERNEL ); +if (!new_entry->data) { + ret = -ENOMEM ; + goto free; + } + + new_entry->len = len; + ret =bootcache_add_entry (new_entry); +if (!ret) +return 0 ; + +free: +kfree (new_entry->data); +kfree (new_entry->key); +kfree (new_entry); +return ret; +} +EXPORT_SYMBOL (bootcache_set); + +static ssize_t writeout_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t count) +{ +/*Implement persistent storage backend */ +return count; +} + +static struct kobj_attribute writeout_attr =__ATTR_WO (writeout); + +static int __init bootcache_init (void ) +{ +int ret; + +pr_info ("bootcache: backend loaded\n" ); + +/* Create /sys/kernel/bootcache/writeout */ + bootcache_kobj =kobject_create_and_add ("bootcache" , kernel_kobj); +if (!bootcache_kobj) +return -ENOMEM ; + + ret =sysfs_create_file (bootcache_kobj, &writeout_attr.attr); +if (ret) { +kobject_put (bootcache_kobj); +return ret; + } + bootcache_initilized = true; +return 0 ; +} +core_initcall (bootcache_init);include/linux/bootcache.h new file +87
-0@@ -0,0 +1,219 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_BOOTCACHE_H +#define _LINUX_BOOTCACHE_H + +#include <linux/types.h> + +#ifdef CONFIG_BOOTCACHE + +#define BOOTCACHE_HASH_BITS 6/* 64 buckets */ + +struct bootcache_entry { +struct hlist_node node; +char *key; +void *data; +size_t len; +}; + +/** +* struct bootcache_info - Structure for registering a boot cache backend. +* +* @name: The name of the backend. +* +* Callbacks: +* @load_cache: Callback function to read and populate the framework from the cache. +*/ + +struct bootcache_info { +const char *name; +/* Callback Function Pointers */ +int (*load_cache)(void ); +}; + +/** +* bootcache_add_entry - Add an entry directly into the hash table +* @entry: bootcache_entry structure +* +* Returns: 0 on success, entry was added, do not free it. +* 1 on success but an existing entry was updated, +* free to deallocate entry. +*/ +int bootcache_add_entry (struct bootcache_entry *entry); + +/** +* bootcache_register_backend - Register a backend provider with the framework +* @bci: bootcache_info structure +* +* Returns: 0 on success, -EPROBE_DEFER if the frontend is not ready, +* -EBUSY if another backend is already registered, +* -EINVAL on invalid registration information. +*/ +int bootcache_register_backend (struct bootcache_info *bci); + +/** +* bootcache_get - Retrieve arbitrary data from the cache +* @name: Key to look up +* @buf: Buffer to store retrieved data +* @len: On input, size of buffer; on output, actual data size +* +* Returns: 0 on success, -EINVAL for invalid parameters, +* -ENOENT if not found, -ENOSPC if buffer too small +*/ +int bootcache_get (const char *name,void *buf,size_t *len); + +/** +* bootcache_set - Store arbitrary data in the cache +* @name: Key to store under +* @data: Data to store +* @len: Length of data +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOMEM on allocation failure +*/ +int bootcache_set (const char *name,const void *data,size_t len); + +#else /* !CONFIG_BOOTCACHE */ + +static inline int bootcache_get (const char *name,void *buf,size_t *len) +{ +return -ENOENT ; +} + +static inline int bootcache_set (const char *name,const void *data,size_t len) +{ +return -EOPNOTSUPP ; +} + +/** +* bootcache_get_u16 - Retrieve a u16 value from the cache
include/linux/bootcache.h line 97
Quoted 10 lines
include/linux/bootcache.h new file +10
-0+* @name: Key to look up +* @out_val: Pointer to store the retrieved value +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOENT if not found +*/ +static inline int bootcache_get_u16 (const char *name,u16 *out_val) +{ +size_t len =sizeof (u16); + +if (IS_ENABLED (CONFIG_BOOTCACHE ))
This seems redundant given we're in a #else /* !CONFIG_BOOTCACHE */ conditional section How could IS_ENABLED() return true? (did I miss an #endif somewhere?)
include/linux/bootcache.h line 100
Quoted 3 lines
include/linux/bootcache.h new file +3
-0+return bootcache_get (name, out_val, &len); + else + return -ENOENT;
Come to think of it, why are these specialized calls in a !CONFIG_BOOTCACHE conditional section? If the base call 'bootcache_get' is already conditional, and turns into a 'return -ENOENT', then this call (bootcache_get_u16()), will turn into 'return -ENOENT' also, which will cause the whole call chain to evaporate at the callsite (due to the conditional there on '==0')
I'm not sure any of these specialized calls need to be in conditional sections - just the base ones.
Quoted 123 lines
include/linux/bootcache.h new file +119
-1+} + +/** +* bootcache_set_u16 - Store a u16 value in the cache +* @name: Key to store under +* @val: Value to store +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOMEM on allocation failure +*/ +static inline int bootcache_set_u16 (const char *name,u16 val) +{ +if (IS_ENABLED (CONFIG_BOOTCACHE )) +return bootcache_set (name, &val,sizeof (u16)); +else +return -EOPNOTSUPP ; +} + +/** +* bootcache_get_u32 - Retrieve a u32 value from the cache +* @name: Key to look up +* @out_val: Pointer to store the retrieved value +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOENT if not found +*/ +static inline int bootcache_get_u32 (const char *name,u32 *out_val) +{ +size_t len =sizeof (u32); + +if (IS_ENABLED (CONFIG_BOOTCACHE )) +return bootcache_get (name, out_val, &len); +else +return -ENOENT ; +} + +/** +* bootcache_set_u32 - Store a u32 value in the cache +* @name: Key to store under +* @val: Value to store +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOMEM on allocation failure +*/ +static inline int bootcache_set_u32 (const char *name,u32 val) +{ +if (IS_ENABLED (CONFIG_BOOTCACHE )) +return bootcache_set (name, &val,sizeof (u32)); +else +return -EOPNOTSUPP ; +} + +/** +* bootcache_get_u64 - Retrieve a u64 value from the cache +* @name: Key to look up +* @out_val: Pointer to store the retrieved value +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOENT if not found +*/ +static inline int bootcache_get_u64 (const char *name,u64 *out_val) +{ +size_t len =sizeof (u64); + +if (IS_ENABLED (CONFIG_BOOTCACHE )) +return bootcache_get (name, out_val, &len); +else +return -ENOENT ; +} + +/** +* bootcache_set_u64 - Store a u64 value in the cache +* @name: Key to store under +* @val: Value to store +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOMEM on allocation failure +*/ +static inline int bootcache_set_u64 (const char *name,u64 val) +{ +if (IS_ENABLED (CONFIG_BOOTCACHE )) +return bootcache_set (name, &val,sizeof (u64)); +else +return -EOPNOTSUPP ; +} + +/** +* bootcache_get_string - Retrieve a string from the cache +* @name: Key to look up +* @buf: Buffer to store retrieved string +* @buflen: Size of buffer +* +* Returns: 0 on success, -EINVAL for invalid parameters, +* -ENOENT if not found, -ENOSPC if buffer too small +*/ +static inline int bootcache_get_string (const char *name,char *buf,size_t buflen) +{ +size_t len = buflen; + +if (IS_ENABLED (CONFIG_BOOTCACHE )) +return bootcache_get (name, buf, &len); +else +return -ENOENT ; +} + +/** +* bootcache_set_string - Store a string in the cache +* @name: Key to store under +* @str: Null-terminated string to store +* +* Returns: 0 on success, -EINVAL for invalid parameters, -ENOMEM on allocation failure +*/ +static inline int bootcache_set_string (const char *name,const char *str) +{ +if (IS_ENABLED (CONFIG_BOOTCACHE )) { +if (!str) +return -EINVAL ; +return bootcache_set (name, str,strlen (str) +1 ); + }else { +return -EOPNOTSUPP ; + } +} + +#endif /* _LINUX_BOOTCACHE_H */ --2.48.1
Re: [PATCH PREVIEW RFC 1/6] base: bootcache: initial commit
Quoted 13 lines · Tim
This seems redundant given we're in a #else /* !CONFIG_BOOTCACHE */ conditional section How could
IS_ENABLED()return true? (did I miss an #endif somewhere?)include/linux/bootcache.h line 100
Quoted 3 lines
include/linux/bootcache.h new file +3
-0+return bootcache_get (name, out_val, &len); + else + return -ENOENT;Come to think of it, why are these specialized calls in a !CONFIG_BOOTCACHE conditional section? If the base call '
bootcache_get' is already conditional, and turns into a 'return-ENOENT', then this call (bootcache_get_u16()), will turn into 'return-ENOENT' also, which will cause the whole call chain to evaporate at the callsite (due to the conditional there on '==0')
Quoted 3 lines
I'm not sure any of these specialized calls need to be in conditional sections - just the base ones.
Hi Tim,
Thanks for your feedback, good spot on that conditional, I will get it fixed in the last patch!
RFC PATCH 2/6 raid6: Add bootcache
In reply to acampanella-thegoodpenguinCheck for previously cached results before benchmark
--- lib/raid6/algos.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
lib/raid6/algos.c +21 -0
@@ -19,6 +19,7 @@#include <linux/module.h> #include <linux/gfp.h> #endif +#include <linux/bootcache.h> struct raid6_calls raid6_call;EXPORT_SYMBOL_GPL (raid6_call);
@@ -159,6 +160,24 @@ static inline const struct raid6_calls *raid6_choose_gen(int start = (disks>>1 )-1 , stop = disks-3 ;/* work on the second half of the disks */ const struct raid6_calls *const *algo;const struct raid6_calls *best; +char cached_algo_name[32 ]; + +/* Try to get cached algorithm by name */ +if (bootcache_get_string ("raid6_best_algo" , cached_algo_name, +sizeof (cached_algo_name)) ==0 ) { +/* Find algorithm by name */ +for (algo = raid6_algos; *algo; algo++) { +if (strcmp ((*algo)->name, cached_algo_name) ==0 ) { +if (!(*algo)->valid || (*algo)->valid ()) { + raid6_call = *algo; +pr_info ("raid6: using algorithm %s (from cache)\n" , + (*algo)->name); +return best; + } +break ; + } + } + }for (bestgenperf =0 , best =NULL , algo = raid6_algos; *algo; algo++) {if (!best || (*algo)->priority >= best->priority) {
@@ -198,6 +217,8 @@ static inline const struct raid6_calls *raid6_choose_gen( goto out; } +bootcache_set_string ("raid6_best_algo" , best->name); + raid6_call = *best;if (!IS_ENABLED (CONFIG_RAID6_PQ_BENCHMARK )) {
Signature · 1 line
2.48.1
kernel test robot
build botRe: [PATCH PREVIEW RFC 2/6] raid6: Add bootcache
kernel test robot noticed the following build errors:
Hi acampanella-thegoodpenguin,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9]
url: https://github.com/intel-lab-lkp/linux/commits/acampanella-thegoodpenguin/base-bootcache-initial-commit/20250923-223719 base: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9 patch link: https://lore.kernel.org/r/20250923-bootcache-v1-2-4f86fdc38b4e%40thegoodpenguin.co.uk patch subject: [PATCH PREVIEW RFC 2/6] raid6: Add bootcache config: i386-buildonly-randconfig-004-20250924 (https://download.01.org/0day-ci/archive/20250924/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250924/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from lib/raid6/algos.c:22:
>> include/linux/bootcache.h:2: error: unterminated #ifndef
2 | #ifndef _LINUX_BOOTCACHE_H
lib/raid6/algos.c: In function 'raid6_choose_gen':
>> lib/raid6/algos.c:172:54: error: incompatible types when assigning to type 'struct raid6_calls' from type 'const struct raid6_calls *'
172 | raid6_call = *algo;
| ^
--
In file included from algos.c:22:
>> include/linux/bootcache.h:2: error: unterminated #ifndef
2 | #ifndef _LINUX_BOOTCACHE_H
algos.c: In function 'raid6_choose_gen':
algos.c:172:54: error: incompatible types when assigning to type 'struct raid6_calls' from type 'const struct raid6_calls *'
172 | raid6_call = *algo;
| ^vim +2 include/linux/bootcache.h
7a47ab44737a5b acampanella-thegoodpenguin 2025-09-23 @2 #ifndef _LINUX_BOOTCACHE_H 7a47ab44737a5b acampanella-thegoodpenguin 2025-09-23 3 #define _LINUX_BOOTCACHE_H 7a47ab44737a5b acampanella-thegoodpenguin 2025-09-23 4
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH 3/6 crypto: use bootcache to cache fastest algorithm
In reply to acampanella-thegoodpenguinFrom: Andrew Murray <[email protected]>
During boot xor_blocks may determine the fastest xor algorithm by using do_xor_speed to perform a speed test on available algorithms. This process can increase the overall boot time.
Let's make use of bootcache to cache the result of the speed test for subsequent boots.
--- crypto/xor.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
crypto/xor.c +28 -1
@@ -14,6 +14,7 @@#include <linux/raid/xor.h> #include <linux/jiffies.h> #include <linux/preempt.h> +#include <linux/bootcache.h> #include <asm/xor.h> #ifndef XOR_SELECT_TEMPLATE
@@ -54,13 +55,13 @@ EXPORT_SYMBOL(xor_blocks);/* Set of all registered templates. */ static struct xor_block_template *__initdata template_list; -#ifndef MODULE static void __init do_xor_register (struct xor_block_template *tmpl) { tmpl->next = template_list; template_list = tmpl; } +#ifndef MODULE static int __init register_xor_blocks (void ) { active_template =XOR_SELECT_TEMPLATE (NULL );
@@ -79,6 +80,21 @@ static int __init register_xor_blocks(void)#define BENCH_SIZE 4096#define REPS 800U +static struct xor_block_template *__init +xor_get_template_by_name (char *fastest_name) +{ +struct xor_block_template *f; + +#define xor_speed do_xor_register +// build a list of templates +XOR_TRY_TEMPLATES ; +#undef xor_speed +for (f = template_list; f; f = f->next) +if (!strcmp (f->name, fastest_name)) +return f; +return NULL ; +} +static void __init do_xor_speed (struct xor_block_template *tmpl,void *b1,void *b2) {
@@ -117,9 +133,18 @@ calibrate_xor_blocks(void) {void *b1, *b2;struct xor_block_template *f, *fastest; +char cached_name[32 ]; +int ret; fastest =XOR_SELECT_TEMPLATE (NULL ); +if (!fastest) { + ret =bootcache_get_string ("xor_blocks_fastest" , + cached_name,sizeof (cached_name)); +if (!ret) + fastest =xor_get_template_by_name (cached_name); + } +if (fastest) { printk(KERN_INFO "xor: automatically using best " "checksumming function %-10s\n",
@@ -149,6 +174,8 @@ calibrate_xor_blocks(void)if (f->speed > fastest->speed) fastest = f; +bootcache_set_string ("xor_blocks_fastest" , fastest->name); +pr_info ("xor: using function: %s (%d MB/sec)\n" , fastest->name, fastest->speed);
Signature · 1 line
2.48.1
RE: [PATCH PREVIEW RFC 3/6] crypto: use bootcache to cache fastest algorithm
Quoted 6 lines
-----Original Message-----
From: acampanella-thegoodpenguin <[email protected]>
Sent: Tuesday, September 23, 2025 8:24 AM
To: [email protected]
Subject: [PATCH PREVIEW RFC 3/6] crypto: use bootcache to cache fastest algorithm
Quoted 7 lines
From: Andrew Murray <amurray@ thegoodpenguin. co. uk> During boot
xor_blocksmay determine the fastest xor algorithm by usingdo_xor_speedto perform a speed test on available algorithms. This process can increase the overall boot time. Let's
From: Andrew Murray <[email protected]>During boot
xor_blocksmay determine the fastest xor algorithm by usingdo_xor_speedto perform a speed test on available algorithms. This process can increase the overall boot time.
It would be good to mention the amount of time we're talking about here. It won't be the same value for all platforms, but you could mention the amount of time this takes on the platform you're working on (or on a particularly slow machine, if you want, just to highlight where it might be a problem for some situations but not all.)
Quoted 62 lines
Let's make use of bootcache to cache the result of the speed test for subsequent boots.
Signed-off-by Andrew Murray <[email protected]>--- crypto/xor.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)crypto/xor.c +17
-1@@ -14,6 +14,7 @@#include <linux/raid/xor.h> #include <linux/jiffies.h> #include <linux/preempt.h> +#include <linux/bootcache.h> #include <asm/xor.h> #ifndef XOR_SELECT_TEMPLATE @@ -54,13 +55,13 @@ EXPORT_SYMBOL(xor_blocks);/* Set of all registered templates. */ static struct xor_block_template *__initdata template_list; -#ifndef MODULE static void __init do_xor_register (struct xor_block_template *tmpl) { tmpl->next = template_list; template_list = tmpl; } +#ifndef MODULE static int __init register_xor_blocks (void ) { active_template =XOR_SELECT_TEMPLATE (NULL );@@ -79,6 +80,21 @@ static int __init register_xor_blocks(void)#define BENCH_SIZE 4096#define REPS 800U +static struct xor_block_template *__init +xor_get_template_by_name (char *fastest_name) +{ +struct xor_block_template *f; + +#define xor_speed do_xor_register +// build a list of templates +XOR_TRY_TEMPLATES ; +#undef xor_speed +for (f = template_list; f; f = f->next) +if (!strcmp (f->name, fastest_name)) +return f; +return NULL ; +} +static void __init do_xor_speed (struct xor_block_template *tmpl,void *b1,void *b2) {@@ -117,9 +133,18 @@ calibrate_xor_blocks(void) {void *b1, *b2;struct xor_block_template *f, *fastest;
crypto/xor.c calibrate_xor_blocks(void) · line 145
Quoted 10 lines
crypto/xor.c +7
-0+char cached_name[32 ]; +int ret; fastest =XOR_SELECT_TEMPLATE (NULL ); +if (!fastest) { + ret =bootcache_get_string ("xor_blocks_fastest" , + cached_name,sizeof (cached_name)); +if (!ret) + fastest =xor_get_template_by_name (cached_name);
I presume that if CONFIG_BOOTCACHE is not defined, then ret ends up being -ENOENT always, which makes this whole block completely evaporate. Is that right? In that case, do you end up with a warning about unused variable cached_name? Or does the compiler know not to complain about that? Do you still end up with space reserved on the stack?
Quoted 18 lines
crypto/xor.c +4
-1+ } +if (fastest) { printk(KERN_INFO "xor: automatically using best " "checksumming function %-10s\n",@@ -149,6 +174,8 @@ calibrate_xor_blocks(void)if (f->speed > fastest->speed) fastest = f; +bootcache_set_string ("xor_blocks_fastest" , fastest->name); +pr_info ("xor: using function: %s (%d MB/sec)\n" , fastest->name, fastest->speed); --2.48.1
Re: [PATCH PREVIEW RFC 3/6] crypto: use bootcache to cache fastest algorithm
Quoted 15 lines · Tim
Quoted 7 lines
-----Original Message-----
From: acampanella-thegoodpenguin <[email protected]>
Sent: Tuesday, September 23, 2025 8:24 AM
To: [email protected]
Subject: [PATCH PREVIEW RFC 3/6] crypto: use bootcache to cache fastest algorithmQuoted 7 lines
From: Andrew Murray <amurray@ thegoodpenguin. co. uk> During boot
xor_blocksmay determine the fastest xor algorithm by usingdo_xor_speedto perform a speed test on available algorithms. This process can increase the overall boot time. Let's
From: Andrew Murray <[email protected]>During boot
xor_blocksmay determine the fastest xor algorithm by usingdo_xor_speedto perform a speed test on available algorithms. This process can increase the overall boot time.
Quoted 5 lines
It would be good to mention the amount of time we're talking about here. It won't be the same value for all platforms, but you could mention the amount of time this takes on the platform you're working on (or on a particularly slow machine, if you want, just to highlight where it might be a problem for some situations but not all.)
Good idea, thanks!
Though, it may be that a whole bunch of patches like this are accumulated, you boot the kernel with them enabled, blindly cache all the results (for reuse in subsequent boots), and gain a quicker boot. I.e. it may not be necessary for users to pick and choose.
Quoted 67 lines
Quoted 67 lines
Let's make use of bootcache to cache the result of the speed test for subsequent boots.
Signed-off-by Andrew Murray <[email protected]>--- crypto/xor.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)crypto/xor.c +19
-1@@ -14,6 +14,7 @@#include <linux/raid/xor.h> #include <linux/jiffies.h> #include <linux/preempt.h> +#include <linux/bootcache.h> #include <asm/xor.h> #ifndef XOR_SELECT_TEMPLATE @@ -54,13 +55,13 @@ EXPORT_SYMBOL(xor_blocks);/* Set of all registered templates. */ static struct xor_block_template *__initdata template_list; -#ifndef MODULE static void __init do_xor_register (struct xor_block_template *tmpl) { tmpl->next = template_list; template_list = tmpl; } +#ifndef MODULE static int __init register_xor_blocks (void ) { active_template =XOR_SELECT_TEMPLATE (NULL );@@ -79,6 +80,21 @@ static int __init register_xor_blocks(void)#define BENCH_SIZE 4096#define REPS 800U +static struct xor_block_template *__init +xor_get_template_by_name (char *fastest_name) +{ +struct xor_block_template *f; + +#define xor_speed do_xor_register +// build a list of templates +XOR_TRY_TEMPLATES ; +#undef xor_speed +for (f = template_list; f; f = f->next) +if (!strcmp (f->name, fastest_name)) +return f; +return NULL ; +} +static void __init do_xor_speed (struct xor_block_template *tmpl,void *b1,void *b2) {@@ -117,9 +133,18 @@ calibrate_xor_blocks(void) {void *b1, *b2;struct xor_block_template *f, *fastest; +char cached_name[32 ]; +int ret; fastest =XOR_SELECT_TEMPLATE (NULL );
Quoted 11 lines
crypto/xor.c calibrate_xor_blocks(void) · line 145
Quoted 5 lines
crypto/xor.c +5
-0+if (!fastest) { + ret =bootcache_get_string ("xor_blocks_fastest" , + cached_name,sizeof (cached_name)); +if (!ret) + fastest =xor_get_template_by_name (cached_name);I presume that if
CONFIG_BOOTCACHEis not defined, then ret ends up being-ENOENTalways, which makes this whole block completely evaporate. Is that right? In that case, do you end up with a warning about unused variablecached_name? Or does the compiler know not to complain about that? Do you still end up with space reserved on the stack?
Good spot, I'm not sure the answer but i'll find out.
Thanks,
Andrew Murray
Quoted 20 lines
Quoted 19 lines
crypto/xor.c +4
-1+ } +if (fastest) { printk(KERN_INFO "xor: automatically using best " "checksumming function %-10s\n",@@ -149,6 +174,8 @@ calibrate_xor_blocks(void)if (f->speed > fastest->speed) fastest = f; +bootcache_set_string ("xor_blocks_fastest" , fastest->name); +pr_info ("xor: using function: %s (%d MB/sec)\n" , fastest->name, fastest->speed); --2.48.1
RFC PATCH 4/6 base: bootcache: Add bootcache test backend
In reply to acampanella-thegoodpenguinFrom: Marc Kelly <[email protected]>
bootcache_backend_test provides a simple test backend of API demonstration and testing purposes.
--- MAINTAINERS | 2 + drivers/base/Kconfig | 14 ++++ drivers/base/Makefile | 1 + drivers/base/bootcache_backend_test.c | 119 ++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+)
MAINTAINERS +2 -0
@@ -4420,8 +4420,10 @@ F: drivers/iio/imu/bmi323/ BOOT CACHE M: Andrea Campanella <[email protected]> +M: Marc Kelly <[email protected]> S: Maintained F: drivers/base/bootcache.c +F: drivers/base/bootcache_* F: include/linux/bootcache.h BPF JIT for ARC
drivers/base/Kconfig +14 -0
@@ -84,6 +84,20 @@ config BOOTCACHE If unsure, say N. + if BOOTCACHE + choice + prompt "Boot-time cache backend" + depends on BOOTCACHE + + config BOOTCACHE_BACKEND_TEST + bool "Test backend" + help + A simple backend for testing and development. + It does not persist any data externally. + + endchoice + endif + config STANDALONE bool "Select only drivers that don't need compile-time external firmware" default y
drivers/base/Makefile +1 -0
@@ -9,6 +9,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ swnode.o faux.o obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_BOOTCACHE) += bootcache.o +obj-$(CONFIG_BOOTCACHE_BACKEND_TEST) += bootcache_backend_test.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.o
drivers/base/bootcache_backend_test.c new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.48.1
RFC PATCH 5/6 base: bootcache: Add bootcache memory backend
In reply to acampanella-thegoodpenguinFrom: Marc Kelly <[email protected]>
bootcache_backend_memory provides a simple memory based backend that can inject data found stored in a reserved-memory block into the bootcache framework.
--- drivers/base/Kconfig | 7 + drivers/base/Makefile | 1 + drivers/base/bootcache_backend_memory.c | 220 ++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+)
drivers/base/Kconfig +7 -0
@@ -95,6 +95,13 @@ config BOOTCACHE A simple backend for testing and development. It does not persist any data externally. + config BOOTCACHE_BACKEND_MEMORY + bool "Memory backend" + help + A backend that reads the cache data from reserved system memory. + The reserved memory block is defined in the device tree and is + assumed to be populated by the bootloader. + endchoice endif
drivers/base/Makefile +1 -0
@@ -10,6 +10,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_BOOTCACHE) += bootcache.o obj-$(CONFIG_BOOTCACHE_BACKEND_TEST) += bootcache_backend_test.o +obj-$(CONFIG_BOOTCACHE_BACKEND_MEMORY) += bootcache_backend_memory.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.o
drivers/base/bootcache_backend_memory.c new file +220 -0
220 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH PREVIEW RFC 5/6] base: bootcache: Add bootcache memory backend
Hi Andrea
Quoted 5 lines · acampanella-thegoodpenguin
From: Marc Kelly <[email protected]>
bootcache_backend_memoryprovides a simple memory based backend that can inject data found stored in a reserved-memory block into the bootcache framework.
This looks really cool and I'd like to give it a go; how are you testing it currently? Do you have a tool to create the blob that you're putting into the reserved memory that I could take a look at?
Thanks
Dan
Quoted 265 lines
Signed-off-by Marc Kelly <[email protected]>--- drivers/base/Kconfig | 7 + drivers/base/Makefile | 1 + drivers/base/bootcache_backend_memory.c | 220 ++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+)drivers/base/Kconfig +7
-0@@ -95,6 +95,13 @@ config BOOTCACHE A simple backend for testing and development. It does not persist any data externally. + config BOOTCACHE_BACKEND_MEMORY + bool "Memory backend" + help + A backend that reads the cache data from reserved system memory. + The reserved memory block is defined in the device tree and is + assumed to be populated by the bootloader. + endchoice endifdrivers/base/Makefile +1
-0@@ -10,6 +10,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_BOOTCACHE) += bootcache.o obj-$(CONFIG_BOOTCACHE_BACKEND_TEST) += bootcache_backend_test.o +obj-$(CONFIG_BOOTCACHE_BACKEND_MEMORY) += bootcache_backend_memory.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.odrivers/base/bootcache_backend_memory.c new file +220
-0@@ -0,0 +1,220 @@ +// SPDX-License-Identifier: GPL-2.0 +#define DEBUG 1 +#include <linux/unaligned.h> +#include <linux/init.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/hashtable.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/of_reserved_mem.h> +#include <linux/platform_device.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/errno.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> + +#include <linux/bootcache.h> + +#define DRIVER_NAME "bootcache_memory_backend" +#define BOOTCACHE_MAGIC ('B' << 24 | 'C' << 16 | 'H' << 8 | 'E') +#define BOOTCACHE_MINSIZE 4096 + +/* +* This defines a cache entry as stored. +*/ +struct cache_memory_store_entry { +u32 key_length; +u32 data_length; +u8 data_type; +u8 data[]; +} __packed; + +/* +* The in memory store of multiple cache entries. +*/ +struct cache_memory_store { +u32 magic; +u32 entry_count; +struct cache_memory_store_entry entries[]; +} __packed; + +struct reserved_mem *rmem; + +/* +* This function processes the loaded data and adds each entry to the +* system cache via the callbck. +*/ +static int memory_backend_load_cache (void ) +{ +const struct cache_memory_store *store; +const u8 *current_ptr; +const void *max_address; +u32 entry_count; +int i; +int ret; + +if (!rmem) { +pr_warn ("%s: No bootcache was found\n" ,DRIVER_NAME ); +return 0 ; + } + + store =ioremap (rmem->base, rmem->size); +if (!store) { +pr_warn ("%s: Unable to map reserved memory 0x%llx\n" ,DRIVER_NAME , rmem->base); +return -ENOMEM ; + } + max_address = store + rmem->size; + current_ptr = (const unsigned char *)store->entries; + entry_count =get_unaligned (&store->entry_count); + +for (i =0 ; i < entry_count; i++) { +struct cache_memory_store_entry *entry; +struct bootcache_entry *new_entry =NULL ; +size_t data_length, key_length; +u8 *src, *dest; +int j; + + entry = (struct cache_memory_store_entry *)current_ptr; + data_length =get_unaligned (&entry->data_length); + key_length =get_unaligned (&entry->key_length); + +/* Check if will go outside the bounds */ +if ((current_ptr +sizeof (struct cache_memory_store_entry ) + + data_length + key_length +1 ) > max_address) { + ret = -ENOMEM ; + goto error; + } + + new_entry =kzalloc (sizeof (*new_entry),GFP_KERNEL ); +if (!new_entry) { + ret = -ENOMEM ; + goto error; + } + + new_entry->len = data_length; + new_entry->key =kzalloc (key_length+1 ,GFP_KERNEL ); + new_entry->data =kzalloc (data_length,GFP_KERNEL ); + +if (!new_entry->key || !new_entry->data) { +pr_err ("%s: Memory Allocation error creating new_entry data\n" , +DRIVER_NAME ); +kfree (new_entry->key); +kfree (new_entry->data); +kfree (new_entry); + ret = -ENOMEM ; + goto error; + } +/* +* Source data is potentially unaligned, so we copy it with the correct +* access functions +*/ + src = &entry->data[0 ]; + dest = new_entry->key; +for (j =0 ; j < key_length; j++) + *dest++ =get_unaligned (src++); + + src = &entry->data[key_length+1 ]; + dest = new_entry->data; +for (j =0 ; j < data_length; j++) + *dest++ =get_unaligned (src++); + +pr_debug ("%s: Setting up Entry (%d) with key: %s, data length is %zu\n" , +DRIVER_NAME , i, new_entry->key, new_entry->len); + +/* call the framework provided function */ + ret =bootcache_add_entry (new_entry); +if (ret) { +kfree (new_entry->key); +kfree (new_entry->data); +kfree (new_entry); + ret =0 ; + } + +/* Sanity check we've got space for the next extry */ + + current_ptr +=sizeof (struct cache_memory_store_entry ) + + data_length + key_length +1 ; +if (current_ptr +sizeof (struct cache_memory_store_entry ) + > max_address) { + ret = ret = -ENOMEM ; + goto error; + } + } + +error: +if (store) +iounmap ((void *)store); + +return ret; +} + +static struct bootcache_info cache_info = { + .name ="memory" , + .load_cache = memory_backend_load_cache, +}; + +static int bootcache_backend_probe (struct platform_device *pdev) +{ +int ret; +size_t table_size; +struct cache_memory_store *temp_store; +struct device_node *reserved_mem_node; + +/* Check for the front end being ready */ + +pr_debug ("%s: %s\n" ,DRIVER_NAME , __func__); + + reserved_mem_node =of_parse_phandle (pdev->dev.of_node,"memory-region" ,0 ); +if (reserved_mem_node) { + rmem =of_reserved_mem_lookup (reserved_mem_node); +of_node_put (reserved_mem_node); + } + +if (!rmem) { +pr_err ("%s: Failed to find reserved memory region.\n" ,DRIVER_NAME ); +return -ENOMEM ; + } +pr_debug ("%s: Found reserved cache memory block (%s):\n" ,DRIVER_NAME , rmem->name); +pr_debug ("%s: Physical Address: 0x%llx\n" ,DRIVER_NAME , rmem->base); +pr_debug ("%s: Size: 0x%llx (%llu bytes)\n" ,DRIVER_NAME , rmem->size, + rmem->size); + +if (rmem->size <BOOTCACHE_MINSIZE ) { +pr_err ("%s: reserved memory too small (%llu bytes)\n" ,DRIVER_NAME , rmem->size); +return -ENOMEM ; + } + + ret =bootcache_register_backend (&cache_info); + +if (ret <0 ) { +pr_err ("%s: bootcache_register_backend() failed with error %d\n" , +DRIVER_NAME , ret); +return ret; + } +pr_info ("%s: Backend loaded\n" ,DRIVER_NAME ); + +return 0 ; +} + +static const struct of_device_id bootcache_backend_driver_dt_ids[] = { + { .compatible ="linux,backend-backend-memory" , }, + { } +}; + +static struct platform_driver bootcache_memory_platform_driver = { + .probe = bootcache_backend_probe, + .driver = { + .name =DRIVER_NAME , + .of_match_table =of_match_ptr (bootcache_backend_driver_dt_ids), + }, +}; + +static int __init bootcache_backend_init (void ) +{ +return platform_driver_register (&bootcache_memory_platform_driver); +} + +core_initcall (bootcache_backend_init);
Re: [PATCH PREVIEW RFC 5/6] base: bootcache: Add bootcache memory backend
Quoted 5 lines · Dan Scally
Hi Andrea
Quoted 2 lines · acampanella-thegoodpenguin
From: Marc Kelly <[email protected]>
Quoted 10 lines
Quoted 3 lines
bootcache_backend_memoryprovides a simple memory based backend that can inject data found stored in a reserved-memory block into the bootcache framework.This looks really cool and I'd like to give it a go; how are you testing it currently? Do you have a tool to create the blob that you're putting into the reserved memory that I could take a look at?
Thanks
Dan
Hello Dan,
I'm glad you are finding it interesting!
We are in the process of cleaning up the demo for public use, we are using qemuarm64 to test it, we should be releasing the test setup soon!
Re: [PATCH PREVIEW RFC 5/6] base: bootcache: Add bootcache memory backend
In reply to Dan ScallyHi Dan,
There is a yocto build that targets qemuarm64 available at
https://github.com/The-Good-Penguin/TGP-kernel-cache-test which has the latest patches and a script for generating the test cache data as a binary for the bootloader to insert into reserved memory.
There are build instructions and it should be able to run with qemu via the commands shown in the readme in the repo.
If you have any questions don't hesitate to get in touch. Our plan is to keep the test distro up to date as we add more features etc as it's a covenant way to test and demo things.
Best regards
Marc.
Quoted 283 lines · Dan Scally
Hi Andrea
Quoted 5 lines · acampanella-thegoodpenguin
From: Marc Kelly <[email protected]>
bootcache_backend_memoryprovides a simple memory based backend that can inject data found stored in a reserved-memory block into the bootcache framework.This looks really cool and I'd like to give it a go; how are you testing it currently? Do you have a tool to create the blob that you're putting into the reserved memory that I could take a look at?
Thanks
DanQuoted 265 lines
Signed-off-by Marc Kelly <[email protected]>--- drivers/base/Kconfig | 7 + drivers/base/Makefile | 1 + drivers/base/bootcache_backend_memory.c | 220 ++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+)drivers/base/Kconfig +7
-0@@ -95,6 +95,13 @@ config BOOTCACHE A simple backend for testing and development. It does not persist any data externally. + config BOOTCACHE_BACKEND_MEMORY + bool "Memory backend" + help + A backend that reads the cache data from reserved system memory. + The reserved memory block is defined in the device tree and is + assumed to be populated by the bootloader. + endchoice endifdrivers/base/Makefile +1
-0@@ -10,6 +10,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_BOOTCACHE) += bootcache.o obj-$(CONFIG_BOOTCACHE_BACKEND_TEST) += bootcache_backend_test.o +obj-$(CONFIG_BOOTCACHE_BACKEND_MEMORY) += bootcache_backend_memory.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.odrivers/base/bootcache_backend_memory.c new file +220
-0@@ -0,0 +1,220 @@ +// SPDX-License-Identifier: GPL-2.0 +#define DEBUG 1 +#include <linux/unaligned.h> +#include <linux/init.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/hashtable.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/of_reserved_mem.h> +#include <linux/platform_device.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/errno.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> + +#include <linux/bootcache.h> + +#define DRIVER_NAME "bootcache_memory_backend" +#define BOOTCACHE_MAGIC ('B' << 24 | 'C' << 16 | 'H' << 8 | 'E') +#define BOOTCACHE_MINSIZE 4096 + +/* +* This defines a cache entry as stored. +*/ +struct cache_memory_store_entry { +u32 key_length; +u32 data_length; +u8 data_type; +u8 data[]; +} __packed; + +/* +* The in memory store of multiple cache entries. +*/ +struct cache_memory_store { +u32 magic; +u32 entry_count; +struct cache_memory_store_entry entries[]; +} __packed; + +struct reserved_mem *rmem; + +/* +* This function processes the loaded data and adds each entry to the +* system cache via the callbck. +*/ +static int memory_backend_load_cache (void ) +{ +const struct cache_memory_store *store; +const u8 *current_ptr; +const void *max_address; +u32 entry_count; +int i; +int ret; + +if (!rmem) { +pr_warn ("%s: No bootcache was found\n" ,DRIVER_NAME ); +return 0 ; + } + + store =ioremap (rmem->base, rmem->size); +if (!store) { +pr_warn ("%s: Unable to map reserved memory 0x%llx\n" ,DRIVER_NAME , rmem->base); +return -ENOMEM ; + } + max_address = store + rmem->size; + current_ptr = (const unsigned char *)store->entries; + entry_count =get_unaligned (&store->entry_count); + +for (i =0 ; i < entry_count; i++) { +struct cache_memory_store_entry *entry; +struct bootcache_entry *new_entry =NULL ; +size_t data_length, key_length; +u8 *src, *dest; +int j; + + entry = (struct cache_memory_store_entry *)current_ptr; + data_length =get_unaligned (&entry->data_length); + key_length =get_unaligned (&entry->key_length); + +/* Check if will go outside the bounds */ +if ((current_ptr +sizeof (struct cache_memory_store_entry ) + + data_length + key_length +1 ) > max_address) { + ret = -ENOMEM ; + goto error; + } + + new_entry =kzalloc (sizeof (*new_entry),GFP_KERNEL ); +if (!new_entry) { + ret = -ENOMEM ; + goto error; + } + + new_entry->len = data_length; + new_entry->key =kzalloc (key_length+1 ,GFP_KERNEL ); + new_entry->data =kzalloc (data_length,GFP_KERNEL ); + +if (!new_entry->key || !new_entry->data) { +pr_err ("%s: Memory Allocation error creating new_entry data\n" , +DRIVER_NAME ); +kfree (new_entry->key); +kfree (new_entry->data); +kfree (new_entry); + ret = -ENOMEM ; + goto error; + } +/* +* Source data is potentially unaligned, so we copy it with the correct +* access functions +*/ + src = &entry->data[0 ]; + dest = new_entry->key; +for (j =0 ; j < key_length; j++) + *dest++ =get_unaligned (src++); + + src = &entry->data[key_length+1 ]; + dest = new_entry->data; +for (j =0 ; j < data_length; j++) + *dest++ =get_unaligned (src++); + +pr_debug ("%s: Setting up Entry (%d) with key: %s, data length is %zu\n" , +DRIVER_NAME , i, new_entry->key, new_entry->len); + +/* call the framework provided function */ + ret =bootcache_add_entry (new_entry); +if (ret) { +kfree (new_entry->key); +kfree (new_entry->data); +kfree (new_entry); + ret =0 ; + } + +/* Sanity check we've got space for the next extry */ + + current_ptr +=sizeof (struct cache_memory_store_entry ) + + data_length + key_length +1 ; +if (current_ptr +sizeof (struct cache_memory_store_entry ) + > max_address) { + ret = ret = -ENOMEM ; + goto error; + } + } + +error: +if (store) +iounmap ((void *)store); + +return ret; +} + +static struct bootcache_info cache_info = { + .name ="memory" , + .load_cache = memory_backend_load_cache, +}; + +static int bootcache_backend_probe (struct platform_device *pdev) +{ +int ret; +size_t table_size; +struct cache_memory_store *temp_store; +struct device_node *reserved_mem_node; + +/* Check for the front end being ready */ + +pr_debug ("%s: %s\n" ,DRIVER_NAME , __func__); + + reserved_mem_node =of_parse_phandle (pdev->dev.of_node,"memory-region" ,0 ); +if (reserved_mem_node) { + rmem =of_reserved_mem_lookup (reserved_mem_node); +of_node_put (reserved_mem_node); + } + +if (!rmem) { +pr_err ("%s: Failed to find reserved memory region.\n" ,DRIVER_NAME ); +return -ENOMEM ; + } +pr_debug ("%s: Found reserved cache memory block (%s):\n" ,DRIVER_NAME , rmem->name); +pr_debug ("%s: Physical Address: 0x%llx\n" ,DRIVER_NAME , rmem->base); +pr_debug ("%s: Size: 0x%llx (%llu bytes)\n" ,DRIVER_NAME , rmem->size, + rmem->size); + +if (rmem->size <BOOTCACHE_MINSIZE ) { +pr_err ("%s: reserved memory too small (%llu bytes)\n" ,DRIVER_NAME , rmem->size); +return -ENOMEM ; + } + + ret =bootcache_register_backend (&cache_info); + +if (ret <0 ) { +pr_err ("%s: bootcache_register_backend() failed with error %d\n" , +DRIVER_NAME , ret); +return ret; + } +pr_info ("%s: Backend loaded\n" ,DRIVER_NAME ); + +return 0 ; +} + +static const struct of_device_id bootcache_backend_driver_dt_ids[] = { + { .compatible ="linux,backend-backend-memory" , }, + { } +}; + +static struct platform_driver bootcache_memory_platform_driver = { + .probe = bootcache_backend_probe, + .driver = { + .name =DRIVER_NAME , + .of_match_table =of_match_ptr (bootcache_backend_driver_dt_ids), + }, +}; + +static int __init bootcache_backend_init (void ) +{ +return platform_driver_register (&bootcache_memory_platform_driver); +} + +core_initcall (bootcache_backend_init);
Signature · 7 lines
Marc Kelly, Engineering Manager https://www.thegoodpenguin.co.uk The Good Penguin Ltd is a company registered in England and Wales with company number 12374667 and VAT number 341687879. Registered office: The Good Penguin Ltd, Merlin House, Priory Drive, Langstone, Newport, Wales, NP18 2HJ, UK.
Re: [PATCH PREVIEW RFC 5/6] base: bootcache: Add bootcache memory backend
Hi Marc
Quoted 7 lines · Marc Kelly
Hi Dan,
There is a yocto build that targets qemuarm64 available at
https://github.com/The-Good-Penguin/TGP-kernel-cache-test which has the latest patches and a script for generating the test cache data as a binary for the bootloader to insert into reserved memory.
Quoted 6 lines
There are build instructions and it should be able to run with qemu via the commands shown in the readme in the repo.
If you have any questions don't hesitate to get in touch. Our plan is to keep the test distro up to date as we add more features etc as it's a covenant way to test and demo things.
Thanks! That's great. I did actually give it a try on a board already and it seems to work well. I knocked up a script that's pretty similar to your generate_test_cache.py but parses a yaml file to create the .bin and load it and yeah - looks good to me.
I don't know if you're wanting reviews on the patches already yet, but if you let me know when you do and CC me on the sets I'll make sure I review them
Thanks
Dan
Quoted 290 lines
Best regards
Marc.Quoted 283 lines · Dan Scally
Hi Andrea
Quoted 5 lines · acampanella-thegoodpenguin
From: Marc Kelly <[email protected]>
bootcache_backend_memoryprovides a simple memory based backend that can inject data found stored in a reserved-memory block into the bootcache framework.This looks really cool and I'd like to give it a go; how are you testing it currently? Do you have a tool to create the blob that you're putting into the reserved memory that I could take a look at?
Thanks
DanQuoted 265 lines
Signed-off-by Marc Kelly <[email protected]>--- drivers/base/Kconfig | 7 + drivers/base/Makefile | 1 + drivers/base/bootcache_backend_memory.c | 220 ++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+)drivers/base/Kconfig +7
-0@@ -95,6 +95,13 @@ config BOOTCACHE A simple backend for testing and development. It does not persist any data externally. + config BOOTCACHE_BACKEND_MEMORY + bool "Memory backend" + help + A backend that reads the cache data from reserved system memory. + The reserved memory block is defined in the device tree and is + assumed to be populated by the bootloader. + endchoice endifdrivers/base/Makefile +1
-0@@ -10,6 +10,7 @@ obj-y := component.o core.o bus.o dd.o syscore.o \ obj-$(CONFIG_AUXILIARY_BUS) += auxiliary.o obj-$(CONFIG_BOOTCACHE) += bootcache.o obj-$(CONFIG_BOOTCACHE_BACKEND_TEST) += bootcache_backend_test.o +obj-$(CONFIG_BOOTCACHE_BACKEND_MEMORY) += bootcache_backend_memory.o obj-$(CONFIG_DEVTMPFS) += devtmpfs.o obj-y += power/ obj-$(CONFIG_ISA_BUS_API) += isa.odrivers/base/bootcache_backend_memory.c new file +220
-0@@ -0,0 +1,220 @@ +// SPDX-License-Identifier: GPL-2.0 +#define DEBUG 1 +#include <linux/unaligned.h> +#include <linux/init.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/hashtable.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/of_reserved_mem.h> +#include <linux/platform_device.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/errno.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> + +#include <linux/bootcache.h> + +#define DRIVER_NAME "bootcache_memory_backend" +#define BOOTCACHE_MAGIC ('B' << 24 | 'C' << 16 | 'H' << 8 | 'E') +#define BOOTCACHE_MINSIZE 4096 + +/* +* This defines a cache entry as stored. +*/ +struct cache_memory_store_entry { +u32 key_length; +u32 data_length; +u8 data_type; +u8 data[]; +} __packed; + +/* +* The in memory store of multiple cache entries. +*/ +struct cache_memory_store { +u32 magic; +u32 entry_count; +struct cache_memory_store_entry entries[]; +} __packed; + +struct reserved_mem *rmem; + +/* +* This function processes the loaded data and adds each entry to the +* system cache via the callbck. +*/ +static int memory_backend_load_cache (void ) +{ +const struct cache_memory_store *store; +const u8 *current_ptr; +const void *max_address; +u32 entry_count; +int i; +int ret; + +if (!rmem) { +pr_warn ("%s: No bootcache was found\n" ,DRIVER_NAME ); +return 0 ; + } + + store =ioremap (rmem->base, rmem->size); +if (!store) { +pr_warn ("%s: Unable to map reserved memory 0x%llx\n" ,DRIVER_NAME , rmem->base); +return -ENOMEM ; + } + max_address = store + rmem->size; + current_ptr = (const unsigned char *)store->entries; + entry_count =get_unaligned (&store->entry_count); + +for (i =0 ; i < entry_count; i++) { +struct cache_memory_store_entry *entry; +struct bootcache_entry *new_entry =NULL ; +size_t data_length, key_length; +u8 *src, *dest; +int j; + + entry = (struct cache_memory_store_entry *)current_ptr; + data_length =get_unaligned (&entry->data_length); + key_length =get_unaligned (&entry->key_length); + +/* Check if will go outside the bounds */ +if ((current_ptr +sizeof (struct cache_memory_store_entry ) + + data_length + key_length +1 ) > max_address) { + ret = -ENOMEM ; + goto error; + } + + new_entry =kzalloc (sizeof (*new_entry),GFP_KERNEL ); +if (!new_entry) { + ret = -ENOMEM ; + goto error; + } + + new_entry->len = data_length; + new_entry->key =kzalloc (key_length+1 ,GFP_KERNEL ); + new_entry->data =kzalloc (data_length,GFP_KERNEL ); + +if (!new_entry->key || !new_entry->data) { +pr_err ("%s: Memory Allocation error creating new_entry data\n" , +DRIVER_NAME ); +kfree (new_entry->key); +kfree (new_entry->data); +kfree (new_entry); + ret = -ENOMEM ; + goto error; + } +/* +* Source data is potentially unaligned, so we copy it with the correct +* access functions +*/ + src = &entry->data[0 ]; + dest = new_entry->key; +for (j =0 ; j < key_length; j++) + *dest++ =get_unaligned (src++); + + src = &entry->data[key_length+1 ]; + dest = new_entry->data; +for (j =0 ; j < data_length; j++) + *dest++ =get_unaligned (src++); + +pr_debug ("%s: Setting up Entry (%d) with key: %s, data length is %zu\n" , +DRIVER_NAME , i, new_entry->key, new_entry->len); + +/* call the framework provided function */ + ret =bootcache_add_entry (new_entry); +if (ret) { +kfree (new_entry->key); +kfree (new_entry->data); +kfree (new_entry); + ret =0 ; + } + +/* Sanity check we've got space for the next extry */ + + current_ptr +=sizeof (struct cache_memory_store_entry ) + + data_length + key_length +1 ; +if (current_ptr +sizeof (struct cache_memory_store_entry ) + > max_address) { + ret = ret = -ENOMEM ; + goto error; + } + } + +error: +if (store) +iounmap ((void *)store); + +return ret; +} + +static struct bootcache_info cache_info = { + .name ="memory" , + .load_cache = memory_backend_load_cache, +}; + +static int bootcache_backend_probe (struct platform_device *pdev) +{ +int ret; +size_t table_size; +struct cache_memory_store *temp_store; +struct device_node *reserved_mem_node; + +/* Check for the front end being ready */ + +pr_debug ("%s: %s\n" ,DRIVER_NAME , __func__); + + reserved_mem_node =of_parse_phandle (pdev->dev.of_node,"memory-region" ,0 ); +if (reserved_mem_node) { + rmem =of_reserved_mem_lookup (reserved_mem_node); +of_node_put (reserved_mem_node); + } + +if (!rmem) { +pr_err ("%s: Failed to find reserved memory region.\n" ,DRIVER_NAME ); +return -ENOMEM ; + } +pr_debug ("%s: Found reserved cache memory block (%s):\n" ,DRIVER_NAME , rmem->name); +pr_debug ("%s: Physical Address: 0x%llx\n" ,DRIVER_NAME , rmem->base); +pr_debug ("%s: Size: 0x%llx (%llu bytes)\n" ,DRIVER_NAME , rmem->size, + rmem->size); + +if (rmem->size <BOOTCACHE_MINSIZE ) { +pr_err ("%s: reserved memory too small (%llu bytes)\n" ,DRIVER_NAME , rmem->size); +return -ENOMEM ; + } + + ret =bootcache_register_backend (&cache_info); + +if (ret <0 ) { +pr_err ("%s: bootcache_register_backend() failed with error %d\n" , +DRIVER_NAME , ret); +return ret; + } +pr_info ("%s: Backend loaded\n" ,DRIVER_NAME ); + +return 0 ; +} + +static const struct of_device_id bootcache_backend_driver_dt_ids[] = { + { .compatible ="linux,backend-backend-memory" , }, + { } +}; + +static struct platform_driver bootcache_memory_platform_driver = { + .probe = bootcache_backend_probe, + .driver = { + .name =DRIVER_NAME , + .of_match_table =of_match_ptr (bootcache_backend_driver_dt_ids), + }, +}; + +static int __init bootcache_backend_init (void ) +{ +return platform_driver_register (&bootcache_memory_platform_driver); +} + +core_initcall (bootcache_backend_init);
RFC PATCH 6/6 dt-bindings: bootcache: Add bindings for bootcache backend
In reply to acampanella-thegoodpenguinFrom: Marc Kelly <[email protected]>
Add device tree bindings for the bootcache_backend_memory.
--- .../bootcache/linux,bootcache-backend-memory.yaml | 67 ++++++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 68 insertions(+)
Documentation/devicetree/bindings/bootcache/linux,bootcache-backend-memory.yaml new file +67 -0
@@ -0,0 +1,67 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/bootcache/linux,bootcache-backend-memory.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Bootcache Framework reserved memory backend + +maintainers: + - Marc Kelly <[email protected]> + - Andrea Campenella <[email protected]> + +description: + This bootcache backend adds support for using reserved memory + for the storage location for bootcache data. On initialization this + memory is read and any bootcache entries found are added to the + bootcache framework. + Data is inserted into these memory locations via the bootloader + or other means before the kernel boots. + +properties: + compatible: + enum: + - linux,backend-backend-memory + + reg: + maxItems: 2 + + memory-region: + maxItems: 1 + description: + Contains the bootcache reserved memory. + Data format is a binary block of packed cache data consisting + of a selection of key and value pairs, along with size indicators. + The format should be considered private to the driver and should + not be interpreted outside of the drivers context. + +required: + - compatible + - reg + - memory-region + +unevaluatedProperties: false + +examples: + - | + /* + * The bootcache-backend is placed in the in the main domain + * of the SOC, with the reserved memory location defined as + * follows. + * This defines 64kBytes in the upper 2GBytes. + * + * The size + * reserved-memory { + * bootcache_reserved: bootcache-reserved@BFFF0000 { + * no-map; + * reg = <0x00 0xBFFF0000 0x00 0x10000>; + * label = "bootcache_backend_memory"; + * }; + * } + */ + + bootcache-backend@BFFF0000 { + compatible = "linux,backend-backend-memory"; + reg = <0x00 0xBFFF0000 0x00 0x10000>; + memory-region = <&bootcache_reserved>; + };
MAINTAINERS +1 -0
@@ -4422,6 +4422,7 @@ BOOT CACHE M: Andrea Campanella <[email protected]> M: Marc Kelly <[email protected]> S: Maintained +F: Documentation/devicetree/bindings/bootcache/linux,bootcache-backend-memory.yaml F: drivers/base/bootcache.c F: drivers/base/bootcache_* F: include/linux/bootcache.h
Signature · 1 line
2.48.1
Quoted 4 lines · acampanella-thegoodpenguin
This patch series provides a simple key-value cache for storing boot-time configuration data. The goal is to allow kernel components and drivers to cache time-consuming-to-compute values during boot, enabling faster subsequent boots by avoiding redundant initialization work.
Does the vmlinux build use --gc-sections yet so this sort of thing can drop out when it's not needed? (Marking it _init still eats early boot and flash space.)
The kernel build is a giant complicated thing using linker scripts so I don't want to blindly throw -ffunction-sections -fdata-sections in there but this plumbing strikes me as something that will penalize systems that _don't_ calculate expensive things and refer to them multiple times (or would rather eat the CPU than eat the RAM to do it). And adding more kconfig nonsense to have the build manually pull it in seems silly when the tools have been able to do this automatically for over 20 years...
Rob