Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1657888ioo; Sun, 22 May 2022 23:18:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQq2pwFFXCCznK1rDXfGXFiap3vKAexNMKlESvnVM1IpYG2kxwjks6iHJMm+VkV2Nxp9eM X-Received: by 2002:a17:90b:795:b0:1df:10a3:84e3 with SMTP id l21-20020a17090b079500b001df10a384e3mr24870105pjz.197.1653286713349; Sun, 22 May 2022 23:18:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653286713; cv=none; d=google.com; s=arc-20160816; b=DyI5zjVzym9FMVeH+bKVPMQpYO8SdFLffD8a9LRI1uDsK8RMRVrfK3N8ct818ti6CF Xw1shrxhThSep1Nny+4+zsq/0QyqIwHMwnFiZnxi2wbXv5aEaaIzHIdSwNDcdu46ti/U 2KP8ifZ5dV33EYTO2VlgNbpm6c2CG1QzzKLdC52f4Z//42jbgyJHOt3GwudaTr4xFV1L JNGa3LLa5sfHljncz06Oc922ZM0drWEWyQtz4lZfw0xz29igLfo4rPbd3Z5fS4k/xhub mtmTycsrL80NEs9wMp7GCQp/KWNSsfK96ogjrW5TUvAKQ4DF8l6SAxVvNb5SN1UoRi0E wW6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=i/+1Nf9pynIYZBbCl879bpnwSZXGbLmtNLpiAYJRkkI=; b=uCLOnzK7Jbe2ZBsaUsgJ1APB6m8btXMB8b+sOTD9329K6TYgDt5yejSmsVpdZGMFgC Oy/850pIcTjz6r5W67YYLAbNQpJssXyCIHrLemSj2Sn7NnnDdoSyvl2HvjH+jEq5mpJc UTQS0FTRhZkMGx+jErGrPQ8JcCfBAGTYYSG8z8c4Np1c0P15r8SyaDCXN4s31yIAGmPq 7St6/MfVqi8UZ7cGu7gX2R8/Aqa+dINDx7RfZQbh3mfrP41ouOnE9UvkhtkxNrSpTzvu Ab4kjeIEHEcbSW7CwFEfT3+NVAc4oai7o+dbi5rdxV+UYFZXe80PqnEBSdS0Qv0E0O34 Uu9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U+sFKkAH; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id h3-20020a625303000000b004fa3a8e002csi11989743pfb.227.2022.05.22.23.18.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:18:33 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U+sFKkAH; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E65BE3EF32; Sun, 22 May 2022 23:03:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232372AbiESXBw (ORCPT + 99 others); Thu, 19 May 2022 19:01:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243647AbiESXBh (ORCPT ); Thu, 19 May 2022 19:01:37 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 166CC24BC2; Thu, 19 May 2022 16:01:32 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id F0798B828B0; 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-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.