From a4879eb2cfa89ca3c5a6d1186e36b5cc0ea36732 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 4 Nov 2026 06:03:52 +0000 Subject: [PATCH] fix(linux): link original buffered folio writes --- .../linux/0150-select-original-buffered.patch | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 third_party/patches/linux/0150-select-original-buffered.patch diff --git a/third_party/patches/linux/0150-select-original-buffered.patch b/third_party/patches/linux/0150-select-original-buffered.patch new file mode 100644 index 0000000000..013e6b1a4c --- /dev/null +++ b/third_party/patches/linux/0150-select-original-buffered.patch @@ -0,0 +1,217 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Tyler Smith <74526874+tylertms@users.noreply.github.com> +Date: Wed, 4 Nov 2026 01:03:39 -0500 +Subject: [PATCH] refactor(mm): select original buffered folio writes + +--- + mm/filemap.c | 94 ---------------------------------------------- + mm/filemap_batch.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++ + 2 files changed, 94 insertions(+), 94 deletions(-) + +diff --git a/mm/filemap.c b/mm/filemap.c +index e120e19037b217a046954ec2823805a4e6e08057..1517cdec708ec8f6e77f665028c96bb5872b05a0 100644 +--- a/mm/filemap.c ++++ b/mm/filemap.c +@@ -1691,100 +1691,6 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) + } + EXPORT_SYMBOL(generic_file_direct_write); + +-ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i) +-{ +- struct file *file = iocb->ki_filp; +- loff_t pos = iocb->ki_pos; +- struct address_space *mapping = file->f_mapping; +- const struct address_space_operations *a_ops = mapping->a_ops; +- size_t chunk = mapping_max_folio_size(mapping); +- long status = 0; +- ssize_t written = 0; +- +- do { +- struct folio *folio; +- size_t offset; /* Offset into folio */ +- size_t bytes; /* Bytes to write to folio */ +- size_t copied; /* Bytes copied from user */ +- void *fsdata = NULL; +- +- bytes = iov_iter_count(i); +-retry: +- offset = pos & (chunk - 1); +- bytes = min(chunk - offset, bytes); +- balance_dirty_pages_ratelimited(mapping); +- +- if (fatal_signal_pending(current)) { +- status = -EINTR; +- break; +- } +- +- status = a_ops->write_begin(iocb, mapping, pos, bytes, +- &folio, &fsdata); +- if (unlikely(status < 0)) +- break; +- +- offset = offset_in_folio(folio, pos); +- if (bytes > folio_size(folio) - offset) +- bytes = folio_size(folio) - offset; +- +- if (mapping_writably_mapped(mapping)) +- flush_dcache_folio(folio); +- +- /* +- * Faults here on mmap()s can recurse into arbitrary +- * filesystem code. Lots of locks are held that can +- * deadlock. Use an atomic copy to avoid deadlocking +- * in page fault handling. +- */ +- copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); +- flush_dcache_folio(folio); +- +- status = a_ops->write_end(iocb, mapping, pos, bytes, copied, +- folio, fsdata); +- if (unlikely(status != copied)) { +- iov_iter_revert(i, copied - max(status, 0L)); +- if (unlikely(status < 0)) +- break; +- } +- cond_resched(); +- +- if (unlikely(status == 0)) { +- /* +- * A short copy made ->write_end() reject the +- * thing entirely. Might be memory poisoning +- * halfway through, might be a race with munmap, +- * might be severe memory pressure. +- */ +- if (chunk > PAGE_SIZE) +- chunk /= 2; +- if (copied) { +- bytes = copied; +- goto retry; +- } +- +- /* +- * 'folio' is now unlocked and faults on it can be +- * handled. Ensure forward progress by trying to +- * fault it in now. +- */ +- if (fault_in_iov_iter_readable(i, bytes) == bytes) { +- status = -EFAULT; +- break; +- } +- } else { +- pos += status; +- written += status; +- } +- } while (iov_iter_count(i)); +- +- if (!written) +- return status; +- iocb->ki_pos += written; +- return written; +-} +-EXPORT_SYMBOL(generic_perform_write); +- + /** + * __generic_file_write_iter - write data to a file + * @iocb: IO state structure (file, offset, etc.) +diff --git a/mm/filemap_batch.c b/mm/filemap_batch.c +index 76c660e76c881ce88331775b9f580db771c8e683..b3f2c85b1f0f87769eb2636f900b99e45b83df56 100644 +--- a/mm/filemap_batch.c ++++ b/mm/filemap_batch.c +@@ -2983,3 +2983,97 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping, + return xas.xa_index - 1; + } + EXPORT_SYMBOL(page_cache_prev_miss); ++ ++ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i) ++{ ++ struct file *file = iocb->ki_filp; ++ loff_t pos = iocb->ki_pos; ++ struct address_space *mapping = file->f_mapping; ++ const struct address_space_operations *a_ops = mapping->a_ops; ++ size_t chunk = mapping_max_folio_size(mapping); ++ long status = 0; ++ ssize_t written = 0; ++ ++ do { ++ struct folio *folio; ++ size_t offset; /* Offset into folio */ ++ size_t bytes; /* Bytes to write to folio */ ++ size_t copied; /* Bytes copied from user */ ++ void *fsdata = NULL; ++ ++ bytes = iov_iter_count(i); ++retry: ++ offset = pos & (chunk - 1); ++ bytes = min(chunk - offset, bytes); ++ balance_dirty_pages_ratelimited(mapping); ++ ++ if (fatal_signal_pending(current)) { ++ status = -EINTR; ++ break; ++ } ++ ++ status = a_ops->write_begin(iocb, mapping, pos, bytes, ++ &folio, &fsdata); ++ if (unlikely(status < 0)) ++ break; ++ ++ offset = offset_in_folio(folio, pos); ++ if (bytes > folio_size(folio) - offset) ++ bytes = folio_size(folio) - offset; ++ ++ if (mapping_writably_mapped(mapping)) ++ flush_dcache_folio(folio); ++ ++ /* ++ * Faults here on mmap()s can recurse into arbitrary ++ * filesystem code. Lots of locks are held that can ++ * deadlock. Use an atomic copy to avoid deadlocking ++ * in page fault handling. ++ */ ++ copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); ++ flush_dcache_folio(folio); ++ ++ status = a_ops->write_end(iocb, mapping, pos, bytes, copied, ++ folio, fsdata); ++ if (unlikely(status != copied)) { ++ iov_iter_revert(i, copied - max(status, 0L)); ++ if (unlikely(status < 0)) ++ break; ++ } ++ cond_resched(); ++ ++ if (unlikely(status == 0)) { ++ /* ++ * A short copy made ->write_end() reject the ++ * thing entirely. Might be memory poisoning ++ * halfway through, might be a race with munmap, ++ * might be severe memory pressure. ++ */ ++ if (chunk > PAGE_SIZE) ++ chunk /= 2; ++ if (copied) { ++ bytes = copied; ++ goto retry; ++ } ++ ++ /* ++ * 'folio' is now unlocked and faults on it can be ++ * handled. Ensure forward progress by trying to ++ * fault it in now. ++ */ ++ if (fault_in_iov_iter_readable(i, bytes) == bytes) { ++ status = -EFAULT; ++ break; ++ } ++ } else { ++ pos += status; ++ written += status; ++ } ++ } while (iov_iter_count(i)); ++ ++ if (!written) ++ return status; ++ iocb->ki_pos += written; ++ return written; ++} ++EXPORT_SYMBOL(generic_perform_write);