Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4252029imm; Mon, 11 Jun 2018 09:16:02 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIOxqSvQgBPcM9zS5ekd3m4JB3+YDNDbDFpIljQgkjohTwyToxAvOjq9dD4O2j41h8r/kNN X-Received: by 2002:a17:902:b483:: with SMTP id y3-v6mr19530210plr.66.1528733762301; Mon, 11 Jun 2018 09:16:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528733762; cv=none; d=google.com; s=arc-20160816; b=lhZGEgDhCwfztjFezmmRkF/F23E5DlQnD4tQyss7Bp0Yj4HiWsPBDLzMSjOxdIFhV8 rknnG+h3UcGojy2RnYQlZ9KiOMnb/xeSIX8DjzavsigjgcvFPYsDuf8oQvT1OcvKEGS1 E0K8ED6k/zwEcIcNiATe95O77eJ9Jn5Wmt0vG5j3VXBr7XbDVEqfvVTiOls3od+zIkIH U+CQ5s2jLwhze/2h8Rtp5bUAvLAxc5utMpnYOK9NX123Djeko/maI0MvLVQGylkpiKhm lSv3D8egpbUSHzrp+jI2T58d4gT6PoM97r6HWOg8uduzdZBHgUl+6IWqy87vOEj019Yj w2Ww== 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=zyhH5+bVh3EAsMsHVZxgQbY7nD/2huoeYClE42xj+rQ=; b=scCevrr4A96MIiz8vcWcheal09Ino3DrOTfrR7RAW2TQAcTPXhLO+PltnnBsmxBgsX GOwuES0E4DsrNQ9mo5mm4l6RzTcJVdu2Sp8Fhe+j4aQVTbxWiQT4vsF7RMrUNJPdUrAK DpxdUghRwGDkmRsDlw3gNf46Tqdri7LfBk3SpaxFz/SsbIpATdufQo6PysSGAXe8Ktx/ Nl0Iq1vlvjjf5tgQWbt+o4eoK2QgMnZh5kx+vYSG7ARgyT9dtjwy2/gR7vKKa1kCvA+K G9EsW8izAGEhOgxuqQSATRK0FDdiTqLjpZ6abEoEFMP8qkSjea9NHfIiQJuHD9KvHnnl 2WDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=hN/yJqA7; 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 z23-v6si7373620pfe.296.2018.06.11.09.15.47; Mon, 11 Jun 2018 09:16:02 -0700 (PDT) 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=hN/yJqA7; 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 S934132AbeFKOSm (ORCPT + 99 others); Mon, 11 Jun 2018 10:18:42 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:45668 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933520AbeFKOG4 (ORCPT ); Mon, 11 Jun 2018 10:06:56 -0400 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=zyhH5+bVh3EAsMsHVZxgQbY7nD/2huoeYClE42xj+rQ=; b=hN/yJqA7082+vh18/gZhykbjF EUvxAlpaZUrJr546BQrmvwPPjaP92lyOTfiGd/cfSIzWbV4UzHdYVwzEr1q9sIYvuY4doxC/14fb7 witrHGPbQa8EP4fDzev2uBbdIbGn458R8zwCD+2/nqOsbQ12TMxk/Lv4NNLUdv3fn8/bSHEUSYzlK Rapc7nEH1t0UMkGVIvnYahj8pR8n9gIf6flMa63zCCoB4q9hgPbhrpiCn963prsMKttdFeYLLcXTC V9DzGBiNCS5zDLS2lRSAwe3womNHnI5zjHUiF4T50A2h6tqvrVp773nbHLSlsorDdF/9gtyc5FiKL JDRbgS95g==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1fSNTE-0004lB-0w; Mon, 11 Jun 2018 14:06:56 +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 v13 37/72] mm: Convert add_to_swap_cache to XArray Date: Mon, 11 Jun 2018 07:06:04 -0700 Message-Id: <20180611140639.17215-38-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180611140639.17215-1-willy@infradead.org> References: <20180611140639.17215-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 Combine __add_to_swap_cache and add_to_swap_cache into one function since there is no more need to preload. Signed-off-by: Matthew Wilcox --- mm/swap_state.c | 93 +++++++++++++++---------------------------------- 1 file changed, 29 insertions(+), 64 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index c6b3eab73fde..ac07db436c15 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -107,14 +107,15 @@ void show_swap_cache_info(void) } /* - * __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space, + * add_to_swap_cache resembles add_to_page_cache_locked on swapper_space, * but sets SwapCache flag and private instead of mapping and index. */ -int __add_to_swap_cache(struct page *page, swp_entry_t entry) +int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp) { - int error, i, nr = hpage_nr_pages(page); - struct address_space *address_space; + struct address_space *address_space = swap_address_space(entry); pgoff_t idx = swp_offset(entry); + XA_STATE(xas, &address_space->i_pages, idx); + unsigned long i, nr = 1UL << compound_order(page); VM_BUG_ON_PAGE(!PageLocked(page), page); VM_BUG_ON_PAGE(PageSwapCache(page), page); @@ -123,50 +124,30 @@ int __add_to_swap_cache(struct page *page, swp_entry_t entry) page_ref_add(page, nr); SetPageSwapCache(page); - address_space = swap_address_space(entry); - xa_lock_irq(&address_space->i_pages); - for (i = 0; i < nr; i++) { - set_page_private(page + i, entry.val + i); - error = radix_tree_insert(&address_space->i_pages, - idx + i, page + i); - if (unlikely(error)) - break; - } - if (likely(!error)) { + do { + xas_lock_irq(&xas); + xas_create_range(&xas, idx + nr - 1); + if (xas_error(&xas)) + goto unlock; + for (i = 0; i < nr; i++) { + VM_BUG_ON_PAGE(xas.xa_index != idx + i, page); + set_page_private(page + i, entry.val + i); + xas_store(&xas, page + i); + xas_next(&xas); + } address_space->nrpages += nr; __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); ADD_CACHE_INFO(add_total, nr); - } else { - /* - * Only the context which have set SWAP_HAS_CACHE flag - * would call add_to_swap_cache(). - * So add_to_swap_cache() doesn't returns -EEXIST. - */ - VM_BUG_ON(error == -EEXIST); - set_page_private(page + i, 0UL); - while (i--) { - radix_tree_delete(&address_space->i_pages, idx + i); - set_page_private(page + i, 0UL); - } - ClearPageSwapCache(page); - page_ref_sub(page, nr); - } - xa_unlock_irq(&address_space->i_pages); +unlock: + xas_unlock_irq(&xas); + } while (xas_nomem(&xas, gfp)); - return error; -} - - -int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask) -{ - int error; + if (!xas_error(&xas)) + return 0; - error = radix_tree_maybe_preload_order(gfp_mask, compound_order(page)); - if (!error) { - error = __add_to_swap_cache(page, entry); - radix_tree_preload_end(); - } - return error; + ClearPageSwapCache(page); + page_ref_sub(page, nr); + return xas_error(&xas); } /* @@ -217,7 +198,7 @@ int add_to_swap(struct page *page) return 0; /* - * Radix-tree node allocations from PF_MEMALLOC contexts could + * XArray node allocations from PF_MEMALLOC contexts could * completely exhaust the page allocator. __GFP_NOMEMALLOC * stops emergency reserves from being allocated. * @@ -229,7 +210,6 @@ int add_to_swap(struct page *page) */ err = add_to_swap_cache(page, entry, __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN); - /* -ENOMEM radix-tree allocation failure */ if (err) /* * add_to_swap_cache() doesn't return -EEXIST, so we can safely @@ -423,19 +403,11 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, break; /* Out of memory */ } - /* - * call radix_tree_preload() while we can wait. - */ - err = radix_tree_maybe_preload(gfp_mask & GFP_KERNEL); - if (err) - break; - /* * Swap entry may have been freed since our caller observed it. */ err = swapcache_prepare(entry); if (err == -EEXIST) { - radix_tree_preload_end(); /* * We might race against get_swap_page() and stumble * across a SWAP_HAS_CACHE swap_map entry whose page @@ -443,26 +415,19 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, */ cond_resched(); continue; - } - if (err) { /* swp entry is obsolete ? */ - radix_tree_preload_end(); + } else if (err) /* swp entry is obsolete ? */ break; - } - /* May fail (-ENOMEM) if radix-tree node allocation failed. */ + /* May fail (-ENOMEM) if XArray node allocation failed. */ __SetPageLocked(new_page); __SetPageSwapBacked(new_page); - err = __add_to_swap_cache(new_page, entry); + err = add_to_swap_cache(new_page, entry, gfp_mask & GFP_KERNEL); if (likely(!err)) { - radix_tree_preload_end(); - /* - * Initiate read into locked page and return. - */ + /* Initiate read into locked page */ lru_cache_add_anon(new_page); *new_page_allocated = true; return new_page; } - radix_tree_preload_end(); __ClearPageLocked(new_page); /* * add_to_swap_cache() doesn't return -EEXIST, so we can safely -- 2.17.1