Received: by 10.213.65.68 with SMTP id h4csp355991imn; Tue, 13 Mar 2018 06:38:08 -0700 (PDT) X-Google-Smtp-Source: AG47ELtucVWgVnmreelT8v+fp6EvVTeWCSi0vJRVPjIOOIH0NVKNKGNFnHXb57rsRPp7zXurwWqj X-Received: by 2002:a17:902:20c9:: with SMTP id v9-v6mr630717plg.41.1520948288108; Tue, 13 Mar 2018 06:38:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520948288; cv=none; d=google.com; s=arc-20160816; b=lNsfD6HjjiGXd4DqwxU6cMg33gVfmd5qSWQy1l38RJPKnjDPfexF5fAFgZ4IhYUP3G Cs/6MLubXkm6MoxbYO0ROBeDvcN6PjOu2Vh098KFOB5sg5FRiFQCG3haT/nIZ9rQ0X2u yW4oFLzBZvizedzimZzIg6VhZ2uzrzLgF/d4ocYL7rIlSG6fcs2RO3luWAp1IUqSHTCq XgZ4q3MFQAbxYpN4x2HtzVKfT8MLAgx5UNTTd4j+nKAq7JxNhK9UUAgyb2sV65HhUAfk mPyBpSdbCiWpjzmKP4Y9jLEQaZKksDNwJbGaJpyo+HVq75rhatga62hH3+kP/K/zERup lsqw== 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=gyb9Qm4DfyPt3dYM4uU9XVaSLgvPmG8u7M2kgi/AW98=; b=e3/JcKX6YHfKMYQxEQdm0G1n/lCgsArtAKY+oE8brNF9mhLvlxDawaEI92OIgAOazz TdDSb/ukkpzmUkKNSm2nKVDWdrWI4SC021584fcg2+ebD5gatxOZ4JhSY08r+PTCNUtm +03TGE5qGKkCV2/kA+ChJ+cM1/SPoUyPVHhj7AdSvSF4GaLincGpq8qvJwkQYwPuDFAJ uryD6iUjUHkaoGxAUD3kP6Bbp4LXA2bsW2gXVbWaN47QVqAd1ymlaD92urVmd0CDRr42 ur9FPFLSM2fj3FogqJ01hTZrc4EdjHGMlskvhrwBz0YgVBCGDbKhTpkH4hETYGRwBNO5 dPkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Zb4sYGdC; 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 x7-v6si135919pln.666.2018.03.13.06.37.53; Tue, 13 Mar 2018 06:38:08 -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=Zb4sYGdC; 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 S932688AbeCMNg0 (ORCPT + 99 others); Tue, 13 Mar 2018 09:36:26 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:35546 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932651AbeCMN1F (ORCPT ); Tue, 13 Mar 2018 09:27:05 -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=gyb9Qm4DfyPt3dYM4uU9XVaSLgvPmG8u7M2kgi/AW98=; b=Zb4sYGdCFt1Rv32iFSYGmH28y mLnjtRY1YhyhXtN0RzcdX6kbUZ7TrgnEliu/RLCIcdUV7KCHSv3AmHF5JlIQlBos9qpvzimSyHb/0 cNqJtGMVBz/aGmi65nJ+JZvyA2aEoLaqVOuuN6l7tnkk9sBvyYkGEROFYQicin+1nD5Q57qy3KnOY lNrK33OlXSOigzfmhZywn27MD2MY7MACrRtrMQ/v1rH5q3omPWwlJxwQpYvo83Dksm/btwK40WDXb zpTXI9nXYmeXrHsKmhFQnUdNvCYvjcYg7mUcf2isAapMH8KIzU002D6M+88J7h3Dvt15Esai0Lx8X S5OFF6USw==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1evjxI-0004lI-0f; Tue, 13 Mar 2018 13:27:04 +0000 From: Matthew Wilcox To: Andrew Morton Cc: Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Ryusuke Konishi , linux-nilfs@vger.kernel.org Subject: [PATCH v9 50/61] memfd: Convert shmem_tag_pins to XArray Date: Tue, 13 Mar 2018 06:26:28 -0700 Message-Id: <20180313132639.17387-51-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180313132639.17387-1-willy@infradead.org> References: <20180313132639.17387-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 Simplify the locking by taking the spinlock while we walk the tree on the assumption that many acquires and releases of the lock will be worse than holding the lock for a (potentially) long time. We could replicate the same locking behaviour with the xarray, but would have to be careful that the xa_node wasn't RCU-freed under us before we took the lock. Signed-off-by: Matthew Wilcox --- mm/memfd.c | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index 4cf7401cb09c..3b299d72df78 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -21,7 +21,7 @@ #include /* - * We need a tag: a new tag would expand every radix_tree_node by 8 bytes, + * We need a tag: a new tag would expand every xa_node by 8 bytes, * so reuse a tag which we firmly believe is never set or cleared on shmem. */ #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE @@ -29,35 +29,28 @@ static void shmem_tag_pins(struct address_space *mapping) { - struct radix_tree_iter iter; - void __rcu **slot; - pgoff_t start; + XA_STATE(xas, &mapping->i_pages, 0); struct page *page; + unsigned int tagged = 0; lru_add_drain(); - start = 0; - rcu_read_lock(); - - radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) { - page = radix_tree_deref_slot(slot); - if (!page || radix_tree_exception(page)) { - if (radix_tree_deref_retry(page)) { - slot = radix_tree_iter_retry(&iter); - continue; - } - } else if (page_count(page) - page_mapcount(page) > 1) { - xa_lock_irq(&mapping->i_pages); - radix_tree_tag_set(&mapping->i_pages, iter.index, - SHMEM_TAG_PINNED); - xa_unlock_irq(&mapping->i_pages); - } - if (need_resched()) { - slot = radix_tree_iter_resume(slot, &iter); - cond_resched_rcu(); - } + xas_lock_irq(&xas); + xas_for_each(&xas, page, ULONG_MAX) { + if (xa_is_value(page)) + continue; + if (page_count(page) - page_mapcount(page) > 1) + xas_set_tag(&xas, SHMEM_TAG_PINNED); + + if (++tagged % XA_CHECK_SCHED) + continue; + + xas_pause(&xas); + xas_unlock_irq(&xas); + cond_resched(); + xas_lock_irq(&xas); } - rcu_read_unlock(); + xas_unlock_irq(&xas); } /* -- 2.16.1