From patchwork Tue Aug 1 22:36:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 9875631 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 188406041F for ; Tue, 1 Aug 2017 22:36:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 098DD28735 for ; Tue, 1 Aug 2017 22:36:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F2A2928747; Tue, 1 Aug 2017 22:36:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 814FF28742 for ; Tue, 1 Aug 2017 22:36:31 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id D47E621D2DCEE; Tue, 1 Aug 2017 15:34:20 -0700 (PDT) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A814921D2DCF5 for ; Tue, 1 Aug 2017 15:34:19 -0700 (PDT) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga104.jf.intel.com with ESMTP; 01 Aug 2017 15:36:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,308,1498546800"; d="scan'208";a="134743812" Received: from sjchrist-ts.jf.intel.com ([10.54.74.20]) by fmsmga006.fm.intel.com with ESMTP; 01 Aug 2017 15:36:28 -0700 From: Sean Christopherson To: intel-sgx-kernel-dev@lists.01.org Date: Tue, 1 Aug 2017 15:36:17 -0700 Message-Id: <1501626977-6983-3-git-send-email-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1501626977-6983-1-git-send-email-sean.j.christopherson@intel.com> References: <1501626977-6983-1-git-send-email-sean.j.christopherson@intel.com> Subject: [intel-sgx-kernel-dev] [PATCH v2 2/2] intel_sgx: ensure ksgxswapd_tsk is never invalid/stale X-BeenThere: intel-sgx-kernel-dev@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Project: Intel® Software Guard Extensions for Linux*: https://01.org/intel-software-guard-extensions" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-sgx-kernel-dev-bounces@lists.01.org Sender: "intel-sgx-kernel-dev" X-Virus-Scanned: ClamAV using ClamSMTP Use a tmp variable to hold and query the result of kthread_run and only set ksgxswapd_tsk if kthread_run is successful, and nullify ksgxswapd_tsk when it is stopped. Ensuring ksgxswapd_tsk is never invalid/stale eliminates a path to a potential kernel panic, e.g. if, in the future, sgx_page_cache_teardown needs to be called even when sgx_page_cache_init fails. Signed-off-by: Sean Christopherson --- drivers/platform/x86/intel_sgx/sgx_page_cache.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/platform/x86/intel_sgx/sgx_page_cache.c b/drivers/platform/x86/intel_sgx/sgx_page_cache.c index f1553324..1368747 100644 --- a/drivers/platform/x86/intel_sgx/sgx_page_cache.c +++ b/drivers/platform/x86/intel_sgx/sgx_page_cache.c @@ -421,9 +421,14 @@ int sgx_add_epc_bank(resource_size_t start, unsigned long size) int sgx_page_cache_init(void) { + struct task_struct *tmp; + sgx_nr_high_pages = 2 * sgx_nr_low_pages; - ksgxswapd_tsk = kthread_run(ksgxswapd, NULL, "ksgxswapd"); - return PTR_ERR_OR_ZERO(ksgxswapd_tsk); + + tmp = kthread_run(ksgxswapd, NULL, "ksgxswapd"); + if (!IS_ERR(tmp)) + ksgxswapd_tsk = tmp; + return PTR_ERR_OR_ZERO(tmp); } void sgx_page_cache_teardown(void) @@ -431,8 +436,10 @@ void sgx_page_cache_teardown(void) struct sgx_epc_page *entry; struct list_head *parser, *temp; - if (ksgxswapd_tsk) + if (ksgxswapd_tsk) { kthread_stop(ksgxswapd_tsk); + ksgxswapd_tsk = NULL; + } spin_lock(&sgx_free_list_lock); list_for_each_safe(parser, temp, &sgx_free_list) {