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: 10455385 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 7B08660234 for ; Sat, 9 Jun 2018 00:01:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7857929486 for ; Sat, 9 Jun 2018 00:01:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 76BE5294BD; Sat, 9 Jun 2018 00:01:15 +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 EDAC629508 for ; Sat, 9 Jun 2018 00:01:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4535E6B0273; Fri, 8 Jun 2018 20:01:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3D8C46B0274; Fri, 8 Jun 2018 20:01:13 -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 27A526B0275; Fri, 8 Jun 2018 20:01:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f72.google.com (mail-pl0-f72.google.com [209.85.160.72]) by kanga.kvack.org (Postfix) with ESMTP id D5FB96B0273 for ; Fri, 8 Jun 2018 20:01:12 -0400 (EDT) Received: by mail-pl0-f72.google.com with SMTP id bf1-v6so8195600plb.2 for ; Fri, 08 Jun 2018 17:01:12 -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=V68iH3L4SpstzyddKXNEA8XqafNl+HRr48m19GFD3Po=; b=gR8xnhjz8A7Wp9epIGExb08Wgkvc6ebIJSaESVSjTUibVFSOqlIVA4is2HIy7bBxXZ rj6zBmrGnus4GquCznujFsZIcqesVk7Som3gA0XrvaQJxqGrPEpEGa6aPQwsM+VFIKAD 6PXqmj9YHwDx2XlUgVIiXCnF6JEmHN1K0Vsx93KbogdFmtrA6yqOcMSQ2KPzz8s9T0DJ FZXiFgl0LD/TbKb/bF2ChqQyGY4cXqNiJGjbd9ph/NdjzYtLLG1IaaQCav2rViN9SUaw 3at6+Sar8K3eoCz0LWDPoY82ZhmyFFDqCHA4tzQPiANpbHsmb/i2BQhRTUTypNBQkzns ZyGQ== 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: APt69E0nxEKp4nDuhU4y7Jmxgl40g8QpGAZogivX8175LfGuBNf9liih DQ3i2scx+DhKCA8R1/jrKNsoSUhGNoKyB5fFK6ClibS1mFlSYATA02TMf8PWDKaGFWzFXCh9hfC 12Tgz6q2gc4diffQdncUfOKzqGVvQGQrWgyzujmg56WTj+pIMb7OTx2tqfjLQ5CfrVA== X-Received: by 2002:a17:902:8347:: with SMTP id z7-v6mr8558690pln.290.1528502472568; Fri, 08 Jun 2018 17:01:12 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIPOUwCyqj63Kk+bCcFC4lYMjVxoGZnmryqnMyT4remHFmaLbTo/IdQ0WZGXnnhN58U80z9 X-Received: by 2002:a17:902:8347:: with SMTP id z7-v6mr8558637pln.290.1528502471736; Fri, 08 Jun 2018 17:01:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528502471; cv=none; d=google.com; s=arc-20160816; b=UFbQYC9XMrDSWdntPcSCZjRcwfnLFvzVFHKn/KOqU47cl9fsmm4HvOvJPyzy2JqURT o7Zo1pYTU2dfWM9ZFkyNERgT7795Ux88vRvjNZ//czB96uHYLfRFswoZsubB0+XgxrsW 9LkPA/R9BipmgBY/DE7nRrCedcniTAmFXkXgkOJNHA0kwy7Jc1tFbPG8Fe/iSz5omOVM yvUtiNtVlhVBVR8lUIhmA+Vc6SJYlr2BB1JI+UeQZ93eEhD7zzUrwKBbisSRFrMczHfG f9Ijlw4B1e4P+4iJgzCIEzncoNR4c+R3BIgiyUancHLFI6EUwOWlisUNV7ggfgAq/X+A lrFQ== 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=V68iH3L4SpstzyddKXNEA8XqafNl+HRr48m19GFD3Po=; b=D2wrRTD2QXkkVRl7Hr1bKRjLigwjqvpedabXd5dP4QMomaH7VYsXzsaIDOrOoEY6pt FdF+p1c3WU08EWYemVBNtItuFJ5hbv72bXlWajlzK8yUiw8eF66L87Z81/DvChf/NHTH aKCUIfkytPvHDQsLlBv4IptdxzcckUVxf6W0wAkzomSW8l7CVQCn/Z6gT+wP4seTFBe4 IAiXujNVinKGcF3zqlHAI2U80gJfeSmXH/3LUB4sWBxcQOu3HfUAjKVw4dmfYJNrD5mn QzbxibO6pzWPIIL3crE7NpJJMxe7QTuhRAwvSMS/jwM3CsxjaZnXzBS38LSZyfsXSlsD XATA== 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 k88-v6si30299796pfk.369.2018.06.08.17.01.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jun 2018 17:01:11 -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 orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.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 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_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);