All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: Isaku Yamahata <isaku.yamahata@intel.com>
Cc: kvm@vger.kernel.org
Subject: [bug report] KVM: TDX: Get system-wide info about TDX module on initialization
Date: Tue, 9 Sep 2025 11:28:37 +0300	[thread overview]
Message-ID: <aL_lNXLD3XG496lW@stanley.mountain> (raw)

Hello Isaku Yamahata,

Commit 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module
on initialization") from Oct 30, 2024 (linux-next), leads to the
following Smatch static checker warning:

	arch/x86/kvm/vmx/tdx.c:3464 __tdx_bringup()
	warn: missing error code 'r'

arch/x86/kvm/vmx/tdx.c
    3416 static int __init __tdx_bringup(void)
    3417 {
    3418         const struct tdx_sys_info_td_conf *td_conf;
    3419         int r, i;
    3420 
    3421         for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) {
    3422                 /*
    3423                  * Check if MSRs (tdx_uret_msrs) can be saved/restored
    3424                  * before returning to user space.
    3425                  *
    3426                  * this_cpu_ptr(user_return_msrs)->registered isn't checked
    3427                  * because the registration is done at vcpu runtime by
    3428                  * tdx_user_return_msr_update_cache().
    3429                  */
    3430                 tdx_uret_msrs[i].slot = kvm_find_user_return_msr(tdx_uret_msrs[i].msr);
    3431                 if (tdx_uret_msrs[i].slot == -1) {
    3432                         /* If any MSR isn't supported, it is a KVM bug */
    3433                         pr_err("MSR %x isn't included by kvm_find_user_return_msr\n",
    3434                                 tdx_uret_msrs[i].msr);
    3435                         return -EIO;
    3436                 }
    3437         }
    3438 
    3439         /*
    3440          * Enabling TDX requires enabling hardware virtualization first,
    3441          * as making SEAMCALLs requires CPU being in post-VMXON state.
    3442          */
    3443         r = kvm_enable_virtualization();
    3444         if (r)
    3445                 return r;
    3446 
    3447         cpus_read_lock();
    3448         r = __do_tdx_bringup();
    3449         cpus_read_unlock();
    3450 
    3451         if (r)
    3452                 goto tdx_bringup_err;
    3453 
    3454         /* Get TDX global information for later use */
    3455         tdx_sysinfo = tdx_get_sysinfo();
    3456         if (WARN_ON_ONCE(!tdx_sysinfo)) {
    3457                 r = -EINVAL;
    3458                 goto get_sysinfo_err;
    3459         }
    3460 
    3461         /* Check TDX module and KVM capabilities */
    3462         if (!tdx_get_supported_attrs(&tdx_sysinfo->td_conf) ||
    3463             !tdx_get_supported_xfam(&tdx_sysinfo->td_conf))
--> 3464                 goto get_sysinfo_err;

error code?

    3465 
    3466         if (!(tdx_sysinfo->features.tdx_features0 & MD_FIELD_ID_FEATURES0_TOPOLOGY_ENUM))
    3467                 goto get_sysinfo_err;

here too?

    3468 
    3469         /*
    3470          * TDX has its own limit of maximum vCPUs it can support for all
    3471          * TDX guests in addition to KVM_MAX_VCPUS.  Userspace needs to
    3472          * query TDX guest's maximum vCPUs by checking KVM_CAP_MAX_VCPU
    3473          * extension on per-VM basis.
    3474          *
    3475          * TDX module reports such limit via the MAX_VCPU_PER_TD global
    3476          * metadata.  Different modules may report different values.
    3477          * Some old module may also not support this metadata (in which
    3478          * case this limit is U16_MAX).
    3479          *
    3480          * In practice, the reported value reflects the maximum logical
    3481          * CPUs that ALL the platforms that the module supports can
    3482          * possibly have.
    3483          *
    3484          * Simply forwarding the MAX_VCPU_PER_TD to userspace could
    3485          * result in an unpredictable ABI.  KVM instead always advertise
    3486          * the number of logical CPUs the platform has as the maximum
    3487          * vCPUs for TDX guests.
    3488          *
    3489          * Make sure MAX_VCPU_PER_TD reported by TDX module is not
    3490          * smaller than the number of logical CPUs, otherwise KVM will
    3491          * report an unsupported value to userspace.
    3492          *
    3493          * Note, a platform with TDX enabled in the BIOS cannot support
    3494          * physical CPU hotplug, and TDX requires the BIOS has marked
    3495          * all logical CPUs in MADT table as enabled.  Just use
    3496          * num_present_cpus() for the number of logical CPUs.
    3497          */
    3498         td_conf = &tdx_sysinfo->td_conf;
    3499         if (td_conf->max_vcpus_per_td < num_present_cpus()) {
    3500                 pr_err("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than number of logical CPUs (%u).\n",
    3501                                 td_conf->max_vcpus_per_td, num_present_cpus());
    3502                 r = -EINVAL;
    3503                 goto get_sysinfo_err;
    3504         }
    3505 
    3506         if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids())) {
    3507                 r = -EINVAL;
    3508                 goto get_sysinfo_err;
    3509         }
    3510 
    3511         /*
    3512          * Leave hardware virtualization enabled after TDX is enabled
    3513          * successfully.  TDX CPU hotplug depends on this.
    3514          */
    3515         return 0;
    3516 
    3517 get_sysinfo_err:
    3518         __tdx_cleanup();
    3519 tdx_bringup_err:
    3520         kvm_disable_virtualization();
    3521         return r;
    3522 }

regards,
dan carpenter

             reply	other threads:[~2025-09-09  8:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-09  8:28 Dan Carpenter [this message]
2025-09-09 10:20 ` [bug report] KVM: TDX: Get system-wide info about TDX module on initialization Tony Lindgren

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aL_lNXLD3XG496lW@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=isaku.yamahata@intel.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.