Skip to content

Commit b0ca403

Browse files
binxingdjbw
authored andcommitted
tsm-mr: Fix init breakage after bin_attrs constification by scoping non-const pointers to init phase
Commit 9bec944 ("sysfs: constify attribute_group::bin_attrs") enforced the ro-after-init principle by making elements of bin_attrs_new pointing to const. To align with this change, introduce a temporary variable `bap` within the initialization loop. This improves code clarity by explicitly marking the initialization scope and eliminates the need for type casts when assigning to bin_attrs_new. Signed-off-by: Cedric Xing <cedric.xing@intel.com> Link: https://patch.msgid.link/20250513164154.10109-1-cedric.xing@intel.com Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent 1f45073 commit b0ca403

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

drivers/virt/coco/tsm-mr.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ tsm_mr_create_attribute_group(const struct tsm_measurements *tm)
173173
* so that we don't have to free MR names one-by-one in
174174
* tsm_mr_free_attribute_group()
175175
*/
176-
const struct bin_attribute * const *attrs __free(kfree) =
176+
const struct bin_attribute **attrs __free(kfree) =
177177
kzalloc(sizeof(*attrs) * (tm->nr_mrs + 1) + nlen, GFP_KERNEL);
178178
struct tm_context *ctx __free(kfree) =
179179
kzalloc(struct_size(ctx, mrs, tm->nr_mrs), GFP_KERNEL);
@@ -187,16 +187,14 @@ tsm_mr_create_attribute_group(const struct tsm_measurements *tm)
187187
end = name + nlen;
188188

189189
for (size_t i = 0; i < tm->nr_mrs; ++i) {
190-
/* break const for init */
191-
struct bin_attribute **bas = (struct bin_attribute **)attrs;
190+
struct bin_attribute *bap = &ctx->mrs[i];
192191

193-
bas[i] = &ctx->mrs[i];
194-
sysfs_bin_attr_init(bas[i]);
192+
sysfs_bin_attr_init(bap);
195193

196194
if (tm->mrs[i].mr_flags & TSM_MR_F_NOHASH)
197-
bas[i]->attr.name = tm->mrs[i].mr_name;
195+
bap->attr.name = tm->mrs[i].mr_name;
198196
else if (name < end) {
199-
bas[i]->attr.name = name;
197+
bap->attr.name = name;
200198
name += snprintf(name, end - name, "%s:%s",
201199
tm->mrs[i].mr_name,
202200
hash_algo_name[tm->mrs[i].mr_hash]);
@@ -206,21 +204,23 @@ tsm_mr_create_attribute_group(const struct tsm_measurements *tm)
206204

207205
/* check for duplicated MR definitions */
208206
for (size_t j = 0; j < i; ++j)
209-
if (!strcmp(bas[i]->attr.name, bas[j]->attr.name))
207+
if (!strcmp(bap->attr.name, attrs[j]->attr.name))
210208
return ERR_PTR(-EINVAL);
211209

212210
if (tm->mrs[i].mr_flags & TSM_MR_F_READABLE) {
213-
bas[i]->attr.mode |= 0444;
214-
bas[i]->read_new = tm_digest_read;
211+
bap->attr.mode |= 0444;
212+
bap->read_new = tm_digest_read;
215213
}
216214

217215
if (tm->mrs[i].mr_flags & TSM_MR_F_WRITABLE) {
218-
bas[i]->attr.mode |= 0200;
219-
bas[i]->write_new = tm_digest_write;
216+
bap->attr.mode |= 0200;
217+
bap->write_new = tm_digest_write;
220218
}
221219

222-
bas[i]->size = tm->mrs[i].mr_size;
223-
bas[i]->private = ctx;
220+
bap->size = tm->mrs[i].mr_size;
221+
bap->private = ctx;
222+
223+
attrs[i] = bap;
224224
}
225225

226226
if (name != end)
@@ -244,7 +244,7 @@ EXPORT_SYMBOL_GPL(tsm_mr_create_attribute_group);
244244
void tsm_mr_free_attribute_group(const struct attribute_group *attr_grp)
245245
{
246246
if (!IS_ERR_OR_NULL(attr_grp)) {
247-
kfree(attr_grp->bin_attrs);
247+
kfree(attr_grp->bin_attrs_new);
248248
kfree(container_of(attr_grp, struct tm_context, agrp));
249249
}
250250
}

0 commit comments

Comments
 (0)