From patchwork Fri Jun 24 08:04:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 12894081 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 7BD6FCCA47F for ; Fri, 24 Jun 2022 08:04:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 131988E01F0; Fri, 24 Jun 2022 04:04:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B9FA8E01E7; Fri, 24 Jun 2022 04:04:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFFF08E01F0; Fri, 24 Jun 2022 04:04:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CF26F8E01E7 for ; Fri, 24 Jun 2022 04:04:50 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 944A3804DD for ; Fri, 24 Jun 2022 08:04:50 +0000 (UTC) X-FDA: 79612393140.21.B468A9F Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf28.hostedemail.com (Postfix) with ESMTP id 3FA8BC002A for ; Fri, 24 Jun 2022 08:04:50 +0000 (UTC) Received: by mail-ed1-f50.google.com with SMTP id c13so2291784eds.10 for ; Fri, 24 Jun 2022 01:04:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ltQpZ7oW1QsVH1f1jCapXhBdDu/pMzI69BR3W0aWJiY=; b=BCLBGzCDM3dicFZ1OGIFkOsdf2V521mMysBEJ9iinveVKjuPTsLrqFdJi734Z1uZvp BrIl/Z9vn8vFkyDOHWmnKxo21xK+tLMDYTST34w4v8TV5nWFn9JLHMcOYZM1aHPYbEeD 282A6fO+GqmoGy5VHzTzkXXC9jDOLaPtweOGDMSO0oqWtHIb7gpQJUaCuMu88rOwe2BD crWt+l2Vv9WpcgtJbnfxcqiFAzAJtXlpuRnoAfr2x/1dRrw8Nn1n/TUXtsDtfcmdgOF3 YGwme/GeeP+MA8N7frvR6cGiLzhNFciaj5ku28X9lFYuS1JLv1ymQIO3PkoxpTVbMdZb ASwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ltQpZ7oW1QsVH1f1jCapXhBdDu/pMzI69BR3W0aWJiY=; b=CZnRY/7kjkA3gWhQnLudr5EV3UnYfU9exNk1sav0zQPMnnVgOFq8XfluIt0Zwe7pDH qAq6/zSmY/JgkxR9ukSBVyOjeFCFixmMPBn/Rhd/9CuTrX1bsdFBiy70VKislsQrajxv vp98uxplQyss32QaTGQ+SACSEpWcAEaiDcAGN2DUsujuSfOGck7B2x29AgD78JLMDnS5 Nfra+46HUet4aT5Y6CPhSdK/r7KRWJd9Wox4QfxdDTL+3n1P/3clHLLSBU9/PGzFPjzz IFlU1N6/bXC0HgHERDDRrcnC4ztF4VBSQNlm/AjtgmBBsqLpd8ZojO29RpCggu0Bbdww gYXw== X-Gm-Message-State: AJIora+w4mJypp2hGK4KjTSL2pjmSaqHyKTF5CurTy5y6imOFyWhtzZ/ QWJDc90WakwMmGHyxtMpH6M= X-Google-Smtp-Source: AGRyM1vqTm2oWNscqLmlMW8s9dcXBhWIE7N6JaZpirobITGNlD2rMAul00q8pM0x95n+ay4p5Kh9SQ== X-Received: by 2002:a05:6402:358c:b0:435:9daf:e825 with SMTP id y12-20020a056402358c00b004359dafe825mr16018406edc.375.1656057889076; Fri, 24 Jun 2022 01:04:49 -0700 (PDT) Received: from able.fritz.box (p57b0bd9f.dip0.t-ipconnect.de. [87.176.189.159]) by smtp.gmail.com with ESMTPSA id c19-20020a170906155300b006fea43db5c1sm697779ejd.21.2022.06.24.01.04.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 01:04:48 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, amd-gfx@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-tegra@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, dri-devel@lists.freedesktop.org Cc: mhocko@suse.com, Andrey Grodzovsky , =?utf-8?q?Christian_K=C3=B6nig?= Subject: [PATCH 01/14] fs: add per file RSS Date: Fri, 24 Jun 2022 10:04:31 +0200 Message-Id: <20220624080444.7619-2-christian.koenig@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220624080444.7619-1-christian.koenig@amd.com> References: <20220624080444.7619-1-christian.koenig@amd.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656057890; a=rsa-sha256; cv=none; b=ZR51RnCy0wXYXV2RQUqOkrieJsEuSty9sxxF1jBT9Cn40LE2rmOcl48wsn8X3i6rz0tsWG BbTs6XVjD0bRJm6p7OUSBVPVXXo8fjuFkNLSWFoEcd2e8dC3O2W0eBCWwDUqczwiLGX/sk kTtgVgtPJXKt5mm9h2EQkARLQvjJjV4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656057890; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ltQpZ7oW1QsVH1f1jCapXhBdDu/pMzI69BR3W0aWJiY=; b=eOBepxkGPEdBRFa5nzKoz1lSwz1ckTgS+KRO8QjAOgsozRYkrvTIKIEKYKzeuSCharsUVQ tLDTgfepWKV+25JneNEh6KNuONQwSTX1MalamoJOxSy8iqK6HJFhJSrEelnC8PTzzgvobT PizzL1vAQ17C0D6b5zvvE6BnC8p1M8k= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=BCLBGzCD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of ckoenig.leichtzumerken@gmail.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=ckoenig.leichtzumerken@gmail.com X-Stat-Signature: 3z1mzifmfk9uucm63mx96xywyt6wzi54 X-Rspamd-Queue-Id: 3FA8BC002A Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=BCLBGzCD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of ckoenig.leichtzumerken@gmail.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=ckoenig.leichtzumerken@gmail.com X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1656057890-816897 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: From: Andrey Grodzovsky Some files allocate large amounts of memory on behalf of userspace without any on disk backing store. This memory isn't necessarily mapped into the address space, but should still accounts towards the RSS of a process just like mapped shared pages do. That information can then be used by the OOM killer to make better decisions which process to reap. For easy debugging this also adds printing of the per file RSS to fdinfo. Signed-off-by: Andrey Grodzovsky Signed-off-by: Christian König --- fs/file.c | 23 +++++++++++++++++++++++ fs/proc/fd.c | 3 +++ include/linux/fdtable.h | 1 + include/linux/fs.h | 1 + 4 files changed, 28 insertions(+) diff --git a/fs/file.c b/fs/file.c index 3bcc1ecc314a..b58730a513be 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1307,3 +1307,26 @@ int iterate_fd(struct files_struct *files, unsigned n, return res; } EXPORT_SYMBOL(iterate_fd); + +static int sumup_file_rss(const void *sum, struct file *file, unsigned n) +{ + if (!file->f_op->file_rss) + return 0; + + *((unsigned long *)sum) += file->f_op->file_rss(file); + return 0; +} + +/** + * files_rss- how much resources are bound by opened files + * @files: opened files + * + * Returns sum of all resources bound by files not accounted in file systems. + */ +unsigned long files_rss(struct files_struct *files) +{ + unsigned long sum = 0; + + iterate_fd(files, 0, sumup_file_rss, &sum); + return sum; +} diff --git a/fs/proc/fd.c b/fs/proc/fd.c index 913bef0d2a36..9943bfca74f7 100644 --- a/fs/proc/fd.c +++ b/fs/proc/fd.c @@ -59,6 +59,9 @@ static int seq_show(struct seq_file *m, void *v) real_mount(file->f_path.mnt)->mnt_id, file_inode(file)->i_ino); + if (file->f_op->file_rss) + seq_printf(m, "rss:\t%lu\n", file->f_op->file_rss(file)); + /* show_fd_locks() never deferences files so a stale value is safe */ show_fd_locks(m, file, files); if (seq_has_overflowed(m)) diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index e066816f3519..101770266f38 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -122,6 +122,7 @@ void do_close_on_exec(struct files_struct *); int iterate_fd(struct files_struct *, unsigned, int (*)(const void *, struct file *, unsigned), const void *); +unsigned long files_rss(struct files_struct *files); extern int close_fd(unsigned int fd); extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags); diff --git a/include/linux/fs.h b/include/linux/fs.h index 9ad5e3520fae..edacbdce5e4c 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2003,6 +2003,7 @@ struct file_operations { loff_t len, unsigned int remap_flags); int (*fadvise)(struct file *, loff_t, loff_t, int); int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags); + long (*file_rss)(struct file *); } __randomize_layout; struct inode_operations {