From patchwork Wed Jul 4 21:41:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10507651 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 C07CC602BC for ; Wed, 4 Jul 2018 21:51:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6D6728CD3 for ; Wed, 4 Jul 2018 21:51:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AB36D28DAA; Wed, 4 Jul 2018 21:51:02 +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.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable 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 26FEA28CD3 for ; Wed, 4 Jul 2018 21:51:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6D4C6B026D; Wed, 4 Jul 2018 17:51:00 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D1AC16B026E; Wed, 4 Jul 2018 17:51:00 -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 C32096B026F; Wed, 4 Jul 2018 17:51:00 -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 8255F6B026D for ; Wed, 4 Jul 2018 17:51:00 -0400 (EDT) Received: by mail-pl0-f70.google.com with SMTP id p91-v6so455780plb.12 for ; Wed, 04 Jul 2018 14:51:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:subject:from :to:cc:date:message-id:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=cAKejaoDR4CjntIa8iOlw7GJwDCeogBRb3syWkOuY1A=; b=fEN3wAZIbd58AMumSO1abDijDFHP9vj633lAOGZRflApGCd/MFROzeCN7K/Dj96L1b rLTvrEK6SxD1GehkDY5lEK6w35h6FpyeojinJoUAvim8kuhcs6p3R8ormzP1URoa67Yk Q4GCc0a9VUlY8C+WsROeeHX5SKgqI2yAFjyw5SuVHle7ekp49NmPRlZBAslYT7H2NYXL XKLbh58HUjV1mL7pkLeuPIp8MJZnQ3ELYLrzyku5kgsdTnxrNHZOExjVczyiTMWHwX4b dyWhWxKYqpugKAZxyN/9+tPz/rkOnnVjBo7sS62HYi+8B5POV5/NJbn/78DfXPTUAGUw 3gvw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: APt69E3FyZKXxDSKSpFJMqyqwNHgNxS024bl4IEkkZtLtOhuMtL5VFku lGT7Ac06n1HwjIp9IRnsvSUlaB1JqpHHTRksutAdf8CXA3MvDqhu4CKmJcbrieKw1MrlQPp4dKi kvPPgH6zB5SLPEk3lr7/dtA63+4dAf+I8Ip9kSYkf2inzjxAKcV4F825FpbHw3CbCDA== X-Received: by 2002:a63:4a07:: with SMTP id x7-v6mr3307811pga.34.1530741060194; Wed, 04 Jul 2018 14:51:00 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfoypZpZeve/v3DSyHEmGxDTP70MD2EJ596F/8/xqqH0xfgV+gbPrcxeWWMFvA8PDy6ZT41 X-Received: by 2002:a63:4a07:: with SMTP id x7-v6mr3307786pga.34.1530741059255; Wed, 04 Jul 2018 14:50:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530741059; cv=none; d=google.com; s=arc-20160816; b=CswJkxZU0ZZif7ndb4dTmceyXkuP0Ht8CSduzPukQAKm6/ZCq1QQaD2FeQPNjTsRDn 0RnpbjcXr4JU8sUeW1R7PuwHM6WNzK/Z0uLnG2s+yDBDf81N1loeg26aZiytpxj5sMOr aXnnXZc/++99s63cTcZq4B4htakMm4tNSkWhpErVWHFmxb3zOBJkxM0y4zURsSa5hX8Y WSxN01Wgvwh+InHNCA97MldUVUdyNsWIpZ4Fnkcbnnm7nCqcPOYGWRmNyS70tS33yS2P WR6Gf25kVmbtEVOwfCnuTBdQukM14sU9l1ynKK6XJG2LS4XaBbNNup7lJAY65PbPVgxm y1hA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject :arc-authentication-results; bh=cAKejaoDR4CjntIa8iOlw7GJwDCeogBRb3syWkOuY1A=; b=fBCWlcwDAMi/rK4cT6Plt4NuYmBgdLcET2X6ziSPCyV4g7JhT29BViNbCCaxxvtt4w EXD/lZ7A+1j4HbNvxXzdoQYPe+wD1aiUcIHBds0tZ4tM7NBdwqnQb5klv0L0sOqC1hl0 aim8Lusrw8PKA1BDpLUWC5VHh9EFLjMNYz7w0iwJeATOrd6yeBT7X8AOTD4PAbyQ4dgp ceCJJXyf5HGCFFAqYJc80McaZgT2lOGsunC+R7NkfJ2OJ5UpSrLGurCCvsGkwBHhrkeH c9WSQolnWvcG0bgjBfN+s8bfhy+LPiFp2gYf+N9TBNpQ/ziXebYSkg1o4Dg0djiwoz1w k6oQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga18.intel.com (mga18.intel.com. [134.134.136.126]) by mx.google.com with ESMTPS id l33-v6si4423827pld.514.2018.07.04.14.50.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 14:50:59 -0700 (PDT) Received-SPF: pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.126 as permitted sender) client-ip=134.134.136.126; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jul 2018 14:50:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,309,1526367600"; d="scan'208";a="242691571" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by fmsmga005.fm.intel.com with ESMTP; 04 Jul 2018 14:50:58 -0700 Subject: [PATCH v5 07/11] filesystem-dax: Introduce dax_lock_mapping_entry() From: Dan Williams To: linux-nvdimm@lists.01.org Cc: hch@lst.de, hch@lst.de, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, jack@suse.cz, ross.zwisler@linux.intel.com Date: Wed, 04 Jul 2018 14:41:00 -0700 Message-ID: <153074046078.27838.5465590228767136915.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <153074042316.27838.17319837331947007626.stgit@dwillia2-desk3.amr.corp.intel.com> References: <153074042316.27838.17319837331947007626.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 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 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_mapping_entry() 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 | 109 ++++++++++++++++++++++++++++++++++++++++++++++++--- include/linux/dax.h | 24 +++++++++++ 2 files changed, 127 insertions(+), 6 deletions(-) diff --git a/fs/dax.c b/fs/dax.c index 4de11ed463ce..57ec272038da 100644 --- a/fs/dax.c +++ b/fs/dax.c @@ -226,8 +226,8 @@ static inline void *unlock_slot(struct address_space *mapping, void **slot) * * Must be called with the i_pages lock held. */ -static void *get_unlocked_mapping_entry(struct address_space *mapping, - pgoff_t index, void ***slotp) +static void *__get_unlocked_mapping_entry(struct address_space *mapping, + pgoff_t index, void ***slotp, bool (*wait_fn)(void)) { void *entry, **slot; struct wait_exceptional_entry_queue ewait; @@ -237,6 +237,8 @@ static void *get_unlocked_mapping_entry(struct address_space *mapping, ewait.wait.func = wake_exceptional_entry_func; for (;;) { + bool revalidate; + entry = __radix_tree_lookup(&mapping->i_pages, index, NULL, &slot); if (!entry || @@ -251,14 +253,31 @@ static void *get_unlocked_mapping_entry(struct address_space *mapping, prepare_to_wait_exclusive(wq, &ewait.wait, TASK_UNINTERRUPTIBLE); xa_unlock_irq(&mapping->i_pages); - schedule(); + revalidate = wait_fn(); finish_wait(wq, &ewait.wait); xa_lock_irq(&mapping->i_pages); + if (revalidate) + return ERR_PTR(-EAGAIN); } } -static void dax_unlock_mapping_entry(struct address_space *mapping, - pgoff_t index) +static bool entry_wait(void) +{ + schedule(); + /* + * Never return an ERR_PTR() from + * __get_unlocked_mapping_entry(), just keep looping. + */ + return false; +} + +static void *get_unlocked_mapping_entry(struct address_space *mapping, + pgoff_t index, void ***slotp) +{ + return __get_unlocked_mapping_entry(mapping, index, slotp, entry_wait); +} + +static void unlock_mapping_entry(struct address_space *mapping, pgoff_t index) { void *entry, **slot; @@ -277,7 +296,7 @@ static void dax_unlock_mapping_entry(struct address_space *mapping, static void put_locked_mapping_entry(struct address_space *mapping, pgoff_t index) { - dax_unlock_mapping_entry(mapping, index); + unlock_mapping_entry(mapping, index); } /* @@ -374,6 +393,84 @@ static struct page *dax_busy_page(void *entry) return NULL; } +static bool entry_wait_revalidate(void) +{ + rcu_read_unlock(); + schedule(); + rcu_read_lock(); + + /* + * Tell __get_unlocked_mapping_entry() to take a break, we need + * to revalidate page->mapping after dropping locks + */ + return true; +} + +bool dax_lock_mapping_entry(struct page *page) +{ + pgoff_t index; + struct inode *inode; + bool did_lock = false; + void *entry = NULL, **slot; + struct address_space *mapping; + + rcu_read_lock(); + for (;;) { + mapping = READ_ONCE(page->mapping); + + if (!dax_mapping(mapping)) + break; + + /* + * In the device-dax case there's no need to lock, a + * struct dev_pagemap pin is sufficient to keep the + * inode alive, and we assume we have dev_pagemap pin + * otherwise we would not have a valid pfn_to_page() + * translation. + */ + inode = mapping->host; + if (S_ISCHR(inode->i_mode)) { + did_lock = true; + break; + } + + xa_lock_irq(&mapping->i_pages); + if (mapping != page->mapping) { + xa_unlock_irq(&mapping->i_pages); + continue; + } + index = page->index; + + entry = __get_unlocked_mapping_entry(mapping, index, &slot, + entry_wait_revalidate); + if (!entry) { + xa_unlock_irq(&mapping->i_pages); + break; + } else if (IS_ERR(entry)) { + WARN_ON_ONCE(PTR_ERR(entry) != -EAGAIN); + continue; + } + lock_slot(mapping, slot); + did_lock = true; + xa_unlock_irq(&mapping->i_pages); + break; + } + rcu_read_unlock(); + + return did_lock; +} + +void dax_unlock_mapping_entry(struct page *page) +{ + struct address_space *mapping = page->mapping; + struct inode *inode = mapping->host; + + if (S_ISCHR(inode->i_mode)) + return; + + 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 deb0f663252f..82b9856faf7f 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -88,6 +88,8 @@ int dax_writeback_mapping_range(struct address_space *mapping, struct block_device *bdev, struct writeback_control *wbc); struct page *dax_layout_busy_page(struct address_space *mapping); +bool dax_lock_mapping_entry(struct page *page); +void dax_unlock_mapping_entry(struct page *page); #else static inline bool bdev_dax_supported(struct block_device *bdev, int blocksize) @@ -119,6 +121,28 @@ static inline int dax_writeback_mapping_range(struct address_space *mapping, { return -EOPNOTSUPP; } + + +static inline bool dax_lock_mapping_entry(struct page *page) +{ + struct page *page = pfn_to_page(pfn); + + if (IS_DAX(page->mapping->host)) + return true; + return false; +} + +void dax_unlock_mapping_entry(struct page *page) +{ +} + +static inline struct page *dax_lock_page(unsigned long pfn) +{ +} + +static inline void dax_unlock_page(struct page *page) +{ +} #endif int dax_read_lock(void);