Skip to contentSkip to conversation

tcm/fileio: Convert to iov allocations for fd_do_readv() and fd_do_writev()

PATCH In review Nicholas A. Bellinger 2 participants 3 messages Sep 22, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcm/fileio: Convert to iov allocations for fd_do_readv() and fd_do_writev()

From: Nicholas Bellinger <[email protected]>

Greetings all,

This patch converts target_core_file.c fd_do_readv() and fd_do_writev() code to use dynamic allocation for *iov[] instead of local scope static memory to properly handle cases with TCM_Loop that can potentially exceed the 8K stack on 64-bit kernels with per 1:1 struct scatterlist <-> 512-byte block_size SGL length mapping up to the current FD_MAX_SECTORS=1024. For more information please see:

http://lkml.org/lkml/2010/9/22/42

Many thanks to Fubo Chen for spotting this one!

---
 drivers/target/target_core_file.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
drivers/target/target_core_file.c +16 -4
index 221b41e..93d3f63 100644
@@ -476,12 +476,16 @@ static int fd_do_readv(struct fd_request *req, struct se_task *task)
 {
 	struct file *fd = req->fd_dev->fd_file;
 	struct scatterlist *sg = task->task_sg;
-	struct iovec iov[req->fd_sg_count];
+	struct iovec *iov;
 	mm_segment_t old_fs;
 	loff_t pos = (req->fd_lba * DEV_ATTRIB(task->se_dev)->block_size);
 	int ret = 0, i;
 
-	memset(iov, 0, sizeof(struct iovec) * req->fd_sg_count);
+	iov = kzalloc(sizeof(struct iovec) * req->fd_sg_count, GFP_KERNEL);
+	if (!(iov)) {
+		printk(KERN_ERR "Unable to allocate fd_do_readv iov[]\n");
+		return -1;
+	}
 
 	for (i = 0; i < req->fd_sg_count; i++) {
 		iov[i].iov_len = sg[i].length;
@@ -492,6 +496,8 @@ static int fd_do_readv(struct fd_request *req, struct se_task *task)
 	set_fs(get_ds());
 	ret = vfs_readv(fd, &iov[0], req->fd_sg_count, &pos);
 	set_fs(old_fs);
+
+	kfree(iov);
 	/*
 	 * Return zeros and GOOD status even if the READ did not return
 	 * the expected virt_size for struct file w/o a backing struct
@@ -583,12 +589,16 @@ static int fd_do_writev(struct fd_request *req, struct se_task *task)
 {
 	struct file *fd = req->fd_dev->fd_file;
 	struct scatterlist *sg = task->task_sg;
-	struct iovec iov[req->fd_sg_count];
+	struct iovec *iov;
 	mm_segment_t old_fs;
 	loff_t pos = (req->fd_lba * DEV_ATTRIB(task->se_dev)->block_size);
 	int ret, i = 0;
 
-	memset(iov, 0, sizeof(struct iovec) * req->fd_sg_count);
+	iov = kzalloc(sizeof(struct iovec) * req->fd_sg_count, GFP_KERNEL);
+	if (!(iov)) {
+		printk(KERN_ERR "Unable to allocate fd_do_writev iov[]\n");
+		return -1;
+	}
 
 	for (i = 0; i < req->fd_sg_count; i++) {
 		iov[i].iov_len = sg[i].length;
@@ -600,6 +610,8 @@ static int fd_do_writev(struct fd_request *req, struct se_task *task)
 	ret = vfs_writev(fd, &iov[0], req->fd_sg_count, &pos);
 	set_fs(old_fs);
 
+	kfree(iov);
+
 	if (ret < 0 || ret != req->fd_size) {
 		printk(KERN_ERR "vfs_writev() returned %d\n", ret);
 		return -1;
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 25 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

Greetings all,

This patch converts target_core_file.c fd_do_readv() and fd_do_writev() code to use dynamic allocation for *iov[] instead of local scope static memory to properly handle cases with TCM_Loop that can potentially exceed the 8K stack on 64-bit kernels with per 1:1 struct scatterlist <-> 512-byte block_size SGL length mapping up to the current FD_MAX_SECTORS=1024. For more information please see:

http://lkml.org/lkml/2010/9/22/42

Many thanks to Fubo Chen for spotting this one!

---
 drivers/target/target_core_file.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
drivers/target/target_core_file.c +0 -0
index 221b41e..93d3f63 100644
@@ -476,12 +476,16 @@ static int fd_do_readv(struct fd_request *req, struct se_task *task)
 {

drivers/target/target_core_file.c static int fd_do_readv(struct fd_request *req, struct se_task *task) · line 484

Quoted 10 lines
drivers/target/target_core_file.c +2 -2
 	struct file *fd = req->fd_dev->fd_file;
 	struct scatterlist *sg = task->task_sg;
-	struct iovec iov[req->fd_sg_count];
+	struct iovec *iov;
 	mm_segment_t old_fs;
 	loff_t pos = (req->fd_lba * DEV_ATTRIB(task->se_dev)->block_size);
 	int ret = 0, i;
 
-	memset(iov, 0, sizeof(struct iovec) * req->fd_sg_count);
+	iov = kzalloc(sizeof(struct iovec) * req->fd_sg_count, GFP_KERNEL);

A kzalloc allocation bigger then PAG_SIZE is very unstable, more so when the system is up for a long time.

Better prepare to loop here, up to PAG_SIZE iov at a time.

Boaz

Quoted 44 lines
drivers/target/target_core_file.c +14 -2
+	if (!(iov)) {
+		printk(KERN_ERR "Unable to allocate fd_do_readv iov[]\n");
+		return -1;
+	}
 
 	for (i = 0; i < req->fd_sg_count; i++) {
 		iov[i].iov_len = sg[i].length;
@@ -492,6 +496,8 @@ static int fd_do_readv(struct fd_request *req, struct se_task *task)
 	set_fs(get_ds());
 	ret = vfs_readv(fd, &iov[0], req->fd_sg_count, &pos);
 	set_fs(old_fs);
+
+	kfree(iov);
 	/*
 	 * Return zeros and GOOD status even if the READ did not return
 	 * the expected virt_size for struct file w/o a backing struct
@@ -583,12 +589,16 @@ static int fd_do_writev(struct fd_request *req, struct se_task *task)
 {
 	struct file *fd = req->fd_dev->fd_file;
 	struct scatterlist *sg = task->task_sg;
-	struct iovec iov[req->fd_sg_count];
+	struct iovec *iov;
 	mm_segment_t old_fs;
 	loff_t pos = (req->fd_lba * DEV_ATTRIB(task->se_dev)->block_size);
 	int ret, i = 0;
 
-	memset(iov, 0, sizeof(struct iovec) * req->fd_sg_count);
+	iov = kzalloc(sizeof(struct iovec) * req->fd_sg_count, GFP_KERNEL);
+	if (!(iov)) {
+		printk(KERN_ERR "Unable to allocate fd_do_writev iov[]\n");
+		return -1;
+	}
 
 	for (i = 0; i < req->fd_sg_count; i++) {
 		iov[i].iov_len = sg[i].length;
@@ -600,6 +610,8 @@ static int fd_do_writev(struct fd_request *req, struct se_task *task)
 	ret = vfs_writev(fd, &iov[0], req->fd_sg_count, &pos);
 	set_fs(old_fs);
 
+	kfree(iov);
+
 	if (ret < 0 || ret != req->fd_size) {
 		printk(KERN_ERR "vfs_writev() returned %d\n", ret);
 		return -1;
#2 lore raw
Quoted 34 lines · Boaz Harrosh
Quoted 33 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

Greetings all,

This patch converts target_core_file.c fd_do_readv() and fd_do_writev() code to use dynamic allocation for *iov[] instead of local scope static memory to properly handle cases with TCM_Loop that can potentially exceed the 8K stack on 64-bit kernels with per 1:1 struct scatterlist <-> 512-byte block_size SGL length mapping up to the current FD_MAX_SECTORS=1024. For more information please see:

http://lkml.org/lkml/2010/9/22/42

Many thanks to Fubo Chen for spotting this one!

---
 drivers/target/target_core_file.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
drivers/target/target_core_file.c +1 -1
index 221b41e..93d3f63 100644
@@ -476,12 +476,16 @@ static int fd_do_readv(struct fd_request *req, struct se_task *task)
 {
 	struct file *fd = req->fd_dev->fd_file;
 	struct scatterlist *sg = task->task_sg;
-	struct iovec iov[req->fd_sg_count];
+	struct iovec *iov;
 	mm_segment_t old_fs;
 	loff_t pos = (req->fd_lba * DEV_ATTRIB(task->se_dev)->block_size);
 	int ret = 0, i;
 
Quoted 8 lines

drivers/target/target_core_file.c static int fd_do_readv(struct fd_request *req, struct se_task *task) · line 484

Quoted 2 lines
drivers/target/target_core_file.c +1 -1
-	memset(iov, 0, sizeof(struct iovec) * req->fd_sg_count);
+	iov = kzalloc(sizeof(struct iovec) * req->fd_sg_count, GFP_KERNEL);

A kzalloc allocation bigger then PAG_SIZE is very unstable, more so when the system is up for a long time.

Better prepare to loop here, up to PAG_SIZE iov at a time.

Hmmm, good point for the extreme TCM_Loop single 512-byte block per SGL case. Looks like we need to split up the struct iovec mappings for FILEIO into multiple allocations in fd_do_readv() and fd_do_writev()..?

Best,

--nab