Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3270078lqo; Wed, 15 May 2024 05:06:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUtF80IluW0fE1unbVGad3ZPTUq+dh18qDcI5RLCGE4rS7PIn97vg/4XJhKtm/2Da2hDLu7UnpSC5dTmlBBIOgoekZ8fP3nnZHL/RBNIQ== X-Google-Smtp-Source: AGHT+IHCiy1F1dGArdDf06bWHQ9JDkOLqn0Sj9Aiz9dwlwFcSzncKeOukH1CgbEJdpi2QCr/gqP/ X-Received: by 2002:a17:902:ec89:b0:1e4:362b:17d5 with SMTP id d9443c01a7336-1ef43c0fd0dmr194774005ad.4.1715774802250; Wed, 15 May 2024 05:06:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715774802; cv=pass; d=google.com; s=arc-20160816; b=F7odiGjZEkOus99FzYzum5RqF5hCSJJ6hWueds2Jx0gAxsB8PVi2GM6rvbxTGHknM+ MlFWkorUDPwQWai0vU1/Q9T2KQcVHpgSHO4HubUTPxafBcKzcGQjqLlG4ym8l7Q1v8QF T5RSJzqd0HXsylqRG4A0sbQu2WPzb0T8B4qeo/BmJuWjIB5z3L63IpxVFfwF7P07E26t jO5nHNIN9WKrbhvE1OPU7gMuPGkF2zfQ+JJYMKsI4BL5ttf0Rwlx6UOV3HHpU8rOfgZl Fcuk4BLrB8zW03xWb0AMZ3PmjbZjNh7SEF7U+uJwKEHD8nRXr0jWkEMpWWwcj1KBc+Rv d3fA== 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=odzY3tUwii6Xe37wvMR/ZF5RwjOtgOSZ8dbQ4LkSUuc=; fh=3FuwIovgl8fOIW90PBUHSo9YWlGZ1zs3q7bioYCWqWs=; b=RT+xp/mNEgoOp3DZW8HnIrjTSofAjcMuncJ987h7AATMM5s4it3lEivUc0qnt7nk9y 7wzQWiGiMq1qay+hzKWr+dOXf7cm8kbY57xBcg5OdD5GAahw72gP/JFlyyyPfL+gNfzB lnHgX5k61BEUDH4YqHbgdT+UjujeCujP1GsAebdEprTrm9YGoyMsweSA9nDa95LuTZMn h5u1NB9dfD52Td6S306k+tXxvBiQDGweMUeZTa8Nb/sTCybYKZot911nDMDPbXykGZ8u EtYH3lvVGJ5Yk71NY+yfF5bgjtCzRsmEiX1gOlupLEOuxsw3+J5DsvCEcSdDuqxXiCrJ MZkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jE1G4tFl; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-179828-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179828-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c138749si141429735ad.444.2024.05.15.05.06.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 05:06:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179828-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jE1G4tFl; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-179828-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179828-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 65DA8283E21 for ; Wed, 15 May 2024 12:06:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C78356F073; Wed, 15 May 2024 12:06:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jE1G4tFl" 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 D908F2CCD0; Wed, 15 May 2024 12:06:21 +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=1715774782; cv=none; b=ebk0BQ3kyeZiKnj5W5oAxe7Ud0+zY0pfb4NhATAiS6nxW10CLwjR6AsbP6P+FpAkMJILZBHQuczFFyMpz1p0Kkw9ATr5Jy0mLdfKnlzMfodsn7Mn7j8uboDUyyqRcrboj+Q+R+FQENmrt5Zhzwk1jF5MaOI9Rc+EeHa2K/QzMK8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715774782; c=relaxed/simple; bh=fBp/tINIW/W/kb+0aD/MBS7XiS6Vf70YTcOdC+Nzzu8=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=BHLV35ZHNN07j06ioT0a3nC+KE6PQFCVbbkTgH6lcFj0zAnWh5I7UVr6BUa/xMazhwJujuDWkv3Hbrl/EGdvAH0bGIcaELmiPX+GIoTvDD2bQBu2QIy9+1CZEjWmNj+9g1LBPVa8swvZ7t+u1VB/G+t0XB3I7mKvF+DVxP0EzAQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jE1G4tFl; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11B7BC32782; Wed, 15 May 2024 12:06:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715774781; bh=fBp/tINIW/W/kb+0aD/MBS7XiS6Vf70YTcOdC+Nzzu8=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=jE1G4tFlE8+tZ8b7ToRce/5HR6xRftU+pd65LJYbajyMV/mZYwddKcvqi4HmBn/05 tTqZMTURRct4InNdr4d3cRWODupifG2OZvahfm2gYqAiXwLfqmJRMSSWsYUgrb4RwA mpE1zQHh54/LLouqht5Fm5a2Xq2gxZTAX3xu0TB2eCZNCr5bwocuywfoWuYmVCaflz 4ox3qPgixEQ8oyUbeTcZtwrZ/WEFvxBZs6vlTzn5l5MzKmZzBR1RkuALCEmAmGnmTF SIW69+AvQTznfCIabF1Xi1L57+rnWomUPJ+FljglAsJ9Aou+Z+W60A9JAIDhryJBui LeMNcZfMH2WBA== 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: Wed, 15 May 2024 15:06:18 +0300 Message-Id: Cc: , =?utf-8?b?5YiY5Y+MKOi9qeWxuSk=?= Subject: Re: [RFC PATCH v3 1/1] x86/sgx: Explicitly give up the CPU in EDMM's ioctl() to avoid softlockup From: "Jarkko Sakkinen" To: "Bojun Zhu" , , , X-Mailer: aerc 0.17.0 References: <20240515065521.67908-1-zhubojun.zbj@antgroup.com> <20240515065521.67908-2-zhubojun.zbj@antgroup.com> In-Reply-To: <20240515065521.67908-2-zhubojun.zbj@antgroup.com> On Wed May 15, 2024 at 9:55 AM EEST, Bojun Zhu wrote: > EDMM's ioctl()s support batch operations, which may be > time-consuming. Try to explicitly give up the CPU as the prefix > operation at the every begin of "for loop" in > sgx_enclave_{ modify_types | restrict_permissions | remove_pages} > to give other tasks a chance to run, and avoid softlockup warning. > > Additionally perform pending signals check as the prefix operation, > and introduce sgx_check_signal_and_resched(), > which wraps all the checks. > > The following has been observed on Linux v6.9-rc5 with kernel > preemptions disabled(by configuring "PREEMPT_NONE=3Dy"), when kernel > is requested to restrict page permissions of a large number of EPC pages. > > ------------[ cut here ]------------ > watchdog: BUG: soft lockup - CPU#45 stuck for 22s! > ... > RIP: 0010:sgx_enclave_restrict_permissions+0xba/0x1f0 > ... > Call Trace: > sgx_ioctl > __x64_sys_ioctl > x64_sys_call > do_syscall_64 > entry_SYSCALL_64_after_hwframe > ------------[ end trace ]------------ > Suggested-by: Jarkko Sakkinen > Signed-off-by: Bojun Zhu > --- > arch/x86/kernel/cpu/sgx/ioctl.c | 40 +++++++++++++++++++++++---------- > 1 file changed, 28 insertions(+), 12 deletions(-) > > diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/io= ctl.c > index b65ab214bdf5..6199f483143e 100644 > --- a/arch/x86/kernel/cpu/sgx/ioctl.c > +++ b/arch/x86/kernel/cpu/sgx/ioctl.c > @@ -365,6 +365,20 @@ static int sgx_validate_offset_length(struct sgx_enc= l *encl, > return 0; > } > =20 > +/* > + * Check signals and invoke scheduler. Return true for a pending signal. > + */ > +static bool sgx_check_signal_and_resched(void) > +{ > + if (signal_pending(current)) > + return true; > + > + if (need_resched()) > + cond_resched(); > + > + return false; > +} > + > /** > * sgx_ioc_enclave_add_pages() - The handler for %SGX_IOC_ENCLAVE_ADD_PA= GES > * @encl: an enclave pointer > @@ -409,7 +423,7 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl= *encl, void __user *arg) > struct sgx_enclave_add_pages add_arg; > struct sgx_secinfo secinfo; > unsigned long c; > - int ret; > + int ret =3D -ERESTARTSYS; > =20 > if (!test_bit(SGX_ENCL_CREATED, &encl->flags) || > test_bit(SGX_ENCL_INITIALIZED, &encl->flags)) > @@ -432,15 +446,8 @@ static long sgx_ioc_enclave_add_pages(struct sgx_enc= l *encl, void __user *arg) > return -EINVAL; > =20 > for (c =3D 0 ; c < add_arg.length; c +=3D PAGE_SIZE) { > - if (signal_pending(current)) { > - if (!c) > - ret =3D -ERESTARTSYS; > - > + if (sgx_check_signal_and_resched()) > break; > - } > - > - if (need_resched()) > - cond_resched(); > =20 > ret =3D sgx_encl_add_page(encl, add_arg.src + c, add_arg.offset + c, > &secinfo, add_arg.flags); > @@ -740,12 +747,15 @@ sgx_enclave_restrict_permissions(struct sgx_encl *e= ncl, > unsigned long addr; > unsigned long c; > void *epc_virt; > - int ret; > + int ret =3D -ERESTARTSYS; > =20 > memset(&secinfo, 0, sizeof(secinfo)); > secinfo.flags =3D modp->permissions & SGX_SECINFO_PERMISSION_MASK; > =20 > for (c =3D 0 ; c < modp->length; c +=3D PAGE_SIZE) { > + if (sgx_check_signal_and_resched()) > + goto out; > + > addr =3D encl->base + modp->offset + c; > =20 > sgx_reclaim_direct(); > @@ -898,7 +908,7 @@ static long sgx_enclave_modify_types(struct sgx_encl = *encl, > unsigned long addr; > unsigned long c; > void *epc_virt; > - int ret; > + int ret =3D -ERESTARTSYS; > =20 > page_type =3D modt->page_type & SGX_PAGE_TYPE_MASK; > =20 > @@ -913,6 +923,9 @@ static long sgx_enclave_modify_types(struct sgx_encl = *encl, > secinfo.flags =3D page_type << 8; > =20 > for (c =3D 0 ; c < modt->length; c +=3D PAGE_SIZE) { > + if (sgx_check_signal_and_resched()) > + goto out; > + > addr =3D encl->base + modt->offset + c; > =20 > sgx_reclaim_direct(); > @@ -1095,12 +1108,15 @@ static long sgx_encl_remove_pages(struct sgx_encl= *encl, > unsigned long addr; > unsigned long c; > void *epc_virt; > - int ret; > + int ret =3D -ERESTARTSYS; > =20 > memset(&secinfo, 0, sizeof(secinfo)); > secinfo.flags =3D SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_X; > =20 > for (c =3D 0 ; c < params->length; c +=3D PAGE_SIZE) { > + if (sgx_check_signal_and_resched()) > + goto out; > + > addr =3D encl->base + params->offset + c; > =20 > sgx_reclaim_direct(); Reviewed-by: Jarkko Sakkinen BR, Jarkko