From nobody Fri Dec 19 13:25:50 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 2E3802E172D for ; Sun, 7 Dec 2025 12:22:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765110121; cv=none; b=jiSa5bLUzLGwjQ9Bna0CEf+F5VKoDltIbyGSfptyB/R6e9BEVypTx1B0XpoiEkjrhuRF8fQXr1DpK5XAzlLsJnmlO2r7F81mTqhx4pn5KAbtOWBaw/fHFo5SVeTbMsLG/fqdR/Ui6seFCVg+OXbnmeP4atzrc7RkULnb0ygjI3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765110121; c=relaxed/simple; bh=w8eGtVtlolLagnlYNt5x+XC8+sk1v+JVwx6mfbCwbOg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TSdC+6m0K+EkiMCO26NG5gkdv2drOS/9QJimEiMHZUu4HtOWsUu82F2abCBDjVf+dt2914sGtCB3e2WuN18xgEy0hxUhP2Y/hvK48Zcrgx/iKMdumR6+Gelm5uqFtjMxTtXxjtshVCunEi0yMTuINh6obqdqBbZGNUugbOsj6xw= 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=T20n3oaE; arc=none smtp.client-ip=209.85.214.178 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="T20n3oaE" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2953ad5517dso34515665ad.0 for ; Sun, 07 Dec 2025 04:22:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765110119; x=1765714919; 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=izKeaJNjYDUXJJUmuNDk2T6fLdmn69WrrecR/pMCWDY=; b=T20n3oaEQwzxXBknNtxQZvm1mNJm9vaJHB77antzABXhJJ0YhkOAOG7P429Hy9vUR5 ZLlqlOXFSRZBQkKYoryHdt8D2vbaNapTHpD/zD9zR8Ff8pPf7lE4iUCcJmRAS9lkLVoY wpCRjAfHXwdf7r5Aq/JkAlmxKb7xQIpdjyFWPZGQOBVI3MapzGQEwUBlgDSYTeWpb6t9 mL9JRfPMJbb9hh+i6FUA0Yj5Nv8oifhSxrYba3UW6xJRp1PCG6Uz/Gor70C2IqyKuC9C jUFiu7DaS2cylWolqkliroma1pEQ6g8Elv0SFLxqShmDDu0lcRv03eMxm12fQqpO0eU+ DchA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765110119; x=1765714919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=izKeaJNjYDUXJJUmuNDk2T6fLdmn69WrrecR/pMCWDY=; b=oE0X8Uk29chSvOL8liXz49qjoaDLLQuK8DbCINN1kBBEpxMlK9R07n8jxEEBCDO6w+ N+ZDNwg2GmRahI9O/RhEkx1VH4jGTU22FvtemchcMu+H/xOtyy5CbTl59svrIRawl369 TWOUDLxMV/FbXrxtc/WT2QOrZWW0ThJ5EXv4IyMuTWyA9KWIinR6d2LO91AmQXkTWvbM J/Uk7xe1lJCIYCvNXu4/06AoOZ+JC0gwp4grc6fYoKKS2RgO/6W3ebGWDn+eC2vKmsAV 7vjThEDgYOg9OXDuQCyG3wCmSiQbPA6oTiTAoHXm+5INFxJMc0Fphcb9bjvUyJcR+zTp s2Ag== X-Forwarded-Encrypted: i=1; AJvYcCVnn2TGZiAcXjiwQUssVpIC+XlpU+3WZr57z1/xMdvt0bt5DREYEZIX64C6ub7Oa089DDcxOmlbKv4URSU=@vger.kernel.org X-Gm-Message-State: AOJu0YxYL/0cd9A9ejyIk6BVLYTPrKeYxBqfRk+Xonutq43/qIb6pPxU t7bCL618puVsKLOb9X7n19NFqsfGhUKpJRG2c/lkC6vTbvirYbVVl2fZ X-Gm-Gg: ASbGnct1UPcUk2ShazFjT7jRqp4+3WSXhAGOrI2hjGWWWVObNtmfsV26ekVCVa7QbLQ 9eMONr8plsxxCe634horl3WnPWgZrgfAEUop1eugW/OHnodzzBY8XE0C2OElle/e5j+oIMqZ/wN h7QBvuerhQnji4Wf9+GnnQtYZY6miogyq7ynrltxNBniklytkFto4IzUHQ3L8pAUHwdS7JPFqql lwURtplFWKL9USjj3eVqkrn4i1PRdjUT/1EucCZBrlvzQKEg6gj3sfUh0qSnYLjtvBQkx/KmX5U zbxE58l52InR7i7AMzbqqcQATEtBCMh4cyrvns3T2v9RfspIJ6kfWMjqvhoqO1JiKauuBglexpB asEj38lCnjfQigfolZo55HvjJUVWZpmr+cuffPLkBz9XPpNcL3V/VJZ/++wLkJu+HpnkEg4Gh/3 nxmEyUwMB9ZWrYtic+0Ct1CSa7qA== X-Google-Smtp-Source: AGHT+IECvKJIBxL4NZCJGy3oY+S1k6hGdcU74QcxSoe7E6jhp3VaKBVwnJ2amzDXJrC/dyJKXDSsxg== X-Received: by 2002:a05:7022:f89:b0:119:e55a:9c04 with SMTP id a92af1059eb24-11e032ac37dmr3497724c88.32.1765110119410; Sun, 07 Dec 2025 04:21:59 -0800 (PST) Received: from localhost.localdomain ([104.128.72.41]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-11df76e2f3csm38598822c88.5.2025.12.07.04.21.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Dec 2025 04:21:59 -0800 (PST) From: zhangshida X-Google-Original-From: zhangshida To: Johannes.Thumshirn@wdc.com, hch@infradead.org, agruenba@redhat.com, ming.lei@redhat.com, hsiangkao@linux.alibaba.com, csander@purestorage.com, colyli@fnnas.com Cc: linux-block@vger.kernel.org, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, zhangshida@kylinos.cn, starzhangzsd@gmail.com, Christoph Hellwig Subject: [PATCH v6 3/3] block: prevent race condition on bi_status in __bio_chain_endio Date: Sun, 7 Dec 2025 20:21:26 +0800 Message-Id: <20251207122126.3518192-4-zhangshida@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251207122126.3518192-1-zhangshida@kylinos.cn> References: <20251207122126.3518192-1-zhangshida@kylinos.cn> 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: Shida Zhang Andreas point out that multiple completions can race setting bi_status. If __bio_chain_endio() is called concurrently from multiple threads accessing the same parent bio, it should use WRITE_ONCE()/READ_ONCE() to access parent->bi_status and avoid data races. On x86 and ARM, these macros compile to the same instruction as a normal write, but they may be required on other architectures to prevent tearing, and to ensure the compiler does not add or remove memory accesses under the assumption that the values are not accessed concurrently. Adopting a cmpxchg approach, as used in other code paths, resolves all these issues, as suggested by Christoph. Suggested-by: Andreas Gruenbacher Suggested-by: Christoph Hellwig Suggested-by: Caleb Sander Mateos Reviewed-by: Christoph Hellwig Signed-off-by: Shida Zhang --- block/bio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/block/bio.c b/block/bio.c index d236ca35271..8b4b6b4e210 100644 --- a/block/bio.c +++ b/block/bio.c @@ -314,8 +314,9 @@ static struct bio *__bio_chain_endio(struct bio *bio) { struct bio *parent =3D bio->bi_private; =20 - if (bio->bi_status && !parent->bi_status) - parent->bi_status =3D bio->bi_status; + if (bio->bi_status) + cmpxchg(&parent->bi_status, 0, bio->bi_status); + bio_put(bio); return parent; } --=20 2.34.1