From nobody Fri Nov 29 14:39:46 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.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 89CE81C8FB9 for ; Wed, 18 Sep 2024 17:21:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726680062; cv=none; b=hED5FSLImlCK0gjgfiNGNtL6U2DMp+35m+LR7Q/gNEnIyXZJIF86MY5UPK5k7/uOgv9m6rj5xhTxV3J9x6aGd+HN+pwPm55dRkZFZhqrDXqYkRgp9S8AreRO4pH7UEySBTSUdRSjFwcbi5V7+N327KArN3qzH56ISjWKddtAK58= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726680062; c=relaxed/simple; bh=dLW7yMaZUHwvr8OqyqLXlmiSaDlBdSZjSstzHtIjIaA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lAryid4yFqxPJS+oHmj+kT/JD/tRjIwGE9kOtq6SlIZSu1Z0wcQi8oaK2Zw2POK8VE3ZfF/5PiTZODW+gHNa5ISy48pMohBJZV4/xxxNkQMLfR3U0QFyY4gjCJXi+GctKw0/5JMvUULD/lXKDEeSBHG/Lvnw4lStKLEtktEQG7o= 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=Y04yPwYR; arc=none smtp.client-ip=209.85.210.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="Y04yPwYR" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7198de684a7so135271b3a.2 for ; Wed, 18 Sep 2024 10:21:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726680060; x=1727284860; 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=X8x33SOcOiQLrcjzIwPJ+0Uf+LjyFv78ehpo58zIPVY=; b=Y04yPwYRB1fP3qxRbn3kgez4YfqUW0rEqXotXKg8AnhDAngt4LXS3GIk1hb93G6daa +3g17xuj3JO4g0iPbqtObUUHxhjCwtWJv6xkWAXYb45cmtHPXhZ5xWm8cR6Tm6eQrvi3 s8/MvkwcAQ3yxsD2WrCurhgCJxAqLnqrEIK+pUjVDBXpGpPtTSJLdXjL2CzvbOMz6xrs SVNNXqgLMe9J9ftv58Hd8aAJAmL4ID+D1epV+cWYQMkQd2TdfZtOIl7G+EOMOq4eFUPk GFQhYE5reJl9Mj6P/3T5f/MdJYG36gOI9r13EznlsBY7dABFZGRCxNs1I6YCsN0V65kd Wa8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726680060; x=1727284860; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X8x33SOcOiQLrcjzIwPJ+0Uf+LjyFv78ehpo58zIPVY=; b=qHDcn7EkcgVZfkZNPQ0O7uFvjNZ5TeqNLLkSyoJB2kybmjBKpsUXMr7yw9lKM1M6Ak YzSARpxqc/1qFw9uWdyd08APc77Is89fVJyxxLDIn1ouZjQTxP8Pcz6bHu1R5fp+qWd0 CMrdEVTWZM/68TzPnVI4RQM6eyXI9rciD3RtFOJFmsCSZSI/XaFwghiWEo5Tojgrc2xR b0hM6LPVVrJ3Bz7BKx9NERKHgGN+YyUUEhTlVB9g/ANfQdyGOpNBIJ1Wzxtpq8AGUnj4 I/aFnFkaA+lyvb5ZUZlet/GKXHejd+ptQk7mo/lVNEHSZbkdGPVte6XEnliXn935lrtN yu/g== X-Forwarded-Encrypted: i=1; AJvYcCWlvwpYstPFkpow0ZkH72XNzNOzmrtoj7G3Tu7uXgntEhpOvR9sECr1NvUXGI0DvUDwOhmxVx5NcUOCNRg=@vger.kernel.org X-Gm-Message-State: AOJu0YzNMJilaUrPcYuc5LZeuQ/4UUfxtq8ykgoekVk8nW2Y7hmHZKck T8u7JrQsA38Zw+Isq6GcqJx0uZ+cpvpGicD499oLsl3U+Z0I4OPo X-Google-Smtp-Source: AGHT+IHPfFq2ebm0eN6ynBUnxXkvTYH9W9Sr+at5r786BHZM1dzVPHVDEIKanFJrN6wz1+6c++K+Gg== X-Received: by 2002:a05:6a00:1703:b0:706:6c38:31f3 with SMTP id d2e1a72fcca58-71926080509mr33181259b3a.8.1726680059568; Wed, 18 Sep 2024 10:20:59 -0700 (PDT) Received: from archlinux.. ([2405:201:e00c:517f:5e87:9cff:fe63:6000]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71944b7aeafsm7186476b3a.99.2024.09.18.10.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 10:20:58 -0700 (PDT) From: Mohammed Anees To: ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Mark Fasheh , Joel Becker , Joseph Qi , Mohammed Anees , syzbot+18a87160c7d64ba2e2f6@syzkaller.appspotmail.com Subject: [PATCH 1/2] ocfs2: Fix deadlock in ocfs2_read_virt_blocks Date: Wed, 18 Sep 2024 17:20:25 +0000 Message-ID: <20240918172026.2532-2-pvmohammedanees2003@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240918172026.2532-1-pvmohammedanees2003@gmail.com> References: <20240918172026.2532-1-pvmohammedanees2003@gmail.com> 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" syzbot has found a kernel BUG in ocfs2_write_cluster_by_desc, while the next patch in the series resolves this, another bug has been detected due to a potential deadlock [1]. The scenario is depicted here, CPU0 CPU1 lock(&ocfs2_file_ip_alloc_sem_key); lock(&osb->system_file_mutex); lock(&ocfs2_file_ip_alloc_sem_key); lock(&osb->system_file_mutex); The function calls which could lead to this are: CPU0 ocfs2_write_begin - lock(&ocfs2_file_ip_alloc_sem_key); =2E =2E =2E ocfs2_get_system_file_inode - lock(&osb->system_file_mutex); CPU1 - ocfs2_get_system_file_inode - lock(&osb->system_file_mutex); =2E =2E =2E ocfs2_read_virt_blocks - lock(&ocfs2_file_ip_alloc_sem_key); This issue can be resolved by making the down_read -> down_read_try in the ocfs2_read_virt_blocks. [1] https://syzkaller.appspot.com/bug?extid=3D18a87160c7d64ba2e2f6 Reported-and-tested-by: syzbot+18a87160c7d64ba2e2f6@syzkaller.appspotmail.c= om Closes: https://syzkaller.appspot.com/bug?extid=3D18a87160c7d64ba2e2f6 Signed-off-by: Mohammed Anees --- fs/ocfs2/extent_map.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c index 70a768b62..f83d0a3b6 100644 --- a/fs/ocfs2/extent_map.c +++ b/fs/ocfs2/extent_map.c @@ -12,6 +12,7 @@ #include #include #include +#include =20 #include =20 @@ -961,6 +962,8 @@ int ocfs2_read_virt_blocks(struct inode *inode, u64 v_b= lock, int nr, int rc =3D 0; u64 p_block, p_count; int i, count, done =3D 0; + int retries, max_retries =3D 5; + int retry_delay_ms =3D 30; =20 trace_ocfs2_read_virt_blocks( inode, (unsigned long long)v_block, nr, bhs, flags, @@ -973,7 +976,18 @@ int ocfs2_read_virt_blocks(struct inode *inode, u64 v_= block, int nr, } =20 while (done < nr) { - down_read(&OCFS2_I(inode)->ip_alloc_sem); + retries =3D 0; + while (retries < max_retries) { + if (down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem)) + break; // Lock acquired + msleep(retry_delay_ms); + retries++; + } + if (retries =3D=3D max_retries) { + rc =3D -EAGAIN; + mlog(ML_ERROR, "Cannot acquire lock\n"); + break; + } rc =3D ocfs2_extent_map_get_blocks(inode, v_block + done, &p_block, &p_count, NULL); up_read(&OCFS2_I(inode)->ip_alloc_sem); --=20 2.46.0 From nobody Fri Nov 29 14:39:46 2024 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (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 C93521C9EA7 for ; Wed, 18 Sep 2024 17:21:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726680066; cv=none; b=QeGnST2cgU8ZxgKMuaeEm1VYHk2Hb3AaQb3B35ju8QeIqyfpPoVmhumQqIkL+4/AGEhrqxbpguyXwtaReZqkujoxUKxrCrWEQqTveOQh93blQ92eMtaguMnfUvjvQJXlTS/2BxQlpbo3Y0kaGEcB0YXuktaNVvjOclzkuWMgF/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726680066; c=relaxed/simple; bh=wdJOrGs35uZAgenFPyRtZVhdFWiwxgcmCBPZMD7rp68=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WpS/5HpTD/5282WBn6VLZ81OHK8q0umHlO2ZZPSMzf5LTQfAOc8w3WCRxLkihfZnMMW9cq3NG+8eNDDWVkr4nDS0aFTV3ZRMDLTkA2jieUAYb2LkUzY6sP41iE7UJ8u3mhRwFzzKabypcSTYWQdg/74RIjIb/hHF6jx+rDS4ZgQ= 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=BSHm9fT3; arc=none smtp.client-ip=209.85.215.176 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="BSHm9fT3" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7db0fb03df5so5234454a12.3 for ; Wed, 18 Sep 2024 10:21:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726680064; x=1727284864; 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=7qrVPgtc9pr0MYXVJUam6GnYq72ewYaQxAFUiuA/F6Y=; b=BSHm9fT3CYHsrEDbWbaJpBlcVG5UtBZXM5HU39EUzxCZfLaMvFNFsRCHzmOXGKw0JA w5twWIcCdlIC5SABN4aeI3UndP33eYHnjA7BwlosOeUQM2sAZZhIbmzHKEhXsNoaVphw 7sfOoEtjnExht45dWMOs95XhhxpTKT2Sx1kO1T+7cKFeada7+LUghgPw2IW/cW/RpsTq VHa57oWEftaDLkt9tRqpycIvwh21QmFrmrZpuf6zX6Ccu6YU/XSbhqzwIl4rXrZwpMvy AY4sQyQikmGj01fdYfA/1d0TAdKS3q7LbhdknhwzpvCHBFM/ihfJCyFvQngEDjsudoPH aXdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726680064; x=1727284864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7qrVPgtc9pr0MYXVJUam6GnYq72ewYaQxAFUiuA/F6Y=; b=WtQfoArT5snNQqtyo4RL8zDEH+v5zrNTq5LLdORD9fnfZ+wG47meU8kuE3ngfH4CQN AhuUpzNoDcI6tSd3QYe4zXB5+zzbB/9aTZiTnVhs9Z5+9BBEHW8orLmr+1DAh7zeGEmU DdKNMHwr36SNtfz948wkSVYaaZGF8zaYS4uar5tIaQKTK9pjJrWQIn+o4Qvi5tt1/EW6 oLUN8F3WTe/JLc/+YTzHK2lSwqPHuPK5kylshh+jLTiLddRzqrjoxQXvGn4Bbn5PnW6A DPGC9C39srdZ/qxPskSKBQ9gVLljRSoIYoO7/ZZrjAhQ+RT1ivuIJo8BgHeNyByK9QjT f17A== X-Forwarded-Encrypted: i=1; AJvYcCWSTIO064kRV0zfe24b9dCJAImKrRDYO+54wf0tjTxYmfCwIUt9sVyi36XG3R6j2XQvN0/iaCKsjK4PidI=@vger.kernel.org X-Gm-Message-State: AOJu0Yxyf1RBvAaUV9kZU7wbaztGiVjHlaZ1AH3j8bFKzEvSwv/etpCp n8fh30uZdtBifer2MYW3V1EzE9TLLqB1zfeaEa/lw0fJwjKXloXt X-Google-Smtp-Source: AGHT+IF04UxO5twV68LGB/UhP/cNf8F9nCgomakgymG/9B3GMBVg5E8jctBNna5xDFm6dp5UHVbAtw== X-Received: by 2002:a05:6a21:4d8a:b0:1cf:2218:1be6 with SMTP id adf61e73a8af0-1cf764b02aemr34176792637.50.1726680063771; Wed, 18 Sep 2024 10:21:03 -0700 (PDT) Received: from archlinux.. ([2405:201:e00c:517f:5e87:9cff:fe63:6000]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71944b7aeafsm7186476b3a.99.2024.09.18.10.21.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 10:21:03 -0700 (PDT) From: Mohammed Anees To: ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Mark Fasheh , Joel Becker , Joseph Qi , Mohammed Anees , syzbot+18a87160c7d64ba2e2f6@syzkaller.appspotmail.com Subject: [PATCH 2/2] osfs2: Fix kernel BUG in ocfs2_write_cluster Date: Wed, 18 Sep 2024 17:20:26 +0000 Message-ID: <20240918172026.2532-3-pvmohammedanees2003@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240918172026.2532-1-pvmohammedanees2003@gmail.com> References: <20240918172026.2532-1-pvmohammedanees2003@gmail.com> 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" syzbot has found a kernel BUG in ocfs2_write_cluster_by_desc [1]. The issue arises because ocfs2_insert_extent receives start_blk as 0, which incorrectly maps to a physical address of 0. This occurs when block is 0 after the call to ocfs2_clusters_to_blocks which is invoked inside the ocfs2_add_clusters_in_btree. The block value is then passed to ocfs2_insert_extent, leading to the problem. [1] https://syzkaller.appspot.com/bug?extid=3D18a87160c7d64ba2e2f6 Reported-and-tested-by: syzbot+18a87160c7d64ba2e2f6@syzkaller.appspotmail.c= om Closes: https://syzkaller.appspot.com/bug?extid=3D18a87160c7d64ba2e2f6 Signed-off-by: Mohammed Anees --- fs/ocfs2/alloc.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index 395e23920..926ffeed8 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -4843,6 +4843,13 @@ int ocfs2_add_clusters_in_btree(handle_t *handle, } =20 block =3D ocfs2_clusters_to_blocks(osb->sb, bit_off); + if (block =3D=3D 0) { + mlog(ML_ERROR, "Conversion resulted in zero block number"); + status =3D -EIO; + need_free =3D 1; + goto bail; + } + trace_ocfs2_add_clusters_in_btree( (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), bit_off, num_bits); --=20 2.46.0