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 12821C433EF for ; Thu, 19 May 2022 21:22:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84CE26B0073; Thu, 19 May 2022 17:22:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7FAE26B0072; Thu, 19 May 2022 17:22:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C3B06B0073; Thu, 19 May 2022 17:22:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5F0EC6B0071 for ; Thu, 19 May 2022 17:22:39 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 30CBC120F4B for ; Thu, 19 May 2022 21:22:39 +0000 (UTC) X-FDA: 79483766838.25.75C97CC Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf05.hostedemail.com (Postfix) with ESMTP id 2E93E100018 for ; Thu, 19 May 2022 21:22:14 +0000 (UTC) Received: by mail-pj1-f41.google.com with SMTP id l14so6370871pjk.2 for ; Thu, 19 May 2022 14:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vaIdWn5NisS2gtNC+4QuEM/yuAIO9HPKMDavTWW4Ekc=; b=EyHJA8M1GvT4SIkxiah4waFP9OR51GmVpDw//ROU9NH/exnIKANQ9jOXBq+BbXndEC eXYB2fi+M3QNst1/gLj+y9bT/ofqyl8ZRSIDPe7mD0VypLwML8AOJInmCUHK/Jb0pZ5w LtyvQoGnvfKmtVw4AACmXU2dapqTxa0TnLdSCN1cFwB0vbhD9TYP9ViOKWeTh67Vcv4v PkBeIswiPwKKOpWzjRFxhUbqYUvYO2gPikMxYWDj/KwAirf9OmOxML+zjEG3ZqKUe5cH OQde/0u95JXdx5PhSwnkK1qVBYulPRsYQS5ARqPaTR50a63X2lkqnedXu7hI5srLtzZg Bokw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vaIdWn5NisS2gtNC+4QuEM/yuAIO9HPKMDavTWW4Ekc=; b=W5NhW7SenD/h4FIX2YZ5Ye+kxqESroUtTM/moWXuWNRXi8rovCmcGEXHRyQBlRTyZK t058KRVShxn6arRAKQJ+eqogDeHII98i/SfEJgU9sz4csE2IargTUxVxtqXfC7zpieVu SFHL/tMqU1Zuto5xkqA437v/gN6ZpYUP3iazPMfCPpVA4grht/t1muccV+SJgdMXTpAJ WTA/L/EI6OrhZ3rv/7RysJTQVcCw4YCLPyaU+NIk2xZjGGk0mJGnoB909C5mSey/lZYH 05ijmGduOmnaayAbqcQtdK+CgivjoY4MkQah4K4FyRKilKPTWJ1EPKwySus1HzSTpzYB 7K5w== X-Gm-Message-State: AOAM532rU6JSoO2wdy/OqDcMa/CtkLJwqFsugQHG+Byr0Er0BZTShKDJ fG39ve9+b2i0g+75wQ0i69s9a8f8yiZxifJ4pWr3Zw== X-Google-Smtp-Source: ABdhPJz+tdX15lgd8Da2jX/rg521Vunwu7uLMab1OAo0EgYaRfZ2wiGY4e98Cjd5vnaf4I0wXhCbHYWbs161c8m1KPQ= X-Received: by 2002:a17:902:b094:b0:15c:dee8:74c8 with SMTP id p20-20020a170902b09400b0015cdee874c8mr6544695plr.6.1652995357361; Thu, 19 May 2022 14:22:37 -0700 (PDT) MIME-Version: 1.0 References: <20220519210445.5310-1-kristen@linux.intel.com> In-Reply-To: <20220519210445.5310-1-kristen@linux.intel.com> From: Shakeel Butt Date: Thu, 19 May 2022 14:22:26 -0700 Message-ID: Subject: Re: [PATCH v2] x86/sgx: Set active memcg prior to shmem allocation 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 , LKML , Linux MM , Michal Hocko , Roman Gushchin , Johannes Weiner Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=EyHJA8M1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of shakeelb@google.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=shakeelb@google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2E93E100018 X-Rspam-User: X-Stat-Signature: kw1bqr5fdthwgpy5mxdyo1b31ndu8jib X-HE-Tag: 1652995334-809795 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 2:05 PM 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 For the memcg part: Reviewed-by: Shakeel Butt