From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796217524; b=X5M6vemcdgKpZjv87vLYY0YMW05UkLvQ5pr2oue2jxCxLl7G6V6zBxfC9iYzPZZFFRZl Y4PXBA0OQ32bq83IO9nQF0Q0hNd7rtkrXLDb49gKrNkG0+NSOCgA9YWTBproLwweKukUG ahyyu4A8eOu4BW8obY5F+33nkiF4/6Ycku/82wxpXy9M0kvUFpsfkelNyoHoDUvfzsqEH QdOcbEPZMKZprIOe0v+iBdb6WJ8w1ScxECvcqyR1tlwVbRPDEH66JcivuJ4dML49l9nY7 dg0ziZgun6Es2QF3ojm1+lBu5nvcJfn2xI/Eipz/GyNSuzE5dWhW4llP1aIu60iRrDA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796217524; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=MLn7gRsXZE2H6nJAGHK1kstxwcNwSidZ2hgLWpCvqvs=; b=qYUosRtGaewWv5/qEgUtg1j5WrAy2LWyJBufGKrSLGlCgF2Lo/CGE2xMLJOSzGv5StRg HtGQ5JrpQqNDutLJ5TwWGzp8uVEMlcQuF5n43EUAxGr5y/ayhYd7s2Bv1jBns64ggeVIq cSI1RPTos5LnhYbDc/p9gsWhoTb8X7P+yRSYWndSUExAcFQr4ObTt8wFAgP4fGaSX7QIr glLVCnpENnWT0mWT8VpYMFK+nIHsT5oRYAwt5sPZmZmv6B5DRR/YLrLw3QKsNvmnIEuH/ tHdeIdeCZzmj9B/pkHmjn4zoUvIP3S2Iv+dSucIknWomgkgHeB5tQv1I/vpJMczUW3A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SsDI8ZT5; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C468606A684; Wed, 2 Dec 2026 13:18:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SsDI8ZT5 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796217523; b=AqTnEUJms6JVQ9SSpSnycqeEOSFKdQxgZ5DOx8V1MgMean06MQOD2BMmiUcTq1CkpmXp Lg6/FILNyOlq3nNr6DdwOlvPPCF6izmtn9fRjOLid5EU+Tox6SiarRXQSvXPfdTOjIKjn 0LKaWWmmcVVSq8lRZV6WaBBi8Hmvg6gqVvx/8hCo/Yq6RqC/0gDGdV0NSHNEo9O/A6kOG jkmAcKJA3M8tBanqpBtxkLFOjeh2iNErerFUTvd9dmOOsxTVFi6JklWouOE/Qbunxyssn BZzjiXhrI4H2snP9uztrgVJXLbn0NCqqLNa6cPcBUqsF+zBYZHE81VSXdg9waB876Bg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796217523; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=MLn7gRsXZE2H6nJAGHK1kstxwcNwSidZ2hgLWpCvqvs=; b=XClLwBKqIiviU0J7u1aCuRaBTNw8QSNMGRiQal3oPASHfvMl1257IUOvNGrjEk4Zj+IV sp1xteST9z53OeBC0zhJXGAdcPjWlr/a7cpeerM8N/UdPk1IspsmiKFvFL9kR5BPtQSja JnuQt8ZXZchsFBB9lTa2ot3EX3qGVZGC4l/drsWILOIYtx3bSPuFumQYMiCibutaQ/35K KKP6mC4r8RBi/OIPE4xH1lqeE4ZXkcWHpvV6sJRTHhEftuThq97F2rXMg+MDNc//uksVw 5z50/oFZLTu4oyUNaR/pMI5u77yFbGDWmWYvyFa1hHHw1P5SrZi5wJcaG/5VQIUFTow== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id A0CDD1F00897; Wed, 2 Dec 2026 13:18:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796217523; bh=MLn7gRsXZE2H6nJAGHK1kstxwcNwSidZ2hgLWpCvqvs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=SsDI8ZT5WJIo+C7jcJUJqcKjqrtRX7jpHgvfeEG/nFOB+g5Xh8KtpyQ6B8wYVleRq IF415BXkxeg1cUsaEGRpPg93Y184dEt6Vfg2QenM/UIVQyzYBADbqTMkOsnbEoSy2X P5+9OgjqL32Mr28xsHlgRmJUY0PVPk6If8GBVZG/piHzKK4G4dLIeu6NkBQY4pDpUk oOWILmHqtR7pbFDjGERsv02n8T5LZ8mMyEUu1/ibjeCL0HU9I0Ing2SC90q8a2eE3n APaZXgGkh0Ypasuy5q5VJqe5P4XXcyCrAfjAd1Oob+hEOd4+HPxLzFem8AKIZ7wJRO Ov8i7Bh1FzNgA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 312274F41F; Wed, 2 Dec 2026 13:18:32 +0000 (UTC) From: Xiubo Li via B4 Relay Date: Wed, 02 Dec 2026 14:18:21 +0100 Subject: [PATCH v3 2/3] ceph: send the cap messages of an inode in the order they were prepared 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 Content-Type: text/plain; charset="utf-8" Message-Id: <20261202-ceph-append-excl-v3-2-68fdbef33f32@clyso.com> References: <20261202-ceph-append-excl-v3-0-68fdbef33f32@clyso.com> In-Reply-To: <20261202-ceph-append-excl-v3-0-68fdbef33f32@clyso.com> To: Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Anthony D'Atri , Xiubo Li X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796217507; l=6052; i=xiubo.li@clyso.com; s=20260625; h=from:subject:message-id; bh=8YHVUnjj0Br3p7SPE+LaNOajN8xE5HB+wvVxwuph7Ts=; b=QrNz0gzPx9Etojfrl5ikfd1G7caGHMIA42ewIG7oFDxBB/laSuG+x/IuiQtqru6EgXDMdTlFB /hZ/VhvwD62BNgw5mRt1ns+PnH5fNpUnEsc3nykMGNgwPPW9hgja78D X-Developer-Key: i=xiubo.li@clyso.com; a=ed25519; pk=V3NGr0AgAopiUhaLY51ipBkLN5LlcLhjOEfLEq1RoZ8= X-Endpoint-Received: by B4 Relay for xiubo.li@clyso.com/20260625 with auth_id=840 X-Original-From: Xiubo Li Reply-To: xiubo.li@clyso.com Content-Transfer-Encoding: 7bit From: Xiubo Li __prep_cap() runs under i_ceph_lock: it drops the released caps from cap->implemented and moves the dirty caps to flushing. __send_cap() then allocates and sends the message after the lock was dropped, so two threads that check the caps of an inode at the same time, e.g. one putting its last cap refs and one handling a revoke, can send their messages in the opposite order. The later message no longer has the dirty caps, nor the caps that the earlier one gives up. If it reaches the MDS first, the MDS sees those caps released with nothing dirty, finishes its gather and may hand the caps to another client before the flush with our new size and mtime arrives. Seen with two clients appending to a file with O_DIRECT: the MDS completed the gather of Fw on an update with dirty=-, sent Fx with the old size to the other client, and the flush with the new size arrived 1 ms later; the other client wrote over our last record. Number the messages of an inode in __prep_cap() and let __send_cap() send them in that order. A message that flushes dirty caps must not get lost on an allocation failure either, or the next one would reach the MDS without them all the same: allocate it with __GFP_NOFAIL. Signed-off-by: Xiubo Li --- fs/ceph/caps.c | 40 +++++++++++++++++++++++++++++++++++++++- fs/ceph/inode.c | 3 +++ fs/ceph/super.h | 3 +++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c index 3f4ac4d94d5b..ac96d6b85612 100644 --- a/fs/ceph/caps.c +++ b/fs/ceph/caps.c @@ -1398,6 +1398,7 @@ struct cap_msg_args { umode_t mode; bool inline_data; bool wake; + u32 ticket; /* send order, see __send_cap() */ bool encrypted; u32 fscrypt_auth_len; u8 fscrypt_auth[sizeof(struct ceph_fscrypt_auth)]; // for context @@ -1575,6 +1576,7 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci, clear_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags); cap->issued &= retain; /* drop bits we don't want */ + arg->ticket = ci->i_cap_msg_next++; /* * Wake up any waiters on wanted -> needed transition. This is due to * the weird transition from buffered to sync IO... we need to flush @@ -1698,14 +1700,48 @@ static inline int cap_msg_size(struct cap_msg_args *arg) * * Caller should hold snap_rwsem (read), s_mutex. */ +/* + * __prep_cap() updates cap->implemented and moves the dirty caps to + * flushing under i_ceph_lock, but the message goes out after the lock + * was dropped. Another thread may prepare a later message for the same + * inode meanwhile, which then no longer has the dirty caps nor the caps + * we are giving up: if it went out first, the MDS would see those caps + * released with nothing dirty, finish its gather and hand them (with an + * out of date size, say) to another client before our flush arrived. + * Send the messages of an inode in the order they were prepared. + */ +static void __cap_msg_wait_turn(struct ceph_inode_info *ci, u32 ticket) +{ + wait_event(ci->i_cap_msg_wq, + READ_ONCE(ci->i_cap_msg_sent) == ticket); +} + +static void __cap_msg_done(struct ceph_inode_info *ci) +{ + spin_lock(&ci->i_ceph_lock); + ci->i_cap_msg_sent++; + spin_unlock(&ci->i_ceph_lock); + wake_up_all(&ci->i_cap_msg_wq); +} + static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) { struct ceph_msg *msg; struct inode *inode = &ci->netfs.inode; struct ceph_client *cl = ceph_inode_to_client(inode); + gfp_t gfp = GFP_NOFS; - msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), GFP_NOFS, + /* + * Do not drop a flush, e.g. of a new size: __prep_cap() moved the + * dirty caps to flushing, so the later messages of this inode no + * longer carry them and may release the caps. The MDS could then + * hand them to another client with the old size. + */ + if (arg->flush_tid) + gfp |= __GFP_NOFAIL; + msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), gfp, false); + __cap_msg_wait_turn(ci, arg->ticket); if (!msg) { pr_err_client(cl, "error allocating cap msg: ino (%llx.%llx)" @@ -1715,6 +1751,7 @@ static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) spin_lock(&ci->i_ceph_lock); __cap_delay_requeue(arg->session->s_mdsc, ci); spin_unlock(&ci->i_ceph_lock); + __cap_msg_done(ci); ceph_buffer_put(arg->old_xattr_buf); ceph_buffer_put(arg->xattr_buf); return; @@ -1722,6 +1759,7 @@ static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) encode_cap_msg(msg, arg); ceph_con_send(&arg->session->s_con, msg); + __cap_msg_done(ci); ceph_buffer_put(arg->old_xattr_buf); ceph_buffer_put(arg->xattr_buf); if (arg->wake) diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c index b615326f83bf..2e5d1256a3a5 100644 --- a/fs/ceph/inode.c +++ b/fs/ceph/inode.c @@ -675,6 +675,9 @@ struct inode *ceph_alloc_inode(struct super_block *sb) ci->i_prealloc_cap_flush = NULL; INIT_LIST_HEAD(&ci->i_cap_flush_list); init_waitqueue_head(&ci->i_cap_wq); + ci->i_cap_msg_next = 0; + ci->i_cap_msg_sent = 0; + init_waitqueue_head(&ci->i_cap_msg_wq); ci->i_hold_caps_max = 0; INIT_LIST_HEAD(&ci->i_cap_delay_list); INIT_LIST_HEAD(&ci->i_cap_snaps); diff --git a/fs/ceph/super.h b/fs/ceph/super.h index 0e6c8968b80d..b11083ad1da7 100644 --- a/fs/ceph/super.h +++ b/fs/ceph/super.h @@ -483,6 +483,9 @@ struct ceph_inode_info { struct ceph_cap_flush *i_prealloc_cap_flush; struct list_head i_cap_flush_list; wait_queue_head_t i_cap_wq; /* threads waiting on a capability */ + /* cap messages prepared and sent, see __send_cap() */ + u32 i_cap_msg_next, i_cap_msg_sent; + wait_queue_head_t i_cap_msg_wq; unsigned long i_hold_caps_max; /* jiffies */ struct list_head i_cap_delay_list; /* for delayed cap release to mds */ struct list_head i_cap_snaps; /* snapped state pending flush to mds */ -- 2.53.0