linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dionna Glaze <dionnaglaze@google.com>
To: linux-kernel@vger.kernel.org, x86@kernel.org
Cc: Dionna Glaze <dionnaglaze@google.com>,
	Tom Lendacky <Thomas.Lendacky@amd.com>,
	Peter Gonda <pgonda@google.com>,
	Borislav Petkov <Borislav.Petkov@amd.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Liam Merwick <liam.merwick@oracle.com>,
	Yang Yingliang <yangyingliang@huawei.com>,
	Haowen Bai <baihaowen@meizu.com>
Subject: [PATCH v13 4/4] virt: sev-guest: self-throttle guest request retries
Date: Tue, 24 Jan 2023 21:14:55 +0000	[thread overview]
Message-ID: <20230124211455.2563674-5-dionnaglaze@google.com> (raw)
In-Reply-To: <20230124211455.2563674-1-dionnaglaze@google.com>

When throttled, the driver will reschedule itself and then try
again after sleeping half its ratelimit time to avoid a big wait queue.
The ioctl may block indefinitely, but that has always been the case
when deferring these requests to the host.

Cc: Tom Lendacky <Thomas.Lendacky@amd.com>
Cc: Peter Gonda <pgonda@google.com>
Cc: Borislav Petkov <Borislav.Petkov@amd.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Liam Merwick <liam.merwick@oracle.com>
Cc: Yang Yingliang <yangyingliang@huawei.com>
Cc: Haowen Bai <baihaowen@meizu.com>

Signed-off-by: Dionna Glaze <dionnaglaze@google.com>
---
 drivers/virt/coco/sev-guest/sev-guest.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
index e82f080aa679..af0645f40e95 100644
--- a/drivers/virt/coco/sev-guest/sev-guest.c
+++ b/drivers/virt/coco/sev-guest/sev-guest.c
@@ -14,6 +14,7 @@
 #include <linux/io.h>
 #include <linux/platform_device.h>
 #include <linux/miscdevice.h>
+#include <linux/ratelimit.h>
 #include <linux/set_memory.h>
 #include <linux/fs.h>
 #include <crypto/aead.h>
@@ -48,12 +49,22 @@ struct snp_guest_dev {
 	struct snp_req_data input;
 	u32 *os_area_msg_seqno;
 	u8 *vmpck;
+
+	struct ratelimit_state rs;
 };
 
 static u32 vmpck_id;
 module_param(vmpck_id, uint, 0444);
 MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
 
+static int rate_s = 1;
+module_param(rate_s, int, 0444);
+MODULE_PARM_DESC(rate_s, "The rate limit interval in seconds to limit requests to.");
+
+static int rate_burst = 2;
+module_param(rate_burst, int, 0444);
+MODULE_PARM_DESC(rate_burst, "The rate limit burst amount to limit requests to.");
+
 /* Mutex to serialize the shared buffer access and command handling. */
 static DEFINE_MUTEX(snp_cmd_mutex);
 
@@ -339,6 +350,15 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,
 		return rc;
 
 retry:
+	/*
+	 * Rate limit commands internally since the host can also throttle, and
+	 * the guest shouldn't create a tight request spin that could end up
+	 * getting this VM throttled more heavily.
+	 */
+	if (!__ratelimit(&snp_dev->rs)) {
+		schedule_timeout_interruptible((rate_s * HZ) / 2);
+		goto retry;
+	}
 	/*
 	 * Call firmware to process the request. In this function the encrypted
 	 * message enters shared memory with the host. So after this call the
@@ -760,6 +780,8 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	if (ret)
 		goto e_free_cert_data;
 
+	ratelimit_state_init(&snp_dev->rs, rate_s * HZ, rate_burst);
+
 	dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id);
 	return 0;
 
-- 
2.39.1.405.gd4c25cc71f-goog


      parent reply	other threads:[~2023-01-24 21:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-24 21:14 [PATCH v13 0/4] Add throttling detection to sev-guest Dionna Glaze
2023-01-24 21:14 ` [PATCH v13 1/4] virt/coco/sev-guest: Add throttling awareness Dionna Glaze
2023-01-25 17:28   ` Tom Lendacky
2023-01-25 17:48     ` Dionna Amalie Glaze
2023-01-25 18:31       ` Tom Lendacky
2023-01-30 11:13   ` Borislav Petkov
2023-01-30 16:36     ` Tom Lendacky
2023-02-08 19:24       ` Dionna Amalie Glaze
2023-02-08 19:29         ` Borislav Petkov
2023-02-08 19:30           ` Dionna Amalie Glaze
2023-01-24 21:14 ` [PATCH v13 2/4] crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL Dionna Glaze
2023-01-24 21:14 ` [PATCH v13 3/4] x86/sev: Change snp_guest_issue_request's fw_err Dionna Glaze
2023-01-24 21:14 ` Dionna Glaze [this message]

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=20230124211455.2563674-5-dionnaglaze@google.com \
    --to=dionnaglaze@google.com \
    --cc=Borislav.Petkov@amd.com \
    --cc=Thomas.Lendacky@amd.com \
    --cc=baihaowen@meizu.com \
    --cc=liam.merwick@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pgonda@google.com \
    --cc=x86@kernel.org \
    --cc=yangyingliang@huawei.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).