Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp137113lqo; Thu, 16 May 2024 01:25:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUH6s/0syJKQvgEhFFjAc4gl+xP2dnBd0oOwp4r0AJcomNtwjr/tbVtNBp46Qf08U0AE+Sm7iVKuKrF7xGhrj9rO28smQz1mCWTOEifMQ== X-Google-Smtp-Source: AGHT+IE+TqfNVodbQQpc1etFKHY34ee+60S1YmmBVy8oywimAxiZvkewj7KGW4WQEoSvlcvADnZk X-Received: by 2002:a05:6808:1520:b0:3c9:9da8:e0d9 with SMTP id 5614622812f47-3c99da8e188mr20240984b6e.18.1715847901081; Thu, 16 May 2024 01:25:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715847901; cv=pass; d=google.com; s=arc-20160816; b=b/Vl8Z8FOZo5WAtQgG0H0D6PVu8WKVKlnXJKQgC5YrTfS4RKsGP+JtjZfAzjUwWkfw EAiAQ2QeWmPERd8ec4T9+QkvMtuERouveNWNkBwvc9dWWdTXw/BTTnkFK92wCaw6fpdY cJx/NoVlQRh+voAnlyR5ZcaJzhJgsSFnqxV7Zp1ySOLU/0QeCzzS5O/0n2gFNL4GO/oF 3P4wddVFa9/9GlYuWDxzcakDjLyAZpmZCHqg9nskCcY0uz+a0piA9Y0x7J+XFi/dF9dS KUvsKcfuqviRzphlJghggjHObCi1ohFjwBl/YtOp32/OrFv5Oaf+KXj46xOc668PAcRy +ikA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:from:subject:cc:to:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=GnHhnj4brX66fA+c/V2qVRYB2dWudtlwJFZxgzToeOM=; fh=5Yu6NsbZq/EnzqF4Svr6td6HZ3/XAqmNFYVTyjtitCw=; b=vUyaXy8Cd1ENNuEZUIIf7jVsS3XSTmVlpCszRZQYU3H/1wTD51VzIP+86vGhDy4Qx8 P1OM0iCzrdcKr6PHtwpuHHunWChAfbprnxwrpZWC72a8WFf7jgyXV3sOpwAtdo56APas S2d9vBvTCKc47usDm8nlBwpfdu1P446DNViz87k2yEZ2yk7kkJ3spSjDsciJKUTPG6ox O/hzZdszNFm1IU0x1zhps0fUupt1GYcOpbVsMRKmGzboiGbJweClkaVBL01NH29XSrjJ fVBNaG7rh7OTnE73gnd+fR2mkWsf7MKZzGr/BomTYM3QHY6hwAHx7nMad2pnVf+PkSx2 +NvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FUWnh2mw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-180756-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-180756-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 d75a77b69052e-43e3b1aeeeesi17378091cf.371.2024.05.16.01.25.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 01:25:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-180756-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=FUWnh2mw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-180756-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-180756-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 691781C21181 for ; Thu, 16 May 2024 08:25:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A4E680C15; Thu, 16 May 2024 08:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FUWnh2mw" 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 62D6838F86; Thu, 16 May 2024 08:24:54 +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=1715847894; cv=none; b=hfg6l1sEgOvK3RGbLxNzgKteK1bSPREKnd/5KOOLrLff/pn3SfkRHYvMatmhWxnSBNX5Wa0EwNz9+eLmZvOrKwAPao2CYut1mYW/NF/QIowOF78JPN2mTBf5mUnsFplVuMi5CkqQo8Vvd4jFaVslGocI5aVRn5QY7UzxTG23DgQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715847894; c=relaxed/simple; bh=mfmOSWeRpr06S6hnFqVfVeZE8LfmZzNlFfPbTukt/dM=; h=Mime-Version:Content-Type:Date:Message-Id:To:Cc:Subject:From: References:In-Reply-To; b=NbTBdbLGli44x6N7FnleKcCf95Y+tn00Zo6nrzqsEZV4AsCHG6AfaScnFa/AZVGJPYVN2M3JhvNKVI6Vk+WOAsCcMPdaxKcjnhvru/bYslGsyqUvfoL+Y70rl/HaNc09FyLAveR+p8klar0H5Vp1u28JuQJnVpWws7g4yEqCQsg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FUWnh2mw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72544C113CC; Thu, 16 May 2024 08:24:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715847893; bh=mfmOSWeRpr06S6hnFqVfVeZE8LfmZzNlFfPbTukt/dM=; h=Date:To:Cc:Subject:From:References:In-Reply-To:From; b=FUWnh2mw8dx4iLAKE+vH3FOKbTruo+x6R9YeaA30lqaDHhvTbW0K2njOKdmZ5XxYs GDXiK9Pr51kX6TzNmiJQ6QWxa/ksZugcB1Pw5NNOHAyYHlkWol97uLONwGDFrgE19J 6zo9Maxe8ppDeRY8idEEY+WxxjdTrzyqaG9HqDFAOBgNm1e3Cpp5mBPc2W9T7YRgyo N5AG0IxKcrr8GN2SWEDIm3Quc/oBT2RErssXjKicNcRLdHGmm4K14ogFmsORwgdkJu zXTA/s+/9BOLFVF1iVLgtP7LxVe5Ps86ybCMj4l0eNIQuNUzXpIb94fbcT81KrvlG2 RdNOy6ZmbN1Mg== 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: Thu, 16 May 2024 11:24:50 +0300 Message-Id: To: "Haitao Huang" , , , , "Bojun Zhu" 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" X-Mailer: aerc 0.17.0 References: <20240515065521.67908-1-zhubojun.zbj@antgroup.com> <20240515065521.67908-2-zhubojun.zbj@antgroup.com> In-Reply-To: On Thu May 16, 2024 at 12:55 AM EEST, Haitao Huang wrote: > On Wed, 15 May 2024 01:55:21 -0500, Bojun Zhu = =20 > 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 page= s. > > > > ------------[ 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 ]------------ > > > > 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 =20 > > b/arch/x86/kernel/cpu/sgx/ioctl.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 =20 > > sgx_encl *encl, > > return 0; > > } > > +/* > > + * Check signals and invoke scheduler. Return true for a pending signa= l. > > + */ > > +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 =20 > > %SGX_IOC_ENCLAVE_ADD_PAGES > > * @encl: an enclave pointer > > @@ -409,7 +423,7 @@ static long sgx_ioc_enclave_add_pages(struct =20 > > 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; > > 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 =20 > > sgx_encl *encl, void __user *arg) > > return -EINVAL; > > 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; > > - } > > ERESTARTSYS is only appropriate if we have not EADDed any pages yet. > If we got interrupted in the middle, we should return 0. User space would= =20 > check the 'count' returned and decide to recall this ioctl() with =20 > 'offset' reset to the next page, and adjust length. Good catch! Thanks Haitao for the remark. BR, Jarkko