Received: by 2002:a05:7208:13c3:b0:82:bbfa:f723 with SMTP id r3csp1543660rbe; Wed, 15 May 2024 06:31:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXqYGgkLIoLX9qb8JCvNgfET+GZQAWbOUcK8L/GCnBnIKEkoCeeIptihO9nRxdhgIgKh4A30htMMuRSP2KtV0/KoOvD5/zlnuY2iJFN3A== X-Google-Smtp-Source: AGHT+IHEJM5bsG3H7l7Qb9RKZAlozMh5on/A0JldB9TtrNHzi7G5swNIx+tXLWW3o3XW67CgqbLR X-Received: by 2002:a05:6358:2813:b0:192:28de:f6ec with SMTP id e5c5f4694b2df-193bb00f6d2mr1617263355d.18.1715779901266; Wed, 15 May 2024 06:31:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715779901; cv=pass; d=google.com; s=arc-20160816; b=fO4yp7LbyPUfO3km0XplMTdeA5TX2I5J/xmxXuee+0Na0TAiyIpDSlNirv/8h5sJHl ebMxfzO/TlKLHqdYpW8OYBerItXnrVTz0tlGTVcH9nsiyHePrGCss6ONnA8MQsKUzf9x DrVn5+2mMnf6Kq0q0xVe2nRCFBDe497atswk+O2I61yUkFrW+A2bXOhJM83qJpdI3aCU yiDOuDf26DsGLZlFQj6Sjrr29W7QxcNwGMumykIn60TolRsamubwcFBsfkObM/kjwCwQ Gmk0Ig82qQfJKvM41gnJo6JVa9u+ejkxM+AThIp0ynBAzaxMpu3jr4RuadE9Kq1lLR80 qT3A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:organization:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:message-id:date :subject:cc:to:from:dkim-signature; bh=tgHzt+lv2fADr6xmPArGNe1Yo3s+LcV+h9a/J8MUmRw=; fh=QJ63/kn2fgdnRaQI5rP/JRDqUoAFHMPV2fhvRjQSyY0=; b=sjZ4v7E5Ou7N6UU+h8UNnxS895xlOf+eI2NnCNh5OpbSNaXH3S8iN6g2cNvpbzHG/v A0nS22PtwYNDRIer3x4uV0pJWn3YS/2WlP3RjkpSE1/BIX8H8lEmpbap6SCF+Vt0zoSu pLS7jV64yTWtXrsfPcRqLnnReK1QrMfliJOVmUg4fdVGb4DvSOIT4Aeh/FlO2EEYDfoS sqBDwlBMqCAMExBEdthazjTFdwATc8oLPhwStIkys7g/C6syrDrpgNgwl/77jBSkelAF Q2nRCf9wpkW/gkzTYk/l9RZYh6vzrEoLpmLkpOwpmDurROXoV7Fj9Q2rs+l37koeHtN9 tCfw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Wc6wZbSZ; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-179906-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179906-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-63412a4deb4si13520971a12.750.2024.05.15.06.31.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 06:31:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179906-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Wc6wZbSZ; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-179906-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179906-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8EDAE284BC4 for ; Wed, 15 May 2024 13:21:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 675CA85C42; Wed, 15 May 2024 13:21:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Wc6wZbSZ" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E01A614AB4; Wed, 15 May 2024 13:21:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715779272; cv=none; b=ZrbiikPG3G0HKzhGwFc/yNiN+sczoo6/5KWV598fOWctAICro8yGEcQ6FQ06ijc5+jS7RJIYVZiJTAMzCGG7wwBOLxX4EqEhSyx7jQWZ7xaE45vJkPlzuf5GamhNe5WPnCxsCIBp6KYs8KxYn1u4A8UWD/5B5jWrwy0CDhUigCM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715779272; c=relaxed/simple; bh=5Ag5vL9wO2ldtZ+Du1UJaAu+pkltrp0z0Af+UtXoOHk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=nvjxcl5WIdMhKYo7fYWV114/A8GrZfCa3MY58L/FNTFpzCISoXV+MjpVDTI4nqwKtZnR/07mhO7n79YMmYyxM5uST7SDHcdjwn1Yj04f0YCYz2PIsqC9i0P0RokFTr3N+Aekvbz1mYYd4dCIebXEj1LLtaeLOhHXbfGdQ9fODw8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Wc6wZbSZ; arc=none smtp.client-ip=192.198.163.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715779272; x=1747315272; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=5Ag5vL9wO2ldtZ+Du1UJaAu+pkltrp0z0Af+UtXoOHk=; b=Wc6wZbSZTmxv3WopMcv8G8AhYUzoLgswL+WChpc9wfFm83yQD5NxIQpR sdc/+u1K0wlBKKtfsuKwASUXtcYD732YKpKNMhkLm8IMQXO4/k9wMFo5J /VJ1fbSEwexDwZ2n6bJBJBWRikLMJ2/1y6KufvaKrfRDbqAe5dAXLQNS+ bPfnK5mJlyOaVT3g30Yb8irwldfnx0TY5H9cSr+/RcUx1l5USf7K84Ppr VL1mcTQThICU+RPtulyPT0OJAwQuSaZ2CpuNOobF6ebluA0PJibPbTcjO zBz8sk5SuEEARKkd08vAXdL0+N+/7JM4MlOCJRXATZaUMegSJwOVMEXjq g==; X-CSE-ConnectionGUID: 7YO6rwSsTBCRPh03dschyA== X-CSE-MsgGUID: pMUwoIpiSbK+8laS51Lu7Q== X-IronPort-AV: E=McAfee;i="6600,9927,11073"; a="15648212" X-IronPort-AV: E=Sophos;i="6.08,161,1712646000"; d="scan'208";a="15648212" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 May 2024 06:21:03 -0700 X-CSE-ConnectionGUID: lHsRm4bTQTywl6OdW4LCCQ== X-CSE-MsgGUID: RwgNCU6ZQ2G0Zz9hNDjskQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,161,1712646000"; d="scan'208";a="68510852" Received: from mehlow-prequal01.jf.intel.com ([10.54.102.156]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 May 2024 06:21:02 -0700 From: Dmitrii Kuvaiskii To: dave.hansen@linux.intel.com, jarkko@kernel.org, kai.huang@intel.com, haitao.huang@linux.intel.com, reinette.chatre@intel.com, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org Cc: mona.vij@intel.com, kailun.qin@intel.com Subject: [PATCH v2 0/2] x86/sgx: Fix two data races in EAUG/EREMOVE flows Date: Wed, 15 May 2024 06:12:38 -0700 Message-Id: <20240515131240.1304824-1-dmitrii.kuvaiskii@intel.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Organization: Intel Deutschland GmbH - Registered Address: Am Campeon 10, 85579 Neubiberg, Germany Content-Transfer-Encoding: 8bit SGX runtimes such as Gramine may implement EDMM-based lazy allocation of enclave pages and may support MADV_DONTNEED semantics [1]. The former implies #PF-based page allocation, and the latter implies the usage of SGX_IOC_ENCLAVE_REMOVE_PAGES ioctl. EDMM-based lazy allocation and MADV_DONTNEED semantics provide significant performance improvement for some workloads that run on Gramine. For example, a Java workload with a 16GB enclave size has approx. 57x improvement in total runtime. Thus, we consider it important to permit these optimizations in Gramine. However, we observed hangs of applications (Node.js, PyTorch, R, iperf, Blender, Nginx) when run on Gramine with EDMM, lazy allocation and MADV_DONTNEED features enabled. We wrote a trivial stress test to reproduce the hangs observed in real-world applications. The test stresses #PF-based page allocation and SGX_IOC_ENCLAVE_REMOVE_PAGES flows in the SGX driver: /* repeatedly touch different enclave pages at random and mix with * madvise(MADV_DONTNEED) to stress EAUG/EREMOVE flows */ static void* thread_func(void* arg) { size_t num_pages = 0xA000 / page_size; for (int i = 0; i < 5000; i++) { size_t page = get_random_ulong() % num_pages; char data = READ_ONCE(((char*)arg)[page * page_size]); page = get_random_ulong() % num_pages; madvise(arg + page * page_size, page_size, MADV_DONTNEED); } } addr = mmap(NULL, 0xA000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0); pthread_t threads[16]; for (int i = 0; i < 16; i++) pthread_create(&threads[i], NULL, thread_func, addr); This test uncovers two data races in the SGX driver. The remaining patches describe and fix these races. I performed several stress tests to verify that there are no other data races (at least with the test program above): - On Icelake server with 128GB of PRM, without madvise(). This stresses the first data race. A Gramine SGX test suite running in the background for additional stressing. Result: 1,000 runs without hangs (result without the first bug fix: hangs every time). - On Icelake server with 128GB of PRM, with madvise(). This stresses the second data race. A Gramine SGX test suite running in the background for additional stressing. Result: 1,000 runs without hangs (result with the first bug fix but without the second bug fix: hangs approx. once in 50 runs). - On Icelake server with 4GB of PRM, with madvise(). This additionally stresses the enclave page swapping flows. Two Gramine SGX test suites running in the background for additional stressing of swapping (I observe 100% CPU utilization from ksgxd which confirms that swapping happens). Result: 1,000 runs without hangs. [1] https://github.com/gramineproject/gramine/pull/1513 v1 -> v2: - No changes in code itself - Expanded cover letter - Added CPU1 vs CPU2 race scenarios in commit messages v1: https://lore.kernel.org/all/20240429104330.3636113-3-dmitrii.kuvaiskii@intel.com/ Dmitrii Kuvaiskii (2): x86/sgx: Resolve EAUG race where losing thread returns SIGBUS x86/sgx: Resolve EREMOVE page vs EAUG page data race arch/x86/kernel/cpu/sgx/encl.c | 10 +++++++--- arch/x86/kernel/cpu/sgx/encl.h | 3 +++ arch/x86/kernel/cpu/sgx/ioctl.c | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) -- 2.34.1