Skip to contentSkip to conversation

ecryptfs: avoid heap allocation for inode size write

PATCH In review Yichong Chen 2 participants 3 messages Jun 30 – Jul 21 Ecrypt file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ecryptfs: avoid heap allocation for inode size write

ecryptfs_write_inode_size_to_header() allocates an 8-byte buffer only to write the encoded inode size to the lower file header.

Use a stack __be64 value instead. This avoids an unnecessary allocation and removes a failure path without changing the data written to disk.

---
 fs/ecryptfs/mmap.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
fs/ecryptfs/mmap.c +4 -11
index 2c2b12fedeae..0222f98143ab 100644
@@ -355,24 +355,17 @@ static int ecryptfs_write_begin(const struct kiocb *iocb,
  */
 static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
 {
-	char *file_size_virt;
+	__be64 file_size;
 	int rc;
 
-	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
-	if (!file_size_virt) {
-		rc = -ENOMEM;
-		goto out;
-	}
-	put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
-	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
-				  sizeof(u64));
-	kfree(file_size_virt);
+	file_size = cpu_to_be64(i_size_read(ecryptfs_inode));
+	rc = ecryptfs_write_lower(ecryptfs_inode, (char *)&file_size, 0,
+				  sizeof(file_size));
 	if (rc < 0)
 		printk(KERN_ERR "%s: Error writing file size to header; "
 		       "rc = [%d]\n", __func__, rc);
 	else
 		rc = 0;
-out:
 	return rc;
 }
 
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 7 lines · Yichong Chen

ecryptfs_write_inode_size_to_header() allocates an 8-byte buffer only to write the encoded inode size to the lower file header.

Use a stack __be64 value instead. This avoids an unnecessary allocation and removes a failure path without changing the data written to disk.

This is much better. Thanks for the improvement!

I'll put your patches through some testing and then respond when they've been applied. Thanks!

Tyler

Quoted 40 lines
---
 fs/ecryptfs/mmap.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
fs/ecryptfs/mmap.c +4 -11
index 2c2b12fedeae..0222f98143ab 100644
@@ -355,24 +355,17 @@ static int ecryptfs_write_begin(const struct kiocb *iocb,
  */
 static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
 {
-	char *file_size_virt;
+	__be64 file_size;
 	int rc;
 
-	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
-	if (!file_size_virt) {
-		rc = -ENOMEM;
-		goto out;
-	}
-	put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
-	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
-				  sizeof(u64));
-	kfree(file_size_virt);
+	file_size = cpu_to_be64(i_size_read(ecryptfs_inode));
+	rc = ecryptfs_write_lower(ecryptfs_inode, (char *)&file_size, 0,
+				  sizeof(file_size));
 	if (rc < 0)
 		printk(KERN_ERR "%s: Error writing file size to header; "
 		       "rc = [%d]\n", __func__, rc);
 	else
 		rc = 0;
-out:
 	return rc;
 }
 
Signature · 1 line
2.51.0
maintainer
#2 lore raw
In reply to Yichong Chen
Quoted 8 lines · Yichong Chen

ecryptfs_write_inode_size_to_header() allocates an 8-byte buffer only to write the encoded inode size to the lower file header.

Use a stack __be64 value instead. This avoids an unnecessary allocation and removes a failure path without changing the data written to disk.

[...]

Thank you! This has been applied to the next branch of the tyhicks/ecryptfs.git tree.

You can find a direct link below but please be aware that the commit hash is unstable and, therefore, the URL may not be valid in the future.

[1/1] ecryptfs: avoid heap allocation for inode size write

      https://git.kernel.org/tyhicks/ecryptfs/c/8fafd7c86fc35bd11eb34d4ab2d6282b3354f30f

Tyler