All of lore.kernel.org
 help / color / mirror / Atom feed
From: Olaf Kirch <okir@suse.de>
To: Peter Staubach <staubach@redhat.com>
Cc: nfs@lists.sourceforge.net, akpm@osdl.org
Subject: Re: [PATCH fs/locks 3 of 3] Fix race conditions with file lock vs close
Date: Mon, 11 Jul 2005 15:20:41 +0200	[thread overview]
Message-ID: <20050711132041.GV27163@suse.de> (raw)
In-Reply-To: <42D26E41.1020605@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1038 bytes --]

On Mon, Jul 11, 2005 at 09:04:01AM -0400, Peter Staubach wrote:
> I didn't change the way that the code works in this area, I just changed the
> indentation.  As far as I can tell, the new code works the same way as the
> old code did, except that it also doesn't leave dangling locks 
> around...  :-)

Doh, you're right.  Time for a break :)

> I wrote a small program to reproduce the race that I thought that the 2.6
> code was not already handling.  It is attached.  (Please note that it needs
> to run with a large-ish delay value, something like 40 or 45 seconds.  There
> is some interaction with the Linux threading which I haven't had/made time
> to investigate yet.)  I can also attach the email that I sent upstream
> which describes my analysis, if you like.

Thanks, I'll give it a try.

While we're on the subject of swapping test cases, here's the one I've
been using.

Cheers
Olaf
-- 
Olaf Kirch   |  --- o --- Nous sommes du soleil we love when we play
okir@suse.de |    / | \   sol.dhoop.naytheet.ah kin.ir.samse.qurax

[-- Attachment #2: fusi-lock-crasher.c --]
[-- Type: text/plain, Size: 2792 bytes --]

/* butchered test case for lock/close problems */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>

#define	MAXLOOPS	10000000

int	fd, i = 0;
int	failures = 0;
int	obtained = 0;
int	running = 1;

void
alrm_hndl(int sig)
{
	printf ("Timeout\n");
}

int
get_lock (int fd)
{
	struct flock    lock_it;
	int		res = 0;

#if 0
	lock_it.l_type = F_WRLCK;
	lock_it.l_whence = SEEK_SET;
	lock_it.l_start = 0;
	lock_it.l_len = 0;

	if (fcntl (fd, F_GETLK, &lock_it) < 0) {
		if (errno != EBADF)
			perror("fcntl(GETLK)");
		return (-1);
	}

	if (lock_it.l_type != F_UNLCK)
		printf("File is locked!\n");
#endif

	lock_it.l_type = F_WRLCK;
	lock_it.l_whence = SEEK_SET;
	lock_it.l_start = 0;
	lock_it.l_len = 0;

	if (fcntl (fd, F_SETLK, &lock_it) < 0) {
		if (errno == EINTR) {
			printf("Timed out.\n");
			return 0;
		}
		if (errno != EBADF && errno != EAGAIN)
			perror ("fcntl(F_WRLCK)");
		failures++;
		res = -1;
	} else {
		obtained++;
	}

	return res;
}


int
release_lock (int fd)
{
	struct flock    lock_it;

	lock_it.l_type = F_UNLCK;
	lock_it.l_whence = SEEK_SET;
	lock_it.l_start = 0;
	lock_it.l_len = 0;

	if (fcntl (fd, F_SETLKW, &lock_it) < 0) {
		if (errno == EINTR)
			return 0;
		if (errno != EBADF)
			perror ("fcntl(F_UNLCK)");
		return -1;
	}
	return 0;
}

void *
loop(void *arg)
{
	printf("Locking "); fflush(stdout);
	while (running) {
		if (get_lock(fd) >= 0) {
			write(1, ".", 1);
			/* usleep(1); */
			if (release_lock(fd) < 0) {
				/* printf("Unlock failed!\n"); */
			} 
		}
	} 
	printf("\nLock thread done, %d successful calls, %d failures\n",
			obtained, failures);
	return (NULL);
}

void *
c_loop(void *arg)
{
	char*	fn = (char*) arg;
	while (running) {
		close(fd);
		if ((fd = open (fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
			perror ("open");
		//usleep(1);
		i++;
	}
	printf("Open thread done\n");
	return NULL;
}

int
main(int argc, char** argv)
{
#define THREADS 1

	struct sigaction act;
	int		i;
	pthread_t	thread[THREADS];
	pthread_t	close_thread;

	if(argc != 2) {
		fprintf(stderr, "%s <path>\n", argv[0]);
		exit(-1);
	}

	memset(&act, 0, sizeof(act));
	act.sa_handler = alrm_hndl;
	if (sigaction (SIGALRM, &act, NULL) < 0) {
		perror("sigaction");
		return 1;
	}

	if ((fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
		perror ("open");
		return 1;
	}

	for (i = 0; i < THREADS; i++) {
		pthread_create(&thread[i], NULL, loop, (void*) NULL);
	}
	usleep(100000);
	pthread_create(&close_thread, NULL, c_loop, (void*) argv[1]);

	sleep(5);

	running = 0;
	for (i = 0; i < THREADS; i++)
		pthread_join(thread[i], NULL);

	return 0;
}

  reply	other threads:[~2005-07-11 13:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-11 10:32 [PATCH fs/locks 3 of 3] Fix race conditions with file lock vs close Olaf Kirch
2005-07-11 12:32 ` Peter Staubach
2005-07-11 12:54   ` Olaf Kirch
2005-07-11 13:04     ` Peter Staubach
2005-07-11 13:20       ` Olaf Kirch [this message]
2005-07-11 13:18     ` Trond Myklebust

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=20050711132041.GV27163@suse.de \
    --to=okir@suse.de \
    --cc=akpm@osdl.org \
    --cc=nfs@lists.sourceforge.net \
    --cc=staubach@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.