From patchwork Thu May 20 12:25:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 12270095 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63C9AC433B4 for ; Thu, 20 May 2021 12:25:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ED5EF61279 for ; Thu, 20 May 2021 12:25:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED5EF61279 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 88E0E6B00CC; Thu, 20 May 2021 08:25:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8550A6B00CB; Thu, 20 May 2021 08:25:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CDAA6B00CC; Thu, 20 May 2021 08:25:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0109.hostedemail.com [216.40.44.109]) by kanga.kvack.org (Postfix) with ESMTP id 3A6776B00CA for ; Thu, 20 May 2021 08:25:51 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C3E8C181AEF3F for ; Thu, 20 May 2021 12:25:50 +0000 (UTC) X-FDA: 78161530860.26.460E7AA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf20.hostedemail.com (Postfix) with ESMTP id A3CA5412 for ; Thu, 20 May 2021 12:25:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621513550; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nwkYYr9VKakeJ7pnq4AzoFfWPLnb5O3KiOprpBwKwMo=; b=Eth7ksranou0edxPzmUCYyfndWln7+nEAGKxud8LauHz18nIYh2y+iM0nTjNx88Vda+jMZ H4Lu9KplQyg/eLBtjVXuFwlFjiLm1oR+lhtr7OyXOQap3nyr4e2M6r0vrkL44rHmfcJEut pID1R8xhdhaXGAsIKHs3TS2gMi9tLbE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-335-sj5KzOTbMoi146jFiq3AbA-1; Thu, 20 May 2021 08:25:46 -0400 X-MC-Unique: sj5KzOTbMoi146jFiq3AbA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 865091A8A61; Thu, 20 May 2021 12:25:45 +0000 (UTC) Received: from max.com (unknown [10.40.195.97]) by smtp.corp.redhat.com (Postfix) with ESMTP id B8AD160C04; Thu, 20 May 2021 12:25:43 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , cluster-devel@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Andreas Gruenbacher , stable@vger.kernel.org Subject: [PATCH 1/6] gfs2: Fix mmap + page fault deadlocks (part 1) Date: Thu, 20 May 2021 14:25:31 +0200 Message-Id: <20210520122536.1596602-2-agruenba@redhat.com> In-Reply-To: <20210520122536.1596602-1-agruenba@redhat.com> References: <20210520122536.1596602-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Eth7ksra; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf20.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=agruenba@redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A3CA5412 X-Stat-Signature: c115j9y4tsqaqe3i3s6ia83ungpa4c8u X-HE-Tag: 1621513548-748683 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When the buffer passed to a read or write system call is memory mapped to the same file, a page fault can occur in gfs2_fault. In that case, the task will already be holding the inode glock, and trying to take it again will result in a BUG in add_to_queue(). Fix that by recognizing the self-recursion case and either skipping the lock taking (when the glock is held in a compatible way), or fail the operation. Likewise, a request to un-share a copy-on-write page can *probably* happen in similar situations, so treat the locking in gfs2_page_mkwrite in the same way. A future patch will handle this case more gracefully, along with addressing more complex deadlock scenarios. Reported-by: Jan Kara Fixes: 20f829999c38 ("gfs2: Rework read and page fault locking") Cc: stable@vger.kernel.org # v5.8+ Signed-off-by: Andreas Gruenbacher --- fs/gfs2/file.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c index 6d77743f11a4..7d88abb4629b 100644 --- a/fs/gfs2/file.c +++ b/fs/gfs2/file.c @@ -423,6 +423,7 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) struct page *page = vmf->page; struct inode *inode = file_inode(vmf->vma->vm_file); struct gfs2_inode *ip = GFS2_I(inode); + struct gfs2_holder *outer_gh = gfs2_glock_is_locked_by_me(ip->i_gl); struct gfs2_sbd *sdp = GFS2_SB(inode); struct gfs2_alloc_parms ap = { .aflags = 0, }; u64 offset = page_offset(page); @@ -436,10 +437,18 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) sb_start_pagefault(inode->i_sb); gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); - err = gfs2_glock_nq(&gh); - if (err) { - ret = block_page_mkwrite_return(err); - goto out_uninit; + if (likely(!outer_gh)) { + err = gfs2_glock_nq(&gh); + if (err) { + ret = block_page_mkwrite_return(err); + goto out_uninit; + } + } else { + if (!gfs2_holder_is_compatible(outer_gh, LM_ST_EXCLUSIVE)) { + /* We could try to upgrade outer_gh here. */ + ret = VM_FAULT_SIGBUS; + goto out_uninit; + } } /* Check page index against inode size */ @@ -540,7 +549,8 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) out_quota_unlock: gfs2_quota_unlock(ip); out_unlock: - gfs2_glock_dq(&gh); + if (likely(!outer_gh)) + gfs2_glock_dq(&gh); out_uninit: gfs2_holder_uninit(&gh); if (ret == VM_FAULT_LOCKED) { @@ -555,6 +565,7 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) { struct inode *inode = file_inode(vmf->vma->vm_file); struct gfs2_inode *ip = GFS2_I(inode); + struct gfs2_holder *outer_gh = gfs2_glock_is_locked_by_me(ip->i_gl); struct gfs2_holder gh; vm_fault_t ret; u16 state; @@ -562,13 +573,22 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) state = (vmf->flags & FAULT_FLAG_WRITE) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; gfs2_holder_init(ip->i_gl, state, 0, &gh); - err = gfs2_glock_nq(&gh); - if (err) { - ret = block_page_mkwrite_return(err); - goto out_uninit; + if (likely(!outer_gh)) { + err = gfs2_glock_nq(&gh); + if (err) { + ret = block_page_mkwrite_return(err); + goto out_uninit; + } + } else { + if (!gfs2_holder_is_compatible(outer_gh, state)) { + /* We could try to upgrade outer_gh here. */ + ret = VM_FAULT_SIGBUS; + goto out_uninit; + } } ret = filemap_fault(vmf); - gfs2_glock_dq(&gh); + if (likely(!outer_gh)) + gfs2_glock_dq(&gh); out_uninit: gfs2_holder_uninit(&gh); return ret; From patchwork Thu May 20 12:25:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 12270097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A912C433ED for ; Thu, 20 May 2021 12:25:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2920461279 for ; Thu, 20 May 2021 12:25:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2920461279 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DADB46B00CA; Thu, 20 May 2021 08:25:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D608E6B00CB; Thu, 20 May 2021 08:25:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4BB56B00CD; Thu, 20 May 2021 08:25:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0171.hostedemail.com [216.40.44.171]) by kanga.kvack.org (Postfix) with ESMTP id 87E556B00CA for ; Thu, 20 May 2021 08:25:51 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2508818018765 for ; Thu, 20 May 2021 12:25:51 +0000 (UTC) X-FDA: 78161530902.23.802B442 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id E073520007F0 for ; Thu, 20 May 2021 12:25:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621513550; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jVHI9tAO4j1d0e06AWNsK9Ts1IDxwkkuCGwpEsW+/6g=; b=GzkPW/hyurEEZ6U6SE7Y5RRExZ0lXwk75E9gmWOmc5ROMn1pr7jvBf3Z/tMoO/tqKCRCU7 qV5PGJOrLTEUCVU7Mktqp5+tV2a868DEobZ+th83D1TA3RpX3alI2HqH0n+GntcwPCDE69 LlcmkjZ0pwcEXOLYW9V2n/hYGxoFbF8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-444-cfoyeOgQNduXqNWdhpx9LA-1; Thu, 20 May 2021 08:25:48 -0400 X-MC-Unique: cfoyeOgQNduXqNWdhpx9LA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 79F75802B4F; Thu, 20 May 2021 12:25:47 +0000 (UTC) Received: from max.com (unknown [10.40.195.97]) by smtp.corp.redhat.com (Postfix) with ESMTP id DF11760C04; Thu, 20 May 2021 12:25:45 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , cluster-devel@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Andreas Gruenbacher Subject: [PATCH 2/6] iov_iter: Add iov_iter_fault_in_writeable() Date: Thu, 20 May 2021 14:25:32 +0200 Message-Id: <20210520122536.1596602-3-agruenba@redhat.com> In-Reply-To: <20210520122536.1596602-1-agruenba@redhat.com> References: <20210520122536.1596602-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Rspamd-Queue-Id: E073520007F0 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="GzkPW/hy"; spf=none (imf11.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=agruenba@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam04 X-Stat-Signature: hdez5fbpy463p919955cyhpfuk4q9qbb X-HE-Tag: 1621513548-842898 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add the equivalent of iov_iter_fault_in_readable(), but for pages that will be written to. While at it, fix an indentation error in iov_iter_fault_in_readable(). Signed-off-by: Andreas Gruenbacher --- include/linux/uio.h | 1 + lib/iov_iter.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/linux/uio.h b/include/linux/uio.h index d3ec87706d75..6811eb6ac6e3 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -124,6 +124,7 @@ size_t iov_iter_copy_from_user_atomic(struct page *page, void iov_iter_advance(struct iov_iter *i, size_t bytes); void iov_iter_revert(struct iov_iter *i, size_t bytes); int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes); +int iov_iter_fault_in_writeable(struct iov_iter *i, size_t bytes); size_t iov_iter_single_seg_count(const struct iov_iter *i); size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index c701b7a187f2..317c94eac907 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -480,13 +480,31 @@ int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes) iterate_iovec(i, bytes, v, iov, skip, ({ err = fault_in_pages_readable(v.iov_base, v.iov_len); if (unlikely(err)) - return err; + return err; 0;})) } return 0; } EXPORT_SYMBOL(iov_iter_fault_in_readable); +int iov_iter_fault_in_writeable(struct iov_iter *i, size_t bytes) +{ + size_t skip = i->iov_offset; + const struct iovec *iov; + int err; + struct iovec v; + + if (!(i->type & (ITER_BVEC|ITER_KVEC))) { + iterate_iovec(i, bytes, v, iov, skip, ({ + err = fault_in_pages_writeable(v.iov_base, v.iov_len); + if (unlikely(err)) + return err; + 0;})) + } + return 0; +} +EXPORT_SYMBOL(iov_iter_fault_in_writeable); + void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov, unsigned long nr_segs, size_t count) From patchwork Thu May 20 12:25:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 12270101 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14A2AC433ED for ; Thu, 20 May 2021 12:25:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B20CB611AD for ; Thu, 20 May 2021 12:25:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B20CB611AD Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CEB7D6B00CE; Thu, 20 May 2021 08:25:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C9C6C6B00D0; Thu, 20 May 2021 08:25:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA06E6B00D1; Thu, 20 May 2021 08:25:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0148.hostedemail.com [216.40.44.148]) by kanga.kvack.org (Postfix) with ESMTP id 6D6656B00CE for ; Thu, 20 May 2021 08:25:56 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0167218047606 for ; Thu, 20 May 2021 12:25:56 +0000 (UTC) X-FDA: 78161531112.07.70E9BCC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf11.hostedemail.com (Postfix) with ESMTP id AE39620007F7 for ; Thu, 20 May 2021 12:25:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621513555; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2c2jw50qd2fCeI4RwK6TC+NJCCNrz2KKMYGOCwuAm3M=; b=Za9jOvN6szuYS62d5+OXIRBKr3VItYVvRXNIWdGLs7aU0v7ptIjKnAsb1jCSTvDC8ySWpn i2Cifb3IG+VhmsrvkWMzptk26qM5jEHxnvBwIhtSMVbbkeJDQLKKyGmPXLf6mELD9PdCKD PVOgymky1dt6U6uCVB0RjH5sWM6V2kE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-375-RmNTZks4MnuVQnz1OAkuxA-1; Thu, 20 May 2021 08:25:50 -0400 X-MC-Unique: RmNTZks4MnuVQnz1OAkuxA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 85C638049CA; Thu, 20 May 2021 12:25:49 +0000 (UTC) Received: from max.com (unknown [10.40.195.97]) by smtp.corp.redhat.com (Postfix) with ESMTP id D00D760C04; Thu, 20 May 2021 12:25:47 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , cluster-devel@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Andreas Gruenbacher Subject: [PATCH 3/6] gfs2: Add wrappers for accessing journal_info Date: Thu, 20 May 2021 14:25:33 +0200 Message-Id: <20210520122536.1596602-4-agruenba@redhat.com> In-Reply-To: <20210520122536.1596602-1-agruenba@redhat.com> References: <20210520122536.1596602-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Rspamd-Queue-Id: AE39620007F7 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Za9jOvN6; spf=none (imf11.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=agruenba@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam04 X-Stat-Signature: 8hhcm3r9kyz33yoc8ditp8uanx9ahfti X-HE-Tag: 1621513553-378280 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: No longer access current->journal_info directly. We'll use that to encode additional information in current->journal_info later. Signed-off-by: Andreas Gruenbacher --- fs/gfs2/aops.c | 6 +++--- fs/gfs2/bmap.c | 28 ++++++++++++++-------------- fs/gfs2/incore.h | 10 ++++++++++ fs/gfs2/inode.c | 2 +- fs/gfs2/log.c | 4 ++-- fs/gfs2/lops.c | 2 +- fs/gfs2/meta_io.c | 6 +++--- fs/gfs2/super.c | 2 +- fs/gfs2/trans.c | 16 ++++++++-------- 9 files changed, 43 insertions(+), 33 deletions(-) diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c index 23b5be3db044..50dd1771d00c 100644 --- a/fs/gfs2/aops.c +++ b/fs/gfs2/aops.c @@ -95,7 +95,7 @@ static int gfs2_writepage(struct page *page, struct writeback_control *wbc) if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) goto out; - if (current->journal_info) + if (current_trans()) goto redirty; return iomap_writepage(page, wbc, &wpc, &gfs2_writeback_ops); @@ -182,7 +182,7 @@ static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) goto out; - if (PageChecked(page) || current->journal_info) + if (PageChecked(page) || current_trans()) goto out_ignore; return __gfs2_jdata_writepage(page, wbc); @@ -620,7 +620,7 @@ void adjust_fs_space(struct inode *inode) static int jdata_set_page_dirty(struct page *page) { - if (current->journal_info) + if (current_trans()) SetPageChecked(page); return __set_page_dirty_buffers(page); } diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c index 0bcf11a9987b..2ff501c413f4 100644 --- a/fs/gfs2/bmap.c +++ b/fs/gfs2/bmap.c @@ -1016,7 +1016,7 @@ static void gfs2_iomap_page_done(struct inode *inode, loff_t pos, unsigned copied, struct page *page, struct iomap *iomap) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); struct gfs2_inode *ip = GFS2_I(inode); struct gfs2_sbd *sdp = GFS2_SB(inode); @@ -1099,7 +1099,7 @@ static int gfs2_iomap_begin_write(struct inode *inode, loff_t pos, } } - tr = current->journal_info; + tr = current_trans(); if (tr->tr_num_buf_new) __mark_inode_dirty(inode, I_DIRTY_DATASYNC); @@ -1347,7 +1347,7 @@ int gfs2_alloc_extent(struct inode *inode, u64 lblock, u64 *dblock, static int gfs2_block_zero_range(struct inode *inode, loff_t from, unsigned int length) { - BUG_ON(current->journal_info); + BUG_ON(current_trans()); return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops); } @@ -1386,7 +1386,7 @@ static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize truncate_pagecache(inode, oldsize - chunk); oldsize -= chunk; - tr = current->journal_info; + tr = current_trans(); if (!test_bit(TR_TOUCHED, &tr->tr_flags)) continue; @@ -1447,7 +1447,7 @@ static int trunc_start(struct inode *inode, u64 newsize) out: brelse(dibh); - if (current->journal_info) + if (current_trans()) gfs2_trans_end(sdp); return error; } @@ -1555,7 +1555,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh, the rgrp. So we estimate. We know it can't be more than the dinode's i_blocks and we don't want to exceed the journal flush threshold, sd_log_thresh2. */ - if (current->journal_info == NULL) { + if (!current_trans()) { unsigned int jblocks_rqsted, revokes; jblocks_rqsted = rgd->rd_length + RES_DINODE + @@ -1577,7 +1577,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh, down_write(&ip->i_rw_mutex); } /* check if we will exceed the transaction blocks requested */ - tr = current->journal_info; + tr = current_trans(); if (tr->tr_num_buf_new + RES_STATFS + RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) { /* We set blks_outside_rgrp to ensure the loop will @@ -1625,7 +1625,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh, if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks outside the rgrp we just processed, do it all over again. */ - if (current->journal_info) { + if (current_trans()) { struct buffer_head *dibh; ret = gfs2_meta_inode_buffer(ip, &dibh); @@ -1991,7 +1991,7 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) } if (btotal) { - if (current->journal_info == NULL) { + if (!current_trans()) { ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_QUOTA, 0); if (ret) @@ -2011,7 +2011,7 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) out: if (gfs2_holder_initialized(&rd_gh)) gfs2_glock_dq_uninit(&rd_gh); - if (current->journal_info) { + if (current_trans()) { up_write(&ip->i_rw_mutex); gfs2_trans_end(sdp); cond_resched(); @@ -2436,7 +2436,7 @@ static int gfs2_journaled_truncate_range(struct inode *inode, loff_t offset, offset += chunk; length -= chunk; - tr = current->journal_info; + tr = current_trans(); if (!test_bit(TR_TOUCHED, &tr->tr_flags)) continue; @@ -2501,7 +2501,7 @@ int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length) } if (gfs2_is_jdata(ip)) { - BUG_ON(!current->journal_info); + BUG_ON(!current_trans()); gfs2_journaled_truncate_range(inode, offset, length); } else truncate_pagecache_range(inode, offset, offset + length - 1); @@ -2509,14 +2509,14 @@ int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length) file_update_time(file); mark_inode_dirty(inode); - if (current->journal_info) + if (current_trans()) gfs2_trans_end(sdp); if (!gfs2_is_stuffed(ip)) error = punch_hole(ip, offset, length); out: - if (current->journal_info) + if (current_trans()) gfs2_trans_end(sdp); return error; } diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h index e6f820f146cb..aa8d1a23132d 100644 --- a/fs/gfs2/incore.h +++ b/fs/gfs2/incore.h @@ -871,5 +871,15 @@ static inline unsigned gfs2_max_stuffed_size(const struct gfs2_inode *ip) return GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize - sizeof(struct gfs2_dinode); } +static inline struct gfs2_trans *current_trans(void) +{ + return current->journal_info; +} + +static inline void set_current_trans(struct gfs2_trans *tr) +{ + current->journal_info = tr; +} + #endif /* __INCORE_DOT_H__ */ diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c index 6e15434b23ac..1b94cbdc00cc 100644 --- a/fs/gfs2/inode.c +++ b/fs/gfs2/inode.c @@ -1883,7 +1883,7 @@ static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr) { int error; - if (current->journal_info) + if (current_trans()) return __gfs2_setattr_simple(inode, attr); error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0); diff --git a/fs/gfs2/log.c b/fs/gfs2/log.c index 42c15cfc0821..3ee29045ab90 100644 --- a/fs/gfs2/log.c +++ b/fs/gfs2/log.c @@ -204,7 +204,7 @@ void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc) ret = 0; if (time_after(jiffies, flush_start + (HZ * 600))) { fs_err(sdp, "Error: In %s for ten minutes! t=%d\n", - __func__, current->journal_info ? 1 : 0); + __func__, current_trans() ? 1 : 0); dump_ail_list(sdp); goto out; } @@ -971,7 +971,7 @@ static void empty_ail1_list(struct gfs2_sbd *sdp) for (;;) { if (time_after(jiffies, start + (HZ * 600))) { fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n", - __func__, current->journal_info ? 1 : 0); + __func__, current_trans() ? 1 : 0); dump_ail_list(sdp); return; } diff --git a/fs/gfs2/lops.c b/fs/gfs2/lops.c index 8ee05d25dfa6..9bd080e5db43 100644 --- a/fs/gfs2/lops.c +++ b/fs/gfs2/lops.c @@ -43,7 +43,7 @@ void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) { struct gfs2_bufdata *bd; - BUG_ON(!current->journal_info); + BUG_ON(!current_trans()); clear_buffer_dirty(bh); if (test_set_buffer_pinned(bh)) diff --git a/fs/gfs2/meta_io.c b/fs/gfs2/meta_io.c index d68184ebbfdd..f5622393de63 100644 --- a/fs/gfs2/meta_io.c +++ b/fs/gfs2/meta_io.c @@ -294,7 +294,7 @@ int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags, bh = *bhp; wait_on_buffer(bh); if (unlikely(!buffer_uptodate(bh))) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); if (tr && test_bit(TR_TOUCHED, &tr->tr_flags)) gfs2_io_error_bh_wd(sdp, bh); brelse(bh); @@ -321,7 +321,7 @@ int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh) wait_on_buffer(bh); if (!buffer_uptodate(bh)) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); if (tr && test_bit(TR_TOUCHED, &tr->tr_flags)) gfs2_io_error_bh_wd(sdp, bh); return -EIO; @@ -337,7 +337,7 @@ void gfs2_remove_from_journal(struct buffer_head *bh, int meta) struct address_space *mapping = bh->b_page->mapping; struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping); struct gfs2_bufdata *bd = bh->b_private; - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); int was_pinned = 0; if (test_clear_buffer_pinned(bh)) { diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c index 4d4ceb0b6903..5cb823e58d01 100644 --- a/fs/gfs2/super.c +++ b/fs/gfs2/super.c @@ -557,7 +557,7 @@ static void gfs2_dirty_inode(struct inode *inode, int flags) } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE)) return; - if (current->journal_info == NULL) { + if (!current_trans()) { ret = gfs2_trans_begin(sdp, RES_DINODE, 0); if (ret) { fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret); diff --git a/fs/gfs2/trans.c b/fs/gfs2/trans.c index 63fec11ef2ce..7681fbb12050 100644 --- a/fs/gfs2/trans.c +++ b/fs/gfs2/trans.c @@ -43,8 +43,8 @@ int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp, { unsigned int extra_revokes; - if (current->journal_info) { - gfs2_print_trans(sdp, current->journal_info); + if (current_trans()) { + gfs2_print_trans(sdp, current_trans()); BUG(); } BUG_ON(blocks == 0 && revokes == 0); @@ -101,7 +101,7 @@ int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp, return -EROFS; } - current->journal_info = tr; + set_current_trans(tr); return 0; } @@ -123,10 +123,10 @@ int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks, void gfs2_trans_end(struct gfs2_sbd *sdp) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); s64 nbuf; - current->journal_info = NULL; + set_current_trans(NULL); if (!test_bit(TR_TOUCHED, &tr->tr_flags)) { gfs2_log_release_revokes(sdp, tr->tr_revokes); @@ -191,7 +191,7 @@ static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl, */ void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; struct gfs2_bufdata *bd; @@ -232,7 +232,7 @@ void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh) struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; struct gfs2_bufdata *bd; struct gfs2_meta_header *mh; - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); lock_buffer(bh); @@ -288,7 +288,7 @@ void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh) void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans(); BUG_ON(!list_empty(&bd->bd_list)); gfs2_add_revoke(sdp, bd); From patchwork Thu May 20 12:25:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 12270099 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F0F6C43460 for ; Thu, 20 May 2021 12:25:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0959C611ED for ; Thu, 20 May 2021 12:25:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0959C611ED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9B6E56B00CD; Thu, 20 May 2021 08:25:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9682C6B00CE; Thu, 20 May 2021 08:25:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 856496B00CF; Thu, 20 May 2021 08:25:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0192.hostedemail.com [216.40.44.192]) by kanga.kvack.org (Postfix) with ESMTP id 5494B6B00CD for ; Thu, 20 May 2021 08:25:55 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E05B2803148B for ; Thu, 20 May 2021 12:25:54 +0000 (UTC) X-FDA: 78161531028.24.01D3BA5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf05.hostedemail.com (Postfix) with ESMTP id 739ABE0007C4 for ; Thu, 20 May 2021 12:25:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621513554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GyKgynniRDUoKP5K5rhBmL1x4hLij6EvDAklIQldN/k=; b=J+uzWEnqwUsRbCfCfAve+LS6xvfsL78iNPpRcRN0jncSuAu3x3rH/mgEQRMyu2se7Z8T6i YG5W0py76X+hfYKowWr+lNAeJpUEYODc+mvbJ0rmEermCi4vNGA0aixhUdwedgIHlNzfJh zyRJU8t/orNi3IwGBBEjrDeevMH1TDs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-499-iGAexnAZPtOlhpFAS6pTVA-1; Thu, 20 May 2021 08:25:52 -0400 X-MC-Unique: iGAexnAZPtOlhpFAS6pTVA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7BBE0180FD61; Thu, 20 May 2021 12:25:51 +0000 (UTC) Received: from max.com (unknown [10.40.195.97]) by smtp.corp.redhat.com (Postfix) with ESMTP id DC70760C04; Thu, 20 May 2021 12:25:49 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , cluster-devel@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Andreas Gruenbacher Subject: [PATCH 4/6] gfs2: Encode glock holding and retry flags in journal_info Date: Thu, 20 May 2021 14:25:34 +0200 Message-Id: <20210520122536.1596602-5-agruenba@redhat.com> In-Reply-To: <20210520122536.1596602-1-agruenba@redhat.com> References: <20210520122536.1596602-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=J+uzWEnq; spf=none (imf05.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=agruenba@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 739ABE0007C4 X-Stat-Signature: u6khrqrebqu5saz5gg8a73c9tj9buujr X-HE-Tag: 1621513552-853392 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use the lowest two bits in current->journal_info to encode when we're holding a glock and when an operation holding a glock needs to be retried. Signed-off-by: Andreas Gruenbacher --- fs/gfs2/incore.h | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/fs/gfs2/incore.h b/fs/gfs2/incore.h index aa8d1a23132d..e32433df119c 100644 --- a/fs/gfs2/incore.h +++ b/fs/gfs2/incore.h @@ -871,14 +871,45 @@ static inline unsigned gfs2_max_stuffed_size(const struct gfs2_inode *ip) return GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize - sizeof(struct gfs2_dinode); } +/* + * Transactions are always memory aligned, so we use bit 0 of + * current->journal_info to indicate when we're holding a glock and so taking + * random additional glocks might deadlock, and bit 1 to indicate when such an + * operation needs to be retried after dropping and re-acquiring that "outer" + * glock. + */ + static inline struct gfs2_trans *current_trans(void) { - return current->journal_info; + return (void *)((long)current->journal_info & ~3); } static inline void set_current_trans(struct gfs2_trans *tr) { - current->journal_info = tr; + long flags = (long)current->journal_info & 3; + current->journal_info = (void *)((long)tr | flags); +} + +static inline bool current_holds_glock(void) +{ + return (long)current->journal_info & 1; +} + +static inline bool current_needs_retry(void) +{ + return (long)current->journal_info & 2; +} + +static inline void set_current_holds_glock(bool b) +{ + current->journal_info = + (void *)(((long)current->journal_info & ~1) | b); +} + +static inline void set_current_needs_retry(bool b) +{ + current->journal_info = + (void *)(((long)current->journal_info & ~2) | (b << 1)); } #endif /* __INCORE_DOT_H__ */ From patchwork Thu May 20 12:25:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 12270103 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83611C43460 for ; Thu, 20 May 2021 12:26:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0D49F611ED for ; Thu, 20 May 2021 12:26:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0D49F611ED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id EC0FF6B00D0; Thu, 20 May 2021 08:25:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E70BA6B00D1; Thu, 20 May 2021 08:25:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C75248E0006; Thu, 20 May 2021 08:25:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0210.hostedemail.com [216.40.44.210]) by kanga.kvack.org (Postfix) with ESMTP id 92FE86B00D0 for ; Thu, 20 May 2021 08:25:57 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 26AE8181AEF3F for ; Thu, 20 May 2021 12:25:57 +0000 (UTC) X-FDA: 78161531154.40.71A6C43 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf02.hostedemail.com (Postfix) with ESMTP id DB59B40B8CC9 for ; Thu, 20 May 2021 12:25:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621513556; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=niIyGypKRI/tMYi74NMa5b60rnjqZJ04fmjRzMuqFao=; b=GpQEbuQIFgdU+Bml88iBPTcuz50urVdzmU0t0CyKjKmBunZtSIra16DZmT708GUm/PPsPk Rj7DYCul4mDgQoIDgZA14TGhzu1hoYaCRJb7F88Qeu374l/tfuGFar3MjLhGgr3t1VNXVs Zfiw4YUw7gUHBYNqtZ0m8/kG56UbzV4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-7-MTTdWM3sNMOefMbCQBY5jw-1; Thu, 20 May 2021 08:25:54 -0400 X-MC-Unique: MTTdWM3sNMOefMbCQBY5jw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 57034101371C; Thu, 20 May 2021 12:25:53 +0000 (UTC) Received: from max.com (unknown [10.40.195.97]) by smtp.corp.redhat.com (Postfix) with ESMTP id D219C60C04; Thu, 20 May 2021 12:25:51 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , cluster-devel@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Andreas Gruenbacher Subject: [PATCH 5/6] gfs2: Add LM_FLAG_OUTER glock holder flag Date: Thu, 20 May 2021 14:25:35 +0200 Message-Id: <20210520122536.1596602-6-agruenba@redhat.com> In-Reply-To: <20210520122536.1596602-1-agruenba@redhat.com> References: <20210520122536.1596602-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GpQEbuQI; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf02.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=agruenba@redhat.com X-Stat-Signature: mu6u11cqo5c9cj34hpcurbitftp5adpd X-Rspamd-Queue-Id: DB59B40B8CC9 X-Rspamd-Server: rspam02 X-HE-Tag: 1621513555-549187 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When a glock holder has the LM_FLAG_OUTER flag set, we set the current_holds_glock() flag upon taking the lock. With that flag set, we can then recognize when trying to take an "inner" glock and react accordingly. Signed-off-by: Andreas Gruenbacher --- fs/gfs2/glock.c | 12 ++++++++++++ fs/gfs2/glock.h | 13 ++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fs/gfs2/glock.c b/fs/gfs2/glock.c index d9cb261f55b0..f6cae2ee1c83 100644 --- a/fs/gfs2/glock.c +++ b/fs/gfs2/glock.c @@ -1427,6 +1427,11 @@ int gfs2_glock_nq(struct gfs2_holder *gh) if (glock_blocked_by_withdraw(gl) && !(gh->gh_flags & LM_FLAG_NOEXP)) return -EIO; + if (gh->gh_flags & LM_FLAG_OUTER) { + BUG_ON(current_holds_glock()); + set_current_holds_glock(true); + } + if (test_bit(GLF_LRU, &gl->gl_flags)) gfs2_glock_remove_from_lru(gl); @@ -1514,6 +1519,11 @@ void gfs2_glock_dq(struct gfs2_holder *gh) __gfs2_glock_queue_work(gl, delay); } spin_unlock(&gl->gl_lockref.lock); + + if (gh->gh_flags & LM_FLAG_OUTER) { + BUG_ON(!current_holds_glock()); + set_current_holds_glock(false); + } } void gfs2_glock_dq_wait(struct gfs2_holder *gh) @@ -2068,6 +2078,8 @@ static const char *hflags2str(char *buf, u16 flags, unsigned long iflags) *p++ = 'p'; if (flags & LM_FLAG_NODE_SCOPE) *p++ = 'n'; + if (flags & LM_FLAG_OUTER) + *p++ = 'o'; if (flags & GL_ASYNC) *p++ = 'a'; if (flags & GL_EXACT) diff --git a/fs/gfs2/glock.h b/fs/gfs2/glock.h index f0ef6fd24ba4..8b145269fb14 100644 --- a/fs/gfs2/glock.h +++ b/fs/gfs2/glock.h @@ -94,6 +94,12 @@ static inline bool gfs2_holder_is_compatible(struct gfs2_holder *gh, int state) * This holder agrees to share the lock within this node. In other words, * the glock is held in EX mode according to DLM, but local holders on the * same node can share it. + * + * LM_FLAG_OUTER + * Use set_current_holds_glock() to indicate when the current task is holding + * this "upper" glock, and current_holds_glock() to detect when the current + * task is trying to take another glock. Used to prevent deadlocks involving + * the inode glock during page faults. */ #define LM_FLAG_TRY 0x0001 @@ -102,9 +108,10 @@ static inline bool gfs2_holder_is_compatible(struct gfs2_holder *gh, int state) #define LM_FLAG_ANY 0x0008 #define LM_FLAG_PRIORITY 0x0010 #define LM_FLAG_NODE_SCOPE 0x0020 -#define GL_ASYNC 0x0040 -#define GL_EXACT 0x0080 -#define GL_SKIP 0x0100 +#define LM_FLAG_OUTER 0x0040 +#define GL_ASYNC 0x0080 +#define GL_EXACT 0x0100 +#define GL_SKIP 0x0200 #define GL_NOCACHE 0x0400 /* From patchwork Thu May 20 12:25:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 12270105 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F820C433B4 for ; Thu, 20 May 2021 12:26:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D7E20611ED for ; Thu, 20 May 2021 12:26:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D7E20611ED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7BA226B00D4; Thu, 20 May 2021 08:26:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 791418E0006; Thu, 20 May 2021 08:26:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60D826B00D6; Thu, 20 May 2021 08:26:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id 307916B00D4 for ; Thu, 20 May 2021 08:26:04 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id C81DAC5B2 for ; Thu, 20 May 2021 12:26:03 +0000 (UTC) X-FDA: 78161531406.30.C9505EF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 520CEC0042E1 for ; Thu, 20 May 2021 12:26:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621513563; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T5mrbqRtsMxsD6ZDRsu0QACrl2UqbZc8cHGI2l2L33w=; b=fs5xZUFiebemtnPuBDRomo3BYDm8tvetE7CDBhwCKrTrF+GFOwXCH7tcs4Yh6puaGCHzZs l4rs/JSN4QPNFvZBwqyZFU/9nKHh3ZfsgVNaYkUarZI6tpbC5tC6zri2CTiVmFf0M8pVqo 1563hoqv47H+nddmrIX5qmd4KqE55v0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-179-Izi-5FhIPdCnX14K1t8m0Q-1; Thu, 20 May 2021 08:25:56 -0400 X-MC-Unique: Izi-5FhIPdCnX14K1t8m0Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 63F17180FD69; Thu, 20 May 2021 12:25:55 +0000 (UTC) Received: from max.com (unknown [10.40.195.97]) by smtp.corp.redhat.com (Postfix) with ESMTP id AFA5B60C04; Thu, 20 May 2021 12:25:53 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , cluster-devel@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Jan Kara , Andreas Gruenbacher Subject: [PATCH 6/6] gfs2: Fix mmap + page fault deadlocks (part 2) Date: Thu, 20 May 2021 14:25:36 +0200 Message-Id: <20210520122536.1596602-7-agruenba@redhat.com> In-Reply-To: <20210520122536.1596602-1-agruenba@redhat.com> References: <20210520122536.1596602-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fs5xZUFi; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf14.hostedemail.com: domain of agruenba@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=agruenba@redhat.com X-Stat-Signature: jsd56jt76d8tkcgdmct96a84hn89a4qb X-Rspamd-Queue-Id: 520CEC0042E1 X-Rspamd-Server: rspam02 X-HE-Tag: 1621513561-194563 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Now that we handle self-recursion on the inode glock in gfs2_fault and gfs2_page_mkwrite, we need to take care of more complex deadlock scenarios like the following (example by Jan Kara): Two independent processes P1, P2. Two files F1, F2, and two mappings M1, M2 where M1 is a mapping of F1, M2 is a mapping of F2. Now P1 does DIO to F1 with M2 as a buffer, P2 does DIO to F2 with M1 as a buffer. They can race like: P1 P2 read() read() gfs2_file_read_iter() gfs2_file_read_iter() gfs2_file_direct_read() gfs2_file_direct_read() locks glock of F1 locks glock of F2 iomap_dio_rw() iomap_dio_rw() bio_iov_iter_get_pages() bio_iov_iter_get_pages() gfs2_fault() gfs2_fault() tries to grab glock of F2 tries to grab glock of F1 Those kinds of scenarios are much harder to reproduce than self-recursion. We deal with such situations by using the LM_FLAG_OUTER flag to mark "outer" glock taking. Then, when taking an "inner" glock, we use the LM_FLAG_TRY flag so that locking attempts that don't immediately succeed will be aborted. In case of a failed locking attempt, we "unroll" to where the "outer" glock was taken, drop the "outer" glock, and fault in the first offending user page. This will re-trigger the "inner" locking attempt but without the LM_FLAG_TRY flag. Once that has happened, we re-acquire the "outer" glock and retry the original operation. Reported-by: Jan Kara Signed-off-by: Andreas Gruenbacher --- fs/gfs2/bmap.c | 3 ++- fs/gfs2/file.c | 62 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c index 2ff501c413f4..82e4506984e3 100644 --- a/fs/gfs2/bmap.c +++ b/fs/gfs2/bmap.c @@ -967,7 +967,8 @@ static int gfs2_write_lock(struct inode *inode) struct gfs2_sbd *sdp = GFS2_SB(inode); int error; - gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh); + gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_OUTER, + &ip->i_gh); error = gfs2_glock_nq(&ip->i_gh); if (error) goto out_uninit; diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c index 7d88abb4629b..8b26893f8dc6 100644 --- a/fs/gfs2/file.c +++ b/fs/gfs2/file.c @@ -431,21 +431,30 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) vm_fault_t ret = VM_FAULT_LOCKED; struct gfs2_holder gh; unsigned int length; + u16 flags = 0; loff_t size; int err; sb_start_pagefault(inode->i_sb); - gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); + if (current_holds_glock()) + flags |= LM_FLAG_TRY; + + gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, flags, &gh); if (likely(!outer_gh)) { err = gfs2_glock_nq(&gh); if (err) { ret = block_page_mkwrite_return(err); + if (err == GLR_TRYFAILED) { + set_current_needs_retry(true); + ret = VM_FAULT_SIGBUS; + } goto out_uninit; } } else { if (!gfs2_holder_is_compatible(outer_gh, LM_ST_EXCLUSIVE)) { /* We could try to upgrade outer_gh here. */ + set_current_needs_retry(true); ret = VM_FAULT_SIGBUS; goto out_uninit; } @@ -568,20 +577,28 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) struct gfs2_holder *outer_gh = gfs2_glock_is_locked_by_me(ip->i_gl); struct gfs2_holder gh; vm_fault_t ret; - u16 state; + u16 state, flags = 0; int err; + if (current_holds_glock()) + flags |= LM_FLAG_TRY; + state = (vmf->flags & FAULT_FLAG_WRITE) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; - gfs2_holder_init(ip->i_gl, state, 0, &gh); + gfs2_holder_init(ip->i_gl, state, flags, &gh); if (likely(!outer_gh)) { err = gfs2_glock_nq(&gh); if (err) { ret = block_page_mkwrite_return(err); + if (err == GLR_TRYFAILED) { + set_current_needs_retry(true); + ret = VM_FAULT_SIGBUS; + } goto out_uninit; } } else { if (!gfs2_holder_is_compatible(outer_gh, state)) { /* We could try to upgrade outer_gh here. */ + set_current_needs_retry(true); ret = VM_FAULT_SIGBUS; goto out_uninit; } @@ -807,13 +824,21 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to, if (!count) return 0; /* skip atime */ - gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh); + gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, LM_FLAG_OUTER, gh); +retry: ret = gfs2_glock_nq(gh); if (ret) goto out_uninit; ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL, 0); gfs2_glock_dq(gh); + if (unlikely(current_needs_retry())) { + set_current_needs_retry(false); + if (ret == -EFAULT) { + if (!iov_iter_fault_in_writeable(to, PAGE_SIZE)) + goto retry; + } + } out_uninit: gfs2_holder_uninit(gh); return ret; @@ -837,7 +862,8 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from, * unfortunately, have the option of only flushing a range like the * VFS does. */ - gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh); + gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, LM_FLAG_OUTER, gh); +retry: ret = gfs2_glock_nq(gh); if (ret) goto out_uninit; @@ -851,6 +877,13 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from, ret = 0; out: gfs2_glock_dq(gh); + if (unlikely(current_needs_retry())) { + set_current_needs_retry(false); + if (ret == -EFAULT) { + if (!iov_iter_fault_in_readable(from, PAGE_SIZE)) + goto retry; + } + } out_uninit: gfs2_holder_uninit(gh); return ret; @@ -883,7 +916,8 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to) return ret; } ip = GFS2_I(iocb->ki_filp->f_mapping->host); - gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); + gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_OUTER, &gh); +retry: ret = gfs2_glock_nq(&gh); if (ret) goto out_uninit; @@ -891,6 +925,13 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to) if (ret > 0) written += ret; gfs2_glock_dq(&gh); + if (unlikely(current_needs_retry())) { + set_current_needs_retry(false); + if (ret == -EFAULT) { + if (!iov_iter_fault_in_writeable(to, PAGE_SIZE)) + goto retry; + } + } out_uninit: gfs2_holder_uninit(&gh); return written ? written : ret; @@ -902,9 +943,18 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb, struct iov_iter *fro struct inode *inode = file_inode(file); ssize_t ret; +retry: current->backing_dev_info = inode_to_bdi(inode); ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops); current->backing_dev_info = NULL; + if (unlikely(current_needs_retry())) { + set_current_needs_retry(false); + if (ret == -EFAULT) { + if (!iov_iter_fault_in_readable(from, PAGE_SIZE)) + goto retry; + } + } + return ret; }