From patchwork Sat Jan 29 21:41:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 538465 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 791DAC433EF for ; Sat, 29 Jan 2022 21:41:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353216AbiA2VlS (ORCPT ); Sat, 29 Jan 2022 16:41:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353214AbiA2VlR (ORCPT ); Sat, 29 Jan 2022 16:41:17 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7927BC061714; Sat, 29 Jan 2022 13:41:17 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 41B67B82812; Sat, 29 Jan 2022 21:41:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4C2EC340E8; Sat, 29 Jan 2022 21:41:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1643492475; bh=HcQaM4uHbzU5KF8uZ0eERG7hSGsc2MgWgOfJIJw+SN8=; h=Date:From:To:Subject:In-Reply-To:From; b=p7TRmllIX3WRnElWKRKuTIoPXLu5lvVYgP26VDXNSzQSP1hsRr1x19MUuMQyizY/2 Rtryo8NfWkNqi7WjMeJPeRgHsaiqzz8YF7vxd8xNxbFH47KgpAYyLn4HbuuKjNeMdk 48pGOxOpWEBIgomD7+rj56pwo0a9ml6jNEXHNIBI= Date: Sat, 29 Jan 2022 13:41:14 -0800 From: Andrew Morton To: akpm@linux-foundation.org, andreyknvl@gmail.com, linux-mm@kvack.org, mm-commits@vger.kernel.org, pcc@google.com, peterz@infradead.org, stable@vger.kernel.org, torvalds@linux-foundation.org Subject: [patch 08/12] mm, kasan: use compare-exchange operation to set KASAN page tag Message-ID: <20220129214114.kH5sHZ6C3%akpm@linux-foundation.org> In-Reply-To: <20220129134026.8ccf701012f26eb2c2c269c9@linux-foundation.org> User-Agent: s-nail v14.8.16 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Peter Collingbourne Subject: mm, kasan: use compare-exchange operation to set KASAN page tag It has been reported that the tag setting operation on newly-allocated pages can cause the page flags to be corrupted when performed concurrently with other flag updates as a result of the use of non-atomic operations. Fix the problem by using a compare-exchange loop to update the tag. Link: https://lkml.kernel.org/r/20220120020148.1632253-1-pcc@google.com Link: https://linux-review.googlesource.com/id/I456b24a2b9067d93968d43b4bb3351c0cec63101 Fixes: 2813b9c02962 ("kasan, mm, arm64: tag non slab memory allocated via pagealloc") Signed-off-by: Peter Collingbourne Reviewed-by: Andrey Konovalov Cc: Peter Zijlstra Cc: Signed-off-by: Andrew Morton --- include/linux/mm.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) --- a/include/linux/mm.h~mm-use-compare-exchange-operation-to-set-kasan-page-tag +++ a/include/linux/mm.h @@ -1506,11 +1506,18 @@ static inline u8 page_kasan_tag(const st static inline void page_kasan_tag_set(struct page *page, u8 tag) { - if (kasan_enabled()) { - tag ^= 0xff; - page->flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT); - page->flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT; - } + unsigned long old_flags, flags; + + if (!kasan_enabled()) + return; + + tag ^= 0xff; + old_flags = READ_ONCE(page->flags); + do { + flags = old_flags; + flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT); + flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT; + } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags))); } static inline void page_kasan_tag_reset(struct page *page) From patchwork Sat Jan 29 21:41:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 538248 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99123C433FE for ; Sat, 29 Jan 2022 21:41:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353232AbiA2Vl1 (ORCPT ); Sat, 29 Jan 2022 16:41:27 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:48038 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353231AbiA2Vl1 (ORCPT ); Sat, 29 Jan 2022 16:41:27 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D5BE9B827EE; Sat, 29 Jan 2022 21:41:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17968C340E5; Sat, 29 Jan 2022 21:41:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1643492484; bh=bFtqr3z8tdckz/RB4dJTJ8Y293P850XkpxoA3JT6Jio=; h=Date:From:To:Subject:In-Reply-To:From; b=V8eO5Bgu7xgONhv7QxWydBEytb3aEpaUifLaEVFtI9bzwiZmHuwSPf9O0yVnolKfL ZLQ7Vmj82PaE7wUJWd/yMkJgEry1VIT81t+zN+uBoGMFkmuIIIhDo0qokk1FNjva73 hmU5mbA+rx7Nfp3jJ6Q0aA6YaN4l7xfJPtyhxqEU= Date: Sat, 29 Jan 2022 13:41:23 -0800 From: Andrew Morton To: adilger.kernel@dilger.ca, akpm@linux-foundation.org, gautham.ananthakrishna@oracle.com, gechangwei@live.cn, ghe@suse.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, junxiao.bi@oracle.com, linux-mm@kvack.org, mark@fasheh.com, mm-commits@vger.kernel.org, piaojun@huawei.com, saeed.mirzamohammadi@oracle.com, stable@vger.kernel.org, torvalds@linux-foundation.org, tytso@mit.edu Subject: [patch 11/12] jbd2: export jbd2_journal_[grab|put]_journal_head Message-ID: <20220129214123.z8JOI6u8G%akpm@linux-foundation.org> In-Reply-To: <20220129134026.8ccf701012f26eb2c2c269c9@linux-foundation.org> User-Agent: s-nail v14.8.16 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Joseph Qi Subject: jbd2: export jbd2_journal_[grab|put]_journal_head Patch series "ocfs2: fix a deadlock case". This fixes a deadlock case in ocfs2. We firstly export jbd2 symbols jbd2_journal_[grab|put]_journal_head as preparation and later use them in ocfs2 insread of jbd_[lock|unlock]_bh_journal_head to fix the deadlock. This patch (of 2): This exports symbols jbd2_journal_[grab|put]_journal_head, which will be used outside modules, e.g. ocfs2. Link: https://lkml.kernel.org/r/20220121071205.100648-2-joseph.qi@linux.alibaba.com Signed-off-by: Joseph Qi Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Changwei Ge Cc: Gang He Cc: Jun Piao Cc: Andreas Dilger Cc: Gautham Ananthakrishna Cc: Saeed Mirzamohammadi Cc: "Theodore Ts'o" Cc: Signed-off-by: Andrew Morton --- fs/jbd2/journal.c | 2 ++ 1 file changed, 2 insertions(+) --- a/fs/jbd2/journal.c~jbd2-export-jbd2_journal__journal_head +++ a/fs/jbd2/journal.c @@ -2972,6 +2972,7 @@ struct journal_head *jbd2_journal_grab_j jbd_unlock_bh_journal_head(bh); return jh; } +EXPORT_SYMBOL(jbd2_journal_grab_journal_head); static void __journal_remove_journal_head(struct buffer_head *bh) { @@ -3024,6 +3025,7 @@ void jbd2_journal_put_journal_head(struc jbd_unlock_bh_journal_head(bh); } } +EXPORT_SYMBOL(jbd2_journal_put_journal_head); /* * Initialize jbd inode head From patchwork Sat Jan 29 21:41:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 538464 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60040C433F5 for ; Sat, 29 Jan 2022 21:41:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353235AbiA2Vle (ORCPT ); Sat, 29 Jan 2022 16:41:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353231AbiA2Vlb (ORCPT ); Sat, 29 Jan 2022 16:41:31 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1FD2C061714; Sat, 29 Jan 2022 13:41:30 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 69D27B827EE; Sat, 29 Jan 2022 21:41:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 97496C340E8; Sat, 29 Jan 2022 21:41:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1643492488; bh=dQ0cPtF6bx+VAZiB0Z8eKeaDUiYQG145jJc72f+A1jw=; h=Date:From:To:Subject:In-Reply-To:From; b=JcSSRwdbsjpSMqZO2YM9EJBiNUEmCTuzi/lsGebYQaVBR3E0gZ+zVy0Cibd9bxhla OIFao0qkK2pSQd94Q+7ykUOxesnwqHo34nJ8NyWJuEWonPiaYlAimDRUBzhn8oONxI bFTKOrsed34ar0fO7qkJHmdZOcgjNpyo2Kq07eU0= Date: Sat, 29 Jan 2022 13:41:27 -0800 From: Andrew Morton To: adilger.kernel@dilger.ca, akpm@linux-foundation.org, gautham.ananthakrishna@oracle.com, gechangwei@live.cn, ghe@suse.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, junxiao.bi@oracle.com, linux-mm@kvack.org, mark@fasheh.com, mm-commits@vger.kernel.org, piaojun@huawei.com, saeed.mirzamohammadi@oracle.com, stable@vger.kernel.org, torvalds@linux-foundation.org, tytso@mit.edu Subject: [patch 12/12] ocfs2: fix a deadlock when commit trans Message-ID: <20220129214127.zeeGfdF7V%akpm@linux-foundation.org> In-Reply-To: <20220129134026.8ccf701012f26eb2c2c269c9@linux-foundation.org> User-Agent: s-nail v14.8.16 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Joseph Qi Subject: ocfs2: fix a deadlock when commit trans commit 6f1b228529ae introduces a regression which can deadlock as follows: Task1: Task2: jbd2_journal_commit_transaction ocfs2_test_bg_bit_allocatable spin_lock(&jh->b_state_lock) jbd_lock_bh_journal_head __jbd2_journal_remove_checkpoint spin_lock(&jh->b_state_lock) jbd2_journal_put_journal_head jbd_lock_bh_journal_head Task1 and Task2 lock bh->b_state and jh->b_state_lock in different order, which finally result in a deadlock. So use jbd2_journal_[grab|put]_journal_head instead in ocfs2_test_bg_bit_allocatable() to fix it. Link: https://lkml.kernel.org/r/20220121071205.100648-3-joseph.qi@linux.alibaba.com Fixes: 6f1b228529ae ("ocfs2: fix race between searching chunks and release journal_head from buffer_head") Signed-off-by: Joseph Qi Reported-by: Gautham Ananthakrishna Tested-by: Gautham Ananthakrishna Reported-by: Saeed Mirzamohammadi Cc: "Theodore Ts'o" Cc: Andreas Dilger Cc: Changwei Ge Cc: Gang He Cc: Joel Becker Cc: Jun Piao Cc: Junxiao Bi Cc: Mark Fasheh Cc: Signed-off-by: Andrew Morton --- fs/ocfs2/suballoc.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) --- a/fs/ocfs2/suballoc.c~ocfs2-fix-a-deadlock-when-commit-trans +++ a/fs/ocfs2/suballoc.c @@ -1251,26 +1251,23 @@ static int ocfs2_test_bg_bit_allocatable { struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data; struct journal_head *jh; - int ret = 1; + int ret; if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap)) return 0; - if (!buffer_jbd(bg_bh)) + jh = jbd2_journal_grab_journal_head(bg_bh); + if (!jh) return 1; - jbd_lock_bh_journal_head(bg_bh); - if (buffer_jbd(bg_bh)) { - jh = bh2jh(bg_bh); - spin_lock(&jh->b_state_lock); - bg = (struct ocfs2_group_desc *) jh->b_committed_data; - if (bg) - ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); - else - ret = 1; - spin_unlock(&jh->b_state_lock); - } - jbd_unlock_bh_journal_head(bg_bh); + spin_lock(&jh->b_state_lock); + bg = (struct ocfs2_group_desc *) jh->b_committed_data; + if (bg) + ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap); + else + ret = 1; + spin_unlock(&jh->b_state_lock); + jbd2_journal_put_journal_head(jh); return ret; }