From nobody Mon Feb 9 07:20:48 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF23F288510 for ; Wed, 14 May 2025 18:41:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747248115; cv=none; b=DFzQnMaldP5c6aFE6JDLXgxBRUS2eda8M/gUyPtYnLHWgVkY4djyRHsT9GvFEo53jX8R+OVo7iu2+64rmtCasMDh4IyZ0Q9S7QwTHuDBxK660kCAx4/9+o4Y888GNfiebffxh19+3Uql8RMbUBENW6K3z6uaZCkPB4nEhu3TR80= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747248115; c=relaxed/simple; bh=jCrq91a6ZGUBMd0hXPZN1ZpYaiLLg06xn/FVy+YAfkM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Rv4veueeC5oOgWRrEpArH8kbsVKbT/Bn1L2xS13Tto7y3CpOJau8BAKuiInVJhGNnLT9JanDzVGDdum2ozKHd2MBjxfNTrPLuNldq+NjqRMsZYUjQzRqZnA6OUpkzZ2lYpBtAxWU6sGEEtxfg7KnZbr3jPGMprHD0Y70XJUh/Ac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=i/nx3Z2g; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="i/nx3Z2g" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-22fdf1d9fcaso896175ad.0 for ; Wed, 14 May 2025 11:41:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747248112; x=1747852912; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sqs1ABBrK/SoRx/WU68XgIhBrfwY+6u36pWyXJ3N2cA=; b=i/nx3Z2gc393HOslsC47VZ1n0qAR8iJuF537G+3+ZnHbt/rAMrLwo4uWiltDsdyomU 3wdklUdxOziVMCrLYuKb4Iq1kMgRET9n2C1xcbctCWRzhZ/nHmQ73Xo6W2w3ScDM2U34 YaA4u3tyt9oipiSFclk4D0MBy0SRTbPeo41DFSL///Oejvd6165FJNQNvtsqWc8gFjnu jG76UW7+AquS2+aUeVd99ISqf3jO5Loa0S5rs/8tht2R22ofQwdMLuAF56LEpBbEPCW3 YlC/G56nMpfweQuDMfYYQeoS9c2Fw524942kvQz3FJgfEfj71TIZ9IfpbgSKyC/qVC/C O7Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747248112; x=1747852912; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sqs1ABBrK/SoRx/WU68XgIhBrfwY+6u36pWyXJ3N2cA=; b=MeOvVNeKF9yz3Tih75xWoZCNZakPeCabARvqVHUXNpQvmvCKBvZmxJ5UIgrD5Dnenc F3XPlfNMksThIzRnAM7s4D7HzAXPHbXFvvhAa//PU9mhxlQZbCB8RE2R3sgWbIWcNm5z NHLNXjrGPgR6qFqpn5brSzC3n1i9+TEZMHHQSa5dCBEQik6/ZkISSBLr8c3BaCZrctSO 6PwtD5xmD6xwzB7NrT4921hod+ylzqBeYTDE7Pqt0LaPt0x2Fx8xCdyNFRoFuVAQht9o LSVL2bLMvmu2BRQxhv/IscZPe+DYexETHJp5iYfKa0F4EYP1iC0T8a2ia6d/Z5kFxtbt GiCw== X-Gm-Message-State: AOJu0YyuYnCV21tgkOHYRCYqTHu2Ry7AG2M/wBP8Hq59HxdtHU/v019P fElEphCuVeUlzo6Hx/31aayX7zshSGxWMLlqfbDktP/X47flS5d6PxEl2LOS6mFKV3OK3UTrq72 H32dvLz5qZyIS9226mf9BSg== X-Google-Smtp-Source: AGHT+IEj0u063ItqXbBprVsscCCcufQDFXSgBslg6Ix6TcQKSTjaE0XjkqMYzsqDG1lkKArewjPTBhxYTeTzIsGYgg== X-Received: from plnr13.prod.google.com ([2002:a17:903:190d:b0:227:967c:4f38]) (user=dionnaglaze job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ea0a:b0:220:e338:8d2 with SMTP id d9443c01a7336-231980fba76mr69818855ad.21.1747248112013; Wed, 14 May 2025 11:41:52 -0700 (PDT) Date: Wed, 14 May 2025 18:41:34 +0000 In-Reply-To: <20250514184136.238446-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250514184136.238446-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.49.0.1045.g170613ef41-goog Message-ID: <20250514184136.238446-2-dionnaglaze@google.com> Subject: [PATCH v4 1/2] kvm: sev: Add SEV-SNP guest request throttling From: Dionna Glaze To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, Dionna Glaze , Thomas Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Borislav Petkov , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The AMD-SP is a precious resource that doesn't have a scheduler other than a mutex lock queue. To avoid customers from causing a DoS, a mem_enc_ioctl command for rate limiting guest requests is added. Recommended values are {.interval_ms =3D 1000, .burst =3D 1} or {.interval_ms =3D 2000, .burst =3D 2} to average 1 request every second. You may need to allow 2 requests back to back to allow for the guest to query the certificate length in an extended guest request without a pause. The 1 second average is our target for quality of service since empirical tests show that 64 VMs can concurrently request an attestation report with a maximum latency of 1 second. We don't anticipate more concurrency than that for a seldom used request for a majority well-behaved set of VMs. The majority point is decided as >64 VMs given the assumed 128 VM count for "extreme load". Cc: Thomas Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Borislav Petkov Cc: Sean Christopherson Signed-off-by: Dionna Glaze --- .../virt/kvm/x86/amd-memory-encryption.rst | 23 ++++++++++++++ arch/x86/include/uapi/asm/kvm.h | 7 +++++ arch/x86/kvm/svm/sev.c | 31 +++++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 ++ 4 files changed, 63 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documen= tation/virt/kvm/x86/amd-memory-encryption.rst index 1ddb6a86ce7f..1b5b4fc35aac 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -572,6 +572,29 @@ Returns: 0 on success, -negative on error See SNP_LAUNCH_FINISH in the SEV-SNP specification [snp-fw-abi]_ for furth= er details on the input parameters in ``struct kvm_sev_snp_launch_finish``. =20 +21. KVM_SEV_SNP_SET_REQUEST_THROTTLE_RATE +----------------------------------------- + +The KVM_SEV_SNP_SET_REQUEST_THROTTLE_RATE command is used to set a per-VM = rate +limit on responding to requests for AMD-SP to process a guest request. +The AMD-SP is a global resource with limited capacity, so to avoid noisy +neighbor effects, the host may set a request rate for guests. + +Parameters (in): struct kvm_sev_snp_set_request_throttle_rate + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_set_request_throttle_rate { + __u32 interval_ms; + __u32 burst; + }; + +The interval will be translated into jiffies, so if it after transformation +the interval is 0, the command will return ``-EINVAL``. The ``burst`` value +must be greater than 0. + Device attribute API =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 460306b35a4b..d92242d9b9af 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -708,6 +708,8 @@ enum sev_cmd_id { KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_SNP_LAUNCH_FINISH, =20 + KVM_SEV_SNP_SET_REQUEST_THROTTLE_RATE, + KVM_SEV_NR_MAX, }; =20 @@ -877,6 +879,11 @@ struct kvm_sev_snp_launch_finish { __u64 pad1[4]; }; =20 +struct kvm_sev_snp_set_request_throttle_rate { + __u32 interval_ms; + __u32 burst; +}; + #define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) #define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) =20 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a7a7dc507336..febf4b45fddf 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2535,6 +2535,29 @@ static int snp_launch_finish(struct kvm *kvm, struct= kvm_sev_cmd *argp) return ret; } =20 +static int snp_set_request_throttle_ms(struct kvm *kvm, struct kvm_sev_cmd= *argp) +{ + struct kvm_sev_info *sev =3D to_kvm_sev_info(kvm); + struct kvm_sev_snp_set_request_throttle_rate params; + int ret; + u64 jiffies; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + jiffies =3D (params.interval_ms * HZ) / 1000; + + if (!jiffies || !params.burst) + return -EINVAL; + + ratelimit_state_init(&sev->snp_guest_msg_rs, jiffies, params.burst); + + return 0; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2640,6 +2663,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *a= rgp) case KVM_SEV_SNP_LAUNCH_FINISH: r =3D snp_launch_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_SET_REQUEST_THROTTLE_RATE_MS: + r =3D snp_set_request_throttle_ms(kvm, &sev_cmd); + break; default: r =3D -EINVAL; goto out; @@ -4015,6 +4041,11 @@ static int snp_handle_guest_req(struct vcpu_svm *svm= , gpa_t req_gpa, gpa_t resp_ =20 mutex_lock(&sev->guest_req_mutex); =20 + if (!__ratelimit(&sev->snp_guest_msg_rs)) { + rc =3D SNP_GUEST_VMM_ERR_BUSY; + goto out_unlock; + } + if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE)) { ret =3D -EIO; goto out_unlock; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f16b068c4228..0a7c8d3a7560 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -112,6 +112,8 @@ struct kvm_sev_info { void *guest_req_buf; /* Bounce buffer for SNP Guest Request input */ void *guest_resp_buf; /* Bounce buffer for SNP Guest Request output */ struct mutex guest_req_mutex; /* Must acquire before using bounce buffers= */ + + struct ratelimit_state snp_guest_msg_rs; /* Limit guest requests */ }; =20 struct kvm_svm { --=20 2.49.0.1045.g170613ef41-goog