From nobody Sat Jun 13 19:04:56 2026 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 AEB2BC2FF for ; Wed, 6 May 2026 01:05:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778029546; cv=none; b=SvF3Kzr6muoGOd+Pqhg4tAklIldCUlir9zi5Y9ar2Rr1OJiFYM9UJYxs24gbnYxSUciP4sQV1wBbgxaL1P43S6PMmjFtN1U7WgbIM6RTMufixcJ/Jb78uevlffvakNybjvIGsPhs2uwCyo1Q7SeRznE5Igdk4M5I1fZ7jIOsDtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778029546; c=relaxed/simple; bh=es4nNkQvw9RkbvnfhA8gDq/y3/eD9NZZ6kUxrsHKmMo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=CZsUrJp/Va83fwHRq1gy0DARs6FiTnkhTe3R0knaV5+EsZ7nW2UqM3WTy0i4pRNTh5h6vctxbcXUquazCcni0HRIztYenoRDQ91EjBxE9xWGmdU9El5X9oRu2BHLJUd8aP5l8GQj636DD8ncXQyKrHm0+RsDkkTcPMdfDt8KjMU= 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=UHL3kwLK; arc=none smtp.client-ip=209.85.214.171 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="UHL3kwLK" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2aae146b604so38375735ad.3 for ; Tue, 05 May 2026 18:05:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778029545; x=1778634345; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=bYn9EWYy6d6qNj1bK/TNzgSNgeUONhEknxsFBug8LyU=; b=UHL3kwLKHbHKJZ5eRNEsGLcMlZ55mnBwnRwb4XumC/LbkrpoYUu4vGKwc/zeTuehSx z6Om51KZQY4rCFz8y511etMe2859Na0C+AmeBZ6ye2mi4uEYoMLx0KuK1DtWRtonBIF1 Xws1qzYmTSGSWEibQkeI+veWQxYM3no0ECJnvaZ/frv7iJlnLgXKL0nLit37Uf9JKBvI I3UPy5+Uo2pwl6zo5XoKUUuYB42UNs9PSQdDf6nTpobvwkw+vhjh94wPA/WixyS3IQZP 8UmMUJhPupc0/TegM6z0mVQD788O0AU/0SNklvMsOYOab/dn3wnjZX3Fo90CR1o6jGMI 7wRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778029545; x=1778634345; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=bYn9EWYy6d6qNj1bK/TNzgSNgeUONhEknxsFBug8LyU=; b=i7OP+xoizFTPpmvazF9SiAzzHWsKWLm77bxb2Ft2AKQKBaQYLPhxvUAgvLAad44ecJ EBbPf/VCvXsp8ZPWVXseZOQTZDrULFgebNSQ91Ub1C+F1C6vGP0/XR1mpk6INqd2utnw VZJPBgHFjVQO15lFG/82ngGoqAij2klNspURGFpaiZMm6UBu3ZgRf5lQ4OrLv70rMZ03 45LIT0h7VO00EMBOgbHZ13eyimNI2leFbggqsqLtyLj9YjzdUJkGLDkd5ihROfdo4fn9 75FW1AhpyG4pg7AqSw89Z+29CjZ6UYyaa6SXc/+8fKQviEsGa0VNf87i+9p7o/A6FwmK /pJg== X-Forwarded-Encrypted: i=1; AFNElJ/SEliS+USPj+IHIGMG9UBvpC3Vfp7ZPsmtfW2siaWJvkQC198jSAINrElWyJBLz+PSftkO5AqNfbXWrbI=@vger.kernel.org X-Gm-Message-State: AOJu0YzeLTAN2Nnt0bIOLFPH+LZIH/pPbCcdIE/JT1bqQqx9gyj546qb eabq7Aq/qi9xgWh0W/rHUyffD9EKCAgQ16+V2BA+oe7uA7Wv9Pju3N7N X-Gm-Gg: AeBDietqVh6+A06td9xjMUV1/ywOws45lopWt9LVI+SupyKavv8DyoWv9lTPpnxkQ7y 23w9RSchifBghlZo1Havz20vYI1m7Cnz0WoXC9U88iJ1RUj40/99ZPs+h+j8hMfoxHzjHYvNPIm GRyar6xUHrsIRDZNVvkHAWYd7P6EhJLLs/KLqw41X5kl4IECY48ZSVltpILik8uCN7KIADd+euM qG/7Hl3GU1zDZcytzukD2R7yrsdcXFB4R7KUpM+ug4KXtt4ZxPasL8IClX/Kt5tzDtRKxFM/uM3 XVub4Z5EqnqHjY8R62e7Vl/LTA2ahDmRqERRAF2SBhCUZN93o1vaSzzYk/YWZqayQQ64M5LYnIs a6dM1oiuBi78xYWbSLoatbXIoB+neuz8U4pxHZ6czc/eEFQHfWTurgiPMeo5TbqeiMaiuDDpC6w 8ByKnS73bBH7C9cpSC+Fe5rcBr/ZSPOQXvXAbqKuE= X-Received: by 2002:a17:903:1a23:b0:2b7:aba0:ac10 with SMTP id d9443c01a7336-2ba78f52dfdmr12284915ad.11.1778029545085; Tue, 05 May 2026 18:05:45 -0700 (PDT) Received: from localhost ([111.228.63.84]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ba7ca106e8sm5215295ad.62.2026.05.05.18.05.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2026 18:05:44 -0700 (PDT) From: Cen Zhang To: cem@kernel.org Cc: linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com, Cen Zhang Subject: [PATCH] xfs: order log tail accounting before grant head returns Date: Wed, 6 May 2026 09:05:33 +0800 Message-Id: <20260506010533.3282845-1-zzzccc427@gmail.com> X-Mailer: git-send-email 2.34.1 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" The log reservation fast path computes available space from l_tail_space and a grant head without taking the AIL lock. CIL completion moves a committed checkpoint's consumed bytes from grant-head accounting to l_tail_space by advancing the AIL head/tail accounting before subtracting the same amount from both grant heads. That transfer must not be observed as a smaller grant head with the old l_tail_space value. Such a mixed snapshot makes the free-space calculation spuriously larger by the transferred amount and can allow a reservation that should have waited. The existing smp_wmb()/smp_rmb() comments describe that intent, but the reader samples l_tail_space after the read barrier and before the grant head. In addition, the writer orders a normal store against unordered atomic64_sub() operations, for which the atomic documentation specifies smp_mb__before_atomic(). Put the write-side barrier immediately before the grant-head atomic subtractions. Sample the grant head before l_tail_space on the read side. This makes observing returned grant space imply visibility of the matching tail-space accounting update. Signed-off-by: Cen Zhang --- fs/xfs/xfs_log.c | 21 +++++++++++++-------- fs/xfs/xfs_log_cil.c | 1 - 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/fs/xfs/xfs_log.c b/fs/xfs/xfs_log.c index f807f8f4f7058..de56c707027d9 100644 --- a/fs/xfs/xfs_log.c +++ b/fs/xfs/xfs_log.c @@ -113,17 +113,21 @@ xlog_grant_return_space( { int64_t diff =3D xlog_lsn_sub(log, new_head, old_head); =20 + /* + * Publish the tail-space update before dropping the grant heads so + * readers cannot observe returned grant space without the matching + * tail-space accounting. + */ + smp_mb__before_atomic(); xlog_grant_sub_space(&log->l_reserve_head, diff); xlog_grant_sub_space(&log->l_write_head, diff); } =20 /* * Return the space in the log between the tail and the head. In the case= where - * we have overrun available reservation space, return 0. The memory barri= er - * pairs with the smp_wmb() in xlog_cil_ail_insert() to ensure that grant = head - * vs tail space updates are seen in the correct order and hence avoid - * transients as space is transferred from the grant heads to the AIL on c= ommit - * completion. + * we have overrun available reservation space, return 0. Sample the grant + * head before l_tail_space so that seeing returned grant space implies se= eing + * the matching tail-space update from xlog_grant_return_space(). */ static uint64_t xlog_grant_space_left( @@ -131,10 +135,11 @@ xlog_grant_space_left( struct xlog_grant_head *head) { int64_t free_bytes; + s64 grant; =20 - smp_rmb(); /* paired with smp_wmb in xlog_cil_ail_insert() */ - free_bytes =3D log->l_logsize - READ_ONCE(log->l_tail_space) - - atomic64_read(&head->grant); + grant =3D atomic64_read(&head->grant); + smp_rmb(); /* paired with smp_mb__before_atomic in xlog_grant_return_spac= e */ + free_bytes =3D log->l_logsize - READ_ONCE(log->l_tail_space) - grant; if (free_bytes > 0) return free_bytes; return 0; diff --git a/fs/xfs/xfs_log_cil.c b/fs/xfs/xfs_log_cil.c index edc368938f305..ea0efabdc9990 100644 --- a/fs/xfs/xfs_log_cil.c +++ b/fs/xfs/xfs_log_cil.c @@ -876,7 +876,6 @@ xlog_cil_ail_insert( * available on return, only for it to disappear again immediately as * the AIL head update accounts in the log tail space. */ - smp_wmb(); /* paired with smp_rmb in xlog_grant_space_left */ xlog_grant_return_space(ailp->ail_log, old_head, ailp->ail_head_lsn); =20 /* unpin all the log items */