From: "George G. Davis" <gdavis@mvista.com>
To: linux-kernel@vger.kernel.org
Subject: Re: [RFC][PATCH] Fix hang in posix_locks_deadlock()
Date: Wed, 17 Oct 2007 19:41:20 -0400 [thread overview]
Message-ID: <20071017234120.GH3785@mvista.com> (raw)
In-Reply-To: <20071017185157.GC3785@mvista.com>
[-- Attachment #1: Type: text/plain, Size: 359 bytes --]
On Wed, Oct 17, 2007 at 02:51:57PM -0400, George G. Davis wrote:
> Not sure if this is the correct fix but it does resolve the hangs we're
> observing in posix_locks_deadlock(). Comments greatly appreciated...
Attached is a test case which exhibits the hang on an F7 host with all
updates applied. Also occurs on linux-2.6.git latest.
--
Regards,
George
[-- Attachment #2: posix_locks_deadlock-hang.c --]
[-- Type: text/plain, Size: 2243 bytes --]
// gcc -o fcntltest -pthread posix_locks_deadlock-hang.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU 1
#include <fcntl.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#define NUM_THREADS 100
#define NUM_PROCS 5
#define NUM_FILES 64
pthread_mutex_t locks[NUM_FILES];
pthread_mutex_t gross_lock = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
static void *thread_routine(int number)
{
const char *fname = NULL;
int f, rc;
struct flock lock_it;
pthread_mutex_t *lock = NULL;
// get the gross lock
pthread_mutex_lock(&gross_lock);
lock_it.l_whence = SEEK_SET; /* from current point */
lock_it.l_start = 0; /* -"- */
lock_it.l_len = 0; /* until end of file */
lock_it.l_type = F_WRLCK; /* set exclusive/write lock */
lock_it.l_pid = 0; /* pid not actually interesting */
fname = calloc(1, 500);
snprintf(fname, 499, "%i.txt", number);
lock = &locks[number];
if (pthread_mutex_trylock(lock) != 0) {
pthread_mutex_unlock(&gross_lock);
pthread_mutex_lock(lock);
pthread_mutex_unlock(&gross_lock);
}
int randm = (rand() % 32768) * 10;
printf("Opening %s...\n", fname);
f = open(fname, O_TRUNC | O_CREAT | O_WRONLY, 0644);
if (f < 0) {
perror(fname);
exit(1);
}
printf("Locking %s...\n", fname);
// release the gross lock before attempting the fcntl
pthread_mutex_unlock(&gross_lock);
rc = fcntl(f, F_SETLKW, &lock_it);
sleep(5);
// gain the gross lock again
pthread_mutex_lock(&gross_lock);
if (rc < 0) {
perror(fname);
usleep(randm);
pthread_mutex_unlock(lock);
pthread_mutex_unlock(&gross_lock);
exit(1);
}
usleep(randm);
// unlocks the file
close(rc);
pthread_mutex_unlock(lock);
pthread_mutex_unlock(&gross_lock);
return NULL;
}
int main(int argc, char **argv)
{
int i, rc;
pthread_t tids[NUM_THREADS];
memset(tids, 0, sizeof(tids));
memset(locks, 0, sizeof(locks));
for (i = 0; i < NUM_PROCS; i++) {
pid_t mypid = fork();
if (mypid == 0)
break;
}
for (i = 0; i < NUM_THREADS; i++) {
rc = pthread_create(&tids[i], NULL, thread_routine,
(rand() % NUM_FILES));
if (rc != 0) {
tids[i] = 0;
}
}
for (i = 0; i < 500; i++) {
if (tids[i] != 0) {
pthread_join(tids[i], NULL);
}
}
return 0;
}
next prev parent reply other threads:[~2007-10-17 23:41 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-17 18:51 [RFC][PATCH] Fix hang in posix_locks_deadlock() George G. Davis
2007-10-17 23:41 ` George G. Davis [this message]
2007-10-18 18:57 ` George G. Davis
2007-10-26 17:07 ` J. Bruce Fields
2007-10-26 22:47 ` J. Bruce Fields
2007-10-28 17:31 ` [PATCH] locks: fix possible infinite loop in posix deadlock detection J. Bruce Fields
2007-10-28 17:43 ` [RFC, PATCH] locks: remove " J. Bruce Fields
2007-10-28 18:27 ` Matthew Wilcox
2007-10-28 18:40 ` Alan Cox
2007-10-28 20:11 ` Matthew Wilcox
2007-10-28 21:38 ` Alan Cox
2007-10-28 21:45 ` Jiri Kosina
2007-10-28 23:38 ` Matthew Wilcox
2007-10-28 23:44 ` Alan Cox
2007-10-28 21:50 ` Trond Myklebust
2007-10-28 22:41 ` Matthew Wilcox
2007-10-28 22:48 ` Alan Cox
2007-10-28 22:55 ` Matthew Wilcox
2007-10-28 23:38 ` Alan Cox
2007-10-29 2:29 ` J. Bruce Fields
2007-10-29 8:08 ` Alan Cox
2007-10-29 9:15 ` Jiri Kosina
2007-10-30 15:35 ` J. Bruce Fields
2007-10-28 22:55 ` Jiri Kosina
2007-10-28 23:31 ` Matthew Wilcox
2007-10-29 9:11 ` Jiri Kosina
2007-10-29 2:10 ` J. Bruce Fields
2007-10-29 3:26 ` Trond Myklebust
2007-10-29 1:13 ` J. Bruce Fields
2007-10-29 8:06 ` Alan Cox
2007-10-30 15:51 ` J. Bruce Fields
2007-10-30 15:20 ` [PATCH, RESEND] locks: fix possible infinite loop in " J. Bruce Fields
2007-10-30 15:35 ` Alan Cox
2007-10-28 17:47 ` [RFC][PATCH] Fix hang in posix_locks_deadlock() J. Bruce Fields
2007-11-02 15:05 ` George G. Davis
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=20071017234120.GH3785@mvista.com \
--to=gdavis@mvista.com \
--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 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.