[PATCH v12 07/23] KVM: selftests: Add kbuild definitons

Sagi Shahar posted 23 patches 1 month, 2 weeks ago
[PATCH v12 07/23] KVM: selftests: Add kbuild definitons
Posted by Sagi Shahar 1 month, 2 weeks ago
Add kbuild.h that can be used by files under tools/

Definitions are taken from the original definitions at
include/linux/kbuild.h

This is needed to expose values from c code to assembly code.

Signed-off-by: Sagi Shahar <sagis@google.com>
---
 tools/include/linux/kbuild.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 tools/include/linux/kbuild.h

diff --git a/tools/include/linux/kbuild.h b/tools/include/linux/kbuild.h
new file mode 100644
index 000000000000..62e20ba9380e
--- /dev/null
+++ b/tools/include/linux/kbuild.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __TOOLS_LINUX_KBUILD_H
+#define __TOOLS_LINUX_KBUILD_H
+
+#include <stddef.h>
+
+#define DEFINE(sym, val) \
+	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
+
+#define BLANK() asm volatile("\n.ascii \"->\"" : : )
+
+#define OFFSET(sym, str, mem) \
+	DEFINE(sym, offsetof(struct str, mem))
+
+#define COMMENT(x) \
+	asm volatile("\n.ascii \"->#" x "\"")
+
+#endif /* __TOOLS_LINUX_KBUILD_H */
-- 
2.51.1.851.g4ebd6896fd-goog
Re: [PATCH v12 07/23] KVM: selftests: Add kbuild definitons
Posted by Reinette Chatre 1 month, 2 weeks ago
Hi Sagi,

typo in subject: definitons -> definitions

This is not actually a KVM selftest change though but an addition to
core tools. I do not know if such an addition can flow via the KVM tree but I
really do not think that it should be disguised as a KVM change as the
subject implies.

On 10/28/25 2:20 PM, Sagi Shahar wrote:
> Add kbuild.h that can be used by files under tools/

Similar to earlier feedback this is obvious from the patch self.

> 
> Definitions are taken from the original definitions at
> include/linux/kbuild.h

Always write in imperative mood.

> 
> This is needed to expose values from c code to assembly code.

I do not think this description is a strong motivation.
Another draft for consideration:

	Add the kbuild definitions to enable a tool to use
	the kbuild filechk_offset script to generate C header files
	containing structure member offset information.
	
	Tools depending on assembly code that operates on structures
	need to hardcode the offsets of structure members. The kernel's
	kbuild scripts can instead generate C header files with offset
	information for inclusion into assembly code. 

Reinette
Re: [PATCH v12 07/23] KVM: selftests: Add kbuild definitons
Posted by Ira Weiny 1 month, 2 weeks ago
Sagi Shahar wrote:
> Add kbuild.h that can be used by files under tools/
> 
> Definitions are taken from the original definitions at
> include/linux/kbuild.h
> 
> This is needed to expose values from c code to assembly code.
> 
> Signed-off-by: Sagi Shahar <sagis@google.com>

Reviewed-by: Ira Weiny <ira.weiny@intel.com>

[snip]
Re: [PATCH v12 07/23] KVM: selftests: Add kbuild definitons
Posted by Binbin Wu 1 month, 2 weeks ago

On 10/29/2025 5:20 AM, Sagi Shahar wrote:
> Add kbuild.h that can be used by files under tools/
>
> Definitions are taken from the original definitions at
> include/linux/kbuild.h
>
> This is needed to expose values from c code to assembly code.
>
> Signed-off-by: Sagi Shahar <sagis@google.com>
> ---
>   tools/include/linux/kbuild.h | 18 ++++++++++++++++++
>   1 file changed, 18 insertions(+)
>   create mode 100644 tools/include/linux/kbuild.h
>
> diff --git a/tools/include/linux/kbuild.h b/tools/include/linux/kbuild.h
> new file mode 100644
> index 000000000000..62e20ba9380e
> --- /dev/null
> +++ b/tools/include/linux/kbuild.h
> @@ -0,0 +1,18 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef __TOOLS_LINUX_KBUILD_H
> +#define __TOOLS_LINUX_KBUILD_H
> +
> +#include <stddef.h>

This is not in kernel's version.
Instead, consumers of kbuild.h include the necessary header.

Maybe it can follow kernel's style?

> +
> +#define DEFINE(sym, val) \
> +	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
> +
> +#define BLANK() asm volatile("\n.ascii \"->\"" : : )
> +
> +#define OFFSET(sym, str, mem) \
> +	DEFINE(sym, offsetof(struct str, mem))
> +
> +#define COMMENT(x) \
> +	asm volatile("\n.ascii \"->#" x "\"")
> +
> +#endif /* __TOOLS_LINUX_KBUILD_H */
Re: [PATCH v12 07/23] KVM: selftests: Add kbuild definitons
Posted by Ira Weiny 1 month, 2 weeks ago
Binbin Wu wrote:
> 
> 
> On 10/29/2025 5:20 AM, Sagi Shahar wrote:
> > Add kbuild.h that can be used by files under tools/
> >
> > Definitions are taken from the original definitions at
> > include/linux/kbuild.h
> >
> > This is needed to expose values from c code to assembly code.
> >
> > Signed-off-by: Sagi Shahar <sagis@google.com>
> > ---
> >   tools/include/linux/kbuild.h | 18 ++++++++++++++++++
> >   1 file changed, 18 insertions(+)
> >   create mode 100644 tools/include/linux/kbuild.h
> >
> > diff --git a/tools/include/linux/kbuild.h b/tools/include/linux/kbuild.h
> > new file mode 100644
> > index 000000000000..62e20ba9380e
> > --- /dev/null
> > +++ b/tools/include/linux/kbuild.h
> > @@ -0,0 +1,18 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +#ifndef __TOOLS_LINUX_KBUILD_H
> > +#define __TOOLS_LINUX_KBUILD_H
> > +
> > +#include <stddef.h>
> 
> This is not in kernel's version.
> Instead, consumers of kbuild.h include the necessary header.
> 
> Maybe it can follow kernel's style?

Is there a need to not include what is needed?  Generally that is a good
idea unless the header file dependencies cause some build slowdowns.  I
don't think that is the case here.

Ira

> 
> > +
> > +#define DEFINE(sym, val) \
> > +	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
> > +
> > +#define BLANK() asm volatile("\n.ascii \"->\"" : : )
> > +
> > +#define OFFSET(sym, str, mem) \
> > +	DEFINE(sym, offsetof(struct str, mem))
> > +
> > +#define COMMENT(x) \
> > +	asm volatile("\n.ascii \"->#" x "\"")
> > +
> > +#endif /* __TOOLS_LINUX_KBUILD_H */
>