[PATCH RFC v2 4/9] attestation: Prepare measurement registers
From: Jiri Pirko
Date: Fri Nov 13 2026 - 14:28:23 EST
From: Jiri Pirko <jiri@xxxxxxxxxx>
Add the fixed MRs from the provider descriptor, their lookup and MR
extension, which advances an instance-wide MR generation.
The MR objects can be compile-checked here. CONFIG_ATTESTATION remains
unwired until the CTLV operation handlers complete the module.
Assisted-by: Cursor: GPT-5.6 Sol
Assisted-by: Cursor: Claude Opus 5.5
Signed-off-by: Jiri Pirko <jiri@xxxxxxxxxx>
---
RFC->RFCv2:
- kept the MR state in struct attestation_dev, which providers embed
- dropped the MR journal and its notifications
- counted MR changes in a plain MR generation
- retitled from "Prepare measurement register journal"
- used the helpers generated for the rewritten spec
- let an extend carry values for some of an MR's banks
- always provided mr-read with MRs
---
drivers/attestation/Makefile | 2 +-
drivers/attestation/core/core.c | 71 +++++++++++++++++++++++++++++
drivers/attestation/core/internal.h | 13 ++++++
drivers/attestation/core/mr.c | 64 ++++++++++++++++++++++++++
include/linux/attestation.h | 5 +-
5 files changed, 153 insertions(+), 2 deletions(-)
create mode 100644 drivers/attestation/core/mr.c
diff --git a/drivers/attestation/Makefile b/drivers/attestation/Makefile
index 8a75558a7013..28036586f5d8 100644
--- a/drivers/attestation/Makefile
+++ b/drivers/attestation/Makefile
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_ATTESTATION) += attestation.o
-attestation-y := core/core.o
+attestation-y := core/core.o core/mr.o
diff --git a/drivers/attestation/core/core.c b/drivers/attestation/core/core.c
index 587a0a9616b6..24abe0923306 100644
--- a/drivers/attestation/core/core.c
+++ b/drivers/attestation/core/core.c
@@ -213,6 +213,12 @@ bool attestation_registered(struct attestation_dev *att)
return registered;
}
+void attestation_mr_changed(struct attestation_dev *att)
+{
+ lockdep_assert_held(&att->lock);
+ att->mr_generation++;
+}
+
void attestation_stop(struct attestation_dev *att)
{
lockdep_assert_held(&att->lock);
@@ -221,6 +227,38 @@ void attestation_stop(struct attestation_dev *att)
xa_unlock(&attestation_instances);
}
+static struct attestation_mr *attestation_mr_alloc(struct attestation_dev *att,
+ const struct attestation_mr_props *props)
+{
+ struct attestation_mr *mr;
+ int ret;
+
+ ret = attestation_mr_props_validate(att->provider, props);
+ if (ret)
+ return ERR_PTR(ret);
+ if (xa_load(&att->mrs, props->id))
+ return ERR_PTR(-EEXIST);
+ mr = kzalloc_obj(*mr, GFP_KERNEL);
+ if (!mr)
+ return ERR_PTR(-ENOMEM);
+ mr->props = props;
+ ret = xa_insert(&att->mrs, props->id, mr, GFP_KERNEL);
+ if (ret) {
+ kfree(mr);
+ return ERR_PTR(ret);
+ }
+ return mr;
+}
+
+struct attestation_mr *attestation_mr_lookup_live(struct attestation_dev *att, unsigned int mr_id)
+{
+ struct attestation_mr *mr;
+
+ lockdep_assert_held(&att->lock);
+ mr = xa_load(&att->mrs, mr_id);
+ return mr ? mr : ERR_PTR(-ENOENT);
+}
+
/**
* attestation_evidence_write - Write an evidence object to its blob.
* @object: the requested object
@@ -252,11 +290,41 @@ void attestation_provide_ops(struct attestation_dev *att)
ctlv_attestation_evidence_get_provide(ctlv);
ctlv_attestation_evidence_get_enable(ctlv);
}
+
+ if (provider->mrs_count) {
+ ctlv_attestation_mr_get_provide(ctlv);
+ ctlv_attestation_mr_read_provide(ctlv);
+ if (provider->mr_extend)
+ ctlv_attestation_mr_extend_provide(ctlv);
+ }
+}
+
+static int attestation_mrs_create(struct attestation_dev *att)
+{
+ const struct attestation_provider *provider = att->provider;
+ size_t index;
+
+ for (index = 0; index < provider->mrs_count; index++) {
+ struct attestation_mr *mr;
+
+ mr = attestation_mr_alloc(att, &provider->mrs[index]);
+ if (IS_ERR(mr))
+ return PTR_ERR(mr);
+ }
+ return 0;
}
/* Runs from the release, so it must not reach the provider's module. */
static void attestation_state_free(struct attestation_dev *att)
{
+ struct attestation_mr *mr;
+ unsigned long index;
+
+ xa_for_each(&att->mrs, index, mr) {
+ xa_erase(&att->mrs, index);
+ kfree(mr);
+ }
+ xa_destroy(&att->mrs);
mutex_destroy(&att->lock);
}
@@ -338,11 +406,14 @@ struct attestation_dev *_attestation_alloc(struct device *parent,
return ERR_PTR(-ENOMEM);
att->provider = provider;
mutex_init(&att->lock);
+ xa_init(&att->mrs);
/* From here on, the release frees whatever was set up. */
ret = ctlv_device_init(&att->node, &attestation_class, parent, &att->ctlv);
if (!ret)
ret = ctlv_init(&att->ctlv, &att->node.dev, family, &attestation_ctlv_hooks);
+ if (!ret)
+ ret = attestation_mrs_create(att);
if (ret) {
put_device(&att->node.dev);
return ERR_PTR(ret);
diff --git a/drivers/attestation/core/internal.h b/drivers/attestation/core/internal.h
index f42bc3cd9a35..19c2ade5e6db 100644
--- a/drivers/attestation/core/internal.h
+++ b/drivers/attestation/core/internal.h
@@ -11,11 +11,24 @@
#define ATTESTATION_REGISTERED XA_MARK_1
+struct attestation_mr {
+ const struct attestation_mr_props *props;
+};
+
bool attestation_registered(struct attestation_dev *att);
void attestation_stop(struct attestation_dev *att);
+void attestation_mr_changed(struct attestation_dev *att);
int attestation_provider_validate(const struct attestation_provider *provider,
const struct ctlv_family *family);
void attestation_provide_ops(struct attestation_dev *att);
+struct attestation_mr *attestation_mr_lookup_live(struct attestation_dev *att, unsigned int mr_id);
+int attestation_mr_values_validate(const struct attestation_mr_props *props,
+ const struct attestation_digest_set *values, bool all_banks);
+int attestation_mr_extend_locked(struct attestation_dev *att, struct attestation_mr *mr,
+ const struct attestation_digest_set *values);
+
+int attestation_mr_props_encode(struct ctlv_cursor *reply, const struct attestation_mr *mr);
+
#endif /* _ATTESTATION_INTERNAL_H */
diff --git a/drivers/attestation/core/mr.c b/drivers/attestation/core/mr.c
new file mode 100644
index 000000000000..44a8c06df0c6
--- /dev/null
+++ b/drivers/attestation/core/mr.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/limits.h>
+
+#include "internal.h"
+
+/* @values follow the banks' order, so each bank comes once at most. */
+int attestation_mr_values_validate(const struct attestation_mr_props *props,
+ const struct attestation_digest_set *values, bool all_banks)
+{
+ size_t bank = 0, index;
+
+ if (!values || !values->values || !values->values_count ||
+ (all_banks && values->values_count != props->algorithms_count))
+ return -EINVAL;
+ for (index = 0; index < values->values_count; index++, bank++) {
+ while (bank < props->algorithms_count &&
+ props->algorithms[bank] != values->values[index].algorithm)
+ bank++;
+ if (bank == props->algorithms_count)
+ return -EINVAL;
+ }
+ return 0;
+}
+
+int attestation_mr_extend_locked(struct attestation_dev *att, struct attestation_mr *mr,
+ const struct attestation_digest_set *values)
+{
+ int ret;
+
+ lockdep_assert_held(&att->lock);
+ if (att->mr_generation == UINT_MAX) {
+ dev_warn(att->node.dev.parent,
+ "attestation instance %u stopped: mr generation exhausted\n",
+ att->index);
+ attestation_stop(att);
+ return -EOVERFLOW;
+ }
+ ret = att->provider->mr_extend(att, mr->props->id, values);
+ if (ret)
+ return ret;
+ attestation_mr_changed(att);
+ return 0;
+}
+
+int attestation_mr_props_encode(struct ctlv_cursor *reply, const struct attestation_mr *mr)
+{
+ u64 caps = 0;
+ size_t index;
+ bool ok = true;
+
+ ok &= ctlv_attestation_mr_get_items_id_put(reply, mr->props->id);
+ ok &= ctlv_attestation_mr_desc_name_put(reply, mr->props->name);
+ for (index = 0; index < mr->props->algorithms_count; index++)
+ ok &= ctlv_attestation_mr_desc_supported_hash_algorithms_put(
+ reply, mr->props->algorithms[index]);
+ if (mr->props->has_register_index)
+ ok &= ctlv_attestation_mr_desc_register_index_put(reply, mr->props->register_index);
+ if (mr->props->extendable)
+ caps |= CTLV_ATTESTATION_MR_CAPS_EXTEND;
+ ok &= ctlv_attestation_mr_desc_caps_put(reply, caps);
+ return ok ? 0 : -EMSGSIZE;
+}
diff --git a/include/linux/attestation.h b/include/linux/attestation.h
index 29bb776bfb41..7745938092c7 100644
--- a/include/linux/attestation.h
+++ b/include/linux/attestation.h
@@ -9,6 +9,7 @@
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/types.h>
+#include <linux/xarray.h>
#include <uapi/ctlv/ctlv_attestation_gen.h>
#include <uapi/linux/hash_info.h>
@@ -200,9 +201,11 @@ struct attestation_dev {
const struct attestation_provider *provider;
unsigned int index;
- /* Mutable instance state. */
+ /* Mutable instance and MR state. */
struct mutex lock;
+ unsigned int mr_generation;
+ struct xarray mrs;
};
/* What a CTLV callback of a family inheriting ctlv-attestation was handed. */
--
2.56.0