mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joseph Qi <joseph.qi@linux.alibaba.com>
To: sashiko-reviews@lists.linux.dev,
	Heming Zhao <heming.zhao@suse.com>,
	Christoph Hellwig <hch@lst.de>
Cc: Mark Fasheh <mark@fasheh.com>, Joel Becker <jlbec@evilplan.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"ocfs2-devel@lists.linux.dev" <ocfs2-devel@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap
Date: Thu, 3 Dec 2026 13:43:38 +0800	[thread overview]
Message-ID: <b6b98ae9-ba00-4d25-8164-11e46ba1a2d3@linux.alibaba.com> (raw)
In-Reply-To: <sashiko-outbox-246055@kernel.org>



On 12/2/26 11:09 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [High] ocfs2: stranded OCFS2_DIO_ORPHANED_FL on failed or short
>   extending direct writes
> - [Medium] ocfs2: w_desc kvcalloc churn and O(N^2) scans in
>   ocfs2_dio_wr_map_blocks()
> --
> 
> commit 842dc9fe75b01af0050260d527cbd4edb7427f5b
> Author: Heming Zhao <heming.zhao@suse.com>
> 
> ocfs2: switch dio write path from buffer_head to iomap
> 
> This commit converts the ocfs2 direct I/O write path from the legacy
> buffer_head API to iomap, introducing multi-cluster block mapping in
> ocfs2_dio_wr_map_blocks() and moving orphan management into
> ocfs2_file_write_iter() and ocfs2_dio_end_io().
> 
>> diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
>> index 4b76be5909689..8d903c55749a9 100644
>> --- a/fs/ocfs2/aops.c
>> +++ b/fs/ocfs2/aops.c
> 
> [ ... ]
> 
>> @@ -2755,6 +2797,548 @@ static int ocfs2_write_end(const struct kiocb *iocb,
> 
> [ ... ]
> 
>> +/*
>> + * The most a single mapping handed out for a direct write spans.
>> + * It bounds the memory of the write context, one descriptor per
>> + * cluster.
>> + */
>> +#define OCFS2_DIO_MAX_MAP_BYTES	(16U << 20)
>> +/* A mapping always leaves room for at least one whole cluster. */
>> +static_assert(OCFS2_DIO_MAX_MAP_BYTES > OCFS2_MAX_CLUSTERSIZE);
> 
> [ ... ]
> 
>> +static int ocfs2_dio_wr_map_blocks(struct inode *inode,
>> +				   struct ocfs2_map_block *map,
>> +				   unsigned int flags, void *owner)
>> +{
> 
> [ ... ]
> 
>> +	/*
>> +	 * Only take what is of the same kind as what is at the start:
>> +	 * all of it is a hole, or all of it is unwritten, or all of
>> +	 * it is written, in one extent.
>> +	 */
>> +	probe = *map;
>> +	probe.flags = 0;
>> +	ret = ocfs2_map_blocks(inode, &probe, 0);
>> +	if (ret) {
>> +		mlog_errno(ret);
>> +		goto unlock;
>> +	}
>> +	map->len = min_t(u64, map->len, probe.len);
>> +	len = map->len << blkbits;
>> +
>> +	/*
>> +	 * Note, Zeroing the gap between i_size and the write is done
>> +	 * once per dio by ocfs2_dio_zero_eof() before the io is issued.
>> +	 */
>> +
>> +	/*
>> +	 * Remember what this attempt covers: if it fails after it has
>> +	 * zeroed the head/tail folios, the folios have to be cleaned up
>> +	 * behind the locks, see out below.  touched_end stays at pos when
>> +	 * nothing was zeroed, and is 0 when the write context was never
>> +	 * reached.
>> +	 */
>> +	ret = ocfs2_write_begin_nolock(inode->i_mapping, pos, len,
>> +				       OCFS2_WRITE_DIRECT, NULL,
>> +				       (void **)&wc, di_bh, NULL, owner,
>> +				       &touched_end);
>> +	if (ret) {
>> +		if (ret != -ENOSPC && ret != -EDQUOT)
>> +			mlog_errno(ret);
>> +		goto unlock;
>> +	}
>> +
>> +	/*
>> +	 * The write context says how many of the clusters are really there.
>> +	 * It is within what we asked for, what is mapped is up to the end of
>> +	 * the last of them.
>> +	 */
>> +	desc = &wc->w_desc[0];
> 
> [Severity: Medium]
> Does passing up to OCFS2_DIO_MAX_MAP_BYTES (16MB) to
> ocfs2_write_begin_nolock() cause vmalloc churn on wc->w_desc and quadratic
> descriptor scans when allocating into a hole on fragmented free space?
> 
> With 4KB clusters, a 16MB span has w_clen = 4096, so
> ocfs2_alloc_write_ctxt() allocates a 96KB w_desc array via kvcalloc()
> (falling back to vmalloc) while holding ocfs2_inode_lock() and
> ip_alloc_sem, even though ocfs2_dio_wr_map_blocks() already clamped
> map->len to a single uniform extent via ocfs2_map_blocks() and only reads
> wc->w_desc[0].  When filling a 16MB hole where ocfs2_dio_alloc_clusters()
> only obtains 1 contiguous cluster per call, iomap_iter() advances by 1
> cluster and calls ocfs2_dio_wr_map_blocks() again for the remaining hole,
> repeating the kvcalloc() and re-scanning all remaining clusters in
> ocfs2_populate_write_desc() and ocfs2_dio_trim_write_ctxt() up to 4,096
> times for a single 16MB write.
> 

A performance issue in case a fragmented 16MB, not a correctness bug.
I'd rather leave it un-resolved in this series.

> [ ... ]
> 
>> +void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end)
>> +{
>> +	struct ocfs2_inode_info *oi = OCFS2_I(inode);
>> +	struct buffer_head *di_bh = NULL;
>> +
>> +	if (READ_ONCE(oi->ip_dio_orphan_pid) != task_pid_nr(current))
>> +		return;
>> +
>> +	/*
>> +	 * @end is 0 when this write added no orphan entry; leave any entry a
>> +	 * previous write of this task left for the recovery.
>> +	 */
>> +	if (!end)
>> +		return;
>> +
>> +	if (ocfs2_inode_lock(inode, &di_bh, 1) < 0) {
>> +		WRITE_ONCE(oi->ip_dio_orphan_pid, 0);
>> +		return;
>> +	}
>> +
>> +	/*
>> +	 * Reaching @end means i_size covers every cluster this write
>> +	 * allocated, so the entry can go.  Otherwise keep it for the truncate
>> +	 * recovery in ocfs2_add_inode_to_orphan() and give up ownership so a
>> +	 * later write by this task cannot remove it without truncating.  A
>> +	 * physical cluster count cannot show this on a sparse file, so only
>> +	 * i_size reaching @end is trusted.
>> +	 */
>> +	if (i_size_read(inode) >= end)
>> +		ocfs2_dio_clear_orphan_locked(inode, di_bh);
>> +	else
>> +		WRITE_ONCE(oi->ip_dio_orphan_pid, 0);
>> +
>> +	ocfs2_inode_unlock(inode, 1);
>> +	brelse(di_bh);
>> +}
> 
> [ ... ]
> 
>> @@ -3304,10 +3635,156 @@ int ocfs2_iomap_next(const struct iomap_iter *iter, struct iomap *iomap,
> 
> [ ... ]
> 
>> +static int ocfs2_dio_end_io(struct kiocb *iocb, ssize_t size,
>> +			int error, unsigned int flags, int level)
>> +{
> 
> [ ... ]
> 
>> +	if (error) {
>> +		/*
>> +		 * Do not convert on error: dio->size still counts bios that
>> +		 * failed, and a short write may leave the cluster tail
>> +		 * unzeroed, so converting would expose stale disk blocks.
>> +		 * Leave the extents unwritten (reads return zeroes) and the
>> +		 * orphan entry for recovery, drop the zeroed folios, and
>> +		 * report the error, as ext4 and xfs do.  Give up orphan
>> +		 * ownership only if this write owns it: an async completion
>> +		 * runs in a worker and must not clobber the pid of a
>> +		 * concurrent extending write that added the entry.
>> +		 */
>> +		if (READ_ONCE(OCFS2_I(inode)->ip_dio_orphan_pid) ==
>> +		    task_pid_nr(current))
>> +			WRITE_ONCE(OCFS2_I(inode)->ip_dio_orphan_pid, 0);
>> +		ocfs2_dio_cleanup_pages(inode, &unwritten, false);
>> +		ocfs2_free_unwritten_list(inode, &unwritten);
>> +		ocfs2_rw_unlock(inode, level);
>> +		return error;
>> +	}
> 
> [Severity: High]
> Does leaving OCFS2_DIO_ORPHANED_FL and the dio orphan entry on disk when an
> extending direct write fails or completes a short buffered fallback prevent
> the inode from being deleted if it is later unlinked?
> 
> When a cluster-extending direct write adds the inode to the orphan dir in
> ocfs2_file_write_iter() and then fails (for example, an extending write at
> EOF with an unmapped user buffer that hits -EFAULT in
> bio_iov_iter_get_pages(), or an -ENOSPC in ocfs2_dio_wr_map_blocks()) or
> bounces on -ENOTBLK to a short buffered write where i_size_read(inode) <
> dio_end, both ocfs2_dio_end_io() and ocfs2_dio_clear_orphan() clear
> ip_dio_orphan_pid and leave OCFS2_DIO_ORPHANED_FL set on disk:
> 
> fs/ocfs2/inode.c:ocfs2_delete_inode() {
>     ...
> 	/* Skip inode deletion and wait for dio orphan entry recovered
> 	 * first */
> 	if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
> 		ocfs2_cleanup_delete_inode(inode, 0);
> 		goto bail_unlock_inode;
> 	}
>     ...
> }
> 
> If the file is subsequently unlinked without another extending direct write
> occurring first, ocfs2_delete_inode() refuses to wipe the inode while
> periodic background orphan scans (ORPHAN_NO_NEED_TRUNCATE) skip dio-
> entries, leaking the unlinked inode, its allocated clusters and its orphan
> entries on disk for the lifetime of the mount.  In addition, if the write
> failed before allocating new clusters or did a short buffered fallback on a
> file with fallocate(FALLOC_FL_KEEP_SIZE) extents past EOF, leaving
> OCFS2_DIO_ORPHANED_FL set causes the next extending direct write to
> truncate those preallocated extents in ocfs2_add_inode_to_orphan().  Also,
> if __iomap_dio_rw() returns -ENOTBLK and ocfs2_dio_upgrade_rw_lock() fails,
> rw_level becomes -1 before jumping to out:, skipping
> ocfs2_dio_clear_orphan() and leaving ip_dio_orphan_pid set to current->pid.
> 

This is a pre-existing one, and I'd rather leave it un-resolved in this series.

Thanks,
Joseph


  parent reply	other threads:[~2026-12-03  5:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 14:34 [RFC PATCH v15 00/10] migrate ocfs2 DIO " Joseph Qi
2026-12-02 14:34 ` [RFC PATCH v15 01/10] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature Joseph Qi
2026-12-02 14:34 ` [RFC PATCH v15 02/10] ocfs2: switch DIO read path from buffer_head to iomap Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 03/10] ocfs2: add ocfs2_get_clusters_uncached() Joseph Qi
2026-12-02 16:16   ` Christoph Hellwig
2026-12-02 14:35 ` [RFC PATCH v15 04/10] ocfs2: refactor ocfs2_lock_allocators() and add a dio variant Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 05/10] ocfs2: record the fsync transaction when CoW makes clusters writable Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 06/10] ocfs2: rework the direct write context for multi-cluster mappings Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap Joseph Qi
     [not found]   ` <sashiko-outbox-246055@kernel.org>
2026-12-03  5:43     ` Joseph Qi [this message]
2026-12-03  7:49   ` Heming Zhao
2026-12-03  8:55     ` Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 08/10] ocfs2: keep sub-page overwrites of written blocks on direct I/O Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 10/10] ocfs2: make buffered writes wait for in-flight direct writes Joseph Qi
2026-12-02 16:17 ` [RFC PATCH v15 00/10] migrate ocfs2 DIO from buffer_head to iomap Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b6b98ae9-ba00-4d25-8164-11e46ba1a2d3@linux.alibaba.com \
    --to=joseph.qi@linux.alibaba.com \
    --cc=akpm@linux-foundation.org \
    --cc=hch@lst.de \
    --cc=heming.zhao@suse.com \
    --cc=jlbec@evilplan.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark@fasheh.com \
    --cc=ocfs2-devel@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®