Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4713943pxv; Tue, 20 Jul 2021 09:56:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1bkmh3cWFIdIZsAPaKJm3LhqJAHT2naZFK5K49BReib3VeNdFve77mVwGKNAP9TYLLfXn X-Received: by 2002:a05:6402:3454:: with SMTP id l20mr41748431edc.170.1626800166957; Tue, 20 Jul 2021 09:56:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626800166; cv=none; d=google.com; s=arc-20160816; b=MT0tlsCdLSi2340bHM5Ql75CfvjMpIeifafSgYikbdBWy3HFx0kPO3YH4ognCZ6q7/ iyy3WZVa+ujdGxeXERWqlDFXdqZjFMmeNUp9xPxpUnALJMoefuAwDkb7oR0szWTDzduf olWJKRdvRdLuuzO/uW9/f7gaH4wf1bpyWQ1hIrbzRRhmcPUyJD+Q//P6ZT+kGnQ1dDNO atPWl3ysfxk15h5JfsUi1xfMcAzcEqs7uLEW3zovz2wq76GeKrqL9JAZXF84vbo8ULBM ZPGVPiB15GiuFfQ2GgzTSUEktDLeiKJFEsVzINv/XBSlArtCM8FrI+PgaX0sgkYbKeed DzIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:autocrypt:from :references:cc:to:subject; bh=A2uugK8LgKmDbbMo/koBOn0Tw3jWTV34CKs9Vcty8Dw=; b=aYmxqyqmalEPumXylZJI2RM/IYyIrWYJbNjq0n/CNw+OC/hW6OH1OHVMxKF7/DdjVx yrlJ1WkOPDHxRAm4y7WD34f8AX/2e4S+CLk/WO2+6ZJFhy4sACb7S5Op90Ku9+m2s/UG pGMZrt39Fo4wDS52T2Fo8JpLSQyWgn8TghWUxHAqw5G1OnnrO3xfqP1oalsnhcvFYRoh k/K70H2iQPA9J10yW8sQeSCs4HI2ne3OBusgBd2fc+JQnId1osmxsgBoN6AWDhnHkyu/ 5bDiNme2Qtn8uA7G6CNxa8cQ+/C4RdpsVirEh+QLWaDuNDL+VH8y1WGont3Tm+r+YDrI kJ4Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z17si2265665edr.452.2021.07.20.09.55.42; Tue, 20 Jul 2021 09:56:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230037AbhGTQNS (ORCPT + 99 others); Tue, 20 Jul 2021 12:13:18 -0400 Received: from mga12.intel.com ([192.55.52.136]:22084 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229638AbhGTQNJ (ORCPT ); Tue, 20 Jul 2021 12:13:09 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10051"; a="190861456" X-IronPort-AV: E=Sophos;i="5.84,255,1620716400"; d="scan'208";a="190861456" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2021 09:53:16 -0700 X-IronPort-AV: E=Sophos;i="5.84,255,1620716400"; d="scan'208";a="469822709" Received: from kvadariv-mobl1.amr.corp.intel.com (HELO [10.212.155.118]) ([10.212.155.118]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2021 09:53:15 -0700 Subject: Re: [PATCH v3 5/6] platform/x86: intel_tdx_attest: Add TDX Guest attestation interface driver To: Kuppuswamy Sathyanarayanan , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , Hans de Goede , Mark Gross , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Peter H Anvin , Tony Luck , Dan Williams , Andi Kleen , Kirill Shutemov , Sean Christopherson , Kuppuswamy Sathyanarayanan , x86@kernel.org, linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org References: <20210720045552.2124688-1-sathyanarayanan.kuppuswamy@linux.intel.com> <20210720045552.2124688-6-sathyanarayanan.kuppuswamy@linux.intel.com> From: Dave Hansen Autocrypt: addr=dave.hansen@intel.com; keydata= xsFNBE6HMP0BEADIMA3XYkQfF3dwHlj58Yjsc4E5y5G67cfbt8dvaUq2fx1lR0K9h1bOI6fC oAiUXvGAOxPDsB/P6UEOISPpLl5IuYsSwAeZGkdQ5g6m1xq7AlDJQZddhr/1DC/nMVa/2BoY 2UnKuZuSBu7lgOE193+7Uks3416N2hTkyKUSNkduyoZ9F5twiBhxPJwPtn/wnch6n5RsoXsb ygOEDxLEsSk/7eyFycjE+btUtAWZtx+HseyaGfqkZK0Z9bT1lsaHecmB203xShwCPT49Blxz VOab8668QpaEOdLGhtvrVYVK7x4skyT3nGWcgDCl5/Vp3TWA4K+IofwvXzX2ON/Mj7aQwf5W iC+3nWC7q0uxKwwsddJ0Nu+dpA/UORQWa1NiAftEoSpk5+nUUi0WE+5DRm0H+TXKBWMGNCFn c6+EKg5zQaa8KqymHcOrSXNPmzJuXvDQ8uj2J8XuzCZfK4uy1+YdIr0yyEMI7mdh4KX50LO1 pmowEqDh7dLShTOif/7UtQYrzYq9cPnjU2ZW4qd5Qz2joSGTG9eCXLz5PRe5SqHxv6ljk8mb ApNuY7bOXO/A7T2j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+8eucbXxKiNEbs21IqBZbbKdY1GkcEGTE7AnkA3Y6YB7I/j9mQ3hCgm5muJuhM/2Fr OPsw5tV/LmQ5GXH0JQ/TZXWygyRFyyI2FqNTx4WHqUn3yFj8rwTAU1tluRUYyeLy0ayUlKBH ybj0N71vWO936MqP6haFERzuPAIpxj2ezwu0xb1GjTk4ynna6h5GjnKgdfOWoRtoWndMZxbA z5cecg== Message-ID: Date: Tue, 20 Jul 2021 09:53:13 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210720045552.2124688-6-sathyanarayanan.kuppuswamy@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > +/* Used in Quote memory allocation */ > +#define QUOTE_SIZE (2 * PAGE_SIZE) > +/* Get Quote timeout in msec */ > +#define GET_QUOTE_TIMEOUT (5000) The comment is good, but even better would be to call this: GET_QUOTE_TIMEOUT_MS > +/* Mutex to synchronize attestation requests */ > +static DEFINE_MUTEX(attestation_lock); > +/* Completion object to track attestation status */ > +static DECLARE_COMPLETION(attestation_done); > +/* Buffer used to copy report data in attestation handler */ > +static u8 report_data[TDX_REPORT_DATA_LEN]; > +/* Data pointer used to get TD Quote data in attestation handler */ > +static void *tdquote_data; > +/* Data pointer used to get TDREPORT data in attestation handler */ > +static void *tdreport_data; Are these *really* totally unknown, opaque blobs? Why not give them an actual data type? > +/* DMA handle used to allocate and free tdquote DMA buffer */ > +dma_addr_t tdquote_dma_handle; That's an unreadable jumble. Please add some line breaks and try to logically group those. > +static void attestation_callback_handler(void) > +{ > + complete(&attestation_done); > +} > + > +static long tdg_attest_ioctl(struct file *file, unsigned int cmd, > + unsigned long arg) > +{ > + void __user *argp = (void __user *)arg; > + long ret = 0; > + > + mutex_lock(&attestation_lock); > + > + switch (cmd) { > + case TDX_CMD_GET_TDREPORT: > + if (copy_from_user(report_data, argp, TDX_REPORT_DATA_LEN)) { > + ret = -EFAULT; > + break; > + } > + > + /* Generate TDREPORT_STRUCT */ > + if (tdx_mcall_tdreport(virt_to_phys(tdreport_data), > + virt_to_phys(report_data))) { Having that take a physical address seems like a mistake. Why not just do the virt_to_phys() inside the helper? Also, this isn't very clear that there is an input and an output. Can you rename these to make that more clear? > + ret = -EIO; > + break; > + } > + > + if (copy_to_user(argp, tdreport_data, TDX_TDREPORT_LEN)) > + ret = -EFAULT; > + break; > + case TDX_CMD_GEN_QUOTE: > + /* Copy TDREPORT data from user buffer */ > + if (copy_from_user(tdquote_data, argp, TDX_TDREPORT_LEN)) { > + ret = -EFAULT; > + break; > + } > + > + /* Submit GetQuote Request */ > + if (tdx_hcall_get_quote(virt_to_phys(tdquote_data))) { > + ret = -EIO; > + break; > + } > + > + /* Wait for attestation completion */ > + ret = wait_for_completion_interruptible_timeout( > + &attestation_done, > + msecs_to_jiffies(GET_QUOTE_TIMEOUT)); > + if (ret <= 0) { > + ret = -EIO; > + break; > + } > + > + if (copy_to_user(argp, tdquote_data, QUOTE_SIZE)) > + ret = -EFAULT; > + > + break; > + case TDX_CMD_GET_QUOTE_SIZE: > + ret = put_user(QUOTE_SIZE, (u64 __user *)argp); > + break; > + default: > + pr_err("cmd %d not supported\n", cmd); > + break; First of all, drivers shouldn't pollute the kernel log on bad input. Second, won't this inherit the ret=0 value and return success? > + } > + > + mutex_unlock(&attestation_lock); > + > + return ret; > +} > + > +static const struct file_operations tdg_attest_fops = { > + .owner = THIS_MODULE, > + .unlocked_ioctl = tdg_attest_ioctl, > + .llseek = no_llseek, > +}; > + > +static struct miscdevice tdg_attest_device = { > + .minor = MISC_DYNAMIC_MINOR, > + .name = "tdx-attest", > + .fops = &tdg_attest_fops, > +}; > + > +static int __init tdg_attest_init(void) > +{ > + dma_addr_t handle; > + long ret = 0; The function returns 'int', yet 'ret' is a long. Why? > + ret = misc_register(&tdg_attest_device); > + if (ret) { > + pr_err("misc device registration failed\n"); > + return ret; > + } > + > + tdreport_data = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 0); > + if (!tdreport_data) { > + ret = -ENOMEM; > + goto failed; > + } Why does this need to use the page allocator directly? Why does it need to zero the memory? Why does it need to get a whole page? If it really only needs a single page, why not use __get_free_page()? > + > + ret = dma_set_coherent_mask(tdg_attest_device.this_device, > + DMA_BIT_MASK(64)); > + if (ret) { > + pr_err("dma set coherent mask failed\n"); > + goto failed; > + } > + > + /* Allocate DMA buffer to get TDQUOTE data from the VMM */ > + tdquote_data = dma_alloc_coherent(tdg_attest_device.this_device, > + QUOTE_SIZE, &handle, > + GFP_KERNEL | __GFP_ZERO); > + if (!tdquote_data) { > + ret = -ENOMEM; > + goto failed; > + } > + > + tdquote_dma_handle = handle; > + > + /* > + * Currently tdg_event_notify_handler is only used in attestation > + * driver. But, WRITE_ONCE is used as benign data race notice. > + */ > + WRITE_ONCE(tdg_event_notify_handler, attestation_callback_handler); > + > + pr_debug("module initialization success\n"); > + > + return 0; > + > +failed: > + if (tdreport_data) > + free_pages((unsigned long)tdreport_data, 0); > + > + misc_deregister(&tdg_attest_device); > + > + pr_debug("module initialization failed\n"); > + > + return ret; > +} ...