linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Halcrow <mhalcrow@us.ibm.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	ecryptfs-devel@lists.sourceforge.net
Subject: Re: [Ecryptfs-devel] [PATCH 3/11] eCryptfs: read_write.c routines
Date: Fri, 21 Sep 2007 16:51:25 -0500	[thread overview]
Message-ID: <20070921215125.GB11833@halcrow.austin.ibm.com> (raw)
In-Reply-To: <20070919223850.3e1132ac.akpm@linux-foundation.org>

On Wed, Sep 19, 2007 at 10:38:50PM -0700, Andrew Morton wrote:
> > +	virt = kmap(page_for_lower);
> > +	rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
> > +	kunmap(page_for_lower);
> > +	return rc;
> > +}
> 
> argh, kmap.  http://lkml.org/lkml/2007/9/15/55

Here is a patch that moves to kmap_atomic(), adding an intermediate
copy. Although I would really like to find a way to avoid having to do
this extra copy.

---

Replace kmap() with kmap_atomic() for read_write.c routines

kmap() can lead to deadlock when multiple tasks attempt to take more
than one simultaneously:

http://lkml.org/lkml/2007/9/15/55

In order to avoid this possibility, eCryptfs must allocate an
intermediate block of memory to use with vfs_read() and vfs_write(),
copying the data through this memory region, since kmap_atomic()
cannot be held during calls which may block.

Signed-off-by: Michael Halcrow <mhalcrow@us.ibm.com>
---
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
index bb92b74..ce7a5d4 100644
--- a/fs/ecryptfs/ecryptfs_kernel.h
+++ b/fs/ecryptfs/ecryptfs_kernel.h
@@ -648,6 +648,6 @@ int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
 				     struct inode *ecryptfs_inode);
 int ecryptfs_read(char *data, loff_t offset, size_t size,
 		  struct file *ecryptfs_file);
-struct page *ecryptfs_get1page(struct file *file, loff_t index);
+struct page *ecryptfs_get_locked_page(struct file *file, loff_t index);
 
 #endif /* #ifndef ECRYPTFS_KERNEL_H */
diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c
index c6a8a33..6abf805 100644
--- a/fs/ecryptfs/mmap.c
+++ b/fs/ecryptfs/mmap.c
@@ -37,23 +37,30 @@
 struct kmem_cache *ecryptfs_lower_page_cache;
 
 /**
- * ecryptfs_get1page
+ * ecryptfs_get_locked_page
  *
  * Get one page from cache or lower f/s, return error otherwise.
  *
- * Returns unlocked and up-to-date page (if ok), with increased
+ * Returns a locked and up-to-date page (if ok), with increased
  * refcnt.
  */
-struct page *ecryptfs_get1page(struct file *file, loff_t index)
+struct page *ecryptfs_get_locked_page(struct file *file, loff_t index)
 {
 	struct dentry *dentry;
 	struct inode *inode;
 	struct address_space *mapping;
+	struct page *page;
 
 	dentry = file->f_path.dentry;
 	inode = dentry->d_inode;
 	mapping = inode->i_mapping;
-	return read_mapping_page(mapping, index, (void *)file);
+	page = read_mapping_page(mapping, index, (void *)file);
+	if (!IS_ERR(page))
+		lock_page(page);
+	else
+		printk(KERN_ERR "%s: Error from read_mapping_page()\n",
+		       __FUNCTION__);
+	return page;
 }
 
 /**
diff --git a/fs/ecryptfs/read_write.c b/fs/ecryptfs/read_write.c
index ccd2599..6732a4c 100644
--- a/fs/ecryptfs/read_write.c
+++ b/fs/ecryptfs/read_write.c
@@ -83,14 +83,24 @@ int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
 				      struct page *page_for_lower,
 				      size_t offset_in_page, size_t size)
 {
-	char *virt;
+	char *page_for_lower_virt;
+	char *tmp_virt;
 	loff_t offset;
 	int rc;
 
-	offset = (page_for_lower->index << PAGE_CACHE_SHIFT) + offset_in_page;
-	virt = kmap(page_for_lower);
-	rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
-	kunmap(page_for_lower);
+	tmp_virt = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
+	if (!tmp_virt) {
+		rc = -ENOMEM;
+		goto out;
+	}
+	offset = ((((loff_t)page_for_lower->index) << PAGE_CACHE_SHIFT)
+		  + offset_in_page);
+	page_for_lower_virt = kmap_atomic(page_for_lower, KM_USER0);
+	memcpy(tmp_virt, page_for_lower_virt, PAGE_CACHE_SIZE);
+	kunmap_atomic(page_for_lower_virt, KM_USER0);
+	rc = ecryptfs_write_lower(ecryptfs_inode, tmp_virt, offset, size);
+	kfree(tmp_virt);
+out:
 	return rc;
 }
 
@@ -140,8 +150,8 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
 			if (num_bytes > total_remaining_zeros)
 				num_bytes = total_remaining_zeros;
 		}
-		ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
-						  ecryptfs_page_idx);
+		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
+							 ecryptfs_page_idx);
 		if (IS_ERR(ecryptfs_page)) {
 			rc = PTR_ERR(ecryptfs_page);
 			printk(KERN_ERR "%s: Error getting page at "
@@ -159,6 +169,7 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
 				printk(KERN_ERR "%s: Error decrypting "
 				       "page; rc = [%d]\n",
 				       __FUNCTION__, rc);
+				unlock_page(ecryptfs_page);
 				page_cache_release(ecryptfs_page);
 				goto out;
 			}
@@ -182,9 +193,11 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
 		if (rc) {
 			printk(KERN_ERR "%s: Error encrypting "
 			       "page; rc = [%d]\n", __FUNCTION__, rc);
+			unlock_page(ecryptfs_page);
 			page_cache_release(ecryptfs_page);
 			goto out;
 		}
+		unlock_page(ecryptfs_page);
 		page_cache_release(ecryptfs_page);
 		pos += num_bytes;
 	}
@@ -245,10 +258,6 @@ int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
 
 		data_page = virt_to_page(data + i);
 		flush_dcache_page(data_page);
-		if (rc)
-			ClearPageUptodate(data_page);
-		else
-			SetPageUptodate(data_page);
 	}
 	return rc;
 }
@@ -273,14 +282,34 @@ int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
 				     size_t offset_in_page, size_t size,
 				     struct inode *ecryptfs_inode)
 {
-	char *virt;
+	char *page_for_ecryptfs_virt;
+	char *tmp_virt;
 	loff_t offset;
 	int rc;
 
-	offset = ((page_index << PAGE_CACHE_SHIFT) + offset_in_page);
-	virt = kmap(page_for_ecryptfs);
-	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
-	kunmap(page_for_ecryptfs);
+	tmp_virt = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
+	if (!tmp_virt) {
+		rc = -ENOMEM;
+		goto out;
+	}
+	offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
+	rc = ecryptfs_read_lower(tmp_virt, offset, size, ecryptfs_inode);
+	if (rc) {
+		printk(KERN_ERR "%s: Error reading lower; rc = [%d]\n",
+		       __FUNCTION__, rc);
+		goto out_free;
+	}
+	page_for_ecryptfs_virt = kmap_atomic(page_for_ecryptfs, KM_USER0);
+	memcpy(page_for_ecryptfs_virt, tmp_virt, PAGE_CACHE_SIZE);
+	kunmap_atomic(page_for_ecryptfs_virt, KM_USER0);
+	flush_dcache_page(page_for_ecryptfs);
+out_free:
+	kfree(tmp_virt);
+out:
+	if (rc)
+		ClearPageUptodate(page_for_ecryptfs);
+	else
+		SetPageUptodate(page_for_ecryptfs);
 	return rc;
 }
 
@@ -328,8 +357,8 @@ int ecryptfs_read(char *data, loff_t offset, size_t size,
 
 		if (num_bytes > total_remaining_bytes)
 			num_bytes = total_remaining_bytes;
-		ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
-						  ecryptfs_page_idx);
+		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
+							 ecryptfs_page_idx);
 		if (IS_ERR(ecryptfs_page)) {
 			rc = PTR_ERR(ecryptfs_page);
 			printk(KERN_ERR "%s: Error getting page at "
@@ -342,6 +371,7 @@ int ecryptfs_read(char *data, loff_t offset, size_t size,
 		if (rc) {
 			printk(KERN_ERR "%s: Error decrypting "
 			       "page; rc = [%d]\n", __FUNCTION__, rc);
+			unlock_page(ecryptfs_page);
 			page_cache_release(ecryptfs_page);
 			goto out;
 		}
@@ -350,6 +380,7 @@ int ecryptfs_read(char *data, loff_t offset, size_t size,
 		       ((char *)ecryptfs_page_virt + start_offset_in_page),
 		       num_bytes);
 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
+		unlock_page(ecryptfs_page);
 		page_cache_release(ecryptfs_page);
 		pos += num_bytes;
 		data_offset += num_bytes;

  reply	other threads:[~2007-09-21 21:51 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-17 21:44 [PATCH 0/11] eCryptfs: Introduce persistent lower files for each eCryptfs inode Michael Halcrow
2007-09-17 21:45 ` [PATCH 1/11] eCryptfs: Remove header_extent_size Michael Halcrow
2007-09-17 21:45 ` [PATCH 2/11] eCryptfs: Remove assignments in if-statements Michael Halcrow
2007-09-17 21:46 ` [PATCH 3/11] eCryptfs: read_write.c routines Michael Halcrow
2007-09-20  5:25   ` Andrew Morton
2007-09-20  5:38   ` Andrew Morton
2007-09-21 21:51     ` Michael Halcrow [this message]
2007-09-21 22:05       ` [Ecryptfs-devel] " Andrew Morton
2007-09-25 15:56         ` Michael Halcrow
2007-09-24 22:12     ` Michael Halcrow
2007-09-17 21:47 ` [PATCH 4/11] eCryptfs: Replace encrypt, decrypt, and inode size write Michael Halcrow
2007-09-20  5:46   ` Andrew Morton
2007-09-20 21:44     ` Michael Halcrow
2007-09-20 23:35       ` Erez Zadok
2007-09-17 21:47 ` [PATCH 5/11] eCryptfs: Set up and destroy persistent lower file Michael Halcrow
2007-09-17 21:48 ` [PATCH 6/11] eCryptfs: Update metadata read/write functions Michael Halcrow
2007-09-20  5:48   ` Andrew Morton
2007-09-24 22:40     ` Michael Halcrow
2007-09-17 21:49 ` [PATCH 7/11] eCryptfs: Make open, truncate, and setattr use persistent file Michael Halcrow
2007-09-17 21:50 ` [PATCH 8/11] eCryptfs: Convert mmap functions to " Michael Halcrow
2007-09-20  5:50   ` Andrew Morton
2007-09-20 22:03     ` Michael Halcrow
2007-09-17 21:50 ` [PATCH 9/11] eCryptfs: Initialize persistent lower file on inode create Michael Halcrow
2007-09-17 21:51 ` [PATCH 10/11] eCryptfs: Remove unused functions and kmem_cache Michael Halcrow
2007-09-17 21:52 ` [PATCH 11/11] eCryptfs: Replace magic numbers Michael Halcrow

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=20070921215125.GB11833@halcrow.austin.ibm.com \
    --to=mhalcrow@us.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=ecryptfs-devel@lists.sourceforge.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).