From nobody Sun Apr 19 02:15:50 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41BF6C43334 for ; Thu, 7 Jul 2022 18:27:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236357AbiGGS1g (ORCPT ); Thu, 7 Jul 2022 14:27:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236331AbiGGS1d (ORCPT ); Thu, 7 Jul 2022 14:27:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 960E15A449; Thu, 7 Jul 2022 11:27:32 -0700 (PDT) Date: Thu, 07 Jul 2022 18:27:29 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1657218451; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a7BKVeFh25xp/+GEhd+Yxb343vnwqW+/NdICjQugKWI=; b=lKcU2DkylekgxpNHemW+ZtHL1JhjpVGJLHwqzkJfgrFn+qHdeeziKCixw08a3azV8ifBrf legacaBqlz0/QHIcZbU9DsvJL5uHY0g8+9VtDo9qO4F1RF5Q1QCiRO3B+iGSHgBs/jgj5J 06nnZZ+YgdAiNUjneJncWdB1/ucJNduowK8HufoZEbAbB+ohZHyj0pozgRiLY3MfIqJUJe ouiiIvfQnizYUiUKNRCbiRzFMSQZJev955A7OAEWO4IzQr3GHhVd28FXalezYcpOcGkrEm HT1ERZ41rHugjhFjDqrDqW5gz36Sd7XN1AyKUjuDxszS6lBzaYoDxpKdgTyStQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1657218451; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a7BKVeFh25xp/+GEhd+Yxb343vnwqW+/NdICjQugKWI=; b=UvoZxcvll/pyHc7bDEUHifvPaSuvOJbl+aV9w9oQa/v1kZC1uZ+QSYdnkaQsrAfKGwnK2J 4ZG/Vr/og+fBh3BA== From: "tip-bot2 for Reinette Chatre" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/sgx] selftests/sgx: Test reclaiming of untouched page Cc: Haitao Huang , Reinette Chatre , Dave Hansen , Jarkko Sakkinen , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: =?utf-8?q?=3C45e1b2a2fcd8c14597d04e40af5d8a9c1c5b017e=2E16521?= =?utf-8?q?37848=2Egit=2Ereinette=2Echatre=40intel=2Ecom=3E?= References: =?utf-8?q?=3C45e1b2a2fcd8c14597d04e40af5d8a9c1c5b017e=2E165213?= =?utf-8?q?7848=2Egit=2Ereinette=2Echatre=40intel=2Ecom=3E?= MIME-Version: 1.0 Message-ID: <165721844997.15455.15614719594364302781.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/sgx branch of tip: Commit-ID: 08ceab2c37d32f422f8d98540656ee5a416ba729 Gitweb: https://git.kernel.org/tip/08ceab2c37d32f422f8d98540656ee5a4= 16ba729 Author: Reinette Chatre AuthorDate: Tue, 10 May 2022 11:09:06 -07:00 Committer: Dave Hansen CommitterDate: Thu, 07 Jul 2022 10:13:05 -07:00 selftests/sgx: Test reclaiming of untouched page Removing a page from an initialized enclave involves three steps: (1) the user requests changing the page type to PT_TRIM via the SGX_IOC_ENCLAVE_MODIFY_TYPES ioctl() (2) on success the ENCLU[EACCEPT] instruction is run from within the enclave to accept the page removal (3) the user initiates the actual removal of the page via the SGX_IOC_ENCLAVE_REMOVE_PAGES ioctl(). Remove a page that has never been accessed. This means that when the first ioctl() requesting page removal arrives, there will be no page table entry, yet a valid page table entry needs to exist for the ENCLU[EACCEPT] function to succeed. In this test it is verified that a page table entry can still be installed for a page that is in the process of being removed. Suggested-by: Haitao Huang Signed-off-by: Reinette Chatre Signed-off-by: Dave Hansen Acked-by: Jarkko Sakkinen Link: https://lkml.kernel.org/r/45e1b2a2fcd8c14597d04e40af5d8a9c1c5b017e.16= 52137848.git.reinette.chatre@intel.com --- tools/testing/selftests/sgx/main.c | 80 +++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/s= gx/main.c index 2c69045..ba16671 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -1790,4 +1790,84 @@ TEST_F(enclave, remove_added_page_invalid_access_aft= er_eaccept) EXPECT_EQ(self->run.exception_addr, data_start); } =20 +TEST_F(enclave, remove_untouched_page) +{ + struct sgx_enclave_remove_pages remove_ioc; + struct sgx_enclave_modify_types modt_ioc; + struct encl_op_eaccept eaccept_op; + unsigned long data_start; + int ret, errno_save; + + ASSERT_TRUE(setup_test_encl(ENCL_HEAP_SIZE_DEFAULT, &self->encl, _metadat= a)); + + /* + * Hardware (SGX2) and kernel support is needed for this test. Start + * with check that test has a chance of succeeding. + */ + memset(&modt_ioc, 0, sizeof(modt_ioc)); + ret =3D ioctl(self->encl.fd, SGX_IOC_ENCLAVE_MODIFY_TYPES, &modt_ioc); + + if (ret =3D=3D -1) { + if (errno =3D=3D ENOTTY) + SKIP(return, + "Kernel does not support SGX_IOC_ENCLAVE_MODIFY_TYPES ioctl()"); + else if (errno =3D=3D ENODEV) + SKIP(return, "System does not support SGX2"); + } + + /* + * Invalid parameters were provided during sanity check, + * expect command to fail. + */ + EXPECT_EQ(ret, -1); + + /* SGX2 is supported by kernel and hardware, test can proceed. */ + memset(&self->run, 0, sizeof(self->run)); + self->run.tcs =3D self->encl.encl_base; + + data_start =3D self->encl.encl_base + + encl_get_data_offset(&self->encl) + PAGE_SIZE; + + memset(&modt_ioc, 0, sizeof(modt_ioc)); + + modt_ioc.offset =3D encl_get_data_offset(&self->encl) + PAGE_SIZE; + modt_ioc.length =3D PAGE_SIZE; + modt_ioc.page_type =3D SGX_PAGE_TYPE_TRIM; + ret =3D ioctl(self->encl.fd, SGX_IOC_ENCLAVE_MODIFY_TYPES, &modt_ioc); + errno_save =3D ret =3D=3D -1 ? errno : 0; + + EXPECT_EQ(ret, 0); + EXPECT_EQ(errno_save, 0); + EXPECT_EQ(modt_ioc.result, 0); + EXPECT_EQ(modt_ioc.count, 4096); + + /* + * Enter enclave via TCS #1 and approve page removal by sending + * EACCEPT for removed page. + */ + + eaccept_op.epc_addr =3D data_start; + eaccept_op.flags =3D SGX_SECINFO_TRIM | SGX_SECINFO_MODIFIED; + eaccept_op.ret =3D 0; + eaccept_op.header.type =3D ENCL_OP_EACCEPT; + + EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0); + EXPECT_EEXIT(&self->run); + EXPECT_EQ(self->run.exception_vector, 0); + EXPECT_EQ(self->run.exception_error_code, 0); + EXPECT_EQ(self->run.exception_addr, 0); + EXPECT_EQ(eaccept_op.ret, 0); + + memset(&remove_ioc, 0, sizeof(remove_ioc)); + + remove_ioc.offset =3D encl_get_data_offset(&self->encl) + PAGE_SIZE; + remove_ioc.length =3D PAGE_SIZE; + ret =3D ioctl(self->encl.fd, SGX_IOC_ENCLAVE_REMOVE_PAGES, &remove_ioc); + errno_save =3D ret =3D=3D -1 ? errno : 0; + + EXPECT_EQ(ret, 0); + EXPECT_EQ(errno_save, 0); + EXPECT_EQ(remove_ioc.count, 4096); +} + TEST_HARNESS_MAIN