From patchwork Wed Jan 4 23:10:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 3580 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp14736wrt; Wed, 4 Jan 2023 15:13:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXveRRPcXWnH0yHEgAF0oE1ax4qv+pPYz8Q8c8QGmmKjhRVpZF0d8dWbUHjVkCwyHHreXRcf X-Received: by 2002:a17:906:bcc6:b0:7c0:e0d7:b73a with SMTP id lw6-20020a170906bcc600b007c0e0d7b73amr40806633ejb.68.1672873998823; Wed, 04 Jan 2023 15:13:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672873998; cv=none; d=google.com; s=arc-20160816; b=gtBNIptKqywenD1WdJIXCLB60jJxYVCOEz/V/8VNJOmNDMJUZFFTku/gqrp0Cs89kQ OzYYvJ0UZgvagCx6PR1Ce9YGBiWGzHdfvwn7BVIPSd+34AClgr9mF+/O8vbCVafIpgP1 az6+kAIZboClUMY+moGi1ajzY9Fkb3r4xqTQMLx/4esWvCeXMoc5YwyGBsRuN38N1Nmk ESKr4VAgYiH+Y54VmQ1FbKP6ttcWpVp040taLOEXx+6ISw80heBgNCDYukkr5V9L+lMF rrvVZG1C44Zlwu+4oPteHFHTwEJ30HSUInU9vaq1Y0XXdHUmIIEX+46aozrKAVKAyFJo RrSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=B01nNF6+kAsKxYPfPU/0+WQhCbAoy0WeTmIq8oeqfFU=; b=WCvhpxjjkglEffzs2/fohriZrhkb5pcvWUyEKyO5RFuX4GAB3asNOh1G4WMQRcy7Bp dMkb7SZwo7FBuIu9wuYgOgCaKZ6o7OxJ4+fFQs6l6+yP3Fb3EVij/EA0dggRcQJre5O/ JvtlSDB6PrZZFtnQAMNs+De+TWq7rHpwBbadvrVTO7+vHktNDJFVZUI5lYxoMEKsqxcO jJsgTvp/T8ct8Sdj7+nziO7EgG4Zv9CNm3PTIhX8zl+WyPxxi+L5hYRm8vzhDvAIitoI VAVNC45B4CLxY48upvB8M22Z2IAF1U4H/wrCdhAhccEAdgC/MwsL+SBdUxRtxgqGfCOc QgKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GyjjMhT7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xj8-20020a170906db0800b007c16d82961csi31536715ejb.278.2023.01.04.15.12.55; Wed, 04 Jan 2023 15:13:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GyjjMhT7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229464AbjADXLE (ORCPT + 99 others); Wed, 4 Jan 2023 18:11:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229571AbjADXLA (ORCPT ); Wed, 4 Jan 2023 18:11:00 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D645A33D53 for ; Wed, 4 Jan 2023 15:10:58 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id h192so18516274pgc.7 for ; Wed, 04 Jan 2023 15:10: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=B01nNF6+kAsKxYPfPU/0+WQhCbAoy0WeTmIq8oeqfFU=; b=GyjjMhT7es/DNqMgW9D/+0rv4LOnbs/7E8Bh/PSBkCsynthD771CdAuWM5BgvLkGIt VTaZ7C7AjYBUJzEjN3GtKIL/gjN6g4j4e3JB6f08yVjtSEa0Cu9pZcKUwUlfrmkyv529 qpmuxa9n37Ylo+l9vEd2lneZlm9Uf/iKISUSRA+/iYnmb8B5ni7X0qZhX6V0pfqiTFkz gZ4dA3drZzm3WLx8gx6DXlxXfXrts1YDTlYj5F1GvAXr3nqA4GQE3csabqp1P2Y6WScS h3bk2P1pmHHzdKT16rN2CbUUJa7JwmhQkHT55giYH0RFqcViaQCNatA0UaCT0zXX/82b w5bg== 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=B01nNF6+kAsKxYPfPU/0+WQhCbAoy0WeTmIq8oeqfFU=; b=XlYATWGpd4OPLk3JMvnab6E8a5GFJ6IaBOus2TQzoO4wtRHhlQBSnczbfTmpk2W18I DX/Imv6AxMbX5XGjXY73sNwnPgQg53s0/VD+eaqXYtb1yGemhLavZEeuyR4AfieJ1wSY aKJd9A/5WbdT1pfA5JeLcArS1AdxbclbBJv8Hv0O5jFkKyq7Z+spa2dnJ7SuzQE4Qx7b NOim41qhwu3JG1VcBVX7KvR7JV7kHgzqyKWKRhYDiHEehCRb/pKmldojvGqnhsO8yiap X2WLPwIF+UdIqW0R0QjkfN/lE4qOsfXLStG+ElJq0/8cb2Nzfog68m185GHouBzuKNpO iNug== X-Gm-Message-State: AFqh2ko/pkw/E0y04oHiRIFyXQFV3DklBwKZH5sEb9RWwUHEhzaFCP22 VHcr5jHlRmVNTd5NrwCwxAE= X-Received: by 2002:a62:8408:0:b0:575:d06d:1bfa with SMTP id k8-20020a628408000000b00575d06d1bfamr54592617pfd.2.1672873858369; Wed, 04 Jan 2023 15:10:58 -0800 (PST) Received: from localhost (fwdproxy-prn-020.fbsv.net. [2a03:2880:ff:14::face:b00c]) by smtp.gmail.com with ESMTPSA id k3-20020aa79983000000b0057462848b94sm10924829pfh.184.2023.01.04.15.10.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 15:10: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, kernel-team@meta.com Subject: [PATCH v5 0/3] cachestat: a new syscall for page cache state of files Date: Wed, 4 Jan 2023 15:10:54 -0800 Message-Id: <20230104231057.2632639-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754135526001266116?= X-GMAIL-MSGID: =?utf-8?q?1754135526001266116?= Changelog: 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/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 | 3 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mman.h | 9 + init/Kconfig | 10 + kernel/sys_ni.c | 1 + mm/filemap.c | 143 ++++++++++ mm/workingset.c | 129 ++++++--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/cachestat/.gitignore | 2 + tools/testing/selftests/cachestat/Makefile | 8 + .../selftests/cachestat/test_cachestat.c | 259 ++++++++++++++++++ 27 files changed, 555 insertions(+), 39 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