Skip to content

Read only accessors#3193

Merged
igaw merged 5 commits intolinux-nvme:masterfrom
hreinecke:read-only-accessors
Mar 20, 2026
Merged

Read only accessors#3193
igaw merged 5 commits intolinux-nvme:masterfrom
hreinecke:read-only-accessors

Conversation

@hreinecke
Copy link
Copy Markdown
Collaborator

Quite some of the 'host', 'subsystem', and 'ctrl' attributes are actually read-only, and cannot be changed during the lifetime of the controller. So mark these attributes as 'const' and re-generate the accessor functions to drop any 'setter' functions for these fields.

@martin-belanger
Copy link
Copy Markdown

Looks good to me

@hreinecke
Copy link
Copy Markdown
Collaborator Author

But I guess you have to fixup the accessor-generator; while running it when applying the first patch everything's cool, but running it after the second patch it complains about missing setter functions which had been removed by the first patch. @martin-belanger , can you check what is going on?

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Mar 18, 2026

I don't like this approach, because having casting of types is often hiding problems. The compilers and likely static analyzer can't work correctly anymore. The implicit annotation approach has it's limit.

@hreinecke
Copy link
Copy Markdown
Collaborator Author

I don't like this approach, because having casting of types is often hiding problems. The compilers and likely static analyzer can't work correctly anymore. The implicit annotation approach has it's limit.

But that was the idea, that the compiler complains if someone tries to assign a non-const string to it. Sure, we have to do a cast for internal functions like 'free()' and assignments, but future changes will generate a compiler warning, so the author is at least forced to have a look. I don't see how we could achieve that with any other annotation.

@hreinecke
Copy link
Copy Markdown
Collaborator Author

(But realistically, the 'const' keyword was merely used to tweak the accessor generator to not generate 'setter' functions. If there's another way of doing that I'm all ears; I'm not wedded to the 'const' thingie.)

@martin-belanger
Copy link
Copy Markdown

But I guess you have to fixup the accessor-generator; while running it when applying the first patch everything's cool, but running it after the second patch it complains about missing setter functions which had been removed by the first patch. @martin-belanger , can you check what is going on?

That's weird. I will have a look.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Mar 18, 2026

	nvme_ctrl_lookup_phy_slot(ctx, c->address, (char **)&c->phy_slot);

	check(asprintf((char **)&c.name, "%s_ctrl", test_name) >= 0,
	      "asprintf() failed");

	free((char *)c.name);

is also hiding problems (and looks awful IMO).

As far I know there is no silver bullet for this problem, something like __assign_once would be really cool to have. I think the medicine is worse than the disease in this case.

Also this is about internal library code and I would like to get the API sorted as soon as possible. Another reason why explicit annotation for the generator is my preference. This would decouple this discussion from the setter discussion.

@martin-belanger
Copy link
Copy Markdown

Another reason why explicit annotation for the generator is my preference.

I understand the motivation, but explicit annotations make the parsing side significantly more complex (and it’s already quite challenging even without them). In general, parsing human-written text—with all its variability and edge cases—tends to be fairly brittle.

That said, one possible way to mitigate this would be to move the code generator from C to Python. The original reason for implementing it in C was to allow it to run at build time alongside libnvme, given that not all build environments reliably provide Python (or C++). Since that constraint no longer really applies, switching to Python could make the parsing logic more manageable and flexible.

@hreinecke
Copy link
Copy Markdown
Collaborator Author

Another reason why explicit annotation for the generator is my preference.

I understand the motivation, but explicit annotations make the parsing side significantly more complex (and it’s already quite challenging even without them). In general, parsing human-written text—with all its variability and edge cases—tends to be fairly brittle.

That said, one possible way to mitigate this would be to move the code generator from C to Python. The original reason for implementing it in C was to allow it to run at build time alongside libnvme, given that not all build environments reliably provide Python (or C++). Since that constraint no longer really applies, switching to Python could make the parsing logic more manageable and flexible.

Or we can make it even easier; only generate 'getter' functions. As my patches demonstrate there are really only a handful of 'setter' functions; most of the attributes cannot be changed once the object is initialized. So why don't we modify the generator to just create 'getter' functions, and create the 'setter' functions by hand?

No reason why it should not be auto-generated.

Signed-off-by: Hannes Reinecke <[email protected]>
@hreinecke hreinecke force-pushed the read-only-accessors branch from a364455 to 2dcbc38 Compare March 19, 2026 08:07
@hreinecke
Copy link
Copy Markdown
Collaborator Author

Thanks, that worked quite nicely. I've reworked the patches to use the '!accessors:' annotation syntax, which required not additional code-change. Nice.

Some controller attributes are fixed for the lifetime of the controller,
so once established they cannot be changed. Consequently it's pointless
to have a 'setter' function as this would not do what's expected.

Signed-off-by: Hannes Reinecke <[email protected]>
Most subsystem attributes are fixed for the lifetime of the subsystem,
so once established they cannot be changed. Consequently it's pointless
to have a 'setter' function as this would not do what's expected.

Signed-off-by: Hannes Reinecke <[email protected]>
Some host attributes are fixed for the lifetime of the host,
so once established they cannot be changed. Consequently it's pointless
to have a 'setter' function as this would not do what's expected.

Signed-off-by: Hannes Reinecke <[email protected]>
No reason why it needs to be treated specially.

Signed-off-by: Hannes Reinecke <[email protected]>
@hreinecke hreinecke force-pushed the read-only-accessors branch from 2dcbc38 to b1bd5be Compare March 19, 2026 08:24
@igaw igaw merged commit ce266bf into linux-nvme:master Mar 20, 2026
28 checks passed
@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Mar 20, 2026

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants