From nobody Thu Oct 2 05:05:14 2025 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A4952836A4 for ; Tue, 23 Sep 2025 04:22:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758601334; cv=none; b=oH4gUApUoVDVWZJpLwdkMIeZmFmOClgMDlhkA8j552BQ83h3ipiadx19qZa26Yk7yiHv86KBgt+29OuyGAEJxR8k4ISxbfjhnIvEhMz8CHR+iMD3yHCxjzT8DC6kP/HvpJqj93VFGkfbuwpfAjWaGEiHenSke7CFVSC5q0U5NZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758601334; c=relaxed/simple; bh=zpvEKZqLhwIOmRHUipSf1LyZBL80ITB37XK2LvCaT4g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C34RsPmbY6Lf7op4i90hN2zL1IN2qOrReppxbT/V8dbd+fisuBYueG+gVL8vzAzPSt9P04mTLsg8WwxCAn3vA0eXWhsP9WwFTAruz3VCmvvvw3JeiPKNjPti3EGh0upYSVNIOuxMD8v4eJs2eAoIIMO1MtFWWWpbAw3YAb8WQmw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=np7XVXeh; arc=none smtp.client-ip=209.85.216.53 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.b="np7XVXeh" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3322e6360bbso2332319a91.0 for ; Mon, 22 Sep 2025 21:22:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758601331; x=1759206131; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1nDNxXFU6/HO7z5iXuMR9cVnSyuTgICPbquGVbvl6vg=; b=np7XVXehliYilDBlAhJdCZXRHsSa4eV3yIB3vm1tEaexFoM4zthPU49+XCWe88jd9m HTOC29E2ZbtC0Ybpqi7lL+PwBjAb+V420zF2u/NAjHoPcWnNAs1qfk8LloMGVSs8yBpc xBJcmxeaX2kskV2tXhzhT76dUJUGC4Jchm5/1aq8wjojwXQFiwkAbwLBxxPxVcclNzdO OAggBCuGk5YgvHo2MZqdxc3Z+5fHADOnK17xZoiKR4QlPlHSLY0X6T5ZF8fQZgz4MwNz S/EDRVxKzyx9js7a3VywfYN3Y0zYZ/28Uvpy/2IVie7eJ4iFGfMQtgHTON9c45ceIzPq Hwfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758601331; x=1759206131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1nDNxXFU6/HO7z5iXuMR9cVnSyuTgICPbquGVbvl6vg=; b=OhtsCbgJZ2epvZH+EZ8EyXNcVDXlkWGj4EcP8ldSbHllP/KpC0Fl+ZqWjOJd/lrcvT 4pbJLDMbsYk4Gyuk23VOoCPz4QmuePwJxyLdTNGf1oo9bQAlYzx+sZwdjrRSyqqYjELG ysnXAF94/eUGwcwu+lYBJMQptSwktWwZnT9axcGpTpkYLKJJH1GE2+/QSCzGbcrmhhNf SiPhmy4U3/6/T9+RDATM/WarROq0QMWDmD8HFmTODJXp1JNrw9wzhsswdVnHX/El7O94 0mmFOO5F4/HduqTw/ZhwFcGuGAh5881M80SRqQc++03XwfwF5j30i9D0J8vbkm9ux/Hf kpSQ== X-Forwarded-Encrypted: i=1; AJvYcCXiGQfnPwN5LYzy0mR2r6tWPlmCA/frEaCVPbdbAQGvbz+QP2VcQ0pC5uyqPaJ1Rtt9Y5wLgSPnz6RyyxI=@vger.kernel.org X-Gm-Message-State: AOJu0Yzmrq9j4ODs0xPujcmcCeBXCccqjpYeXAloNqwD3LZ9OhEAXnjI WA+CImYwJak4fxzXQS/SEGAlpT7qaQWN8/oSlrSQdSOpN9Jl67Fk6m3v X-Gm-Gg: ASbGncuwrA1ErL7ce+oz0qKbX4Fc8ujax46gYTuknxdRq3KmAVaovWi6UVy5s6M2HfN pfYLeO+0Nz1dCagmr3CeOlXR7l0N+MdaBYFN9qXzeeRFEZX/LdjrdSVLssT9r1A/YP2gIpWcBhG VQYPsz/ktrqeweS0U2m/lEfP3DRn6g5lJE5pOF5D97OslhsRpIAs15FDO8nJ+Jy9vwJOBCHjbsg 2H4KZK+zKx+NwbNGcIbMIeVgR86UF9N1UUyxFLMqUpZYunwHLA5+rnsqnhocPYICrf8TCoq3Yro tmIkrRLkP2zz948JRqex+QfD6F9QhEVCIUaua3ksHPkcGPvN2ppAtoVC+Yb51VA4gSQFHMa3TbY FNQRGdYXvVbTJD/g2ECnCRqXErFyytBe9YA== X-Google-Smtp-Source: AGHT+IF7vHoIXKK7ahfVkrWmvrmfXhwv4d6xCB5n1yI9ZZQycPuvOWR5cPz7VJstzs3WJ12MlmIo+Q== X-Received: by 2002:a17:90b:4b82:b0:330:604a:1009 with SMTP id 98e67ed59e1d1-332a95c82d3mr1254994a91.23.1758601330689; Mon, 22 Sep 2025 21:22:10 -0700 (PDT) Received: from VM-16-24-fedora.. ([43.153.32.141]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-77d8c3adfd4sm13316513b3a.82.2025.09.22.21.22.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Sep 2025 21:22:10 -0700 (PDT) From: alexjlzheng@gmail.com X-Google-Original-From: alexjlzheng@tencent.com To: brauner@kernel.org, djwong@kernel.org, hch@infradead.org, kernel@pankajraghav.com Cc: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, Jinliang Zheng Subject: [PATCH v5 4/4] iomap: don't abandon the whole copy when we have iomap_folio_state Date: Tue, 23 Sep 2025 12:21:58 +0800 Message-ID: <20250923042158.1196568-5-alexjlzheng@tencent.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250923042158.1196568-1-alexjlzheng@tencent.com> References: <20250923042158.1196568-1-alexjlzheng@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jinliang Zheng Currently, if a partial write occurs in a buffer write, the entire write wi= ll be discarded. While this is an uncommon case, it's still a bit wasteful and we can do better. With iomap_folio_state, we can identify uptodate states at the block level, and a read_folio reading can correctly handle partially uptodate folios. Therefore, when a partial write occurs, accept the block-aligned partial write instead of rejecting the entire write. For example, suppose a folio is 2MB, blocksize is 4kB, and the copied bytes are 2MB-3kB. Without this patchset, we'd need to recopy from the beginning of the folio in the next iteration, which means 2MB-3kB of bytes is copy duplicately. |<-------------------- 2MB -------------------->| +-------+-------+-------+-------+-------+-------+ | block | ... | block | block | ... | block | folio +-------+-------+-------+-------+-------+-------+ |<-4kB->| |<--------------- copied 2MB-3kB --------->| first time copied |<-------- 1MB -------->| next time we need copy = (chunk /=3D 2) |<-------- 1MB -------->| next next time we need = copy. |<------ 2MB-3kB bytes duplicate copy ---->| With this patchset, we can accept 2MB-4kB of bytes, which is block-aligned. This means we only need to process the remaining 4kB in the next iteration, which means there's only 1kB we need to copy duplicately. |<-------------------- 2MB -------------------->| +-------+-------+-------+-------+-------+-------+ | block | ... | block | block | ... | block | folio +-------+-------+-------+-------+-------+-------+ |<-4kB->| |<--------------- copied 2MB-3kB --------->| first time copied |<-4kB->| next time we need copy |<>| only 1kB bytes duplicate copy Although partial writes are inherently a relatively unusual situation and do not account for a large proportion of performance testing, the optimization here still makes sense in large-scale data centers. Signed-off-by: Jinliang Zheng --- fs/iomap/buffered-io.c | 44 +++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 6e516c7d9f04..3304028ce64f 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -873,6 +873,25 @@ static int iomap_write_begin(struct iomap_iter *iter, return status; } =20 +static int iomap_trim_tail_partial(struct inode *inode, loff_t pos, + size_t copied, struct folio *folio) +{ + struct iomap_folio_state *ifs =3D folio->private; + unsigned block_size, last_blk, last_blk_bytes; + + if (!ifs || !copied) + return 0; + + block_size =3D 1 << inode->i_blkbits; + last_blk =3D offset_in_folio(folio, pos + copied - 1) >> inode->i_blkbits; + last_blk_bytes =3D (pos + copied) & (block_size - 1); + + if (!ifs_block_is_uptodate(ifs, last_blk)) + copied -=3D min(copied, last_blk_bytes); + + return copied; +} + static int __iomap_write_end(struct inode *inode, loff_t pos, size_t len, size_t copied, struct folio *folio) { @@ -881,17 +900,24 @@ static int __iomap_write_end(struct inode *inode, lof= f_t pos, size_t len, /* * The blocks that were entirely written will now be uptodate, so we * don't have to worry about a read_folio reading them and overwriting a - * partial write. However, if we've encountered a short write and only - * partially written into a block, it will not be marked uptodate, so a - * read_folio might come in and destroy our partial write. + * partial write. * - * Do the simplest thing and just treat any short write to a - * non-uptodate page as a zero-length write, and force the caller to - * redo the whole thing. + * However, if we've encountered a short write and only partially + * written into a block, we must discard the short-written _tail_ block + * and not mark it uptodate in the ifs, to ensure a read_folio reading + * can handle it correctly via iomap_adjust_read_range(). It's safe to + * keep the non-tail block writes because we know that for a non-tail + * block: + * - is either fully written, since copy_from_user() is sequential + * - or is a partially written head block that has already been read in + * and marked uptodate in the ifs by iomap_write_begin(). */ - if (unlikely(copied < len && !folio_test_uptodate(folio))) - return 0; - iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len); + if (unlikely(copied < len && !folio_test_uptodate(folio))) { + copied =3D iomap_trim_tail_partial(inode, pos, copied, folio); + if (!copied) + return 0; + } + iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), copied); iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied); filemap_dirty_folio(inode->i_mapping, folio); return copied; --=20 2.49.0