From patchwork Thu Jul 5 03:33:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10507883 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 2209E60116 for ; Thu, 5 Jul 2018 03:44:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1094428D9A for ; Thu, 5 Jul 2018 03:44:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 04E3528DA2; Thu, 5 Jul 2018 03:44:05 +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 E1DE428D9A for ; Thu, 5 Jul 2018 03:44:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD6BD6B0007; Wed, 4 Jul 2018 23:44:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C867E6B0008; Wed, 4 Jul 2018 23:44:02 -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 B4EF86B000A; Wed, 4 Jul 2018 23:44:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f199.google.com (mail-pf0-f199.google.com [209.85.192.199]) by kanga.kvack.org (Postfix) with ESMTP id 6B97D6B0007 for ; Wed, 4 Jul 2018 23:44:02 -0400 (EDT) Received: by mail-pf0-f199.google.com with SMTP id a12-v6so3949358pfn.12 for ; Wed, 04 Jul 2018 20:44:02 -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=g1d/QMwzGIIN1CtPyo8YzFDwSDACF2TZrz3WDYRycug=; b=i/iXygzvCfSERUEly038CeLg16xwlVdccy78snqLnxlBMzU5anatvl1rU3qbhDy8cR tWkwNiosCGboYeXpWewCv+J7zmFaLWhkLAj98Wz6JdylVgLTRkNONb4vSkyWeB+7LzyY 6sLXROSOn35ZsRDbL9ReLBEO94nF3frUv982MCLkrLk3rcaz97qXJMOfc7Pf73Ft++51 h/m2LbTnBpv5olJqRNgfC6sDSjX00yf+Y7mQEKhUHrfO6kbo//AyVVBRLhg5JjuH0Quj ZIUZSPkRubnI1VGbd5GNZ+J72dN+BkR4GRHlbqxGZF0bR9NlGH25zPB/rTrtV5VFQiEJ 2ZXg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.20 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: APt69E3lYU/vt0W2Fc3haUAtLtIhSCK3kBrt/aiHxDxCASgsKjiyyqQe nMlFSn/EjUIAwD1L1uhQ7WhpSjGGq1y0XjV7Egd3QbPSHdT5SpRQDGbdzTiyOdLoZ5q0vFoW2AE kHNxXd32o42Bs9Wpug55KpTqhv3iX6MI+p8vnNOekuTBpoZu6kUF50nBOWTpibG9fwA== X-Received: by 2002:a65:608c:: with SMTP id t12-v6mr4068127pgu.159.1530762242073; Wed, 04 Jul 2018 20:44:02 -0700 (PDT) X-Google-Smtp-Source: AAOMgpf5bZmqjk+BsgY5JC/xmOuGLxl3wwguz1akAT9wxd9/wCXwZ+eyixlIEEBvVpbqmq/6n5pk X-Received: by 2002:a65:608c:: with SMTP id t12-v6mr4068081pgu.159.1530762240992; Wed, 04 Jul 2018 20:44:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530762240; cv=none; d=google.com; s=arc-20160816; b=fhg923JXE1zG9TntQ/ctt+jS5zX1PGgIqLgAOn752fti2RdtwoxXmkTluVLJpV4kho PF/IsjxRSO49lKCwxaxx1hayGFRs/f3WliK88myCR4FQPGOQ2vTEqlU/pzBj6dUKpYfB GQ5bIP8r3nsUS03rLEvE8dgzPLlidYb3gSH0Rn4AEJ7VrWdSmLMfReZkvSXJG5/DqaSC FEMS2CmtR6ARk9Aljo7HlLeCfKBXx7FvTylczkpCoiWogQ6O9P+LRfg1m5G08V/jN1T0 dynPwFs6Uke3Qe8Hv6LbNpm5adQntwJoAE9VEVhgFOyisRNg15zzi9qUhCsA67yfg+wb GFCg== 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=g1d/QMwzGIIN1CtPyo8YzFDwSDACF2TZrz3WDYRycug=; b=AeN5eTCpjGooIjK2lR+Qr7hNyXvJ+DUIv8cDMpfmtJ8C0ukY0eUcnk04nszg5scSv2 f6rZIzyjNZARrTru/gdxl8ItJXDB+TDQzLDTZ6pOoAFNKV/NVVx9eusuk2YtkfLj880C vP44JhI/qwJDxSZjqzjQbLtHRwU/w2rLOH8MTkPppkIOmTCEnKDZjYIAkWTqikHGujm2 pJshzEZh8o9EdO1AE3j+wJiD9PRBNTBcbdr6HedTws8hZDcNdMb4aDhgag5uO8gQhVeL H95D65oo3cIbBM69tUvZf05TWunzemGOLWSkFApSKH6oHgV2aJyrntQMZ9AL1n/tqU34 Xktw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga02.intel.com (mga02.intel.com. [134.134.136.20]) by mx.google.com with ESMTPS id q124-v6si5769984pfc.93.2018.07.04.20.44.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 20:44:00 -0700 (PDT) Received-SPF: pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.20 as permitted sender) client-ip=134.134.136.20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 134.134.136.20 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 fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jul 2018 20:44:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,310,1526367600"; d="scan'208";a="243088704" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by fmsmga006.fm.intel.com with ESMTP; 04 Jul 2018 20:43:50 -0700 Subject: [PATCH v6] filesystem-dax: Introduce dax_lock_mapping_entry() From: Dan Williams To: linux-nvdimm@lists.01.org Cc: jack@suse.cz, hch@lst.de, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Date: Wed, 04 Jul 2018 20:33:52 -0700 Message-ID: <153076161392.9314.16967170107658011515.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <153074046078.27838.5465590228767136915.stgit@dwillia2-desk3.amr.corp.intel.com> References: <153074046078.27838.5465590228767136915.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 --- Changes since v5, fixup kbuild robot reports. fs/dax.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++--- include/linux/dax.h | 13 ++++++ 2 files changed, 116 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..450b28db9533 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,17 @@ static inline int dax_writeback_mapping_range(struct address_space *mapping, { return -EOPNOTSUPP; } + +static inline bool dax_lock_mapping_entry(struct page *page) +{ + if (IS_DAX(page->mapping->host)) + return true; + return false; +} + +static inline void dax_unlock_mapping_entry(struct page *page) +{ +} #endif int dax_read_lock(void);