From patchwork Thu Oct 3 12:53:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeongjun Park X-Patchwork-Id: 1992324 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hxGpOvbU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=ozlabs.org (client-ip=150.107.74.76; helo=mail.ozlabs.org; envelope-from=srs0=kc03=q7=vger.kernel.org=linux-ext4+bounces-4469-patchwork-incoming=ozlabs.org@ozlabs.org; receiver=patchwork.ozlabs.org) Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XKBVB2LXVz1xt2 for ; Thu, 3 Oct 2024 22:55:13 +1000 (AEST) Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4XKBV40Ygwz4x3r for ; Thu, 3 Oct 2024 22:55:08 +1000 (AEST) Received: by gandalf.ozlabs.org (Postfix) id 4XKBV40NH7z4x6k; Thu, 3 Oct 2024 22:55:08 +1000 (AEST) Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: gandalf.ozlabs.org; arc=pass smtp.remote-ip=139.178.88.99 arc.chain=subspace.kernel.org ARC-Seal: i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1727960108; cv=pass; b=CLQ7J1sCLdjy5EHZ2SvcztM82SaS3PB6Y5A3HVL/oL30o7qGeaSgkOMAuJR2+U1ss3j8K9yM0nilNZqk7iBy6nPlv9chSpX4yKVJChsAGildxzAGLC/NW1eKAPe/cElDAKcgPH9Czf2keRkzynyolxDhPMEieV+5fvt1l/gKyDYMi4AfEbiY6j6Wldq3FK3/AJGyeXPXslHqlNDksLKUiir+kO3c4WTHasGlCzUivLUE3YJK96jVZ+bk35slugIgfBY/3m5ugejtXVjzNxMAxRDo+7kAIwHKnAWOU/Z7WSdu0ywTv3QePcpIuFSY/Gr5F0u/fL464y8+3R8OJVe91Q== ARC-Message-Signature: i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1727960108; c=relaxed/relaxed; bh=LsC4l5sUrgtE5C3AhIFngE9xrlbDasfd0CJezQP/zPM=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=xZ1D0p9b1BG/i91pU2Yimg5ZogVSulnqwkPrL4oFSiXwXG8k/8CNSjBgQPMyZ8rFpsa6T71ppmzBx9xoDHM3k3kQAd2WNakeMQZj9zl1d13IHHHCC3jMeyWpKG13crhoTxCKMn5db0wCIZq1LhErocGPDEW297tQsuLz4ehKTtzudGSuGyGa9GCGXlIkXOwhOv1CEyvA8l5PFIicJX+s/WO98BrrW9vi8ICYPvHn8/Yx/e9Q7YwuIl5N495CCZ9cxzXoT2wtlFXd/1EOePXVQIZASSbHadKSl2t/9Zek7ozqA3UBQNK4s+roiXWwxW2TJJcwx9qPWWjzyMGv3iENPw== ARC-Authentication-Results: i=2; gandalf.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hxGpOvbU; dkim-atps=neutral; spf=pass (client-ip=139.178.88.99; helo=sv.mirrors.kernel.org; envelope-from=linux-ext4+bounces-4469-patchwork-incoming=ozlabs.org@vger.kernel.org; receiver=ozlabs.org) smtp.mailfrom=vger.kernel.org Authentication-Results: gandalf.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: gandalf.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hxGpOvbU; dkim-atps=neutral Authentication-Results: gandalf.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=139.178.88.99; helo=sv.mirrors.kernel.org; envelope-from=linux-ext4+bounces-4469-patchwork-incoming=ozlabs.org@vger.kernel.org; receiver=ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [139.178.88.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by gandalf.ozlabs.org (Postfix) with ESMTPS id 4XKBV359ZRz4x3r for ; Thu, 3 Oct 2024 22:55:07 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id F157528352F for ; Thu, 3 Oct 2024 12:55:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 12EA11991C2; Thu, 3 Oct 2024 12:54:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hxGpOvbU" X-Original-To: linux-ext4@vger.kernel.org Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 6825F15539D; Thu, 3 Oct 2024 12:54:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727960095; cv=none; b=mhWnlkccL+mcVZ22PDe3KVIN4MGivowdy3SCKIDq9iPSGZGQx1x3JIHqTF7WsodSmIsyWGNB6V9espboliqZE8ZOqYBdmdt8YO7BdjDyfagk2lm5JHV6Edc2XVwnN7uasrpzEPo1Srj7Ogib8mBzqO69agmqzpYErVGz3TUn6dQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727960095; c=relaxed/simple; bh=KljAQHtSfrZelRNu/DeQx5LVY5cSuSW2WcANOdnrzUc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=TCtKyi9Qfsy/Ig5seDd8pWEtROtLbCWZydj9Hz2E9tDDNyyQ0k7WChyZ7WSJeluX4cfstjHbBo0U3JCn2g5LXBdmA94awIAMjcaxeolZw7tJ8EqYMPiDf6fnkwsKrtvz5opsidDPUgCFrJSsbjkVhz5aFv5DTTxumbiHrESWzeM= 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=hxGpOvbU; arc=none smtp.client-ip=209.85.214.170 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 Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-20b0b5cdb57so14280245ad.1; Thu, 03 Oct 2024 05:54:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727960094; x=1728564894; 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=LsC4l5sUrgtE5C3AhIFngE9xrlbDasfd0CJezQP/zPM=; b=hxGpOvbUQf/tayGvBmdEgNENJXZMfND0qFUTb9WtKeIULuvfy9EzB/Bn+PCHeT6BIW AujUhzskKNHJBVHpRXq5dDDiUOCCL8O/tHyxwBLeh+3moqOoVghUik2jiTaF2YPTSaRw 23AFP0WkJAlADVM18co0v5sGJVdhoAq2Phbo2OKruE53h3ilCg+oQTE7L1BiKu+7gaqN uHWCe/z0Sw73x6OP+I3FTGWgCQgvUkT+6bKxbyryDr0KiWLma7jaLO4h8R30zSDCPyjN c8ygi2i1hOadgHey6qVXvgRn77q7dXIPgErQ0ODRYYMjmFRO72e6fRyBaoPnedoXE9Eq 66VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727960094; x=1728564894; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LsC4l5sUrgtE5C3AhIFngE9xrlbDasfd0CJezQP/zPM=; b=NdoSmOUSELL/F5FAIOIQ7rohNdEivx+fqeypkSZsWRNR1N3hQ3w10yonRZIwFa9La3 gkAp305+sMWe5omZBshBD0LTAvvJ/NkvBUmw5J/Tu/lJTSrmzbUztiV2Vb9tZ6KqjE2E wMk3zgOl2E2/Q3ArX8D/lC226aJY4L+3ofcL0TKWar/7BMC1mBBJeqfNdT3fEyNYFXE7 ODTj21S8BZHO4ejLC11O7FhATDXUlgYPmHn6kajmb6zzh1QTTpy01ci6xgwKh/pEmKgs 9k8BXpayTpT4sL8fbHetd2CeYV7O9Cm/p0uPylYeUq8c6YUmWzVNJRyM0/Kuq8jn+8ne QLtA== X-Forwarded-Encrypted: i=1; AJvYcCUQCyODv/1x/yx4V2zRTZs+Uv6umultSR5KToecaEYiCo5eySQEQFd34XKeH1Qj5uhaY2DmMceQhLhJZuZ6@vger.kernel.org, AJvYcCVlxyHAewJ6XiuvgFNtOWSu7o+jleaYjbRUltFPpzC+zfczfkbj8ik4gvKrZfmnP4rGwWqio4304uHG@vger.kernel.org, AJvYcCXD0o8uGnHd8iZlLkbEhWLWxmqN5sw1XOlxXVftuE7myxy62e6HUzKCh0debk1jPlVrPDJ3sBbY@vger.kernel.org X-Gm-Message-State: AOJu0YyWBNhuFXXU4fxTCY3nWJDkPRL4o3tQ/oa0mcLOUJ1KWexwtj2z aHIbrfMj8bdX5NedOPyawHGGDytJC+VDqVDNu4EoOt+IV41aJC// X-Google-Smtp-Source: AGHT+IEVVMmS1YZ2exZ1znFQnn9/34PbEJhr5acgKukRACDsDk/3Nx413ukvQ2Vv4QCXPWtKG24YCA== X-Received: by 2002:a17:903:41ce:b0:206:9c9b:61bb with SMTP id d9443c01a7336-20be187334amr45591905ad.6.1727960093539; Thu, 03 Oct 2024 05:54:53 -0700 (PDT) Received: from localhost.localdomain ([121.185.186.233]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beefad240sm8324875ad.206.2024.10.03.05.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Oct 2024 05:54:53 -0700 (PDT) From: Jeongjun Park To: tytso@mit.edu, adilger.kernel@dilger.ca Cc: akpm@osdl.org, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Jeongjun Park Subject: [PATCH v3] ext4: prevent data-race that occur when read/write ext4_group_desc structure members Date: Thu, 3 Oct 2024 21:53:37 +0900 Message-Id: <20241003125337.47283-1-aha310510@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, data-race like [1] occur in fs/ext4/ialloc.c find_group_other() and find_group_orlov() read *_lo, *_hi with ext4_free_inodes_count without additional locking. This can cause data-race, but since the lock is held for most writes and free inodes value is generally not a problem even if it is incorrect, it is more appropriate to use READ_ONCE()/WRITE_ONCE() than to add locking. [1] ================================================================== BUG: KCSAN: data-race in ext4_free_inodes_count / ext4_free_inodes_set write to 0xffff88810404300e of 2 bytes by task 6254 on cpu 1: ext4_free_inodes_set+0x1f/0x80 fs/ext4/super.c:405 __ext4_new_inode+0x15ca/0x2200 fs/ext4/ialloc.c:1216 ext4_symlink+0x242/0x5a0 fs/ext4/namei.c:3391 vfs_symlink+0xca/0x1d0 fs/namei.c:4615 do_symlinkat+0xe3/0x340 fs/namei.c:4641 __do_sys_symlinkat fs/namei.c:4657 [inline] __se_sys_symlinkat fs/namei.c:4654 [inline] __x64_sys_symlinkat+0x5e/0x70 fs/namei.c:4654 x64_sys_call+0x1dda/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:267 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x54/0x120 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x76/0x7e read to 0xffff88810404300e of 2 bytes by task 6257 on cpu 0: ext4_free_inodes_count+0x1c/0x80 fs/ext4/super.c:349 find_group_other fs/ext4/ialloc.c:594 [inline] __ext4_new_inode+0x6ec/0x2200 fs/ext4/ialloc.c:1017 ext4_symlink+0x242/0x5a0 fs/ext4/namei.c:3391 vfs_symlink+0xca/0x1d0 fs/namei.c:4615 do_symlinkat+0xe3/0x340 fs/namei.c:4641 __do_sys_symlinkat fs/namei.c:4657 [inline] __se_sys_symlinkat fs/namei.c:4654 [inline] __x64_sys_symlinkat+0x5e/0x70 fs/namei.c:4654 x64_sys_call+0x1dda/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:267 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x54/0x120 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x76/0x7e value changed: 0x185c -> 0x185b Cc: Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3") Signed-off-by: Jeongjun Park Reviewed-by: Andreas Dilger --- fs/ext4/super.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) -- diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 16a4ce704460..8337c4999f90 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -346,9 +346,9 @@ __u32 ext4_free_group_clusters(struct super_block *sb, __u32 ext4_free_inodes_count(struct super_block *sb, struct ext4_group_desc *bg) { - return le16_to_cpu(bg->bg_free_inodes_count_lo) | + return le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_lo)) | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? - (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0); + (__u32)le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_hi)) << 16 : 0); } __u32 ext4_used_dirs_count(struct super_block *sb, @@ -402,9 +402,9 @@ void ext4_free_group_clusters_set(struct super_block *sb, void ext4_free_inodes_set(struct super_block *sb, struct ext4_group_desc *bg, __u32 count) { - bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count); + WRITE_ONCE(bg->bg_free_inodes_count_lo, cpu_to_le16((__u16)count)); if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) - bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16); + WRITE_ONCE(bg->bg_free_inodes_count_hi, cpu_to_le16(count >> 16)); } void ext4_used_dirs_set(struct super_block *sb,