Received: by 10.223.185.116 with SMTP id b49csp4139278wrg; Mon, 19 Feb 2018 11:53:09 -0800 (PST) X-Google-Smtp-Source: AH8x227sYno+0uB7hSh7peUrz1XU2yHCY5adEtSR7z0pM7A6C2T0tgBobCMjIbt7nTho1YsIEekP X-Received: by 2002:a17:902:2803:: with SMTP id e3-v6mr11213996plb.238.1519069989824; Mon, 19 Feb 2018 11:53:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519069989; cv=none; d=google.com; s=arc-20160816; b=OnwPsRaVpCnqomMuCOYSn5RogXegBd+NPovxctJZCjz7ys8FuQPX/Ey//ol4097xem oEW3F0cbKI+s3f6izSxk1fJ5I3raqKLL+jIEpVonxDrVSxLCCGK+ObAT9K0EQ5sVSJJZ UnPMwjt/u6TJtnTKI1WjGd/ByI+dajo2sDWtyuMqlcyhOlgoan7mX/Htuc86A8fvADI5 ETEYDMr+LVJFIQ5hZZOIQlMt03PrHxy22zNQqrn1UyzFEWp+VIYY9w2AMeRKBnYcvr6w gdm6fRJEd88HFB+2zuM0aqyySIE3rLGX9yMekAycBpWRlCYmJ1G2d5G96QGN0fmd/5cD +etw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=thKXeXQ0NYBRqq5cZhoDHF7ET2DRJM+5etNrAM7dJIY=; b=Hs6yVizljI5RKIE/3lAwPkMIp1VgMU1+85tfNpvJWjZ+c36OlZxpX4t+dQ1aWSTFfr fYNCCaorkTuIyS4o/YAPH1y9LoOYP5N7Djn55VtIEea1PZlL2R24lSYhXYriI52ryEtz 5qvyYhqTCw+aqFzFNS1zQrDu2HdJZNoTlC52eqeXOO8dRFFjXMDk0UmjoFedFhAm+BOY ePbS7qEapfpAUXKbpt46Hz3t84ACsqHygMZj7LrHtGa4b8sv5MVqzQCSrV1tpG2v3OBN voglY0U1iXnNydcromSO/zmpgp8gs/vhsLdcrqz6ssb0liuyKLTkb57pABbU1xITvKq4 AqlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=egET3/Ec; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t24-v6si2757490plo.340.2018.02.19.11.52.55; Mon, 19 Feb 2018 11:53:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=egET3/Ec; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753842AbeBSTtg (ORCPT + 99 others); Mon, 19 Feb 2018 14:49:36 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:45074 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753819AbeBSTqb (ORCPT ); Mon, 19 Feb 2018 14:46:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=thKXeXQ0NYBRqq5cZhoDHF7ET2DRJM+5etNrAM7dJIY=; b=egET3/EcBryLsrL4JFqxgSod1 /mID8m4Ct3qFG9/HbRjz1bWZ/d7qVK6f7ctQ/IcPSER82FGlR64auLfbtiytIdY2bMgI6p34gXT6m oHQ/dU7BpriNJxvbAc/JrDhA7GKvsEFJ6W1Q5yDtv4KrB/12S9F6OKIID2UrOFBKAEpH1+TEFM3Jx t93SyGVNLO7P2e41E4EnGPX94YCc8o16CjMZieSD501YzXiQaWbgKb2UIQLZfuL3JYaR9koP/PIw8 nP2IwTGgOpx29+kY5FsVLAIN/BqCrQztiZo177aFCX7heIiNX9ToAw7oPnwPIU5Hl8YItwCnPDmVU XH89k2MKQ==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1enrOQ-0001xF-8R; Mon, 19 Feb 2018 19:46:30 +0000 From: Matthew Wilcox To: Andrew Morton Cc: Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v7 57/61] nilfs2: Convert to XArray Date: Mon, 19 Feb 2018 11:45:52 -0800 Message-Id: <20180219194556.6575-58-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180219194556.6575-1-willy@infradead.org> References: <20180219194556.6575-1-willy@infradead.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox I'm not 100% convinced that the rewrite of nilfs_copy_back_pages is correct, but it will at least have different bugs from the current version. Signed-off-by: Matthew Wilcox --- fs/nilfs2/btnode.c | 37 +++++++++++----------------- fs/nilfs2/page.c | 72 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/fs/nilfs2/btnode.c b/fs/nilfs2/btnode.c index 9e2a00207436..b5997e8c5441 100644 --- a/fs/nilfs2/btnode.c +++ b/fs/nilfs2/btnode.c @@ -177,42 +177,36 @@ int nilfs_btnode_prepare_change_key(struct address_space *btnc, ctxt->newbh = NULL; if (inode->i_blkbits == PAGE_SHIFT) { - lock_page(obh->b_page); - /* - * We cannot call radix_tree_preload for the kernels older - * than 2.6.23, because it is not exported for modules. - */ + void *entry; + struct page *opage = obh->b_page; + lock_page(opage); retry: - err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); - if (err) - goto failed_unlock; /* BUG_ON(oldkey != obh->b_page->index); */ - if (unlikely(oldkey != obh->b_page->index)) - NILFS_PAGE_BUG(obh->b_page, + if (unlikely(oldkey != opage->index)) + NILFS_PAGE_BUG(opage, "invalid oldkey %lld (newkey=%lld)", (unsigned long long)oldkey, (unsigned long long)newkey); - xa_lock_irq(&btnc->pages); - err = radix_tree_insert(&btnc->pages, newkey, obh->b_page); - xa_unlock_irq(&btnc->pages); + entry = xa_cmpxchg(&btnc->pages, newkey, NULL, opage, GFP_NOFS); /* * Note: page->index will not change to newkey until * nilfs_btnode_commit_change_key() will be called. * To protect the page in intermediate state, the page lock * is held. */ - radix_tree_preload_end(); - if (!err) + if (!entry) return 0; - else if (err != -EEXIST) + if (xa_is_err(entry)) { + err = xa_err(entry); goto failed_unlock; + } err = invalidate_inode_pages2_range(btnc, newkey, newkey); if (!err) goto retry; /* fallback to copy mode */ - unlock_page(obh->b_page); + unlock_page(opage); } nbh = nilfs_btnode_create_block(btnc, newkey); @@ -252,9 +246,8 @@ void nilfs_btnode_commit_change_key(struct address_space *btnc, mark_buffer_dirty(obh); xa_lock_irq(&btnc->pages); - radix_tree_delete(&btnc->pages, oldkey); - radix_tree_tag_set(&btnc->pages, newkey, - PAGECACHE_TAG_DIRTY); + __xa_erase(&btnc->pages, oldkey); + __xa_set_tag(&btnc->pages, newkey, PAGECACHE_TAG_DIRTY); xa_unlock_irq(&btnc->pages); opage->index = obh->b_blocknr = newkey; @@ -283,9 +276,7 @@ void nilfs_btnode_abort_change_key(struct address_space *btnc, return; if (nbh == NULL) { /* blocksize == pagesize */ - xa_lock_irq(&btnc->pages); - radix_tree_delete(&btnc->pages, newkey); - xa_unlock_irq(&btnc->pages); + xa_erase(&btnc->pages, newkey); unlock_page(ctxt->bh->b_page); } else brelse(nbh); diff --git a/fs/nilfs2/page.c b/fs/nilfs2/page.c index 1c6703efde9e..31d20f624971 100644 --- a/fs/nilfs2/page.c +++ b/fs/nilfs2/page.c @@ -304,10 +304,10 @@ int nilfs_copy_dirty_pages(struct address_space *dmap, void nilfs_copy_back_pages(struct address_space *dmap, struct address_space *smap) { + XA_STATE(xas, &dmap->pages, 0); struct pagevec pvec; unsigned int i, n; pgoff_t index = 0; - int err; pagevec_init(&pvec); repeat: @@ -317,43 +317,56 @@ void nilfs_copy_back_pages(struct address_space *dmap, for (i = 0; i < pagevec_count(&pvec); i++) { struct page *page = pvec.pages[i], *dpage; - pgoff_t offset = page->index; + xas_set(&xas, page->index); lock_page(page); - dpage = find_lock_page(dmap, offset); + do { + xas_lock_irq(&xas); + dpage = xas_create(&xas); + if (!xas_error(&xas)) + break; + xas_unlock_irq(&xas); + if (!xas_nomem(&xas, GFP_NOFS)) { + unlock_page(page); + /* + * Callers have a touching faith that this + * function cannot fail. Just leak the page. + * Other pages may be salvagable if the + * xarray doesn't need to allocate memory + * to store them. + */ + WARN_ON(1); + page->mapping = NULL; + put_page(page); + goto shadow_remove; + } + } while (1); + if (dpage) { - /* override existing page on the destination cache */ + get_page(dpage); + xas_unlock_irq(&xas); + lock_page(dpage); + /* override existing page in the destination cache */ WARN_ON(PageDirty(dpage)); nilfs_copy_page(dpage, page, 0); unlock_page(dpage); put_page(dpage); } else { - struct page *page2; - - /* move the page to the destination cache */ - xa_lock_irq(&smap->pages); - page2 = radix_tree_delete(&smap->pages, offset); - WARN_ON(page2 != page); - - smap->nrpages--; - xa_unlock_irq(&smap->pages); - - xa_lock_irq(&dmap->pages); - err = radix_tree_insert(&dmap->pages, offset, page); - if (unlikely(err < 0)) { - WARN_ON(err == -EEXIST); - page->mapping = NULL; - put_page(page); /* for cache */ - } else { - page->mapping = dmap; - dmap->nrpages++; - if (PageDirty(page)) - radix_tree_tag_set(&dmap->pages, - offset, - PAGECACHE_TAG_DIRTY); - } + xas_store(&xas, page); + page->mapping = dmap; + dmap->nrpages++; + if (PageDirty(page)) + xas_set_tag(&xas, PAGECACHE_TAG_DIRTY); xa_unlock_irq(&dmap->pages); } + +shadow_remove: + /* remove the page from the shadow cache */ + xa_lock_irq(&smap->pages); + WARN_ON(__xa_erase(&smap->pages, xas.xa_index) != page); + smap->nrpages--; + xa_unlock_irq(&smap->pages); + unlock_page(page); } pagevec_release(&pvec); @@ -476,8 +489,7 @@ int __nilfs_clear_page_dirty(struct page *page) if (mapping) { xa_lock_irq(&mapping->pages); if (test_bit(PG_dirty, &page->flags)) { - radix_tree_tag_clear(&mapping->pages, - page_index(page), + __xa_clear_tag(&mapping->pages, page_index(page), PAGECACHE_TAG_DIRTY); xa_unlock_irq(&mapping->pages); return clear_page_dirty_for_io(page); -- 2.16.1