]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
virt: tdx-guest: Transition to scoped_cond_guard for mutex operations
authorCedric Xing <cedric.xing@intel.com>
Tue, 6 May 2025 22:57:13 +0000 (17:57 -0500)
committerDan Williams <dan.j.williams@intel.com>
Fri, 9 May 2025 02:17:43 +0000 (19:17 -0700)
Replace mutex_lock_interruptible()/mutex_unlock() with scoped_cond_guard to
enhance code readability and maintainability.

Signed-off-by: Cedric Xing <cedric.xing@intel.com>
Acked-by: Dionna Amalie Glaze <dionnaglaze@google.com>
Link: https://patch.msgid.link/20250506-tdx-rtmr-v6-7-ac6ff5e9d58a@intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/virt/coco/tdx-guest/tdx-guest.c

index 34125c50183318e439d51d2ddc5055aa04dc99cc..60466c439a4bc63cb59bc80d68840727d12462fd 100644 (file)
@@ -262,7 +262,7 @@ static int wait_for_quote_completion(struct tdx_quote_buf *quote_buf, u32 timeou
        return (i == timeout) ? -ETIMEDOUT : 0;
 }
 
-static int tdx_report_new(struct tsm_report *report, void *data)
+static int tdx_report_new_locked(struct tsm_report *report, void *data)
 {
        u8 *buf;
        struct tdx_quote_buf *quote_buf = quote_data;
@@ -270,24 +270,16 @@ static int tdx_report_new(struct tsm_report *report, void *data)
        int ret;
        u64 err;
 
-       /* TODO: switch to guard(mutex_intr) */
-       if (mutex_lock_interruptible(&quote_lock))
-               return -EINTR;
-
        /*
         * If the previous request is timedout or interrupted, and the
         * Quote buf status is still in GET_QUOTE_IN_FLIGHT (owned by
         * VMM), don't permit any new request.
         */
-       if (quote_buf->status == GET_QUOTE_IN_FLIGHT) {
-               ret = -EBUSY;
-               goto done;
-       }
+       if (quote_buf->status == GET_QUOTE_IN_FLIGHT)
+               return -EBUSY;
 
-       if (desc->inblob_len != TDX_REPORTDATA_LEN) {
-               ret = -EINVAL;
-               goto done;
-       }
+       if (desc->inblob_len != TDX_REPORTDATA_LEN)
+               return -EINVAL;
 
        memset(quote_data, 0, GET_QUOTE_BUF_SIZE);
 
@@ -298,26 +290,23 @@ static int tdx_report_new(struct tsm_report *report, void *data)
        ret = tdx_do_report(KERNEL_SOCKPTR(desc->inblob),
                            KERNEL_SOCKPTR(quote_buf->data));
        if (ret)
-               goto done;
+               return ret;
 
        err = tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE);
        if (err) {
                pr_err("GetQuote hypercall failed, status:%llx\n", err);
-               ret = -EIO;
-               goto done;
+               return -EIO;
        }
 
        ret = wait_for_quote_completion(quote_buf, getquote_timeout);
        if (ret) {
                pr_err("GetQuote request timedout\n");
-               goto done;
+               return ret;
        }
 
        buf = kvmemdup(quote_buf->data, quote_buf->out_len, GFP_KERNEL);
-       if (!buf) {
-               ret = -ENOMEM;
-               goto done;
-       }
+       if (!buf)
+               return -ENOMEM;
 
        report->outblob = buf;
        report->outblob_len = quote_buf->out_len;
@@ -326,12 +315,16 @@ static int tdx_report_new(struct tsm_report *report, void *data)
         * TODO: parse the PEM-formatted cert chain out of the quote buffer when
         * provided
         */
-done:
-       mutex_unlock(&quote_lock);
 
        return ret;
 }
 
+static int tdx_report_new(struct tsm_report *report, void *data)
+{
+       scoped_cond_guard(mutex_intr, return -EINTR, &quote_lock)
+               return tdx_report_new_locked(report, data);
+}
+
 static bool tdx_report_attr_visible(int n)
 {
        switch (n) {