From patchwork Sat Jul 17 10:20:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yutian Yang X-Patchwork-Id: 12383479 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49FCDC636CB for ; Sat, 17 Jul 2021 10:20:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EADFD613D4 for ; Sat, 17 Jul 2021 10:20:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EADFD613D4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 642328D00F4; Sat, 17 Jul 2021 06:20:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F2B98D00EC; Sat, 17 Jul 2021 06:20:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 493478D00F4; Sat, 17 Jul 2021 06:20:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0095.hostedemail.com [216.40.44.95]) by kanga.kvack.org (Postfix) with ESMTP id 2094D8D00EC for ; Sat, 17 Jul 2021 06:20:19 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id DDDB42201B for ; Sat, 17 Jul 2021 10:20:17 +0000 (UTC) X-FDA: 78371684874.39.488EF6E Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf12.hostedemail.com (Postfix) with ESMTP id 92A6110000A6 for ; Sat, 17 Jul 2021 10:20:17 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id b12so11143179pfv.6 for ; Sat, 17 Jul 2021 03:20:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=+StolKAcDufVlmzojkSC3TjMvCFftVBsdtQJ52EleJY=; b=Jns2zr2mXKa/1SI1vWueB4vMVJ2ot3Vw405+C5xjcQU4KrIEF5mq7ltcWCbKzf5UdS aXFUOSLIdbtzDS41F64DGUbAl2t8cXIfxRUTGCsALub42QITeLC1d9mYPli6gMakRYC2 ynKij2pzLRF+9ckXst5vlg9aasuqT1f9dhvTKSSTS9ssKvZ34W3g3vdgJWO9OjZe8tFG MW5TAEOY+O1eHxeB0d1HXSWe1jYoIs/3fqkWYabv4lriA9HWEGKxh6KHfhj+TwppX0zi MSaarbS23cUIAjyYtxrK4RLHfHNX9AJ8k+wda1fEJQjXIUpXVN4QYFqAVhyoylaMjc1W 472Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+StolKAcDufVlmzojkSC3TjMvCFftVBsdtQJ52EleJY=; b=BjOR0pP9SKBjO6vrp8pxeXl3idpecSlY6NAYFLEWFLKbXoJpdm0SjfzCg2RLLyxCFN Ml4CzM45sTfryBgKBmu5BcHxi4H1LehS7RNv+7M9SHiEG8yx0LSJtTRisr7PqQj0ehq9 L27m2bGWC5pMSmwpzUAkUfvTA/YFsqj63uXL9IxFRFC5ij05fK8zTraFeEGL/+t+XG8D qyA7sUG3HGSkvEqKWXVYr7OnIKhjK8MGSmgQ/n4GcsYJjWAbaerd7189sNNieD6gaIGO TOa3pyHjgvvbFEJiwwlS4ljnevFnj3Y68hGY5dEcu1DsA6Y2usFjoFPIMNSyZMGYm8rZ 5FYw== X-Gm-Message-State: AOAM530Fvw9UIb3kIBIszlw1FrG1BKalxLx49glwE40DqCz5+Eg3NtsT VwwSFI07GN75Y1afphsqyA4= X-Google-Smtp-Source: ABdhPJyhB4vIYsRTlhbL+Sw67vvnSkTnHDK8xLi59hoMQnYOk5viSUPvn/lTX5e49o7VuFsiEt7N5Q== X-Received: by 2002:a63:5144:: with SMTP id r4mr14462549pgl.223.1626517216587; Sat, 17 Jul 2021 03:20:16 -0700 (PDT) Received: from honest-machine-1.localdomain.localdomain (80.251.213.191.16clouds.com. [80.251.213.191]) by smtp.gmail.com with ESMTPSA id u16sm14638454pgh.53.2021.07.17.03.20.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Jul 2021 03:20:16 -0700 (PDT) From: Yutian Yang To: mhocko@kernel.org, hannes@cmpxchg.org, vdavydov.dev@gmail.com Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, shenwenbo@zju.edu.cn, Yutian Yang Subject: [PATCH] memcg: charge fs_context and legacy_fs_context Date: Sat, 17 Jul 2021 06:20:01 -0400 Message-Id: <1626517201-24086-1-git-send-email-nglaive@gmail.com> X-Mailer: git-send-email 1.8.3.1 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 92A6110000A6 X-Stat-Signature: rsgunaxppf4p496fc7irrrj97woxgozd Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=Jns2zr2m; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nglaive@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=nglaive@gmail.com X-HE-Tag: 1626517217-162154 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: This patch adds accounting flags to fs_context and legacy_fs_context allocation sites so that kernel could correctly charge these objects. We have written a PoC to demonstrate the effect of the missing-charging bugs. The PoC takes around 1,200MB unaccounted memory, while it is charged for only 362MB memory usage. We evaluate the PoC on QEMU x86_64 v5.2.90 + Linux kernel v5.10.19 + Debian buster. All the limitations including ulimits and sysctl variables are set as default. Specifically, the hard NOFILE limit and nr_open in sysctl are both 1,048,576. /*------------------------- POC code ----------------------------*/ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0) #define STACK_SIZE (8 * 1024) #ifndef __NR_fsopen #define __NR_fsopen 430 #endif static inline int fsopen(const char *fs_name, unsigned int flags) { return syscall(__NR_fsopen, fs_name, flags); } static char thread_stack[512][STACK_SIZE]; int thread_fn(void* arg) { for (int i = 0; i< 800000; ++i) { int fsfd = fsopen("nfs", FSOPEN_CLOEXEC); if (fsfd == -1) { errExit("fsopen"); } } while(1); return 0; } int main(int argc, char *argv[]) { int thread_pid; for (int i = 0; i < 1; ++i) { thread_pid = clone(thread_fn, thread_stack[i] + STACK_SIZE, \ SIGCHLD, NULL); } while(1); return 0; } /*-------------------------- end --------------------------------*/ Thanks! Yutian Yang, Zhejiang University Signed-off-by: Yutian Yang Reviewed-by: Shakeel Butt --- fs/fs_context.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/fs_context.c b/fs/fs_context.c index 2834d1afa..4858645ca 100644 --- a/fs/fs_context.c +++ b/fs/fs_context.c @@ -231,7 +231,7 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type, struct fs_context *fc; int ret = -ENOMEM; - fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL); + fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT); if (!fc) return ERR_PTR(-ENOMEM); @@ -631,7 +631,7 @@ const struct fs_context_operations legacy_fs_context_ops = { */ static int legacy_init_fs_context(struct fs_context *fc) { - fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL); + fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT); if (!fc->fs_private) return -ENOMEM; fc->ops = &legacy_fs_context_ops;