From patchwork Fri Jun 8 23:51:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10455383 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 29F246053F for ; Sat, 9 Jun 2018 00:01:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 25FB0294A1 for ; Sat, 9 Jun 2018 00:01:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1AD12294D1; Sat, 9 Jun 2018 00:01:14 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9965229505 for ; Sat, 9 Jun 2018 00:01:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753249AbeFIABM (ORCPT ); Fri, 8 Jun 2018 20:01:12 -0400 Received: from mga17.intel.com ([192.55.52.151]:41866 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753224AbeFIABM (ORCPT ); Fri, 8 Jun 2018 20:01:12 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Jun 2018 17:01:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,492,1520924400"; d="scan'208";a="63047249" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga001.jf.intel.com with ESMTP; 08 Jun 2018 17:01:11 -0700 Subject: [PATCH v4 10/12] filesystem-dax: Introduce dax_lock_page() From: Dan Williams To: linux-nvdimm@lists.01.org Cc: hch@lst.de, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, jack@suse.cz Date: Fri, 08 Jun 2018 16:51:14 -0700 Message-ID: <152850187437.38390.2257981090761438811.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <152850182079.38390.8280340535691965744.stgit@dwillia2-desk3.amr.corp.intel.com> References: <152850182079.38390.8280340535691965744.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In preparation for implementing support for memory poison (media error) handling via dax mappings, implement a lock_page() equivalent. Poison error handling requires rmap and needs guarantees that the page->mapping association is maintained / valid (inode not freed) for the duration of the lookup. In the device-dax case it is sufficient to simply hold a dev_pagemap reference. In the filesystem-dax case we need to use the entry lock. Export the entry lock via dax_lock_page() that uses rcu_read_lock() to protect against the inode being freed, and revalidates the page->mapping association under xa_lock(). Signed-off-by: Dan Williams --- fs/dax.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/dax.h | 15 ++++++++++ 2 files changed, 91 insertions(+) diff --git a/fs/dax.c b/fs/dax.c index cccf6cad1a7a..b7e71b108fcf 100644 --- a/fs/dax.c +++ b/fs/dax.c @@ -361,6 +361,82 @@ static void dax_disassociate_entry(void *entry, struct address_space *mapping, } } +struct page *dax_lock_page(unsigned long pfn) +{ + pgoff_t index; + struct inode *inode; + wait_queue_head_t *wq; + void *entry = NULL, **slot; + struct address_space *mapping; + struct wait_exceptional_entry_queue ewait; + struct page *ret = NULL, *page = pfn_to_page(pfn); + + rcu_read_lock(); + for (;;) { + mapping = READ_ONCE(page->mapping); + + if (!mapping || !IS_DAX(mapping->host)) + break; + + /* + * In the device-dax case there's no need to lock, a + * struct dev_pagemap pin is sufficient to keep the + * inode alive. + */ + inode = mapping->host; + if (S_ISCHR(inode->i_mode)) { + ret = page; + break; + } + + xa_lock_irq(&mapping->i_pages); + if (mapping != page->mapping) { + xa_unlock_irq(&mapping->i_pages); + continue; + } + index = page->index; + + init_wait(&ewait.wait); + ewait.wait.func = wake_exceptional_entry_func; + + entry = __radix_tree_lookup(&mapping->i_pages, index, NULL, + &slot); + if (!entry || + WARN_ON_ONCE(!radix_tree_exceptional_entry(entry))) { + xa_unlock_irq(&mapping->i_pages); + break; + } else if (!slot_locked(mapping, slot)) { + lock_slot(mapping, slot); + ret = page; + xa_unlock_irq(&mapping->i_pages); + break; + } + + wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key); + prepare_to_wait_exclusive(wq, &ewait.wait, + TASK_UNINTERRUPTIBLE); + xa_unlock_irq(&mapping->i_pages); + rcu_read_unlock(); + schedule(); + finish_wait(wq, &ewait.wait); + rcu_read_lock(); + } + rcu_read_unlock(); + + return page; +} + +void dax_unlock_page(struct page *page) +{ + struct address_space *mapping = page->mapping; + struct inode *inode = mapping->host; + + if (S_ISCHR(inode->i_mode)) + return; + + dax_unlock_mapping_entry(mapping, page->index); +} + /* * Find radix tree entry at given index. If it points to an exceptional entry, * return it with the radix tree entry locked. If the radix tree doesn't diff --git a/include/linux/dax.h b/include/linux/dax.h index f9eb22ad341e..641cab7e1fa7 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -83,6 +83,8 @@ static inline void fs_put_dax(struct dax_device *dax_dev) struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev); int dax_writeback_mapping_range(struct address_space *mapping, struct block_device *bdev, struct writeback_control *wbc); +struct page *dax_lock_page(unsigned long pfn); +void dax_unlock_page(struct page *page); #else static inline int bdev_dax_supported(struct super_block *sb, int blocksize) { @@ -108,6 +110,19 @@ static inline int dax_writeback_mapping_range(struct address_space *mapping, { return -EOPNOTSUPP; } + +static inline struct page *dax_lock_page(unsigned long pfn) +{ + struct page *page = pfn_to_page(pfn); + + if (IS_DAX(page->mapping->host)) + return page; + return NULL; +} + +static inline void dax_unlock_page(struct page *page) +{ +} #endif int dax_read_lock(void);