From patchwork Thu Jan 26 17:53:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13117561 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 CE4D3C52D11 for ; Thu, 26 Jan 2023 17:54:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 31BB16B0074; Thu, 26 Jan 2023 12:54:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A4CB6B0075; Thu, 26 Jan 2023 12:54:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11F668E0001; Thu, 26 Jan 2023 12:54:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id F0F166B0074 for ; Thu, 26 Jan 2023 12:54:00 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C200114070F for ; Thu, 26 Jan 2023 17:54:00 +0000 (UTC) X-FDA: 80397698640.25.A7CB0BB Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf03.hostedemail.com (Postfix) with ESMTP id EE6EA20010 for ; Thu, 26 Jan 2023 17:53:58 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hElnnUwA; spf=pass (imf03.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674755639; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=n192hm/L4uRs68EENWfUDYuKeHNwIAhNrH1oIJOJdPo=; b=lCBqEYR5PBaHF5fHHD1zst3wxDuCDA6Npz1chvMNNeqp+nKCTR8fP3f/fpHplWtYFcL1IA AqeEBk/LGgMYUJO/0d0tsFs4C1M3xJLK0ISwmSzoRHzdFK/HxFmN6y2Z4/v7OjU5anHBX/ A8Z47ixPQmTapkQEinjKByoknNcMpUI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hElnnUwA; spf=pass (imf03.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674755639; a=rsa-sha256; cv=none; b=dSXt9gapAhAx1j5hCCsNk+rraijGzcIR47DK3668GiNEvo1QhBn8w/NqPTqXJ79KMYNAdY 49wLC5sCxyNBmAnY6kvFLs2dGfHuzcb10q7421VFwvDaNZeQK6CIGZ7ZN/tmT4HeNjKf3w TJQSkeXXrUIcjzE+jDkGMY6pvNlRO1s= Received: by mail-pj1-f53.google.com with SMTP id rm7-20020a17090b3ec700b0022c05558d22so2464581pjb.5 for ; Thu, 26 Jan 2023 09:53:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=n192hm/L4uRs68EENWfUDYuKeHNwIAhNrH1oIJOJdPo=; b=hElnnUwAEXjPU1BMKqHU8Nbc5nyi2JcmAH76rGsl1zMxULVcq4BUAaP/1NF6fm2Qwq InontjRzWfvnNedHLbEfElVIh5rtVFwmkYbWpDEBZaXWUjs/rq2NLVPqyQyLJmjJQzh4 nltLqoqFNXBGrZsXROXTqP6k3DD+UdRjizX/CzHln7bQZMhwCRsXKIsiYH55AhrxMsqI M/oPXPfxbpnO//3s8Zs/8GiJLARAB9fN/qgjJ95eJxHu+n9WlOxL2IY4/590KpGba4XD NAzTVHRm5iYQlJRdwj2of8lT4zrlZG4enNCm3mVNXjeSae0raXI/vcC3MWSe5LYgoMxy G+Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=n192hm/L4uRs68EENWfUDYuKeHNwIAhNrH1oIJOJdPo=; b=JBxLm41BBfBkVRU6Jei8f8QpUkVnEUfRnzDvYjDiIIpVQAtIYQPL0HXAWcrANtUyNK 0PP34/WU0gByKtJNTzLuUbxzJx1oLfdKLm+TJgKO3354D7QyaJ9otHjVEz3LDELgsVxT 0CLRwwv8xlPxoR6awDEE13+i3mjbeZsBMNcfr4xmMcZ5nHxYzusM2sxU3qRHe9CNojxH Hg9QSYdMskCXq5+AB1fA03dmuH8udzwpHqEfpz1iZsiEwBkWyPMQKzb8Op10pEkiAiw/ fEVpKSegZZlvcbXQfOOXNJIlmvwaWPIbOFVQyAI3nB6UDDf3eLwdG8hobmrGd+1eAHip R+fg== X-Gm-Message-State: AO0yUKWW+ntrsqrh8q3vwGvqbBuo5/YWwB7XPVmG/H+DnNUZOCU1z7xP rah7iq+EwPTapkCEuafnCgY= X-Google-Smtp-Source: AK7set+3H/Cx43TB4eyf+4vgEpiUx/Qk3m7um1t94EtC9faczX5to4JK2c67kX0G+Mb2VpIwagYidQ== X-Received: by 2002:a17:902:dacf:b0:196:2bf1:b68d with SMTP id q15-20020a170902dacf00b001962bf1b68dmr7722875plx.14.1674755637765; Thu, 26 Jan 2023 09:53:57 -0800 (PST) Received: from localhost (fwdproxy-prn-015.fbsv.net. [2a03:2880:ff:f::face:b00c]) by smtp.gmail.com with ESMTPSA id l10-20020a170902eb0a00b00196503444b0sm56169plb.43.2023.01.26.09.53.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 09:53:57 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, linux-api@vger.kernel.org, kernel-team@meta.com Subject: [PATCH v8 0/3] cachestat: a new syscall for page cache state of files Date: Thu, 26 Jan 2023 09:53:53 -0800 Message-Id: <20230126175356.1582123-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: EE6EA20010 X-Stat-Signature: 5tpn745ntcp6h4bbdsauo3gmo9xs7ki1 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674755638-865668 X-HE-Meta: U2FsdGVkX1+GH39iEp89Aq/3BXAyxx6rDsSw8mmQqnwYgGfp7czUSfCt/0y6yWrKl62XMvs//9aJPsPNN/AiWL/y4fNXXsznYwpr4eW5lHj7eczwmz5DSHh1z7p8XBCydA/1byb/IPbZ4KUb24ChdQrUgeeZ16CaoC8mVI0YFWLn7QPIQIKCOtijOJeYAZDh4mBeSH3efjnPFKgn5idqYcy0AvHKCBYLGTyT9l0eMNF0RzPnNqAcZ0bk8XKlGS2ImpOFbpM1MnM7G2FrmoqDxcsrjo+vt54EaJ5fYKIdORP0fz2hdRgpMMXi9xMQuFHcu+kRY6LyZ3p5PRbFKgOVzdRvEkUyHRR/syR6HZG9/NfVyTVR8ADDbsBM2bgJnnnVSojYsXb9bBtTWcfHuCZQQwwS7OG6T+uLBZl9E05Ktg+BVt5i3ynKCO+8P1hfCsKCO3hUvxpfSy3ZeYWDkGSIksi+4Aklk0v8RMzCAiz+CHdQ6dfMnMp+gnFHS2xdAUBlbY5w+IneDHPL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sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Changelog: v8: * Add syscall to mips syscall tables (detected by kernel test robot) (patch 2) * Add a missing return (suggested by Yu Zhao) (patch 2) v7: * Fix and use lru_gen_test_recent (suggested by Brian Foster) (patch 2) * Small formatting and organizational fixes v6: * Add a missing fdput() (suggested by Brian Foster) (patch 2) * Replace cstat_size with cstat_version (suggested by Brian Foster) (patch 2) * Add conditional resched to the xas walk. (suggested by Hillf Danton) (patch 2) v5: * Separate first patch into its own series. (suggested by Andrew Morton) * Expose filemap_cachestat() to non-syscall usage (patch 2) (suggested by Brian Foster). * Fix some build errors from last version. (patch 2) * Explain eviction and recent eviction in the draft man page and documentation (suggested by Andrew Morton). (patch 2) v4: * Refactor cachestat and move it to mm/filemap.c (patch 3) (suggested by Brian Foster) * Remove redundant checks (!folio, access_ok) (patch 3) (suggested by Matthew Wilcox and Al Viro) * Fix a bug in handling multipages folio. (patch 3) (suggested by Matthew Wilcox) * Add a selftest for shmem files, which can be used to test huge pages (patch 4) (suggested by Johannes Weiner) v3: * Fix some minor formatting issues and build errors. * Add the new syscall entry to missing architecture syscall tables. (patch 3). * Add flags argument for the syscall. (patch 3). * Clean up the recency refactoring (patch 2) (suggested by Yu Zhao) * Add the new Kconfig (CONFIG_CACHESTAT) to disable the syscall. (patch 3) (suggested by Josh Triplett) v2: * len == 0 means query to EOF. len < 0 is invalid. (patch 3) (suggested by Brian Foster) * Make cachestat extensible by adding the `cstat_size` argument in the syscall (patch 3) There is currently no good way to query the page cache state of large file sets and directory trees. There is mincore(), but it scales poorly: the kernel writes out a lot of bitmap data that userspace has to aggregate, when the user really doesn not care about per-page information in that case. The user also needs to mmap and unmap each file as it goes along, which can be quite slow as well. This series of patches introduces a new system call, cachestat, that summarizes the page cache statistics (number of cached pages, dirty pages, pages marked for writeback, evicted pages etc.) of a file, in a specified range of bytes. It also include a selftest suite that tests some typical usage This interface is inspired by past discussion and concerns with fincore, which has a similar design (and as a result, issues) as mincore. Relevant links: https://lkml.indiana.edu/hypermail/linux/kernel/1302.1/04207.html https://lkml.indiana.edu/hypermail/linux/kernel/1302.1/04209.html For comparison with mincore, I ran both syscalls on a 2TB sparse file: Using mincore: real 0m37.510s user 0m2.934s sys 0m34.558s Using cachestat: real 0m0.009s user 0m0.000s sys 0m0.009s This series should be applied on top of: workingset: fix confusion around eviction vs refault container https://lkml.org/lkml/2023/1/4/1066 This series consist of 3 patches: Nhat Pham (3): workingset: refactor LRU refault to expose refault recency check cachestat: implement cachestat syscall selftests: Add selftests for cachestat MAINTAINERS | 7 + arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/fs.h | 3 + include/linux/swap.h | 1 + include/linux/syscalls.h | 4 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mman.h | 9 + init/Kconfig | 10 + kernel/sys_ni.c | 1 + mm/filemap.c | 154 +++++++++++ mm/workingset.c | 142 ++++++---- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/cachestat/.gitignore | 2 + tools/testing/selftests/cachestat/Makefile | 8 + .../selftests/cachestat/test_cachestat.c | 260 ++++++++++++++++++ 30 files changed, 576 insertions(+), 47 deletions(-) create mode 100644 tools/testing/selftests/cachestat/.gitignore create mode 100644 tools/testing/selftests/cachestat/Makefile create mode 100644 tools/testing/selftests/cachestat/test_cachestat.c base-commit: 1440f576022887004f719883acb094e7e0dd4944 prerequisite-patch-id: 171a43d333e1b267ce14188a5beaea2f313787fb