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 19886C5479D for ; Wed, 11 Jan 2023 12:52:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9717F8E0002; Wed, 11 Jan 2023 07:52:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9047F8E0001; Wed, 11 Jan 2023 07:52:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C1F78E0002; Wed, 11 Jan 2023 07:52:52 -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 694918E0001 for ; Wed, 11 Jan 2023 07:52:52 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2630EC05FD for ; Wed, 11 Jan 2023 12:52:52 +0000 (UTC) X-FDA: 80342507784.24.83000F2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 3E8CE160005 for ; Wed, 11 Jan 2023 12:52:50 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Y2XgBZVv; spf=pass (imf08.hostedemail.com: domain of bfoster@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673441570; a=rsa-sha256; cv=none; b=4bqfHCgXUgNM6z7J6m6e5cvyoLvNGHc90CNWfAonG6HoCyOMVXI/GgUbfzZgmm3IzWMfcw g6VBPelb+JSDv/+lE1vD9tmBprYygJGO+QN5L4MIMfftSuvgTedTUxbuMLioRYIWxi5fnr r2MOK5ECrLOeRPMQVrRydfu3DjBo/jM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Y2XgBZVv; spf=pass (imf08.hostedemail.com: domain of bfoster@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=bfoster@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673441570; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7Ai5priIlF3jLKtBB0hlO6hYc9ZVkmV7mOhvE1Zn4rE=; b=A1cHMWL4bQEvR/AvNBSeJzXf4nbuZXYGiZNk/rZyAGrCEaB2Gkc4zsL4l9HFpdDJY5KxuK Hwm+R68I3+rfkE4puIcFhoximHfWUpsqOPCAKtMjGvlDoYCF+lzS0YazrbHYkrAsSqF/QR dGKbE5EVmJXNLb/4RCCDhpLrkjBDA6U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673441569; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7Ai5priIlF3jLKtBB0hlO6hYc9ZVkmV7mOhvE1Zn4rE=; b=Y2XgBZVvC/iz5E66l6NHJPZKjmoV31joKZlHXoaWD2lk4oYKm3qbBCXjhc7cryqx/+9yUf GvIqD9amd5vGC9Rq85ya/rTLx5J2CwUVHv9YaEoNey1pqZRVDKZmDg4z+2rPaHqon5TBUr idAJV8lqGAijrTVjXTWHhHO60mcmuu4= Received: from mail-ua1-f70.google.com (mail-ua1-f70.google.com [209.85.222.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-412-21gafBWKODuaqnVWLXgGBQ-1; Wed, 11 Jan 2023 07:52:48 -0500 X-MC-Unique: 21gafBWKODuaqnVWLXgGBQ-1 Received: by mail-ua1-f70.google.com with SMTP id i16-20020ab04750000000b005cf05ebd014so3284449uac.9 for ; Wed, 11 Jan 2023 04:52:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7Ai5priIlF3jLKtBB0hlO6hYc9ZVkmV7mOhvE1Zn4rE=; b=Ax327WzTnRYD/mUQYiGIn06QhSunlkawmolxEU7LHEyAZnklzZDqp/my+npnqFzlDr GrXvVH5kwSGLuOt6XNYddbGKSuSVk3nLRyk0yPsohZRO4gCPcLLEVNJlXWQW2p1FvADE 7C1rOIbtfrT36wTbXur+mY3Yu32p2H56Bu3vCf9H+EpFF9CXBkWvtMuvbLqALhEU9G+j Js9+QkN/9Bx6paHcJg0V7fh9u3TlSH25O8XpsHBgbWvVG7b2qOMOLKa4SgYXuM2I1hUq aLV5VAb5P9jCAB4iBoFMgUZ4JDIVLSLe/iZZJbHEJQY4oeoPm+ADG9YdHZdG0cqrr7yf vuXA== X-Gm-Message-State: AFqh2kp3KyVLGzVV//SZbmhNtyV+PjAW3IDCbtVDEWS+lCUySxaynuPt 45wQuB7RqWHD1KcpEwXvc46aQToEXujsPVAhBHFV4pqG3RHXuM8SEI5rFvCsDk7max8Dkhp2yqW No5OHGNiwr0M= X-Received: by 2002:a67:e2c3:0:b0:3ce:e9bd:aa0f with SMTP id i3-20020a67e2c3000000b003cee9bdaa0fmr10621633vsm.32.1673441567938; Wed, 11 Jan 2023 04:52:47 -0800 (PST) X-Google-Smtp-Source: AMrXdXvQaryx8x1AiOUADAxBgk+2e/cvaMXQdfn1FRaAY2h5sWu4sa5+bZtWUdTDnsWwGYlp8Vk+hQ== X-Received: by 2002:a67:e2c3:0:b0:3ce:e9bd:aa0f with SMTP id i3-20020a67e2c3000000b003cee9bdaa0fmr10621624vsm.32.1673441567611; Wed, 11 Jan 2023 04:52:47 -0800 (PST) Received: from bfoster (c-24-61-119-116.hsd1.ma.comcast.net. [24.61.119.116]) by smtp.gmail.com with ESMTPSA id c23-20020a05620a269700b006fb11eee465sm9068639qkp.64.2023.01.11.04.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 04:52:46 -0800 (PST) Date: Wed, 11 Jan 2023 07:53:48 -0500 From: Brian Foster To: Nhat Pham Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, willy@infradead.org, kernel-team@meta.com Subject: Re: [PATCH v5 2/3] cachestat: implement cachestat syscall Message-ID: References: <20230104231127.2634648-1-nphamcs@gmail.com> <20230104231127.2634648-3-nphamcs@gmail.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 3E8CE160005 X-Stat-Signature: z6wqdh5wxn1dc5ty6icn9e6oqkcygyr6 X-HE-Tag: 1673441570-845992 X-HE-Meta: U2FsdGVkX18A03dr0XUnBpaWbx/34aL5Be2PTPPzvAv64oham1ga72pnkusWlyfC72SGT6gADr4GuSf6lAVPYyaDlboReaJoKmMYTUjBtkDjKn8Tpuda6Bj0zlSBLKo44TOzfMAlZaOqv+LpjC/ZG1zmgAWXIM7xxU2CWSZDcak8fBmAarTOFRJWHUUoSwT+RHD2PNgb90FDVScxWoZTiUGDhEZGJ4NCc7S1TkJ68JXPk1g1wxPf/5/+iUnre/NTPIk0/phR72YFxwws2wqaG7i+FWOM4jZfePxfvMIedDNWkqhaSsKHuca3RxEaPBQQ3xgeXFNx5CUrROGz0Znq1vhN2AszgWXIKgO9fKCtLwmHo1V1a7xm013AUM+r6aEOaigbDv3mrKALf92Hrwk2yNkg6cOv1i6JkUrmmCq30IeFEJJRrrRtTmhyCGLT0UoszmV2D9SgwVEdExPELVoZWKyQNbj+hKoHC6RWaiVidUYHOdYhTyM76iw17piVGid8pBa4tH70WwOUIea4t1Y0ouAoy7iEuv3gZRgf+8/X3LA0ZYer0w0PPS4fSV13h5YpGmQO3Q30ZBn8EDT87mo+CS+UPU+DaRLXBlv5StherQnRUct5vchkGuiVVUPXHihDIKcQqLnK/A9Aiyy40BtJp9rZkvfUsrgehdEgYDOH1QV5L2TvTpW+KjwHGLjYujJ9Hxmr8hm5gVSsbebOd10/snpX0lwmIU1rR2LymQMl7brkFcNe3p2Znpr0VEmKtu1KvJVNopfJ9EhPJdDNt1HhIh+MwR7US1NvYyz/KRDkDkS135wwt2z2nAVmfhOevhE7O0fekIM1fEIBxPuQ8DTkw6+amuaq6GP+g3plJYe0AY/aD+f/dWQ3zXfXbL5nwUwt77kW9R9XSAebuqGHQBGECPuZfPeM4HoKChutFOsAnnXQBd1qsNoYy9TRZ91cxIRrmp0wVWeYBXZ10fgzBIE 79ht4MgN sDRmudxgo5R14bIzCOWGdRoqwPa5wY2snGvQbAy8WPNKx0i68TlzNkGq4rwWzuRGTRqkpE62B0xnIz+ON262leUeD9g5IUw0i5aOkElgPkmO5nl4= 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 Tue, Jan 10, 2023 at 01:16:27PM -0800, Nhat Pham wrote: > On Tue, Jan 10, 2023 at 6:21 AM Brian Foster wrote: > > > > On Wed, Jan 04, 2023 at 03:11:26PM -0800, Nhat Pham wrote: > > > Implement a new syscall that queries cache state of a file and > > > summarizes the number of cached pages, number of dirty pages, number of > > > pages marked for writeback, number of (recently) evicted pages, etc. in > > > a given range. > > > > > > NAME > > > cachestat - query the page cache statistics of a file. > > > > > > SYNOPSIS > > > #include > > > > > > struct cachestat { > > > __u64 nr_cache; > > > __u64 nr_dirty; > > > __u64 nr_writeback; > > > __u64 nr_evicted; > > > __u64 nr_recently_evicted; > > > }; > > > > > > int cachestat(unsigned int fd, off_t off, size_t len, > > > size_t cstat_size, struct cachestat *cstat, > > > unsigned int flags); > > > > > > DESCRIPTION > > > cachestat() queries the number of cached pages, number of dirty > > > pages, number of pages marked for writeback, number of evicted > > > pages, number of recently evicted pages, in the bytes range given by > > > `off` and `len`. > > > > > > An evicted page is a page that is previously in the page cache but > > > has been evicted since. A page is recently evicted if its last > > > eviction was recent enough that its reentry to the cache would > > > indicate that it is actively being used by the system, and that > > > there is memory pressure on the system. > > > > > > These values are returned in a cachestat struct, whose address is > > > given by the `cstat` argument. > > > > > > The `off` and `len` arguments must be non-negative integers. If > > > `len` > 0, the queried range is [`off`, `off` + `len`]. If `len` == > > > 0, we will query in the range from `off` to the end of the file. > > > > > > `cstat_size` allows users to obtain partial results. The syscall > > > will copy the first `csstat_size` bytes to the specified userspace > > > memory. `cstat_size` must be a non-negative value that is no larger > > > than the current size of the cachestat struct. > > > > > > The `flags` argument is unused for now, but is included for future > > > extensibility. User should pass 0 (i.e no flag specified). > > > > > > RETURN VALUE > > > On success, cachestat returns 0. On error, -1 is returned, and errno > > > is set to indicate the error. > > > > > > ERRORS > > > EFAULT cstat points to an invalid address. > > > > > > EINVAL invalid `cstat_size` or `flags` > > > > > > EBADF invalid file descriptor. > > > > > > Signed-off-by: Nhat Pham > > > --- > > > 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/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 ++++++++++++++++++++ > > > 20 files changed, 186 insertions(+), 1 deletion(-) > > > > > ... > > > diff --git a/mm/filemap.c b/mm/filemap.c > > > index 08341616ae7a..d70d47b20700 100644 > > > --- a/mm/filemap.c > > > +++ b/mm/filemap.c > > ... > > > @@ -3949,3 +3953,142 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp) > > > return try_to_free_buffers(folio); > > > } > > > EXPORT_SYMBOL(filemap_release_folio); > > ... > > > +#ifdef CONFIG_CACHESTAT_SYSCALL > > > +/* > > > + * The cachestat(5) system call. > > > + * > > > + * cachestat() returns the page cache statistics of a file in the > > > + * bytes range specified by `off` and `len`: number of cached pages, > > > + * number of dirty pages, number of pages marked for writeback, > > > + * number of evicted pages, and number of recently evicted pages. > > > + * > > > + * An evicted page is a page that is previously in the page cache > > > + * but has been evicted since. A page is recently evicted if its last > > > + * eviction was recent enough that its reentry to the cache would > > > + * indicate that it is actively being used by the system, and that > > > + * there is memory pressure on the system. > > > + * > > > + * `off` and `len` must be non-negative integers. If `len` > 0, > > > + * the queried range is [`off`, `off` + `len`]. If `len` == 0, > > > + * we will query in the range from `off` to the end of the file. > > > + * > > > + * `cstat_size` allows users to obtain partial results. The syscall > > > + * will copy the first `csstat_size` bytes to the specified userspace > > > + * memory. It also makes the cachestat struct extensible - new fields > > > + * can be added in the future without breaking existing usage. > > > + * `cstat_size` must be a non-negative value that is no larger than > > > + * the current size of the cachestat struct. > > > + * > > > + * The `flags` argument is unused for now, but is included for future > > > + * extensibility. User should pass 0 (i.e no flag specified). > > > + * > > > + * Because the status of a page can change after cachestat() checks it > > > + * but before it returns to the application, the returned values may > > > + * contain stale information. > > > + * > > > + * return values: > > > + * zero - success > > > + * -EFAULT - cstat points to an illegal address > > > + * -EINVAL - invalid arguments > > > + * -EBADF - invalid file descriptor > > > + */ > > > +SYSCALL_DEFINE6(cachestat, unsigned int, fd, off_t, off, size_t, len, > > > + size_t, cstat_size, struct cachestat __user *, cstat, > > > + unsigned int, flags) > > > +{ > > > + struct fd f = fdget(fd); > > > + struct address_space *mapping; > > > + struct cachestat cs; > > > + pgoff_t first_index = off >> PAGE_SHIFT; > > > + pgoff_t last_index = > > > + len == 0 ? ULONG_MAX : (off + len - 1) >> PAGE_SHIFT; > > > + > > > + if (off < 0 || cstat_size > sizeof(struct cachestat) || flags != 0) > > > + return -EINVAL; > > > + > > > + if (!f.file) > > > + return -EBADF; > > > + > > > > It looks like we miss an fdput() before returning via the above error > > checks. > > Ooops yeah I missed that. I'll fix it. > > > > > The only other thing that stands out as a bit odd to me is the > > cstat_size check and associated ability to return a partial cachestat > > struct. Do other syscalls do anything like that? I'd think we'd want to > > ensure we always at least return a fully populated cachestat struct, > > even if it happened to be an old/compat version if the size does ever > > increase. Hm? > > Not that I know of, but the idea is that the user might expect a smaller > struct cachestat in their code (and allocate memory accordingly). > With this cstat_size, we can make sure that the expansion of cachestat > struct (with new fields) does not break existing user's code - we only > copy part of the struct. > Yup, I get the idea of supporting future expansion. The part that doesn't make sense to me is allowing a partial copy of struct cachestat. For example, what happens if the user passes a cstat_size of 2? We copy out two bytes of the first field of the struct and return 0 for "success?" The way I've commonly seen this sort of support for future expansion is by versioning the structure and providing backwards compatibility for old versions of the structure. I'd guess there are various ways to implement that, but a simple example that comes to mind for me is xfs_ioc_fsgeometry(). The caveat there is that it's an ioctl where IIRC the command essentially changes when the structure size does, which allows the kernel to determine what version is being used. I'm not quite sure what the ideal way to do that for a syscall is. I suppose the kernel could just check that the size param matches some version of the structure it knows about and assume that. Or since you have the flags field, you could require a flag to be passed in to return values for any new fields. Or yet another option could be to pad out the size of the userspace structure to support future expansion for the foreseeable future without the need for such a flag. Perhaps this is something linux-api can help with? Brian > > > > Brian > > > > > + memset(&cs, 0, sizeof(struct cachestat)); > > > + mapping = f.file->f_mapping; > > > + filemap_cachestat(mapping, first_index, last_index, &cs); > > > + fdput(f); > > > + > > > + if (copy_to_user(cstat, &cs, cstat_size)) > > > + return -EFAULT; > > > + > > > + return 0; > > > +} > > > +#endif /* CONFIG_CACHESTAT_SYSCALL */ > > > -- > > > 2.30.2 > > > > > >