From mboxrd@z Thu Jan 1 00:00:00 1970 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=-6.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, 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 DBCCFC433E1 for ; Fri, 3 Jul 2020 09:53:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 97EEB20723 for ; Fri, 3 Jul 2020 09:53:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DJo8Z0cV" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97EEB20723 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 10C308D0068; Fri, 3 Jul 2020 05:53:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C2248D0066; Fri, 3 Jul 2020 05:53:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB85F8D0068; Fri, 3 Jul 2020 05:53:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0118.hostedemail.com [216.40.44.118]) by kanga.kvack.org (Postfix) with ESMTP id C345E8D0066 for ; Fri, 3 Jul 2020 05:53:37 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8CE32A2CC for ; Fri, 3 Jul 2020 09:53:37 +0000 (UTC) X-FDA: 76996302474.02.worm10_310af6e26e91 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id 55798600014A7906 for ; Fri, 3 Jul 2020 09:53:37 +0000 (UTC) X-HE-Tag: worm10_310af6e26e91 X-Filterd-Recvd-Size: 8021 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by imf49.hostedemail.com (Postfix) with ESMTP for ; Fri, 3 Jul 2020 09:53:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593770016; 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=mT7+S/JZ0X5UTjCHTtZu+xMVOeg7qKFbo24bRgqj2g8=; b=DJo8Z0cVyyjco+5EDgF1G+hMnTIgYXZOOdwtM5WucqefPRSLOqFeIAkfghbF6udVWUyE8h V6vyr5qll59DiYGrMntlXL43my+Nz38hs+B1zIuhwi0uxlmiYjeEgsDJdzIO7b2A1haicr cw53ZAmVAqm6TlIPuBkZHyzjKi9KbH4= 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-68-E3iaSV3zOIiSfGk0c4lyhw-1; Fri, 03 Jul 2020 05:53:34 -0400 X-MC-Unique: E3iaSV3zOIiSfGk0c4lyhw-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 54630107ACCA; Fri, 3 Jul 2020 09:53:33 +0000 (UTC) Received: from max.home.com (unknown [10.40.192.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71F656111F; Fri, 3 Jul 2020 09:53:31 +0000 (UTC) From: Andreas Gruenbacher To: Linus Torvalds Cc: Matthew Wilcox , Dave Chinner , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andreas Gruenbacher Subject: [RFC v2 2/2] gfs2: Rework read and page fault locking Date: Fri, 3 Jul 2020 11:53:25 +0200 Message-Id: <20200703095325.1491832-3-agruenba@redhat.com> In-Reply-To: <20200703095325.1491832-1-agruenba@redhat.com> References: <20200703095325.1491832-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Rspamd-Queue-Id: 55798600014A7906 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 Content-Transfer-Encoding: quoted-printable 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: So far, gfs2 has taken the inode glocks inside the ->readpage and ->readahead address space operations. Since commit d4388340ae0b ("fs: convert mpage_readpages to mpage_readahead"), gfs2_readahead is passed the pages to read ahead locked. With that, the current holder of the inode glock may be trying to lock one of those pages while gfs2_readahead is trying to take the inode glock, resulting in a deadlock. Fix that by moving the lock taking to the higher-level ->read_iter file and ->fault vm operations. This also gets rid of an ugly lock inversion workaround in gfs2_readpage. The cache consistency model of filesystems like gfs2 is such that if data is found in the page cache, the data is up to date and can be used without taking any filesystem locks. If a page is not cached, filesystem locks must be taken before populating the page cache. To avoid taking the inode glock when the data is already cached, gfs2_file_read_iter first tries to read the data with the IOCB_NOIO flag set. If that fails, the inode glock is taken and the operation is retried with the IOCB_NOIO flag cleared. Signed-off-by: Andreas Gruenbacher --- fs/gfs2/aops.c | 45 +------------------------------------------ fs/gfs2/file.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c index 72c9560f4467..68cd700a2719 100644 --- a/fs/gfs2/aops.c +++ b/fs/gfs2/aops.c @@ -468,21 +468,10 @@ static int stuffed_readpage(struct gfs2_inode *ip, = struct page *page) } =20 =20 -/** - * __gfs2_readpage - readpage - * @file: The file to read a page for - * @page: The page to read - * - * This is the core of gfs2's readpage. It's used by the internal file - * reading code as in that case we already hold the glock. Also it's - * called by gfs2_readpage() once the required lock has been granted. - */ - static int __gfs2_readpage(void *file, struct page *page) { struct gfs2_inode *ip =3D GFS2_I(page->mapping->host); struct gfs2_sbd *sdp =3D GFS2_SB(page->mapping->host); - int error; =20 if (i_blocksize(page->mapping->host) =3D=3D PAGE_SIZE && @@ -505,36 +494,11 @@ static int __gfs2_readpage(void *file, struct page = *page) * gfs2_readpage - read a page of a file * @file: The file to read * @page: The page of the file - * - * This deals with the locking required. We have to unlock and - * relock the page in order to get the locking in the right - * order. */ =20 static int gfs2_readpage(struct file *file, struct page *page) { - struct address_space *mapping =3D page->mapping; - struct gfs2_inode *ip =3D GFS2_I(mapping->host); - struct gfs2_holder gh; - int error; - - unlock_page(page); - gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); - error =3D gfs2_glock_nq(&gh); - if (unlikely(error)) - goto out; - error =3D AOP_TRUNCATED_PAGE; - lock_page(page); - if (page->mapping =3D=3D mapping && !PageUptodate(page)) - error =3D __gfs2_readpage(file, page); - else - unlock_page(page); - gfs2_glock_dq(&gh); -out: - gfs2_holder_uninit(&gh); - if (error && error !=3D AOP_TRUNCATED_PAGE) - lock_page(page); - return error; + return __gfs2_readpage(file, page); } =20 /** @@ -598,16 +562,9 @@ static void gfs2_readahead(struct readahead_control = *rac) { struct inode *inode =3D rac->mapping->host; struct gfs2_inode *ip =3D GFS2_I(inode); - struct gfs2_holder gh; =20 - gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); - if (gfs2_glock_nq(&gh)) - goto out_uninit; if (!gfs2_is_stuffed(ip)) mpage_readahead(rac, gfs2_block_map); - gfs2_glock_dq(&gh); -out_uninit: - gfs2_holder_uninit(&gh); } =20 /** diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c index fe305e4bfd37..bebde537ac8c 100644 --- a/fs/gfs2/file.c +++ b/fs/gfs2/file.c @@ -558,8 +558,29 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault = *vmf) return block_page_mkwrite_return(ret); } =20 +static vm_fault_t gfs2_fault(struct vm_fault *vmf) +{ + struct inode *inode =3D file_inode(vmf->vma->vm_file); + struct gfs2_inode *ip =3D GFS2_I(inode); + struct gfs2_holder gh; + vm_fault_t ret; + int err; + + gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); + err =3D gfs2_glock_nq(&gh); + if (err) { + ret =3D block_page_mkwrite_return(err); + goto out_uninit; + } + ret =3D filemap_fault(vmf); + gfs2_glock_dq(&gh); +out_uninit: + gfs2_holder_uninit(&gh); + return ret; +} + static const struct vm_operations_struct gfs2_vm_ops =3D { - .fault =3D filemap_fault, + .fault =3D gfs2_fault, .map_pages =3D filemap_map_pages, .page_mkwrite =3D gfs2_page_mkwrite, }; @@ -824,6 +845,9 @@ static ssize_t gfs2_file_direct_write(struct kiocb *i= ocb, struct iov_iter *from) =20 static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *= to) { + struct gfs2_inode *ip; + struct gfs2_holder gh; + size_t written =3D 0; ssize_t ret; =20 if (iocb->ki_flags & IOCB_DIRECT) { @@ -832,7 +856,31 @@ static ssize_t gfs2_file_read_iter(struct kiocb *ioc= b, struct iov_iter *to) return ret; iocb->ki_flags &=3D ~IOCB_DIRECT; } - return generic_file_read_iter(iocb, to); + iocb->ki_flags |=3D IOCB_NOIO; + ret =3D generic_file_read_iter(iocb, to); + iocb->ki_flags &=3D ~IOCB_NOIO; + if (ret >=3D 0) { + if (!iov_iter_count(to)) + return ret; + written =3D ret; + } else { + if (ret !=3D -EAGAIN) + return ret; + if (iocb->ki_flags & IOCB_NOWAIT) + return ret; + } + ip =3D GFS2_I(iocb->ki_filp->f_mapping->host); + gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh); + ret =3D gfs2_glock_nq(&gh); + if (ret) + goto out_uninit; + ret =3D generic_file_read_iter(iocb, to); + if (ret > 0) + written +=3D ret; + gfs2_glock_dq(&gh); +out_uninit: + gfs2_holder_uninit(&gh); + return written ? written : ret; } =20 /** --=20 2.26.2