Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2177745pxb; Fri, 5 Feb 2021 10:46:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJyXkTNZesWRJT0m2htc6u/CMyxdoaMD9IAjwg7tmw946oGc6clLcTboftDRdD76nfBY2A4Z X-Received: by 2002:a17:906:a149:: with SMTP id bu9mr5387311ejb.185.1612550781558; Fri, 05 Feb 2021 10:46:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612550781; cv=none; d=google.com; s=arc-20160816; b=qo24SlxOqHprlzKqUrL4c66NH8uNnpjZw+Fqi6wQo06m1s5uIguzyDsPNO5lSedWYk J0Eg0EwgKIf81yzCoNY3d+FvCSZDOItoq/WIKf5J1aIQtDhn2tpvVA5YeKJ3y2M604Q/ BZD26DeRVKS55G8jwYxHwnvhTpyv7aRm0tro9WTi2vju211l3pY6YJ5wTqD6GXIlrs79 slkqm6xgDLdusZv4JC5kQvJfELrg+cZwQNC3RhFjD+EPisQ5eZW50nTWRVL6OdAm2RQa fR0FZmjY6t6WFMso3L8AhMShq8gkaKtIbLPGa4A/rJDQYurdOxIpGClXlhUC7vySmmyw K04g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gdyHJC7FPcnOHWf4HPvtCoSfVWDu/Ay0KHynxdH3WG0=; b=luFShxOMd07mBUKWKL7BREpTXHaCLJNyPAZjEY2wL1RlOQJxwE++d71JQDYkQYpcWA VsPhUmMYdt5zViAVUtJ7I4SLhBalR1IF/19FxlmqePnewDgETf/fjTMkI/Z0h0cOCqB6 ZcVbyzqGcLEx1BeC9pmwcpOfUtIQVJMtsXgSRNutaEICDin2CeHWqN3xqIvOu+lLE0gY /7dNhO9LTT0sZ7MdXNLW/YnAWhjKXATGeEmrF7T0Srhz3PPOv1jxGl/iRFPCn08NJ382 4r+M6AhjjPfTKCU2Ct85D0epuy8auKXZKrUUee/ftIX0Cmk+qk0pqCROMoGeysAfVu1h 9HBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="fcvX5j/d"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v5si6209293eda.107.2021.02.05.10.45.57; Fri, 05 Feb 2021 10:46:21 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="fcvX5j/d"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233657AbhBERAb (ORCPT + 99 others); Fri, 5 Feb 2021 12:00:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:45902 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233396AbhBEQrS (ORCPT ); Fri, 5 Feb 2021 11:47:18 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4204F64EFE; Fri, 5 Feb 2021 18:28:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1612549738; bh=A1gIOBt0Yj1LPCJqqJKqVkqlMHU5nv3NGnZ0yYnY45c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fcvX5j/dNeOl2NZPRDPdrRKIb+EMGqImml9aMynDEzt+//bta0Lq20fRfwSgQTda0 oBpS61PUECN6LQsZydcHhxypipA/9LsLRgdDSCv0LvgTYizSxLFMp4dBl9PLNdHcgz T5KKHfYHPsPIKkTu9RQyfsteZojKJoE/Q4auOfo3QmRwYSmq00vA5HIjJHO9ocOv/r TSVvHdy2SBUiUeuTMIgxkMKDDaCjaBaLZ8Ht+BoMfGIaDCwBIIc0AbCSD35pc2lwMA heuo+GE159aCIZST7wZ3pG2q4i41YvRo3WiL8+hj/eesoMHxTKu8mKlc4T/l45sZ5l tpo1RiMUBh6jA== From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: dave.hansen@intel.com, Jarkko Sakkinen , Haitao Huang , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Jethro Beekman , Sean Christopherson , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] x86/sgx: Maintain encl->refcount for each encl->mm_list entry Date: Fri, 5 Feb 2021 20:28:40 +0200 Message-Id: <20210205182840.2260-2-jarkko@kernel.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205182840.2260-1-jarkko@kernel.org> References: <20210205182840.2260-1-jarkko@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This has been shown in tests: [ +0.000008] WARNING: CPU: 3 PID: 7620 at kernel/rcu/srcutree.c:374 cleanup_srcu_struct+0xed/0x100 There are two functions that drain encl->mm_list: - sgx_release() (i.e. VFS release) removes the remaining mm_list entries. - sgx_mmu_notifier_release() removes mm_list entry for the registered process, if it still exists. If encl->refcount is taken only for VFS, this can lead to sgx_encl_release() being executed before sgx_mmu_notifier_release() completes, which is exactly what happens in the above klog entry. Each process also needs its own enclave reference. In order to fix the race condition, increase encl->refcount when an entry to encl->mm_list added for a process. Release this reference when the mm_list entry is cleaned up, either in sgx_mmu_notifier_release() or sgx_release(). Fixes: 1728ab54b4be ("x86/sgx: Add a page reclaimer") Cc: Dave Hansen Signed-off-by: Jarkko Sakkinen --- v7: - No changes from v6. Resend of https://patchwork.kernel.org/project/intel-sgx/patch/20210204143845.39697-1-jarkko@kernel.org/ v6: - Maintain refcount for each encl->mm_list entry. v5: - To make sure that the instance does not get deleted use kref_get() kref_put(). This also removes the need for additional synchronize_srcu(). v4: - Rewrite the commit message. - Just change the call order. *_expedited() is out of scope for this bug fix. v3: Fine-tuned tags, and added missing change log for v2. v2: Switch to synchronize_srcu_expedited(). arch/x86/kernel/cpu/sgx/driver.c | 6 ++++++ arch/x86/kernel/cpu/sgx/encl.c | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c index f2eac41bb4ff..8d8fcc91c0d6 100644 --- a/arch/x86/kernel/cpu/sgx/driver.c +++ b/arch/x86/kernel/cpu/sgx/driver.c @@ -72,6 +72,12 @@ static int sgx_release(struct inode *inode, struct file *file) synchronize_srcu(&encl->srcu); mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm); kfree(encl_mm); + + /* + * Release the mm_list reference, as sgx_mmu_notifier_release() + * will only do this only, when it grabs encl_mm. + */ + kref_put(&encl->refcount, sgx_encl_release); } kref_put(&encl->refcount, sgx_encl_release); diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index ee50a5010277..c1d9c86c0265 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -474,6 +474,7 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn, if (tmp == encl_mm) { synchronize_srcu(&encl_mm->encl->srcu); mmu_notifier_put(mn); + kref_put(&encl_mm->encl->refcount, sgx_encl_release); } } @@ -545,6 +546,13 @@ int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm) } spin_lock(&encl->mm_lock); + + /* + * Take a reference to guarantee that the enclave is not destroyed, + * while sgx_mmu_notifier_release() is active. + */ + kref_get(&encl->refcount); + list_add_rcu(&encl_mm->list, &encl->mm_list); /* Pairs with smp_rmb() in sgx_reclaimer_block(). */ smp_wmb(); -- 2.30.0