All of lore.kernel.org
 help / color / mirror / Atom feed
* fanotify hangs with multi-threaded programs
@ 2012-06-13  9:20 Jussi Maki
  2012-06-13 11:34 ` Lino Sanfilippo
  0 siblings, 1 reply; 2+ messages in thread
From: Jussi Maki @ 2012-06-13  9:20 UTC (permalink / raw)
  To: LinoSanfilippo, eparis; +Cc: linux-kernel

Hi,

Ran into a hang in open with a fanotify event listener which delegated the
processing via unix socket to a multi-threaded program that was opening
files from several threads. A patch for fixing this was posted in december
(http://marc.info/?l=linux-kernel&m=131822913806350&w=2), but it
wasn't merged.

Are there any plans for fixing this? This is a show stopper here as we can't
really use fanotify in production if there's a chance that multi-threaded
programs might hang any time they use open.

Here's test programs to reproduce this.

First one marks mount "/" for open perm events and returns
allow for everything and the second spawns 5 threads
which all loop opening a temporary file. If the first program
is running threads in the second will hang in
fanotify_get_response_from_access.

----
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/fanotify.h>

#define xperror(args...) { perror(args); exit(1); }

int main()
{
        int fd = fanotify_init(FAN_CLASS_CONTENT, O_RDONLY);
        if (fd < 0) {
                xperror("init");
        }

        int ret = fanotify_mark(fd, FAN_MARK_ADD|FAN_MARK_MOUNT,
                                             FAN_OPEN_PERM, AT_FDCWD, "/");
        if (ret < 0) {
                xperror("mark");
        }

        for(;;) {
                struct fanotify_event_metadata event;
                if (read(fd, &event, sizeof(event)) < 0) {
                        xperror("read");
                }

                struct fanotify_response resp = {
                        .fd = event.fd,
                        .response = FAN_ALLOW,
                };

                if (write(fd, &resp, sizeof(resp)) < 0) {
                        xperror("write");
                }

                close(event.fd);
        }
}

----

#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

static char *testfile;

void *thread(void *ctx)
{
	char buf[512];
	int fd;
	for(;;) {
		fd = open(testfile, O_RDONLY);
		if (fd < 0) abort();
		close(fd);
	}
}

int main(int argc, char **argv)
{
	int i = 0;
	testfile = strdup("/tmp/fantest.XXXXXX");
	int fd = mkstemp(testfile);
	close(fd);

	for(i = 0; i < 5; i++) {
		pthread_t tid;
		pthread_create(&tid, NULL, thread, NULL);
	}
	sleep(5);
	unlink(testfile);
}

----

 -- Jussi

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: fanotify hangs with multi-threaded programs
  2012-06-13  9:20 fanotify hangs with multi-threaded programs Jussi Maki
@ 2012-06-13 11:34 ` Lino Sanfilippo
  0 siblings, 0 replies; 2+ messages in thread
From: Lino Sanfilippo @ 2012-06-13 11:34 UTC (permalink / raw)
  To: Jussi Maki; +Cc: eparis, linux-kernel, linux-fsdevel

I sent a patch some time ago that should fix this issue
(see http://marc.info/?l=linux-fsdevel&m=133246703731488&w=2).
I think its in linux-next now.

Lino

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-06-13 11:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-13  9:20 fanotify hangs with multi-threaded programs Jussi Maki
2012-06-13 11:34 ` Lino Sanfilippo

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.