Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1341978lqa; Mon, 29 Apr 2024 06:08:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVljO40OqZ+77Yjfo+0goPiG5NKAPLQ1uOAF8a1METwHRlkD8UdKLWKKyHqpG0v2ZsOZ88qr6jR7V0KPRv1wq+Bl5ukqcIx6HGcusvHpw== X-Google-Smtp-Source: AGHT+IGCe4VV60YD6997cCsEo1+GwxvU5EUOPpSt99HUwZFf/JwTTnPUMy4rAwec5L0iF2R2zlX9 X-Received: by 2002:a05:620a:8801:b0:790:e856:7cc7 with SMTP id qj1-20020a05620a880100b00790e8567cc7mr6499256qkn.21.1714396085816; Mon, 29 Apr 2024 06:08:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714396085; cv=pass; d=google.com; s=arc-20160816; b=rT0Yh0hIeRD1LeAresYt+7aBYym5y3VKRjxfFiqJkPcZnTcrx5ikHmKk/a2nuDnfKF QXRhBMwumMdLYYklT0nK99pcMSzn4v15lXA5Rln27/VlsFayokMpYiarLAUBxMK9tNfH eUP+4jMu5vy4aQjHuKUlmtsc8VfWX7sFhboqSJeuTJVLr0Ywe0jpIxtTfBJs02Pn0JC6 yNlhbBlcxIxcbL604/P+qrmK/t4dKtalpFpyvZ9a0gSjw/SC4oGftP/byrap1NpO5YdP j7V52sDKdfP6M+SlE8cbsE1stopcu0RNea5lgQ1NgknA5Zn4xS2sfVV0U6YvmEUI6oFM jWKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=3xyDyRQKG1lymA6qVwXwrf22ASZsejrwARwk93/sVAM=; fh=ds1Omzz2GcFJvk+n5oUFazlbkLHVWszCJJPkyB2xv6A=; b=plVE75r43tujaGkwIoQ9N/uvA5VugimswLzJT1qazRqORWTtnBV228FX0AzXnLoX70 wv3dIFMRg/Wp86HnGcXY0cGNa4X6TKCFJCcLrwi/4vYSxDrZi9Fvu3kPyInzSi7x/6YR XqsD3LEKf48MSAIRiAvprrrhSc+/NXktolWVf+xFNYxtm3EfBgm22jBV0lkF0WlYkaOD R+GDaOMnYp/7bjgphZ26Xu/DAHIvLPe+1IgERXx7iX+3cx3cvWaGT/bO8xgTnBZid9w7 XMwkbitxMJj6w5hvXJGyYBfcRQp3wUF45sduiwBBJO3EOz4NhvptRWJKi+Y5OQoshc/W oPlA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZiH1TfTw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-162325-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162325-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id pc19-20020a05620a841300b0078eeeb50609si25800417qkn.375.2024.04.29.06.08.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 06:08:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162325-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZiH1TfTw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-162325-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162325-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id F0D551C23C40 for ; Mon, 29 Apr 2024 13:07:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3974A6FE14; Mon, 29 Apr 2024 13:06:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZiH1TfTw" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 62B22C157; Mon, 29 Apr 2024 13:06:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714396003; cv=none; b=Eo1uWLoOpjC4mgscsazFb5IY2OHYXemWdi6FnaiYuds9cCrY9ci99y8PBcRv7K1PC67aOWLSSZ8e8B+lLkNYZVXwfJxdJv9AH9FtkF1I7opAZXxAzn9m5m0QW0jBt6AGS04aiQMZ9o3jJ+jJhGY5Xil3rPoNZn4Fl8u7iwP2/8o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714396003; c=relaxed/simple; bh=rZ8Z2pUcpPP3Q2H18yeuPh2A8Fkeenaxddz/S6gwAFM=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=sh2DaDxGWl1JtgKAtA5R2w0zvbUL8H40cXj+5VJyVOGMvsYhGyU/MjfX5+cAk7fRp7LqqkoMaH8eYdv7K+tQll8+sfbXlfXNIGtZzj1QYTlab+QhMGYuXWOYdE29AYop9bK5fWmmOMie1LvztWYt9ot/8t6NIrm8qmJtqHM9vE0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZiH1TfTw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CF56C113CD; Mon, 29 Apr 2024 13:06:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714396003; bh=rZ8Z2pUcpPP3Q2H18yeuPh2A8Fkeenaxddz/S6gwAFM=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=ZiH1TfTwjL1ui8iuu+JRSBlEsR7ExhktYPKOFOwNrqB4LuN7L3rYAYwm897fOmZXV hWjbBVN+GM9oyyv9Ohf9UjM1JAVtYdp7X392I3p9NkOh5haxxUx08ASdYOgxKc0/8k YAjFJAls6nOMnmn58JtTLDXBJcnBotEkI3TqYgG5OHYRxIryK8iohfn0TiammN7x6J Fp5MDJLbxlkp1Yr5SjoJoIDTYKn2DYRgXR49FulNasegGJ8YnTvGs+8ibxU/dGvCw6 B7EGz2llTZ0dnkzFXJrY8QGKizSV6C4JO0TGKlyF38BNOkf5G+zjieZmQ98ljvIlC8 +f3G5wa34tXfg== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 29 Apr 2024 16:06:39 +0300 Message-Id: Cc: , Subject: Re: [PATCH 0/2] x86/sgx: Fix two data races in EAUG/EREMOVE flows From: "Jarkko Sakkinen" To: "Dmitrii Kuvaiskii" , , , , , , X-Mailer: aerc 0.17.0 References: <20240429104330.3636113-1-dmitrii.kuvaiskii@intel.com> In-Reply-To: <20240429104330.3636113-1-dmitrii.kuvaiskii@intel.com> On Mon Apr 29, 2024 at 1:43 PM EEST, Dmitrii Kuvaiskii wrote: > 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. > > A trivial program like below (run under Gramine and with EDMM enabled) > stresses these two flows in the SGX driver and hangs: > > /* 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 =3D 0xA000 / page_size; > for (int i =3D 0; i < 5000; i++) { > size_t page =3D get_random_ulong() % num_pages; > char data =3D READ_ONCE(((char*)arg)[page * page_size]); > > page =3D get_random_ulong() % num_pages; > madvise(arg + page * page_size, page_size, MADV_DONTNEED); > } > } > > addr =3D mmap(NULL, 0xA000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0)= ; > pthread_t threads[16]; > for (int i =3D 0; i < 16; i++) > pthread_create(&threads[i], NULL, thread_func, addr); I'm not convinced that kernel is the problem here but it could be also how Gramine is implemented. So maybe you could make a better case of that. The example looks a bit artificial to me. > > This program 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 PRMRR (EPC), 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 PRMRR (EPC), 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 PRMRR (EPC), 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. > > (Sorry for the previous copy of this email, accidentally sent to > stable@vger.kernel.org. Failed to use `--suppress-cc` during a test send.= ) > > 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(-) BR, Jarkko