From patchwork Sun Jun 17 02:00:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 10468263 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 13704600CC for ; Sun, 17 Jun 2018 02:02:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 047B728C71 for ; Sun, 17 Jun 2018 02:02:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED40B28C75; Sun, 17 Jun 2018 02:02:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C94528C6A for ; Sun, 17 Jun 2018 02:02:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E73C6B0280; Sat, 16 Jun 2018 22:01:12 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 268F86B0281; Sat, 16 Jun 2018 22:01:12 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10E2B6B0282; Sat, 16 Jun 2018 22:01:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f70.google.com (mail-pl0-f70.google.com [209.85.160.70]) by kanga.kvack.org (Postfix) with ESMTP id B2D896B0280 for ; Sat, 16 Jun 2018 22:01:11 -0400 (EDT) Received: by mail-pl0-f70.google.com with SMTP id t19-v6so7721374plo.9 for ; Sat, 16 Jun 2018 19:01:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=EUefWlPtQqMMGRj8qCB7qo8Y+Ow2jmws/Yul8+rze/I=; b=tSidONMpKCNnditLvhb04HmDOCQgGJ5yc+5M/P/8fMUAIT9cHmLcSJ2n59URA95Kyl GwSh4ccPBZgkEXYPoh5KYnNE0TDnWoNZS5tMGL2s04ld7o5aITokK45aQqTRB/KlnfR1 aLPKdAScSqupooFXgOEnw/3aYfb7Q2TibZMUJ3aWT+GQ5BkmYSYK/sMZoEACIJP1lygv ZizzsmGOCPU4uJrud1fk1K+rVaYd6FQYDl9FhnZ6LJTZWuUEroMlVTf/R+WCVs3oDVCE DZwYZmSq8qH1K7zkPgZLbEQTnQPhmPby21EHVIEnXRCteNOK3VM4WR4KVZ1W/QxMzG4O 1n0w== X-Gm-Message-State: APt69E0YfGxYGOKnyGNQCOwKomjUA9jAIlCF5Dj2e0cW/WKBHqRlZ7vd VMrfcGMzEECR9pGLVvYqD4WcE6qO97RJq6zpa4qkFHil89iYKmLiP4d1oQ9o1GCzrpxkwLJmc8h CXpvWOD98sEO2t9dvER6NiPj7w9K2uSVGzKyVgGG7AUyMNzLNBcK67SgtVSxKIuvHVA== X-Received: by 2002:a17:902:2c83:: with SMTP id n3-v6mr8311031plb.211.1529200871418; Sat, 16 Jun 2018 19:01:11 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ3N+LnwzFWjItqWCIbJLjc9vVfskHiDsi12DttXHJjPFUrAnl9QdVECNckNQ8fGTrHBn+q X-Received: by 2002:a17:902:2c83:: with SMTP id n3-v6mr8310994plb.211.1529200870648; Sat, 16 Jun 2018 19:01:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529200870; cv=none; d=google.com; s=arc-20160816; b=uSr4ov8cbt+Qup2Tppb1Wp03mvNZKfn0/q5ooLxAhg0W6S1pssjrF3hHhrnUAQKSkE o9GYl+EHCfVH484y1WtcbBm48LgSyoq7619CK17dL2yxDYtRWXkVceiJWJcmI5exjIYE nZmuS9iu4CZNEeQTyZmsf71RRIZVKLg+wL2i37pgp6/u3X6ffS7FjdVFNMtLlDA0+Out LERpaIk6wKTGiyZ/RHlwVXf4acEQk+ALgsBfEtdbDFGn58bs0zKWBWzZ2VmXQCp7uVda fAxVBY+7cUTz7zVcRTmEVcGcNF3GaIqV5IlU/Ijn1Fa+F7xdUtFk6PwNHtupN4lhGELE V5uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=EUefWlPtQqMMGRj8qCB7qo8Y+Ow2jmws/Yul8+rze/I=; b=mGfbjzcRdbQvwPrramqRYMZIeg0YZ8CASX0cJSnVIUDmsgdQGQsjNXTMNKRq382Ao6 jAekuKXSWw2dddtbnP1me/9hB1GiqPCJV/sVyFWw8CDh68043npbafiFuhPTNihHvntO XHP/yZ/fv4EMQF0PjRz7BPM9QKiIIrQT4upvg7SXNaUlllTNrHHD94+7G5tuPv1sze36 PDJ0MKF6B7zZa8xlWhxaR7J+eNkl2rsFpGohRMkJgOOeHk3pwLEEBVcwc7ZeaGKMQR9W gwQEoeUVEKmDORlJeO4OnjDWKIJ8HtL4z/tVf3TKOY6LBmryTFLVrGP4vXz6FymwVXJf U9Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20170209 header.b="ubmAvRa/"; spf=pass (google.com: best guess record for domain of willy@infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom=willy@infradead.org Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id l26-v6si11552885pfe.299.2018.06.16.19.01.10 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 16 Jun 2018 19:01:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of willy@infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20170209 header.b="ubmAvRa/"; spf=pass (google.com: best guess record for domain of willy@infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom=willy@infradead.org 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=EUefWlPtQqMMGRj8qCB7qo8Y+Ow2jmws/Yul8+rze/I=; b=ubmAvRa/gSM066Y2eaNBAl66n 611KdyOjA+KprTOl7abAVHlueGLZsWCxfpyp98t5v6FvfebiiVz4aScCUmiHz4W5WgUujN+d04FHm QzEXp2mUWHlfo0gTL2AbYpw9kh2Qf0AFYN1k1Wxle+QgX9bOautnNBV79m/Kg4DZjZSfxsS6bhdKa YCYu1U+3Khh1oUSMEGdcp72OF9tymGgV+6jkFhJ8SjY+wEM8NN6yfYIWgvQBclawyE2JxzqQMx6vZ 1QzB1FYRkt4OC5gFQr81GIqCEY+ygu11V5s8uFIINtpAxyZiuvMVMOGrUfZoL+ZK9EEWpPb3gLBfu rpL7BRdzw==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1fUN09-0001OT-DY; Sun, 17 Jun 2018 02:01:09 +0000 From: Matthew Wilcox To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Jan Kara , Jeff Layton , Lukas Czerner , Ross Zwisler , Christoph Hellwig , Goldwyn Rodrigues , Nicholas Piggin , Ryusuke Konishi , linux-nilfs@vger.kernel.org, Jaegeuk Kim , Chao Yu , linux-f2fs-devel@lists.sourceforge.net Subject: [PATCH v14 50/74] shmem: Convert shmem_add_to_page_cache to XArray Date: Sat, 16 Jun 2018 19:00:28 -0700 Message-Id: <20180617020052.4759-51-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180617020052.4759-1-willy@infradead.org> References: <20180617020052.4759-1-willy@infradead.org> 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: X-Virus-Scanned: ClamAV using ClamSMTP This removes the last caller of radix_tree_maybe_preload_order(). Simpler code, unless we run out of memory for new xa_nodes partway through inserting entries into the xarray. Hopefully we can support multi-index entries in the page cache soon and all the awful code goes away. Signed-off-by: Matthew Wilcox --- mm/shmem.c | 87 ++++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 983a27656e2e..8e702b6d84a5 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -576,9 +576,10 @@ static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) */ static int shmem_add_to_page_cache(struct page *page, struct address_space *mapping, - pgoff_t index, void *expected) + pgoff_t index, void *expected, gfp_t gfp) { - int error, nr = hpage_nr_pages(page); + XA_STATE(xas, &mapping->i_pages, index); + unsigned long i, nr = 1UL << compound_order(page); VM_BUG_ON_PAGE(PageTail(page), page); VM_BUG_ON_PAGE(index != round_down(index, nr), page); @@ -587,49 +588,47 @@ static int shmem_add_to_page_cache(struct page *page, VM_BUG_ON(expected && PageTransHuge(page)); page_ref_add(page, nr); - page->mapping = mapping; page->index = index; + page->mapping = mapping; - xa_lock_irq(&mapping->i_pages); - if (PageTransHuge(page)) { - void __rcu **results; - pgoff_t idx; - int i; - - error = 0; - if (radix_tree_gang_lookup_slot(&mapping->i_pages, - &results, &idx, index, 1) && - idx < index + HPAGE_PMD_NR) { - error = -EEXIST; + do { + xas_lock_irq(&xas); + xas_create_range(&xas, index + nr - 1); + if (xas_error(&xas)) + goto unlock; + for (i = 0; i < nr; i++) { + void *entry = xas_load(&xas); + if (entry != expected) + xas_set_err(&xas, -ENOENT); + if (xas_error(&xas)) + goto undo; + xas_store(&xas, page + i); + xas_next(&xas); } - - if (!error) { - for (i = 0; i < HPAGE_PMD_NR; i++) { - error = radix_tree_insert(&mapping->i_pages, - index + i, page + i); - VM_BUG_ON(error); - } + if (PageTransHuge(page)) { count_vm_event(THP_FILE_ALLOC); + __inc_node_page_state(page, NR_SHMEM_THPS); } - } else if (!expected) { - error = radix_tree_insert(&mapping->i_pages, index, page); - } else { - error = shmem_replace_entry(mapping, index, expected, page); - } - - if (!error) { mapping->nrpages += nr; - if (PageTransHuge(page)) - __inc_node_page_state(page, NR_SHMEM_THPS); __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); - xa_unlock_irq(&mapping->i_pages); - } else { + goto unlock; +undo: + while (i-- > 0) { + xas_store(&xas, NULL); + xas_prev(&xas); + } +unlock: + xas_unlock_irq(&xas); + } while (xas_nomem(&xas, gfp)); + + if (xas_error(&xas)) { page->mapping = NULL; - xa_unlock_irq(&mapping->i_pages); page_ref_sub(page, nr); + return xas_error(&xas); } - return error; + + return 0; } /* @@ -1182,7 +1181,7 @@ static int shmem_unuse_inode(struct shmem_inode_info *info, */ if (!error) error = shmem_add_to_page_cache(*pagep, mapping, index, - radswap); + radswap, gfp); if (error != -ENOMEM) { /* * Truncation and eviction use free_swap_and_cache(), which @@ -1698,7 +1697,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, false); if (!error) { error = shmem_add_to_page_cache(page, mapping, index, - swp_to_radix_entry(swap)); + swp_to_radix_entry(swap), gfp); /* * We already confirmed swap under page lock, and make * no memory allocation here, so usually no possibility @@ -1804,13 +1803,8 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, PageTransHuge(page)); if (error) goto unacct; - error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK, - compound_order(page)); - if (!error) { - error = shmem_add_to_page_cache(page, mapping, hindex, - NULL); - radix_tree_preload_end(); - } + error = shmem_add_to_page_cache(page, mapping, hindex, + NULL, gfp & GFP_RECLAIM_MASK); if (error) { mem_cgroup_cancel_charge(page, memcg, PageTransHuge(page)); @@ -2277,11 +2271,8 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, if (ret) goto out_release; - ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); - if (!ret) { - ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL); - radix_tree_preload_end(); - } + ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL, + gfp & GFP_RECLAIM_MASK); if (ret) goto out_release_uncharge;