From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794219938; b=tX4XTWGkGJCEsD1q20QWAJ0v1bkF19pWbxpGXeTgWCgr6yYfISCsqx/fUJeei09a+gKJ DX+FlkAB1s8kQayyRkkVZa2QXwSH8Lf/LEwRPRdd+vhhVkiycsagit3ZPAIhW7jnuL7q6 TtG/sdFVkjD1DnBjernuF4mkXWRpX3H7SMHjrspGSHRQvrMWk665LA/oxo5xr2mzA8lFu MlckRNWgiFdM9So+I+OoR9uzuplP7ggTDNhqxDycuQ0SR/qb5MwZ3e9M8Pxwkf3EPEHOf MHy/0hfpv3KNkxMuLzLMgSgEx1xwG/6ERVVEcMspEOrABtmwGuFZXN5Vm3QKSU+eg0Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794219938; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=S+EuBSGaK4YOu+FEw+Tb1D5cwuOQcvEDWrfjmQfxZFs=; b=0z8SpQ7wbvZ6vhx8G6E5JsfcZcvb3fPiVb6IycY1qERm0sXJ4kwmTyhRtgB4arpitGKJ UF7neZfbUxC83sYY1AQApqMY0smN6fWi+GBRsvW1w8So1IPjkWfRfmd2oJTcFhLz18a8e lk3PnDLqZRbeRVzis+8AUS8lO5SkJh1WMF51OWdQqi8ev0C5uL+q26NUlkLVhe8u/0f4q puPOuyFLNTvlWOV7wpvUJhzFMmrWAeI1KuVxMZ5sPKKrMTuJYckp1oEmEXE5vDRb+7+qr tJIxrq1h19vf2Z5zUXLl4wyOxmFYQ2qbkYwUyXHSUDSxzXZiqWFERa+t0jcRWDDH3Rw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uk2T4Dr4; arc=none smtp.remote-ip=209.85.221.47 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 822D7606A31C for ; Mon, 9 Nov 2026 10:25:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 822D7606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uk2T4Dr4 Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-48e1d0e5dafso432981f8f.1 for ; Mon, 09 Nov 2026 02:25:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794219936; x=1794824736; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=S+EuBSGaK4YOu+FEw+Tb1D5cwuOQcvEDWrfjmQfxZFs=; b=uk2T4Dr4GZTjEVSeZcxFCEEn2VyKvKuqar3yC/PtzjVaEOzUWbCrPIPPU57mI85JH5 VQvhv5VOS3zCBKd8AXiThuHQ5m8nC5kJM0fMwHqIuZqaWRWCYKYC3J2D+TJIt7airiei w0eSPJfsM8dVhZ4vEGajVNhagC12YjKc1xCS45PnACKUi6R3sr21S4Wm9NGz66c761FV Y8jqY/hS82ZxwH3HaswY7gAlnD33JPnh37k4oXCjY8Bvl7BXgFsYVkBHf6pnKB3mEYdy Rhl7C0p/At+lWDpyZznmlapl/nIYaBHsy8XuU0DWObQ0QeiI5vxlEh42neSNsHEOUOO6 fh+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794219936; x=1794824736; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=S+EuBSGaK4YOu+FEw+Tb1D5cwuOQcvEDWrfjmQfxZFs=; b=KUJA4JW3DIlPFML4nrrZqknxKt229ihpVX5UPJZgnPI1HJNtZEOqBpTPa/8NmzFDKg eEcPBqGTRB61csuNTSHOOC8tbEXpoNwsPjD8Sw2kRnYRIQfFbIeVVLfT1M3dUT4LZ70W +plbdZKZFuFDBSBXCzDpJ7IYv0HkR93gMe+Zj7FgFf7oemBPuEA/uPxSNclLbNqWaGHO xMFaXieZ6+mh0GBnrautmNR3yAk8h/sT2wAqX4ao7pprK/wef+0RWvZyp6zSl+AXMjtt kHH+tYgLfwPWV2rrJ42FA/OyDknOeHDzd7h0jsYcnZvQfGo7xef5xBQ0BJGdfPLpACst /enw== X-Forwarded-Encrypted: i=1; AL+hErFepaFp1lbhtIcXD4vPOzHB+naVfxMe0Buw2F89hTgtk8eUpKNsW64P24W8lLc/Ddea9DXoNrSNQ/vWd4s=@vger.kernel.org X-Gm-Message-State: APuU4Z8SROYze91VUuyC3jqBbDmKOix+3mYftwKL7y49kSQomyHQQbhv b30XnGmWG5iEpNyCh3EY8U6EUyNZHnSY1UmubP7C+JRUhenraAWuBmYe X-Gm-Gg: AYBFou27ENdtKeWlDdRtxnwJW799O4qZ7FJAZDTidm3B+PWl4Rq92T6wb2/pjZjFJf/ 59diNlmtzQJUYiiIN+7Kku8VF42BuDNYgQKsxcnIIWsisE0FnBPveSXUIUesWf+xbLv9JFvOvec kOY5+bLV6uu4HvRIASmf46bHYx+chXWeYZnJW6+Ww3xCz25UlJmJgIG+PFdDtD85pkIu1rB/t9h rj8BRj770ULFtbar74sHnxztHP4xOm3a2Ba4yPCBp/Pxg869aYZN2kCqgdCaG2T4sFrDGpGR92p Su30QpW/y5dNT3st8J9ck8T8v6yag5NkoExRVx7PJhrICt05x9GV1qZSOj423cdCX6DTe4vbHwM 3qkYcIlwsA4hAsUvsAreS/IUh1x0rqyx5AQWxQ/oiOC9BsfKErmRwXYjpKxWetGDyGhlpSmaaYR A3Zhtj1nXlFY8HATpvRrIVqUR1FIE9s6NbwhJovGjdrIhRpopdS+9mk/PI4sddJywSeif7Of17G 0tJWxdKrzr3b+ECtfbtbUAZZd8dVJ+PRTDYpUfnvHE9tmpDQT4/e3+DGEHqAQ2WJflfNsHVpAvt 7pnN0qgLakeYnlOfcDmEVSk= X-Received: by 2002:a05:6000:41d3:b0:491:632f:2c8f with SMTP id ffacd0b85a97d-4941f75cac1mr12200348f8f.2.1794219935759; Mon, 09 Nov 2026 02:25:35 -0800 (PST) Received: from [192.168.1.22] ([83.231.69.9]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49597371cc4sm8791060f8f.36.2026.11.09.02.25.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2026 02:25:35 -0800 (PST) Message-ID: Date: Mon, 9 Nov 2026 11:25:32 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/5] mm: limit pagecache folio refcount when splicing into pipes To: "David Hildenbrand (Arm)" , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , "Matthew Wilcox (Oracle)" , Jan Kara , Andrew Morton , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Peter Xu , Johannes Weiner , Song Liu , Rik van Riel , Miklos Szeredi , Alexander Viro , Christian Brauner , Hugh Dickins , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Peter Zijlstra , Arnd Bergmann , Jason Gunthorpe , Kevin Tian , "Joerg Roedel (AMD)" , Robin Murphy , Steve Sistare , John Hubbard , Josef Bacik Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, fuse-devel@lists.linux.dev, linux-arch@vger.kernel.org, iommu@lists.linux.dev, stable@vger.kernel.org References: <20261107-refcount_overflow_stable-v2-0-c38f6809b106@kernel.org> <20261107-refcount_overflow_stable-v2-2-c38f6809b106@kernel.org> <8d23b1eb-72aa-479d-9486-dbfd86d8414c@kernel.org> <43fc45ff-8636-4961-a80a-29844c15d7a1@kernel.org> Content-Language: en-US From: "Jose A. Perez de Azpillaga" In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/9/26 11:08 AM, David Hildenbrand (Arm) wrote: > On 11/9/26 10:49, David Hildenbrand (Arm) wrote: >> On 11/9/26 10:30, David Hildenbrand (Arm) wrote: >>> On 11/9/26 09:57, Jose A. Perez de Azpillaga wrote: >>>> >>>> here it can't tell where the -EOVERFLOW came from, if another file >>>> returns -EOVERFLOW (i.e. tty_fops uses copy_splice_read, and >>>> n_hdlc_tty_read() returns -EOVERFLOW after dropping the frame), it now >>>> gets a second read. >>> >>> >>> That's what claude told me during review, and when asked whether that actually >>> happened in practice it said no ;) >>> >>> So is that actually a problem? >> >> Double checked, claude managed to confuse itself. >> >> n_hdlc_tty_read() can indeed return -EOVERFLOW. >> >> I should just have let splice keep failing, dealing with copy fallback starts to >> get annoying. >> >> I guess switching to -ETOOMANYREFS should work (and might be a better fitting error) > > Yeah, the following should do I think, please double check: > > From 7ce030711737e8c26fb5500f2705940e93da0d44 Mon Sep 17 00:00:00 2001 > From: "David Hildenbrand (Arm)" > Date: Wed, 4 Nov 2026 19:02:48 +0100 > Subject: [PATCH] mm: limit pagecache folio refcount when splicing into pipes > > Similar to mapping pagecache folios into page tables, let's reject > additional references when splicing folios into a pipe if the refcount > is at its limit, similar to how we handle it during GUP. > > This will prevent more cases of possible folio refcount overflows with > large pagecache folios. > > Let's keep it simple and check in the two callers of > splice_folio_into_pipe(). Return -ETOOMANYREFS if the limit prevents any > progress, so do_splice_read() can fall back to copy_splice_read() when > read_iter is available. This keeps sendfile() and other splice users > working when ordinary reads would still succeed. > > Also fall back to copying in FUSE when taking another reference through > fuse_ref_folio() would exceed the limit. > > Also check in try_get_page() to cover pipe-buffer duplication by tee() > and partial pipe-to-pipe splicing. > > Fixes: 800d8c63b2e9 ("shmem: add huge pages support") > Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") > Fixes: 793917d997df ("mm/readahead: Add large folio readahead") > Cc: stable@vger.kernel.org > Signed-off-by: David Hildenbrand (Arm) > --- > fs/fuse/dev.c | 5 +++-- > fs/splice.c | 8 +++++++- > include/linux/mm.h | 9 ++++++--- > mm/filemap.c | 11 ++++++++--- > mm/shmem.c | 4 ++++ > 5 files changed, 28 insertions(+), 9 deletions(-) > > diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c > index 3641abb90d6bb..4f6b7b42bfd3e 100644 > --- a/fs/fuse/dev.c > +++ b/fs/fuse/dev.c > @@ -1265,9 +1265,10 @@ int fuse_copy_folio(struct fuse_copy_state *cs, struct folio **foliop, > if (cs->write && cs->pipebufs && folio) { > /* > * Can't control lifetime of pipe buffers, so always > - * copy user pages. > + * copy user pages. Also copy folios at their refcount limit. > */ > - if (cs->req->args->user_pages) { > + if (cs->req->args->user_pages || > + unlikely(folio_ref_count_over_limit(folio))) { > err = fuse_copy_fill(cs); > if (err) > return err; > diff --git a/fs/splice.c b/fs/splice.c > index e6d43329ed3f0..2fc1279628af5 100644 > --- a/fs/splice.c > +++ b/fs/splice.c > @@ -956,6 +956,7 @@ static ssize_t do_splice_read(struct file *in, loff_t *ppos, > unsigned int flags) > { > unsigned int p_space; > + ssize_t ret; > > if (unlikely(!(in->f_mode & FMODE_READ))) > return -EBADF; > @@ -977,7 +978,12 @@ static ssize_t do_splice_read(struct file *in, loff_t *ppos, > */ > if ((in->f_flags & O_DIRECT) || IS_DAX(in->f_mapping->host)) > return copy_splice_read(in, ppos, pipe, len, flags); > - return in->f_op->splice_read(in, ppos, pipe, len, flags); > + ret = in->f_op->splice_read(in, ppos, pipe, len, flags); > + > + /* Fallback to copying when hitting folio refcount limits. */ > + if (unlikely(ret == -ETOOMANYREFS) && in->f_op->read_iter) > + ret = copy_splice_read(in, ppos, pipe, len, flags); > + return ret; > } > > /** > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 3356f7a670d1e..dd50262a70758 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -2288,10 +2288,13 @@ static inline void get_page(struct page *page) > > static inline __must_check bool try_get_page(struct page *page) > { > - page = compound_head(page); > - if (WARN_ON_ONCE(page_ref_count(page) <= 0)) > + struct folio *folio = page_folio(page); > + > + if (WARN_ON_ONCE(folio_ref_count(folio) <= 0)) > return false; > - page_ref_inc(page); > + if (unlikely(folio_ref_count_over_limit(folio))) > + return false; > + folio_ref_inc(folio); > return true; > } > > diff --git a/mm/filemap.c b/mm/filemap.c > index 920bbf86dfdff..44b23c51ea45f 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -3094,9 +3094,10 @@ size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, > * Return: On success, the number of bytes read will be returned and *@ppos > * will be updated if appropriate; 0 will be returned if there is no more data > * to be read; -EAGAIN will be returned if the pipe had no space, and some > - * other negative error code will be returned on error. A short read may occur > - * if the pipe has insufficient space, we reach the end of the data or we hit a > - * hole. > + * other negative error code will be returned on error; -ETOOMANYREFS will be > + * returned if the folio's refcount is too high and the caller can fall back to > + * copying. A short read may occur if the pipe has insufficient space, we reach > + * the end of the data or we hit a hole. > */ > ssize_t filemap_splice_read(struct file *in, loff_t *ppos, > struct pipe_inode_info *pipe, > @@ -3168,6 +3169,10 @@ ssize_t filemap_splice_read(struct file *in, loff_t *ppos, > if (writably_mapped) > flush_dcache_folio(folio); > > + if (unlikely(folio_ref_count_over_limit(folio))) { > + error = -ETOOMANYREFS; > + goto out; > + } > n = min_t(loff_t, len, isize - *ppos); > n = splice_folio_into_pipe(pipe, folio, *ppos, n); > if (!n) > diff --git a/mm/shmem.c b/mm/shmem.c > index be4ab228cf67c..0b30b1de81a7c 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -3830,6 +3830,10 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, > * Ok, we have the page, and it's up-to-date, so we can > * now splice it into the pipe. > */ > + if (unlikely(folio_ref_count_over_limit(folio))) { > + error = -ETOOMANYREFS; > + break; > + } > n = splice_folio_into_pipe(pipe, folio, *ppos, part); > folio_put(folio); > folio = NULL; this LGTM :) Reviewed-by: Jose A. Perez de Azpillaga -- cheers, jose a. p-a