[PATCH 0/1] x86: install modules-load.d file to load msr module

This submission is a follow-up of the discussion (RFC) that was happening at: https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/thread/ADJ4A... On x86, libvirt makes use of the API exposed by the msr kernel module to read the MSR (Model Specific Registers) values. On some Linux distros, this module is not loaded by default so libvirt might fail to read the MSR values and falls back to /dev/kvm to obtain these values. KVM might be behind in term of exposing all the values of the MSRs so using /dev/kvm is not ideal. This submission tries to make libvirt deploy the modules-load.d configuration so that linux loads the msr module at next boot. One caveat, libvirt installs only the file and does not trigger the load that is only done at next reboot, it is up to each distro to trigger immediately the load of the module without a reboot. Hector Cao (1): x86: install modules-load.d file to load msr module meson_options.txt | 1 + src/util/meson.build | 17 +++++++++++++++++ src/util/modules-load.d/msr.conf | 1 + tools/virt-host-validate-qemu.c | 7 +++++++ 4 files changed, 26 insertions(+) create mode 100644 src/util/modules-load.d/msr.conf -- 2.45.2

On recent Intel CPUs, some of the CPU features (mostly vmx-* subfeatures) are listed and controlled via the MSRs (Model Specific Registers) instead of the traditional CPUID instruction method. To be able to read the MSR's values, the kernel module msr has to be loaded and the values can be read via /dev/cpu/*/msr. This commit introduces following changes: - install modules-load file for msr this is only enabled: - On x86 arch - If user asks for it explicitly via a build option: meson -Dmsr_module_load=true build By default, it is disabled. - modify virt-host-validate to check of /dev/cpu/*/msr is available. Signed-off-by: Hector Cao <hector.cao@canonical.com> --- meson_options.txt | 1 + src/util/meson.build | 17 +++++++++++++++++ src/util/modules-load.d/msr.conf | 1 + tools/virt-host-validate-qemu.c | 7 +++++++ 4 files changed, 26 insertions(+) create mode 100644 src/util/modules-load.d/msr.conf diff --git a/meson_options.txt b/meson_options.txt index 3dc3e8667b..5a9e067407 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -135,3 +135,4 @@ option('sysctl_config', type: 'feature', value: 'auto', description: 'Whether to # dep:sysctl_config option('userfaultfd_sysctl', type: 'feature', value: 'auto', description: 'Whether to install sysctl config for enabling unprivileged userfaultfd') option('tls_priority', type: 'string', value: 'NORMAL', description: 'set the default TLS session priority string') +option('msr_module_load', type: 'boolean', value: true, description: 'Install modules-load.d file for msr module on x86') diff --git a/src/util/meson.build b/src/util/meson.build index 69ef49139a..5471a1e9ea 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -225,3 +225,20 @@ if conf.has('WITH_LIBVIRTD') endif util_inc_dir = include_directories('.') + +# Install the modules-load.d file to load the MSR module +# NB: For the file to taken into account and the module load to happen, users must trigger +# the modules-load.d reload (by reboot or restart the systemd-modules-load service). +msr_module_load = get_option('msr_module_load') +if msr_module_load and (host_machine.cpu_family() == 'x86' or host_machine.cpu_family() == 'x86_64') + message('msr_module_load: Enabling installation of modules-load.d/msr.conf for x86') + + install_data( + 'modules-load.d/msr.conf', + install_dir: join_paths(sysconfdir, 'modules-load.d') + ) +elif msr_module_load + message('msr_module_load: option enabled, but not x86/x86_64 : skipping') +else + message('msr_module_load: option disabled: skipping') +endif \ No newline at end of file diff --git a/src/util/modules-load.d/msr.conf b/src/util/modules-load.d/msr.conf new file mode 100644 index 0000000000..3e5ee7fa15 --- /dev/null +++ b/src/util/modules-load.d/msr.conf @@ -0,0 +1 @@ +msr diff --git a/tools/virt-host-validate-qemu.c b/tools/virt-host-validate-qemu.c index 833bb1b914..f582e1146e 100644 --- a/tools/virt-host-validate-qemu.c +++ b/tools/virt-host-validate-qemu.c @@ -95,6 +95,13 @@ int virHostValidateQEMU(void) virValidatePass(); } + if (arch == VIR_ARCH_X86_64) { + if (virHostValidateDeviceExists("QEMU", "/dev/cpu/0/msr", + VIR_HOST_VALIDATE_WARN, + _("Load the 'msr' module for better x86 CPU features detection")) < 0) + ret = -1; + } + if (virHostValidateDeviceExists("QEMU", "/dev/vhost-net", VIR_VALIDATE_WARN, _("Load the 'vhost_net' module to improve performance of virtio networking")) < 0) -- 2.45.2

On 9/3/25 06:49, Hector Cao wrote:
On recent Intel CPUs, some of the CPU features (mostly vmx-* subfeatures) are listed and controlled via the MSRs (Model Specific Registers) instead of the traditional CPUID instruction method.
To be able to read the MSR's values, the kernel module msr has to be loaded and the values can be read via /dev/cpu/*/msr.
This commit introduces following changes:
- install modules-load file for msr this is only enabled: - On x86 arch - If user asks for it explicitly via a build option: meson -Dmsr_module_load=true build By default, it is disabled.
Thanks for the build option, and having it disabled by default! Some distros or distro flavors may not want this functionality. Also, AFAIK it's only needed for the qemu driver. If libvirt is built with support for multiple hypervisors, the file will be installed on those deployments too. E.g. it would needlessly be installed on a Xen deployment when libvirt is built with '-Ddriver_qemu=enabled -Ddriver_libxl=enabled -Dmsr_module_load=true'. Regards, Jim

On Thu, Sep 4, 2025 at 11:40 PM Jim Fehlig <jfehlig@suse.com> wrote:
On 9/3/25 06:49, Hector Cao wrote:
On recent Intel CPUs, some of the CPU features (mostly vmx-* subfeatures) are listed and controlled via the MSRs (Model Specific Registers) instead of the traditional CPUID instruction method.
To be able to read the MSR's values, the kernel module msr has to be loaded and the values can be read via /dev/cpu/*/msr.
This commit introduces following changes:
- install modules-load file for msr this is only enabled: - On x86 arch - If user asks for it explicitly via a build option: meson -Dmsr_module_load=true build By default, it is disabled.
Thanks for the build option, and having it disabled by default! Some distros or distro flavors may not want this functionality.
Hello Jim, Thanks for the feedback, I realized that the commit description is wrong wrt to the actual code, my bad. I propose the let the discussions continue in this thread to gather all the feedbacks but ultimately I will need to submit a v2 So here is the updated commit message: This commit introduces following changes: - install modules-load file for msr this is only enabled: - On x86 arch - the file installation is enabled by default, it can be disabled via a build option: meson -Dmsr_module_load=false build
Also, AFAIK it's only needed for the qemu driver. If libvirt is built with support for multiple hypervisors, the file will be installed on those deployments too. E.g. it would needlessly be installed on a Xen deployment when libvirt is built with '-Ddriver_qemu=enabled -Ddriver_libxl=enabled -Dmsr_module_load=true'.
Regards, Jim
-- Hector CAO Software Engineer – Partner Engineering Team hector.cao@canonical.com https://launc <https://launchpad.net/~hectorcao>hpad.net/~hectorcao <https://launchpad.net/~hectorcao> <https://launchpad.net/~hectorcao>

On 9/4/25 17:00, Hector Cao wrote:
On Thu, Sep 4, 2025 at 11:40 PM Jim Fehlig <jfehlig@suse.com <mailto:jfehlig@suse.com>> wrote:
On 9/3/25 06:49, Hector Cao wrote: > On recent Intel CPUs, some of the CPU features (mostly > vmx-* subfeatures) are listed and controlled via the > MSRs (Model Specific Registers) instead of the traditional > CPUID instruction method. > > To be able to read the MSR's values, the kernel module msr > has to be loaded and the values can be read via /dev/cpu/*/msr. > > This commit introduces following changes: > > - install modules-load file for msr > this is only enabled: > - On x86 arch > - If user asks for it explicitly via a build option: > meson -Dmsr_module_load=true build > By default, it is disabled.
Thanks for the build option, and having it disabled by default! Some distros or distro flavors may not want this functionality.
Hello Jim, Thanks for the feedback,
I realized that the commit description is wrong wrt to the actual code, my bad.
Heh, I totally missed that. I'd much prefer if the code matched the original description :-).
I propose the let the discussions continue in this thread to gather all the feedbacks but ultimately I will need to submit a v2 So here is the updated commit message:
This commit introduces following changes:
- install modules-load file for msr this is only enabled: - On x86 arch - the file installation is enabled by default, it can be disabled via a build option: meson -Dmsr_module_load=false build
Let's hear what others say, but my vote is to have it disabled by default. Regards, Jim

On Fri, Sep 5, 2025 at 1:48 AM Jim Fehlig <jfehlig@suse.com> wrote:
On 9/4/25 17:00, Hector Cao wrote:
On Thu, Sep 4, 2025 at 11:40 PM Jim Fehlig <jfehlig@suse.com <mailto:jfehlig@suse.com>> wrote:
On 9/3/25 06:49, Hector Cao wrote: > On recent Intel CPUs, some of the CPU features (mostly > vmx-* subfeatures) are listed and controlled via the > MSRs (Model Specific Registers) instead of the traditional > CPUID instruction method. > > To be able to read the MSR's values, the kernel module msr > has to be loaded and the values can be read via /dev/cpu/*/msr. > > This commit introduces following changes: > > - install modules-load file for msr > this is only enabled: > - On x86 arch > - If user asks for it explicitly via a build option: > meson -Dmsr_module_load=true build > By default, it is disabled.
Thanks for the build option, and having it disabled by default! Some
distros or
distro flavors may not want this functionality.
Hello Jim, Thanks for the feedback,
I realized that the commit description is wrong wrt to the actual code,
my bad.
Heh, I totally missed that. I'd much prefer if the code matched the original description :-).
Sorry about this again
I propose the let the discussions continue in this thread to gather all the feedbacks but ultimately I will need to submit a v2 So here is the updated commit message:
This commit introduces following changes:
- install modules-load file for msr this is only enabled: - On x86 arch - the file installation is enabled by default, it can be disabled via a build option: meson -Dmsr_module_load=false build
Let's hear what others say, but my vote is to have it disabled by default.
I hear and understand your preference. If we enable the module installation only when qemu driver is enabled, do you think that it is more "acceptable" ?
Regards, Jim
Regards Hector

On 9/4/25 17:59, Hector Cao wrote:
On Fri, Sep 5, 2025 at 1:48 AM Jim Fehlig <jfehlig@suse.com <mailto:jfehlig@suse.com>> wrote:
On 9/4/25 17:00, Hector Cao wrote: > > > On Thu, Sep 4, 2025 at 11:40 PM Jim Fehlig <jfehlig@suse.com <mailto:jfehlig@suse.com> > <mailto:jfehlig@suse.com <mailto:jfehlig@suse.com>>> wrote: > > On 9/3/25 06:49, Hector Cao wrote: > > On recent Intel CPUs, some of the CPU features (mostly > > vmx-* subfeatures) are listed and controlled via the > > MSRs (Model Specific Registers) instead of the traditional > > CPUID instruction method. > > > > To be able to read the MSR's values, the kernel module msr > > has to be loaded and the values can be read via /dev/cpu/*/msr. > > > > This commit introduces following changes: > > > > - install modules-load file for msr > > this is only enabled: > > - On x86 arch > > - If user asks for it explicitly via a build option: > > meson -Dmsr_module_load=true build > > By default, it is disabled. > > Thanks for the build option, and having it disabled by default! Some distros or > distro flavors may not want this functionality. > > > Hello Jim, > Thanks for the feedback, > > I realized that the commit description is wrong wrt to the actual code, my bad.
Heh, I totally missed that. I'd much prefer if the code matched the original description :-).
Sorry about this again
> I propose the let the discussions continue in this thread to gather all the > feedbacks but ultimately I will need to submit a v2 > So here is the updated commit message: > > This commit introduces following changes: > > - install modules-load file for msr > this is only enabled: > - On x86 arch > - the file installation is enabled by default, it can be disabled via a > build option: > meson -Dmsr_module_load=false build
Let's hear what others say, but my vote is to have it disabled by default.
I hear and understand your preference. If we enable the module installation only when qemu driver is enabled, do you think that it is more "acceptable" ?
That would be a nice improvement to this patch IMO. The file could be packaged with the qemu driver, which reminds me you'll need a corresponding change to libvirt.spec.in. Regards, Jim
participants (2)
-
Hector Cao
-
Jim Fehlig