Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp7092426rwp; Tue, 18 Jul 2023 09:55:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlHlrvNo2iu0h79C8yu6NNFbuAJmaTocA/RoASrThQkYuIkAkOIk46ht5RsXYOw2gJ5k7aqm X-Received: by 2002:a05:6a20:5483:b0:133:862e:3ba0 with SMTP id i3-20020a056a20548300b00133862e3ba0mr14702849pzk.5.1689699339235; Tue, 18 Jul 2023 09:55:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689699339; cv=none; d=google.com; s=arc-20160816; b=of1+tSj69IrkKF+6DHiQNDgGi9jXTB5i9esY8zMebMOmHX3aioN1EII4mIFzvc80sC rTpPZgqC1HS0leABB+JAtXyLfZiT3cg1hoB71TkNUo22q8sYKzL4fomk1ObP4mX9WvPx xiqre+6SNXLTmHUM97FbwdbDxW5O/3kwjZfBsdXsI7b1++1KPV0L+fr9Q4+RjeSoL7G+ ChVgg9zVRWkkGWl4edY9IQpYqybQsDO/9YA1JzK/ZqzFrgC5a5jvLwo98BBw7QhfkkLd lyrtSQKeqQc6wbc27yR+l67jdV1oFc+jCw6YBT+YVBmV98ec9ScUgRFUrar/WgnYgZfY PMcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:message-id:organization :from:content-transfer-encoding:mime-version:date:references:subject :cc:to:dkim-signature; bh=ediveXKDCBiXut9B+yGRXqd4IX5lRrxEznGDg+mi4wU=; fh=TkVbJDo0Vvtz8LjoRiW/5MO289g6kUpB9sJckopOxXI=; b=RIl51DJipA16wKnfGvKAyW6ugbCPB+CkwGhk8mk5GfI0enqoc3Am+J7YHkigsnN+cN 2bkfrqSlyPbWJy7TI0KbSALokZ+GrShsnJWKBdyreBpgH7BzQjZPJ5rQ9RLimRvUwCV1 K6vgPZltMN6mxnmcH2cGRgOxLs6EjUbhKnj723vvOkI61yF17AKQPW7mVZUaK3eX/1Ey PGWlNuXr8md3dx1lJTJJ+S4f9uUvckUOBGyHSWwdu9gOHgATeqJweI9Ejsgt590FOf2q MW4BHoUW3rcgvOf4mVySleVPEtt+xeHY7KAfFYPGqM3/0toOqA81fD1kG/F/1+/IXFBZ k7uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PAIMvxww; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o11-20020a63920b000000b005582ab00bedsi1874857pgd.387.2023.07.18.09.55.25; Tue, 18 Jul 2023 09:55:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PAIMvxww; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233321AbjGRQk0 (ORCPT + 99 others); Tue, 18 Jul 2023 12:40:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233259AbjGRQkQ (ORCPT ); Tue, 18 Jul 2023 12:40:16 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94362171C; Tue, 18 Jul 2023 09:40:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689698409; x=1721234409; h=to:cc:subject:references:date:mime-version: content-transfer-encoding:from:message-id:in-reply-to; bh=UD4tx2ko1Ntid4p2qUjYdfLq2O8toM+6lgpiCZu5QuE=; b=PAIMvxwwyrULQCWvkXKiUzmDKJM86RRoFTRhDfxmz838paZdN7Rjs8J9 m+rQH2AGJzIF1VzW8aEc4ra4aNbYrk4usrQGDEMy6U5rOMwnotk+S/dD6 i7EHPO0xsrTGY1Cqr/FeBCufs1OZdg0U3mn5xBjZBGAJSzGXy22HTopcF aA1mOkD/8J5g8GIwZw9ROOO+MloCxXvzUJVneKfLrPEHgfs7hLyJCNiQN HM25LEQKzlLPbI2BQpIlfNyqzY05ZCzkT2NzgZhKNTtYlx4/DwuFg0Mik XQhLRoYuOwNJH2RLEXlTp04y5HtsI0kn/P0x221JSALFL+ehzZVUlBgcq A==; X-IronPort-AV: E=McAfee;i="6600,9927,10775"; a="397096521" X-IronPort-AV: E=Sophos;i="6.01,214,1684825200"; d="scan'208";a="397096521" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Jul 2023 09:40:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10775"; a="700975518" X-IronPort-AV: E=Sophos;i="6.01,214,1684825200"; d="scan'208";a="700975518" Received: from hhuan26-mobl.amr.corp.intel.com ([10.92.48.113]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-SHA; 18 Jul 2023 09:39:57 -0700 Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes To: "Jarkko Sakkinen" , dave.hansen@linux.intel.com, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , x86@kernel.org, "H. Peter Anvin" , "Dave Hansen" Cc: kai.huang@intel.com, reinette.chatre@intel.com, kristen@linux.intel.com, seanjc@google.com, stable@vger.kernel.org Subject: Re: [PATCH] x86/sgx: fix a NULL pointer References: <20230717202938.94989-1-haitao.huang@linux.intel.com> <95371eef-73ec-5541-ad97-829954cfb848@intel.com> Date: Tue, 18 Jul 2023 11:39:56 -0500 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: "Haitao Huang" Organization: Intel Message-ID: In-Reply-To: <95371eef-73ec-5541-ad97-829954cfb848@intel.com> User-Agent: Opera Mail/1.0 (Win32) X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 On Tue, 18 Jul 2023 09:30:11 -0500, Dave Hansen wrote: > On 7/17/23 13:29, Haitao Huang wrote: > ... >> @@ -248,11 +258,9 @@ static struct sgx_encl_page >> *__sgx_encl_load_page(struct sgx_encl *encl, >> return entry; >> } >> >> - if (!(encl->secs.epc_page)) { >> - epc_page = sgx_encl_eldu(&encl->secs, NULL); >> - if (IS_ERR(epc_page)) >> - return ERR_CAST(epc_page); >> - } >> + epc_page = sgx_encl_load_secs(encl); >> + if (IS_ERR(epc_page)) >> + return ERR_CAST(epc_page); >> >> epc_page = sgx_encl_eldu(entry, encl->secs.epc_page); >> if (IS_ERR(epc_page)) >> @@ -339,6 +347,13 @@ static vm_fault_t sgx_encl_eaug_page(struct >> vm_area_struct *vma, >> >> mutex_lock(&encl->lock); >> >> + epc_page = sgx_encl_load_secs(encl); >> + if (IS_ERR(epc_page)) { >> + if (PTR_ERR(epc_page) == -EBUSY) >> + vmret = VM_FAULT_NOPAGE; >> + goto err_out_unlock; >> + } > > Whenever I see one of these "make sure it isn't NULL", I always jump to > asking what *keeps* it from becoming NULL again. In both cases here, I > think that's encl->lock. > Yes, encl->lock protects all enclave states, the xarray holding encl_pages, SECS, VAs, etc. > A comment would be really nice here, maybe on sgx_encl_load_secs(). > Maybe: > > /* > * Ensure the SECS page is not swapped out. Must be called with > * encl->lock to protect _____ and ensure the SECS page is not > * swapped out again. > */ > Thanks for the suggestion. Lock should be held for the duration of SECS usage. So something like this? /* * Ensure the SECS page is not swapped out. Must be called with * encl->lock to protect the enclave states including SECS and * ensure the SECS page is not swapped out again while being used. */ >> diff --git a/arch/x86/kernel/cpu/sgx/main.c >> b/arch/x86/kernel/cpu/sgx/main.c >> index 166692f2d501..4662a364ce62 100644 >> --- a/arch/x86/kernel/cpu/sgx/main.c >> +++ b/arch/x86/kernel/cpu/sgx/main.c >> @@ -257,6 +257,10 @@ static void sgx_reclaimer_write(struct >> sgx_epc_page *epc_page, >> >> mutex_lock(&encl->lock); >> >> + /* Should not be possible */ >> + if (WARN_ON(!(encl->secs.epc_page))) >> + goto out; > > That comment isn't super helpful. We generally don't WARN_ON() things > that should happen. *Why* is it not possible? > When this part of code is reached, the reclaimer is holding at least one reclaimable EPC page to reclaim for the enclave and the code below only reclaims SECS when no reclaimable EPCs (number of SECS children being zero) of the enclave left. So it should not be possible. I'll remove this change because this is really not needed for fixing the bug as Kai pointed out. I added this for sanity check when implementing multiple EPC tracking lists for cgroups. At one point there were list corruption issues if moving EPCs between lists not managed well. With those straightened out, and clear definitions of EPC states for moving them from one list to another, I no longer see much value to keep this even in later cgroup patches. Thanks Haitao