mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: ira.weiny@intel.com
To: Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Dan Williams <dan.j.williams@intel.com>
Cc: Ira Weiny <ira.weiny@intel.com>,
	Fenghua Yu <fenghua.yu@intel.com>,
	Rick Edgecombe <rick.p.edgecombe@intel.com>,
	"Shankar, Ravi V" <ravi.v.shankar@intel.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH V10 34/44] dax: Stray access protection for dax_direct_access()
Date: Tue, 19 Apr 2022 10:06:39 -0700	[thread overview]
Message-ID: <20220419170649.1022246-35-ira.weiny@intel.com> (raw)
In-Reply-To: <20220419170649.1022246-1-ira.weiny@intel.com>

From: Ira Weiny <ira.weiny@intel.com>

dax_direct_access() provides a way to obtain the direct map address of
PMEM memory.  With the new devmap protections the use of this address
needs to be bracketed by calls to enable and disable protection of
those pages.  These calls only need to be used to guard actual access to
the memory.  Other uses of dax_direct_access() do not need to use these
guards.

Introduce 2 new calls dax_set_readwrite() and dax_set_noaccess().
Bracket all uses of the address returned by dax_direct_access() with
those calls.

For consumers who require a permanent address to the dax device, such as
the DM write cache, dax_map_protected() is used to query for additional
protections.

Update the DM write cache code to create a permanent mapping if
dax_map_protected() is true.

Cc: Jane Chu <jane.chu@oracle.com>
Signed-off-by: Ira Weiny <ira.weiny@intel.com>

---
Changes for V9
	Do not add a new dax operation.  Instead teach struct dax_device
		about the dev_pagemap PGMAP_PROTECTION flag and call the
		ops directly if needed.
	s/dax_mk_*/dax_set_*/

Changes for V8
	Rebase changes on 5.17-rc1
	Clean up the cover letter
		dax_read_lock() is not required
		s/dax_protected()/dax_map_protected()/
	Testing revealed a dax_flush() which was not properly protected.

Changes for V7
	Rework cover letter.
	Do not include a FS_DAX_LIMITED restriction for dcss.  It  will
		simply not implement the protection and there is no need
		to special case this.
		Clean up commit message because I did not originally
		understand the nuance of the s390 device.
	Introduce dax_{protected,mk_readwrite,mk_noaccess}()
	From Dan Williams
		Remove old clean up cruft from previous versions
		Remove map_protected
	Remove 'global' parameters all calls
---
 drivers/dax/super.c        | 60 ++++++++++++++++++++++++++++++++++++++
 drivers/md/dm-writecache.c |  8 ++++-
 fs/dax.c                   |  8 +++++
 fs/fuse/virtio_fs.c        |  2 ++
 include/linux/dax.h        |  5 ++++
 5 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/drivers/dax/super.c b/drivers/dax/super.c
index 0211e6f7b47a..3105794f55f7 100644
--- a/drivers/dax/super.c
+++ b/drivers/dax/super.c
@@ -13,6 +13,7 @@
 #include <linux/uio.h>
 #include <linux/dax.h>
 #include <linux/fs.h>
+#include <linux/memremap.h>
 #include "dax-private.h"
 
 /**
@@ -29,6 +30,7 @@ struct dax_device {
 	void *private;
 	unsigned long flags;
 	const struct dax_operations *ops;
+	struct dev_pagemap *pgmap;
 };
 
 static dev_t dax_devt;
@@ -118,6 +120,8 @@ enum dax_device_flags {
  * @pgoff: offset in pages from the start of the device to translate
  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
  * @kaddr: output parameter that returns a virtual address mapping of pfn
+ *         Direct access through this pointer must be guarded by calls to
+ *         dax_set_{readwrite,noaccess}()
  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
  *
  * Return: negative errno if an error occurs, otherwise the number of
@@ -210,6 +214,56 @@ void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
 #endif
 EXPORT_SYMBOL_GPL(dax_flush);
 
+bool dax_map_protected(struct dax_device *dax_dev)
+{
+	struct dev_pagemap *pgmap = dax_dev->pgmap;
+
+	if (!dax_alive(dax_dev))
+		return false;
+
+	return pgmap && (pgmap->flags & PGMAP_PROTECTION);
+}
+EXPORT_SYMBOL_GPL(dax_map_protected);
+
+/**
+ * dax_set_readwrite() - make protected dax devices read/write
+ * @dax_dev: the dax device representing the memory to access
+ *
+ * Any access of the kaddr memory returned from dax_direct_access() must be
+ * guarded by dax_set_readwrite() and dax_set_noaccess().  This ensures that any
+ * dax devices which have additional protections are allowed to relax those
+ * protections for the thread using this memory.
+ *
+ * NOTE these calls must be contained within a single thread of execution and
+ * both must be guarded by dax_read_lock()  Which is also a requirement for
+ * dax_direct_access() anyway.
+ */
+void dax_set_readwrite(struct dax_device *dax_dev)
+{
+	if (!dax_map_protected(dax_dev))
+		return;
+
+	__pgmap_set_readwrite(dax_dev->pgmap);
+}
+EXPORT_SYMBOL_GPL(dax_set_readwrite);
+
+/**
+ * dax_set_noaccess() - restore protection to dax devices if needed
+ * @dax_dev: the dax device representing the memory to access
+ *
+ * See dax_direct_access() and dax_set_readwrite()
+ *
+ * NOTE Must be called prior to dax_read_unlock()
+ */
+void dax_set_noaccess(struct dax_device *dax_dev)
+{
+	if (!dax_map_protected(dax_dev))
+		return;
+
+	__pgmap_set_noaccess(dax_dev->pgmap);
+}
+EXPORT_SYMBOL_GPL(dax_set_noaccess);
+
 void dax_write_cache(struct dax_device *dax_dev, bool wc)
 {
 	if (wc)
@@ -249,6 +303,12 @@ void set_dax_nomc(struct dax_device *dax_dev)
 }
 EXPORT_SYMBOL_GPL(set_dax_nomc);
 
+void set_dax_pgmap(struct dax_device *dax_dev, struct dev_pagemap *pgmap)
+{
+	dax_dev->pgmap = pgmap;
+}
+EXPORT_SYMBOL_GPL(set_dax_pgmap);
+
 bool dax_alive(struct dax_device *dax_dev)
 {
 	lockdep_assert_held(&dax_srcu);
diff --git a/drivers/md/dm-writecache.c b/drivers/md/dm-writecache.c
index 5630b470ba42..8fd26a237de3 100644
--- a/drivers/md/dm-writecache.c
+++ b/drivers/md/dm-writecache.c
@@ -297,7 +297,13 @@ static int persistent_memory_claim(struct dm_writecache *wc)
 		r = -EOPNOTSUPP;
 		goto err2;
 	}
-	if (da != p) {
+
+	/*
+	 * Force the write cache to map the pages directly if the dax device
+	 * mapping is protected or if the number of pages returned was not what
+	 * was requested.
+	 */
+	if (dax_map_protected(wc->ssd_dev->dax_dev) || da != p) {
 		long i;
 		wc->memory_map = NULL;
 		pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
diff --git a/fs/dax.c b/fs/dax.c
index 67a08a32fccb..7cc76c6752ae 100644
--- a/fs/dax.c
+++ b/fs/dax.c
@@ -727,7 +727,9 @@ static int copy_cow_page_dax(struct vm_fault *vmf, const struct iomap_iter *iter
 		return rc;
 	}
 	vto = kmap_atomic(vmf->cow_page);
+	dax_set_readwrite(iter->iomap.dax_dev);
 	copy_user_page(vto, kaddr, vmf->address, vmf->cow_page);
+	dax_set_noaccess(iter->iomap.dax_dev);
 	kunmap_atomic(vto);
 	dax_read_unlock(id);
 	return 0;
@@ -936,8 +938,10 @@ static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
 	count = 1UL << dax_entry_order(entry);
 	index = xas->xa_index & ~(count - 1);
 
+	dax_set_readwrite(dax_dev);
 	dax_entry_mkclean(mapping, index, pfn);
 	dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE);
+	dax_set_noaccess(dax_dev);
 	/*
 	 * After we have flushed the cache, we can clear the dirty tag. There
 	 * cannot be new dirty data in the pfn after the flush has completed as
@@ -1124,8 +1128,10 @@ static int dax_memzero(struct dax_device *dax_dev, pgoff_t pgoff,
 
 	ret = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
 	if (ret > 0) {
+		dax_set_readwrite(dax_dev);
 		memset(kaddr + offset, 0, size);
 		dax_flush(dax_dev, kaddr + offset, size);
+		dax_set_noaccess(dax_dev);
 	}
 	return ret;
 }
@@ -1259,12 +1265,14 @@ static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 		if (map_len > end - pos)
 			map_len = end - pos;
 
+		dax_set_readwrite(dax_dev);
 		if (iov_iter_rw(iter) == WRITE)
 			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
 					map_len, iter);
 		else
 			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
 					map_len, iter);
+		dax_set_noaccess(dax_dev);
 
 		pos += xfer;
 		length -= xfer;
diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index 86b7dbb6a0d4..58bb949dcdfc 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -775,8 +775,10 @@ static int virtio_fs_zero_page_range(struct dax_device *dax_dev,
 	rc = dax_direct_access(dax_dev, pgoff, nr_pages, &kaddr, NULL);
 	if (rc < 0)
 		return rc;
+	dax_set_readwrite(dax_dev);
 	memset(kaddr, 0, nr_pages << PAGE_SHIFT);
 	dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
+	dax_set_noaccess(dax_dev);
 	return 0;
 }
 
diff --git a/include/linux/dax.h b/include/linux/dax.h
index 9fc5f99a0ae2..30fe49f9ec9d 100644
--- a/include/linux/dax.h
+++ b/include/linux/dax.h
@@ -91,6 +91,7 @@ static inline bool daxdev_mapping_supported(struct vm_area_struct *vma,
 
 void set_dax_nocache(struct dax_device *dax_dev);
 void set_dax_nomc(struct dax_device *dax_dev);
+void set_dax_pgmap(struct dax_device *dax_dev, struct dev_pagemap *pgmap);
 
 struct writeback_control;
 #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
@@ -187,6 +188,10 @@ int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
 			size_t nr_pages);
 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size);
 
+bool dax_map_protected(struct dax_device *dax_dev);
+void dax_set_readwrite(struct dax_device *dax_dev);
+void dax_set_noaccess(struct dax_device *dax_dev);
+
 ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops);
 vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
-- 
2.35.1


  parent reply	other threads:[~2022-04-19 17:08 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-19 17:06 [PATCH V10 00/44] PKS/PMEM: Add Stray Write Protection ira.weiny
2022-04-19 17:06 ` [PATCH V10 01/44] Documentation/protection-keys: Clean up documentation for User Space pkeys ira.weiny
2022-06-07 23:09   ` [tip: x86/mm] " tip-bot2 for Ira Weiny
2022-04-19 17:06 ` [PATCH V10 02/44] x86/pkeys: Clarify PKRU_AD_KEY macro ira.weiny
2022-06-07 23:09   ` [tip: x86/mm] " tip-bot2 for Ira Weiny
2022-04-19 17:06 ` [PATCH V10 03/44] x86/pkeys: Make PKRU macros generic ira.weiny
2022-04-19 17:06 ` [PATCH V10 04/44] x86/fpu: Refactor arch_set_user_pkey_access() ira.weiny
2022-04-19 17:06 ` [PATCH V10 05/44] mm/pkeys: Add Kconfig options for PKS ira.weiny
2022-04-19 17:06 ` [PATCH V10 06/44] x86/pkeys: Add PKS CPU feature bit ira.weiny
2022-04-19 17:06 ` [PATCH V10 07/44] x86/fault: Adjust WARN_ON for pkey fault ira.weiny
2022-04-19 17:06 ` [PATCH V10 08/44] Documentation/pkeys: Add initial PKS documentation ira.weiny
2022-05-09 22:03   ` Kees Cook
2022-05-10 17:18     ` Ira Weiny
2022-05-10 20:17       ` Kees Cook
2022-04-19 17:06 ` [PATCH V10 09/44] mm/pkeys: Provide for PKS key allocation ira.weiny
2022-04-19 17:06 ` [PATCH V10 10/44] x86/pkeys: Enable PKS on cpus which support it ira.weiny
2022-04-19 17:06 ` [PATCH V10 11/44] mm/pkeys: Define PKS page table macros ira.weiny
2022-04-19 17:06 ` [PATCH V10 12/44] x86/pkeys: Introduce pks_write_pkrs() ira.weiny
2022-04-19 17:06 ` [PATCH V10 13/44] x86/pkeys: Preserve the PKS MSR on context switch ira.weiny
2022-04-19 17:06 ` [PATCH V10 14/44] mm/pkeys: Introduce pks_set_readwrite() ira.weiny
2022-05-09 21:38   ` Kees Cook
2022-05-10 21:33     ` Ira Weiny
2022-05-10 22:08       ` Kees Cook
2022-05-10 22:26         ` Edgecombe, Rick P
2022-05-11  3:15           ` Kees Cook
2022-05-11 17:59             ` Ira Weiny
2022-04-19 17:06 ` [PATCH V10 15/44] mm/pkeys: Introduce pks_set_noaccess() ira.weiny
2022-04-19 17:06 ` [PATCH V10 16/44] mm/pkeys: Introduce PKS fault callbacks ira.weiny
2022-04-19 17:06 ` [PATCH V10 17/44] x86/entry: Add auxiliary pt_regs space ira.weiny
2022-04-19 17:06 ` [PATCH V10 18/44] entry: Pass pt_regs to irqentry_exit_cond_resched() ira.weiny
2022-04-19 17:06 ` [PATCH V10 19/44] entry: Add calls for save/restore auxiliary pt_regs ira.weiny
2022-04-19 17:06 ` [PATCH V10 20/44] x86/entry: Define arch_{save|restore}_auxiliary_pt_regs() ira.weiny
2022-04-19 17:06 ` [PATCH V10 21/44] x86/pkeys: Preserve PKRS MSR across exceptions ira.weiny
2022-04-19 17:06 ` [PATCH V10 22/44] x86/fault: Print PKS MSR on fault ira.weiny
2022-04-19 17:06 ` [PATCH V10 23/44] mm/pkeys: Introduce pks_update_exception() ira.weiny
2022-04-19 17:06 ` [PATCH V10 24/44] mm/pkeys: Add pks_available() ira.weiny
2022-04-19 17:06 ` [PATCH V10 25/44] memremap_pages: Add Kconfig for DEVMAP_ACCESS_PROTECTION ira.weiny
2022-04-19 17:06 ` [PATCH V10 26/44] memremap_pages: Introduce pgmap_protection_available() ira.weiny
2022-04-19 17:06 ` [PATCH V10 27/44] memremap_pages: Introduce a PGMAP_PROTECTION flag ira.weiny
2022-04-19 17:06 ` [PATCH V10 28/44] memremap_pages: Introduce devmap_protected() ira.weiny
2022-04-19 17:06 ` [PATCH V10 29/44] memremap_pages: Reserve a PKS pkey for eventual use by PMEM ira.weiny
2022-04-19 17:06 ` [PATCH V10 30/44] memremap_pages: Set PKS pkey in PTEs if requested ira.weiny
2022-04-19 17:06 ` [PATCH V10 31/44] memremap_pages: Define pgmap_set_{readwrite|noaccess}() calls ira.weiny
2022-04-19 17:06 ` [PATCH V10 32/44] memremap_pages: Add memremap.pks_fault_mode ira.weiny
2022-04-19 17:06 ` [PATCH V10 33/44] kmap: Make kmap work for devmap protected pages ira.weiny
2022-04-28 15:50   ` Christoph Hellwig
2022-05-12  1:25     ` Ira Weiny
2022-05-17 22:46       ` Ira Weiny
2022-05-18  7:33         ` Christoph Hellwig
2022-05-19 20:29           ` Ira Weiny
2022-04-19 17:06 ` ira.weiny [this message]
2022-04-19 17:06 ` [PATCH V10 35/44] nvdimm/pmem: Enable stray access protection ira.weiny
2022-04-19 17:06 ` [PATCH V10 36/44] devdax: " ira.weiny
2022-04-19 17:06 ` [PATCH V10 37/44] mm/pkeys: PKS testing, add initial test code ira.weiny
2022-04-19 17:06 ` [PATCH V10 38/44] x86/selftests: Add test_pks ira.weiny
2022-04-19 17:06 ` [PATCH V10 39/44] mm/pkeys: PKS testing, add a fault call back ira.weiny
2022-04-19 17:06 ` [PATCH V10 40/44] mm/pkeys: PKS testing, add pks_set_*() tests ira.weiny
2022-04-19 17:06 ` [PATCH V10 41/44] mm/pkeys: PKS testing, test context switching ira.weiny
2022-04-19 17:06 ` [PATCH V10 42/44] mm/pkeys: PKS testing, Add exception test ira.weiny
2022-04-19 17:06 ` [PATCH V10 43/44] mm/pkeys: PKS testing, test pks_update_exception() ira.weiny
2022-04-19 17:06 ` [PATCH V10 44/44] mm/pkeys: PKS testing, add test for all keys ira.weiny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220419170649.1022246-35-ira.weiny@intel.com \
    --to=ira.weiny@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=fenghua.yu@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ravi.v.shankar@intel.com \
    --cc=rick.p.edgecombe@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome