Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3497180pxb; Mon, 4 Apr 2022 18:53:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfxymnKmSxSQgfOeoI5wFqFbVsrg5RF4ywpV5L1X1YW2fVspfYjXldT8G+GvLMX3dEYyDL X-Received: by 2002:a17:90b:17ca:b0:1c7:3010:5901 with SMTP id me10-20020a17090b17ca00b001c730105901mr1404332pjb.22.1649123588235; Mon, 04 Apr 2022 18:53:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649123588; cv=none; d=google.com; s=arc-20160816; b=SSwq+k/AIjL8jRJvxm6Y5jCDQnB+SnrOz9tPEGMAcazmJTEGMkztBRxN7IPSNjsDzd V0/VDNzX4N3oOoUFUhUB6ZGgkvpP751iXiRfhWNwS8EpLgx87zLnWzOpnQxu54t6cFSo 4poZ7T3Zw8DuBEQqDDLFC8ANYD9q9n84oO6fvwupA2wc0L1MT/eA8SPFSZ9DS2KM+YbE okkw5Se6d0H21MatpvqX6UHOxUN0IeIvzRJFGJZzZ+npn3MTlnjzarWLAon/tt71cPIQ e+yxBdEYJnKfQdKlCcGOIVLere7KJL04eCzSmTysPr+tk/hBV6mHRIwhYO7OMAiPFnko kIOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=0Ey2lNdsCChzoLCUy+zIncItUh4YgdUSQVkOFsPY+PQ=; b=0ZXCXbqq7IvlGNmUzTn7WlhaYlZQPznT0wLdKZEuc7UsZtZpTRg3KN6b0oOb6kiwvi JMMSG1FJcVds6E8bJxFqJcZrClxThpx9ILy/t57aScoIl2jh3hSOg5pOwEIUwWCtwQ0P Ezfl62QzMH8XGsZTiqDukKCeBx+c4ZBEuT97FcRUK1TNLoMlXt8Y5BcJB40EX47uBKfv cPQByonL9jFr8wIGMSSK7mo9njfYYljl8ByFvepR5NIF0XUog68ZGYUC3Be9lJJobwit H7j6f9+6nAhL4SdSIMNjKUXLJCYycFV1k5rW4EOJXofTyOM/PgREugu8VSUrSNnkder6 c/NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cCdp8jrm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c14-20020a170902b68e00b0015403597e3asi9864046pls.248.2022.04.04.18.53.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 18:53:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cCdp8jrm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3294B371DA7; Mon, 4 Apr 2022 17:55:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380949AbiDDVWM (ORCPT + 99 others); Mon, 4 Apr 2022 17:22:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380392AbiDDT6d (ORCPT ); Mon, 4 Apr 2022 15:58:33 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DC9B2FE42; Mon, 4 Apr 2022 12:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649102196; x=1680638196; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=qVd2+lCgjElgVwX9Ld2sTZVzh1JVcgMBp6qK+c5LCoo=; b=cCdp8jrmCBythpZyyT6LT89aC5fwDremHSG/wm+26i64/4TyA8S+31Cg cQtlBlHddCIOlAbrBjUHGwwImYJmVAZOBJbavQWHIxpgKcsQIxaZvOjly kx/K7fHyHIVBXOsLm/n1l7fF7UeWC3kLn/+DKcKx/EWCrOrPB/IEFXDKf U6EK/YGD/0fatA4/Kkp0vj0w+SZ995FwAflTTZU0IWNi0XfEVPpAHIres yMDjhz7TvvK7YL4OeMGw36BXLPibzVsH4Bq3gvtjDsCp+GR1WpxTTwFtL vD4yrZAFAafHJIQYhvL5aTVH7Is9eiFlhLYJ3FBOkVZtBW6uUAuAUNx75 g==; X-IronPort-AV: E=McAfee;i="6200,9189,10307"; a="241183795" X-IronPort-AV: E=Sophos;i="5.90,235,1643702400"; d="scan'208";a="241183795" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2022 12:56:36 -0700 X-IronPort-AV: E=Sophos;i="5.90,235,1643702400"; d="scan'208";a="721773941" Received: from skokoori-mobl1.amr.corp.intel.com (HELO [10.209.7.74]) ([10.209.7.74]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2022 12:56:35 -0700 Message-ID: Date: Mon, 4 Apr 2022 12:56:35 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0 Thunderbird/91.5.0 Subject: Re: [PATCH v2 5/6] platform/x86: intel_tdx_attest: Add TDX Guest attestation interface driver Content-Language: en-US To: Hans de Goede , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Mark Gross Cc: "H . Peter Anvin" , "Kirill A . Shutemov" , Tony Luck , Andi Kleen , linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org References: <054b22e81e88379a5a8459c19e89a335531c1bdd.1648664666.git.sathyanarayanan.kuppuswamy@intel.com> <8308a830-3096-3f94-4f12-5fd2c290524e@redhat.com> From: Sathyanarayanan Kuppuswamy In-Reply-To: <8308a830-3096-3f94-4f12-5fd2c290524e@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Hans, On 4/4/22 3:07 AM, Hans de Goede wrote: >> +static int __init tdx_attest_init(void) >> +{ >> + dma_addr_t handle; >> + long ret = 0; >> + >> + mutex_lock(&attestation_lock); >> + >> + ret = misc_register(&tdx_attest_device); >> + if (ret) { >> + pr_err("misc device registration failed\n"); >> + mutex_unlock(&attestation_lock); >> + return ret; >> + } > Why not do this as the last thing of the probe? We need misc device reference in dma_alloc_coherent() and dma_set_coherent_mask() calls. This is the reason for keeping misc_register() at the beginining of the init function. > > That will avoid the need to unregister this again in all > the error-exit paths and also fixes a possible deadlock. > Agree. But, unless we create another device locally, I don't think we can avoid this. Do you prefer this approach? > Right now you possibly have: > > 1. probe() locks attestation_lock > 2. probe() registers misc-device > 3. userspace calls tdx_attest_ioctl > 4. tdx_attest_ioctl blocks waiting for attestastion_lock > 5. Something goes wrong in probe, probe calls > misc_deregister() > 6. misc_deregister waits for the ioctl to finish > 7. deadlock > > I'm not sure about 6, but if 6 does not happen then > instead we now have tdx_attest_ioctl running > after the misc_deregister, with tdquote_data and > tdreport_data as NULL, or pointing to free-ed memory > leading to various crash scenarios. Makes sense. But as I have mentioned above, we have reason for keeping the misc_register() at the begining of the init function. One way to avoid this deadlock is to use global initalization check. --- a/drivers/platform/x86/intel/tdx/intel_tdx_attest.c +++ b/drivers/platform/x86/intel/tdx/intel_tdx_attest.c @@ -48,6 +48,8 @@ static void *tdreport_data; /* DMA handle used to allocate and free tdquote DMA buffer */ dma_addr_t tdquote_dma_handle; +static bool device_initialized; + static void attestation_callback_handler(void) { complete(&attestation_done); @@ -60,6 +62,9 @@ static long tdx_attest_ioctl(struct file *file, unsigned int cmd, struct tdx_gen_quote tdquote_req; long ret = 0; + if (!device_initialized) + return -ENODEV; + mutex_lock(&attestation_lock); switch (cmd) { @@ -191,6 +196,8 @@ static int __init tdx_attest_init(void) mutex_unlock(&attestation_lock); + device_initialized = true; + pr_debug("module initialization success\n"); return 0; Please let me know your comment on above solution. > > TL;DR: you must always delay registering any > interfaces for userspace until your code is > ready to deal with userspace calls. > > Regards, > > Hans > > p.s. > > As I mentioned with v1: > > -- Sathyanarayanan Kuppuswamy Linux Kernel Developer