From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0E46C433EF for ; Thu, 19 May 2022 23:01:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE2DE6B007E; Thu, 19 May 2022 19:01:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E91636B0080; Thu, 19 May 2022 19:01:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5A9B6B0081; Thu, 19 May 2022 19:01:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C73306B007E for ; Thu, 19 May 2022 19:01:31 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8EFE3342C2 for ; Thu, 19 May 2022 23:01:31 +0000 (UTC) X-FDA: 79484015982.30.004AFA0 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id 29C201C00DB for ; Thu, 19 May 2022 23:01:18 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 39D2E61805; Thu, 19 May 2022 23:01:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D794C385AA; Thu, 19 May 2022 23:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653001289; bh=omajtcogrQYaXP4cqKjhNTb/VO5d+uoMyeRqV8OjHds=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=U+sFKkAHVZoDq+648Cf2hd05C5GVSTK7z2SZl5k4GbPlQ1Hk/LG3AO8iVNUKPUfS1 6lwVDQX4Dd/VcJt/1vvQnOHYWDLSXHTgnbBbWekKhKAhBp+9Xmuty+IF8Z3LCZ+6jE 31bxfqyBVH/WApZckKys6FFlOmPsm/y2Gj/aCAgw= Date: Fri, 20 May 2022 01:01:25 +0200 From: Greg KH To: Kristen Carlson Accardi Cc: linux-sgx@vger.kernel.org, Jarkko Sakkinen , Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, roman.gushchin@linux.dev, hannes@cmpxchg.org, shakeelb@google.com Subject: Re: [PATCH v2] x86/sgx: Set active memcg prior to shmem allocation Message-ID: References: <20220519210445.5310-1-kristen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220519210445.5310-1-kristen@linux.intel.com> X-Rspamd-Queue-Id: 29C201C00DB X-Stat-Signature: 4uzcpq7g98eziofuou5c7z3c3ys8t39m Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=U+sFKkAH; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf20.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1653001278-357310 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, May 19, 2022 at 02:04:45PM -0700, Kristen Carlson Accardi wrote: > When the system runs out of enclave memory, SGX can reclaim EPC pages > by swapping to normal RAM. These backing pages are allocated via a > per-enclave shared memory area. Since SGX allows unlimited over > commit on EPC memory, the reclaimer thread can allocate a large > number of backing RAM pages in response to EPC memory pressure. > > When the shared memory backing RAM allocation occurs during > the reclaimer thread context, the shared memory is charged to > the root memory control group, and the shmem usage of the enclave > is not properly accounted for, making cgroups ineffective at > limiting the amount of RAM an enclave can consume. > > For example, when using a cgroup to launch a set of test > enclaves, the kernel does not properly account for 50% - 75% of > shmem page allocations on average. In the worst case, when > nearly all allocations occur during the reclaimer thread, the > kernel accounts less than a percent of the amount of shmem used > by the enclave's cgroup to the correct cgroup. > > SGX stores a list of mm_structs that are associated with > an enclave. Pick one of them during reclaim and charge that > mm's memcg with the shmem allocation. The one that gets picked > is arbitrary, but this list almost always only has one mm. The > cases where there is more than one mm with different memcg's > are not worth considering. > > Create a new function - sgx_encl_alloc_backing(). This function > is used whenever a new backing storage page needs to be > allocated. Previously the same function was used for page > allocation as well as retrieving a previously allocated page. > Prior to backing page allocation, if there is a mm_struct associated > with the enclave that is requesting the allocation, it is set > as the active memory control group. > > Signed-off-by: Kristen Carlson Accardi > --- > V1 -> V2: > Changed sgx_encl_set_active_memcg() to simply return the correct > memcg for the enclave and renamed to sgx_encl_get_mem_cgroup(). > > Created helper function current_is_ksgxd() to improve readability. > > Use mmget_not_zero()/mmput_async() when searching mm_list. > > Move call to set_active_memcg() to sgx_encl_alloc_backing() and > use mem_cgroup_put() to avoid leaking a memcg reference. > > Address review feedback regarding comments and commit log. > This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read: https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html for how to do this properly.