Skip to contentSkip to conversation

ltp-aiodio: report posix_memalign errors properly

PATCH Superseded Eryu Guan 2 participants 5 messages May 27, 2016 – Jun 1, 2016

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/3 ltp-aiodio: report posix_memalign errors properly

From posix_memalign(3) the value of errno is indeterminate after a call to posix_memalign(). So TERRNO doesn't work in this case.
dio_sparse 1 TBROK : dio_sparse.c:75: posix_memalign(): errno=SUCCESS(0): Success

Report posix_memalign() errors by calling strerror on the return value.
dio_sparse 1 TBROK : dio_sparse.c:74: posix_memalign(): EINVAL

---
 testcases/kernel/io/ltp-aiodio/aiodio_append.c |  7 +++++--
 testcases/kernel/io/ltp-aiodio/aiodio_sparse.c |  7 ++++---
 testcases/kernel/io/ltp-aiodio/dio_append.c    |  8 ++++++--
 testcases/kernel/io/ltp-aiodio/dio_sparse.c    |  7 ++++---
 testcases/kernel/io/ltp-aiodio/dio_truncate.c  | 15 +++++++++++----
 5 files changed, 30 insertions(+), 14 deletions(-)
testcases/kernel/io/ltp-aiodio/aiodio_append.c +5 -2
index 56e9c09..1670351 100644
@@ -98,6 +98,7 @@ void aiodio_append(char *filename)
 	void *bufptr;
 	int i;
 	int w;
+	int ret;
 	struct iocb iocb_array[NUM_AIO];
 	struct iocb *iocbs[NUM_AIO];
 	off_t offset = 0;
@@ -115,8 +116,10 @@ void aiodio_append(char *filename)
 	io_queue_init(NUM_AIO, &myctx);
 
 	for (i = 0; i < NUM_AIO; i++) {
-		if (posix_memalign(&bufptr, 4096, AIO_SIZE)) {
-			perror("cannot malloc aligned memory");
+		ret = posix_memalign(&bufptr, 4096, AIO_SIZE);
+		if (ret) {
+			tst_resm(TBROK, "cannot malloc aligned memory: %s",
+					tst_strerrno(ret));
 			return;
 		}
 		memset(bufptr, 0, AIO_SIZE);
testcases/kernel/io/ltp-aiodio/aiodio_sparse.c +4 -3
index 944e12b..edd3cc2 100644
@@ -60,7 +60,7 @@ int aiodio_sparse(char *filename, int align, int writesize, int filesize,
 		  int num_aio)
 {
 	int fd;
-	int i, w;
+	int i, w, ret;
 	struct iocb **iocbs;
 	off_t offset;
 	io_context_t myctx;
@@ -97,8 +97,9 @@ int aiodio_sparse(char *filename, int align, int writesize, int filesize,
 	for (i = 0; i < num_aio; i++) {
 		void *bufptr;
 
-		if (posix_memalign(&bufptr, align, writesize)) {
-			tst_resm(TBROK | TERRNO, "posix_memalign()");
+		ret = posix_memalign(&bufptr, align, writesize);
+		if (ret) {
+			tst_resm(TBROK, "posix_memalign(): %s", tst_strerrno(ret));
 			close(fd);
 			unlink(filename);
 			return 1;
testcases/kernel/io/ltp-aiodio/dio_append.c +6 -2
index 878c465..0460a0e 100644
@@ -99,6 +99,7 @@ void dio_append(char *filename)
 	void *bufptr;
 	int i;
 	int w;
+	int ret;
 
 	fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
 
@@ -107,8 +108,11 @@ void dio_append(char *filename)
 		return;
 	}
 
-	if (posix_memalign(&bufptr, 4096, 64 * 1024)) {
-		perror("cannot malloc aligned memory");
+	ret = posix_memalign(&bufptr, 4096, 64 * 1024);
+	if (ret) {
+		tst_resm(TBROK, "cannot malloc aligned memory: %s",
+				tst_strerrno(ret));
+		close(fd);
 		return;
 	}
 
testcases/kernel/io/ltp-aiodio/dio_sparse.c +4 -3
index 7ad5f80..12e478e 100644
@@ -55,7 +55,7 @@ int TST_TOTAL = 1;
  */
 int dio_sparse(char *filename, int align, int writesize, int filesize)
 {
-	int fd;
+	int fd, ret;
 	void *bufptr;
 	int i, w;
 
@@ -68,9 +68,10 @@ int dio_sparse(char *filename, int align, int writesize, int filesize)
 
 	SAFE_FTRUNCATE(cleanup, fd, filesize);
 
-	if (posix_memalign(&bufptr, align, writesize)) {
+	ret = posix_memalign(&bufptr, align, writesize);
+	if (ret) {
+		tst_resm(TBROK, "posix_memalign(): %s", tst_strerrno(ret));
 		close(fd);
-		tst_resm(TBROK | TERRNO, "posix_memalign()");
 		return 1;
 	}
 
testcases/kernel/io/ltp-aiodio/dio_truncate.c +11 -4
index 7458a19..a9b3c96 100644
@@ -66,10 +66,13 @@ int dio_read(char *filename)
 {
 	int fd;
 	int r;
+	int ret;
 	void *bufptr;
 
-	if (posix_memalign(&bufptr, 4096, 64 * 1024)) {
-		perror("cannot malloc aligned memory");
+	ret = posix_memalign(&bufptr, 4096, 64 * 1024);
+	if (ret) {
+		tst_resm(TBROK, "cannot malloc aligned memory: %s",
+				tst_strerrno(ret));
 		return -1;
 	}
 
@@ -104,6 +107,7 @@ void dio_append(char *filename, int fill)
 	void *bufptr;
 	int i;
 	int w;
+	int ret;
 
 	fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
 
@@ -112,8 +116,11 @@ void dio_append(char *filename, int fill)
 		return;
 	}
 
-	if (posix_memalign(&bufptr, 4096, 64 * 1024)) {
-		perror("cannot malloc aligned memory");
+	ret = posix_memalign(&bufptr, 4096, 64 * 1024);
+	if (ret) {
+		tst_resm(TBROK, "cannot malloc aligned memory: %s",
+				tst_strerrno(ret));
+		close(fd);
 		return;
 	}
 
Signature · 1 line
2.5.5
author
#1 lore raw

PATCH 2/3 ltp-aiodio/dio_sparse: add offset support

Introduce a new option "-o" to specify the write offset.

---
 testcases/kernel/io/ltp-aiodio/dio_sparse.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
testcases/kernel/io/ltp-aiodio/dio_sparse.c +12 -6
index 12e478e..33fda94 100644
@@ -53,11 +53,11 @@ int TST_TOTAL = 1;
 /*
  * Write zeroes using O_DIRECT into sparse file.
  */
-int dio_sparse(char *filename, int align, int writesize, int filesize)
+int dio_sparse(char *filename, int align, int writesize, int filesize, int offset)
 {
 	int fd, ret;
 	void *bufptr;
-	int i, w;
+	int w;
 
 	fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT | O_EXCL, 0600);
 
@@ -76,7 +76,8 @@ int dio_sparse(char *filename, int align, int writesize, int filesize)
 	}
 
 	memset(bufptr, 0, writesize);
-	for (i = 0; i < filesize;) {
+	lseek(fd, offset, SEEK_SET);
+	for (i = offset; i < filesize;) {
 		if ((w = write(fd, bufptr, writesize)) != writesize) {
 			tst_resm(TBROK | TERRNO, "write() returned %d", w);
 			close(fd);
@@ -95,7 +96,7 @@ int dio_sparse(char *filename, int align, int writesize, int filesize)
 void usage(void)
 {
 	fprintf(stderr, "usage: dio_sparse [-d] [-n children] [-s filesize]"
-		" [-w writesize]\n");
+		" [-w writesize] [-o offset]]\n");
 	exit(1);
 }
 
@@ -108,11 +109,12 @@ int main(int argc, char **argv)
 	long alignment = 512;
 	int writesize = 65536;
 	int filesize = 100 * 1024 * 1024;
+	int offset = 0;
 	int c;
 	int children_errors = 0;
 	int ret;
 
-	while ((c = getopt(argc, argv, "dw:n:a:s:")) != -1) {
+	while ((c = getopt(argc, argv, "dw:n:a:s:o:")) != -1) {
 		char *endp;
 		switch (c) {
 		case 'd':
@@ -130,6 +132,10 @@ int main(int argc, char **argv)
 			filesize = strtol(optarg, &endp, 0);
 			filesize = scale_by_kmg(filesize, *endp);
 			break;
+		case 'o':
+			offset = strtol(optarg, &endp, 0);
+			offset = scale_by_kmg(offset, *endp);
+			break;
 		case 'n':
 			num_children = atoi(optarg);
 			if (num_children > NUM_CHILDREN) {
@@ -167,7 +173,7 @@ int main(int argc, char **argv)
 	}
 	tst_sig(FORK, DEF_HANDLER, cleanup);
 
-	ret = dio_sparse(filename, alignment, writesize, filesize);
+	ret = dio_sparse(filename, alignment, writesize, filesize, offset);
 
 	tst_resm(TINFO, "Killing childrens(s)");
 
Signature · 1 line
2.5.5
author
#2 lore raw

PATCH 3/3 ltp-aiodio: new dio_sparse tests specifying offset

In reply to Eryu Guan

The first 4 tests are to cover the second issue described in this patch

direct-io: fix direct write stale data exposure from concurrent buffered read
https://patchwork.ozlabs.org/patch/622065/

Other tests are added to test various offsets.

---
 runtest/ltp-aiodio.part2 | 8 ++++++++
 1 file changed, 8 insertions(+)
runtest/ltp-aiodio.part2 +8 -0
index 28a84ac..00b9abf 100644
@@ -78,3 +78,11 @@ ADSP076 dio_sparse  -a 8k -w 8192k -s 8192k -n 64
 ADSP077 dio_sparse  -a 8k -w 518192k -s 18192k -n 128
 ADSP078 dio_sparse  -a 8k -w 518192k -s 518192k -n 512
 ADSP079 dio_sparse  -a 8k -w 518192k -s 518192k -n 1000
+ADSP080 dio_sparse  -a 4k -w 4k -s 2k -o 2k -n 2
+ADSP081 dio_sparse  -a 2k -w 2k -s 1k -o 1k -n 2
+ADSP082 dio_sparse  -a 1k -w 1k -s 512 -o 512 -n 2
+ADSP083 dio_sparse  -a 4k -w 4k -s 2k -o 3k -n 2
+ADSP084 dio_sparse  -a 4k -w 4k -s 4k -o 4k -n 2
+ADSP085 dio_sparse  -a 4k -w 4k -s 4k -o 6k -n 2
+ADSP086 dio_sparse  -a 4k -w 4k -s 4k -o 8k -n 2
+ADSP087 dio_sparse  -a 4k -w 16k -s 8k -o 8k -n 2
Signature · 1 line
2.5.5
#3 lore raw

PATCH 1/3 ltp-aiodio: report posix_memalign errors properly

In reply to Eryu Guan

Hi!

Quoted 6 lines

From posix_memalign(3) the value of errno is indeterminate after a call to posix_memalign(). So TERRNO doesn't work in this case.
dio_sparse 1 TBROK : dio_sparse.c:75: posix_memalign(): errno=SUCCESS(0): Success

Report posix_memalign() errors by calling strerror on the return value.
dio_sparse 1 TBROK : dio_sparse.c:74: posix_memalign(): EINVAL

That is good catch but the *_sparse.c and dio_truncate.c does not use the test library and hence the compilation fails on undeclared and implicit declarations.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#4 lore raw

PATCH 1/3 ltp-aiodio: report posix_memalign errors properly

Quoted 11 lines · Cyril Hrubis

Hi!

Quoted 6 lines

From posix_memalign(3) the value of errno is indeterminate after a call to posix_memalign(). So TERRNO doesn't work in this case.
dio_sparse 1 TBROK : dio_sparse.c:75: posix_memalign(): errno=SUCCESS(0): Success

Report posix_memalign() errors by calling strerror on the return value.
dio_sparse 1 TBROK : dio_sparse.c:74: posix_memalign(): EINVAL

That is good catch but the *_sparse.c and dio_truncate.c does not use the test library and hence the compilation fails on undeclared and implicit declarations.

Sorry about that! I tested the dio_sparse.c and it worked, but not others. Lesson learned, thanks for the review!

Eryu