From nobody Fri Dec 19 21:10:53 2025 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 60B962FD680 for ; Thu, 4 Dec 2025 02:48:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816514; cv=none; b=aB35vzH4lpECotwe5RcauFbn9vKR273aSAwhnX0aP+FVdhsDp4kMoN3pF9GzGh0ULiFNjMRaGE3s+L600ZyQmrDhdZEUKnTI+0dJuJNFIuel8OIJXz34c703h90XIXRe+PapiH5sW5JE0bQ/IKt7ktttjMZOEyzG3goS21QuAUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816514; c=relaxed/simple; bh=GWGqYe6NZTrI9deOI6ejOoQBWm5NR0OgoP9ZCzjXKGw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kCjtS2MqcvyF1pSGPSQHSSpelZDQGZyQTjwsNxPV17+1TD2wDMkDWs8Nyhyy9S1ZM+lRTiSOcmd2HvNvNSxXAEI0qYYo/69McWfuVfHujW/HojzMt7Q4OT8ktVPXeqLJlYQaTHD+NGk0B1bfXiwJQENNbie/2lF2LDFgzK9kpsk= 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=m1B06UWI; arc=none smtp.client-ip=209.85.214.172 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="m1B06UWI" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2956d816c10so5374305ad.1 for ; Wed, 03 Dec 2025 18:48:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764816507; x=1765421307; 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=ODLAjaeZWmKKDwhHXqzdB6DoouDdnHDHsOcQ2a6RqDY=; b=m1B06UWIUHYuAmSdR3uhDTaDpbqxedM8WF6af2dRS9/R62CHeg01oFYuFoxvDgsuq7 voBeipjgYzcV40KTCF2Qh7Syy29iTSkZHzs1SdGFmGMsusQ/IFqOXEoWLcPrgUWm4T9i A+92ZsFyniEPHedFbSJAR7OpqocbbqByi77fO1hVH9ey5zhVNLd+e9olloTkCkl8gb+9 BmBSlKtjmCQCXJQn3Xw5lBFcFdi9jNzJWJH3N3WnHgjoDI4KBfmXfcaN1nxVMvESiRr0 oGh6xuVih+AXtKIJIf47k4v7UHJqksZT33MwYKA6V2bcRRxdJ9L2Rhxd8yb8p7+IVVfx Kd+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764816507; x=1765421307; 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=ODLAjaeZWmKKDwhHXqzdB6DoouDdnHDHsOcQ2a6RqDY=; b=oQ+XGe/tOgTbQ5YSz3hCdhPM2uMXOs4t7DlGXpsQIbk8LzBnG+4bkjckQsgWuf7wZ+ ZGLQi88NKDX6YpcugyrDycKkjAKohs7RawIAZBD6Uxju7a5XP55AbfLVa0iSKXOL8gU7 6F6HGSExEc5m4c+1wmRsOEnrUYNx5ZYJB0La/pyJVa8vahpT0kxISSy4dxjQBE50gS9W ZZgJq+FxAKAglsdQnsk+f57ec1Pd8Gj+U0Q/w30rBjrvtU7KvUb+/Wvnp8orx2AJZFl/ Uf/BwrHMH3iqLYEs54rm3IiaXxjIvRJCCmoWeOyLgizg63Sz9ZULmoVeO/yTlMzhy713 qWbw== X-Forwarded-Encrypted: i=1; AJvYcCWH9nFsgv/ZYo8T1tzi6DZNnlCdbOjxHBIIY927iwM5QnL2NaX5A6s0S+1FcnOqYecE6rsQqZvAazKS61M=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7t6mPy1YbuMNXt+a+N81ThSkWOVbYssJQ+/KMiQngh90rQNgb XlaQUaeY4r/PA5NB5uQ4n19FSatQIlHhdqeFRuz1CC11o6liqe4cDCes X-Gm-Gg: ASbGncuXrL+D2Kl6EN+1xP5euBzcZt8LwJaaxdldWJb0R20Q2gwNq5Ea7Ucos0cbDA0 4XwrdaKzFS96uguExbuKd0wl3BOZ+n2AJ5dje/ib6mUFVwiAqfZycgTp+SNfsFHbv9U3DBnt+tr Hd2uQCHwE5biE3TPtWDh9tHG7E6ABluJrR7OzXhRbrD4xZtXlxU2UhrBQXeF+Kiv854uzPWFQaK 4Vzic81Z4oZHixJzyvdaTSRW3QABneDPJO0SUM2yUZuVdYsHmxq+G/VOqR4gGM6hK+yik7zvgYl U3l0cSQc9LObvOIc5zPBXO9RaIoAjppsLMegciut+h4bVXWdtYghrl2RaQQbzeSz8Uazk2iVDHN meGJ7Hh8yLgW5acM4ymIAEjtgPiK/zSbhgGfVjZ+uovuh8VHj5XZ/HG4qNODk4AvMj7i4HTKVGT woiWpEe8hxQJHsAr9lN6Z+qargcA== X-Google-Smtp-Source: AGHT+IGM6SlIysHbulWdQG8xyoxU4IAcs1YhWt2VJRjAfRnb3Xvrgep+D+UgkBMDQdlNnkJeXkWtsQ== X-Received: by 2002:a05:7022:63aa:b0:11d:f464:38b3 with SMTP id a92af1059eb24-11df4643926mr2045006c88.2.1764816506518; Wed, 03 Dec 2025 18:48:26 -0800 (PST) Received: from localhost.localdomain ([104.128.72.41]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-11df76e2f3csm1838847c88.5.2025.12.03.18.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Dec 2025 18:48:26 -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 v5 3/3] block: prevent race condition on bi_status in __bio_chain_endio Date: Thu, 4 Dec 2025 10:47:48 +0800 Message-Id: <20251204024748.3052502-4-zhangshida@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20251204024748.3052502-1-zhangshida@kylinos.cn> References: <20251204024748.3052502-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 cfb751dfcf5..51b57f9d8bd 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