public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Timo Sirainen <tss@iki.fi>
To: linux-kernel@vger.kernel.org
Subject: SMP read() stopping at memory page boundaries
Date: Wed, 20 Jun 2007 17:52:51 +0300	[thread overview]
Message-ID: <1182351171.3768.65.camel@hurina> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 464 bytes --]

Tested with various 2.6.x i386/x86-64 SMP kernels and CPUs, for example
2.6.21.3/x86-64.

Process 1:

 - lock file
 - write(4096 + 16 bytes)
 - unlock file

Process 2:

 - lock file
 - read(8192 bytes)
 - unlock file

Sometimes read() returns only 4096 bytes. I'm locking the file, so I
don't think this should ever happen, right?

Attached a test program. Takes from a few seconds to half a minute with
my computer to print "page size cut".


[-- Attachment #1.2: concurrency2.c --]
[-- Type: text/x-csrc, Size: 1556 bytes --]

/*
   gcc concurrency2.c -o concurrency -Wall

   start two both a reader and a writer:

   ./concurrency
   ./concurrency 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/file.h>

#define MAX_PAGESIZE 8192

int main(int argc, char *argv[])
{
	char buf[MAX_PAGESIZE*2];
	int fd, ret, pagesize;

	memset(buf, 0, sizeof(buf));

	pagesize = getpagesize();
	assert(pagesize <= MAX_PAGESIZE);

	buf[pagesize] = 'h';
	if (argc == 1) {
		printf("writing, page size = %d\n", pagesize);
		for (;;) {
			fd = open("foo", O_RDWR | O_CREAT | O_TRUNC, 0600);
			if (fd == -1) {
				perror("open()");
				return 1;
			}

			if (flock(fd, LOCK_EX) < 0)
				perror("flock()");
			write(fd, buf, pagesize+16);
			if (flock(fd, LOCK_UN) < 0)
				perror("flock()");
			usleep(rand() % 1000);
			close(fd);
		}
	} else {
		printf("reading, page size = %d\n", pagesize);
		fd = open("foo", O_RDWR, 0600);
		if (fd == -1) {
			perror("open()");
			return 1;
		}
		for (;;) {
			usleep(rand() % 1000);
			if (flock(fd, LOCK_SH) < 0)
				perror("flock()");
			lseek(fd, 0, SEEK_SET);
			ret = read(fd, buf, sizeof(buf));
			if (flock(fd, LOCK_UN) < 0)
				perror("flock()");

			if (ret < pagesize) {
				if (ret > 0)
					printf("less than a page: %d\n", ret);
			} else if (ret == pagesize) {
				printf("page size cut\n");
			} else if (buf[pagesize] != 'h') {
				printf("broken data\n");
			}
		}
	}
	return 0;
}

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

             reply	other threads:[~2007-06-20 15:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-20 14:52 Timo Sirainen [this message]
2007-06-20 15:48 ` SMP read() stopping at memory page boundaries Timo Sirainen
2007-06-20 16:22   ` Ray Lee
2007-06-20 16:57     ` Timo Sirainen
2007-06-20 17:05       ` Alan Cox
2007-06-20 17:48         ` Timo Sirainen
2007-06-20 23:22           ` Jiri Kosina

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=1182351171.3768.65.camel@hurina \
    --to=tss@iki.fi \
    --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