Skip to contentSkip to conversation

change thread-unsafe readdir to thread-safe readdir_r calls

PATCH In review Steven Dake 3 participants 9 messages Jul 7, 2010 7 messages not in the archive InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 change thread-unsafe readdir to thread-safe readdir_r calls

From: Steven Dake <sdake-AqqL/iNfm4CbU/[email protected]>

The readdir POSIX api is not thread safe. This presents problems in multithreaded programs that use the libibverbs APIs. This patch has been tested with a libibverbs application (http://www.corosync.org) on Mellanox MT26428 cards.

---
 src/init.c |   31 +++++++++++++++++++++++++------
 1 files changed, 25 insertions(+), 6 deletions(-)
src/init.c +25 -6
index 4f0130e..ac3efdc 100644
@@ -46,10 +46,12 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <dirent.h>
+#include <stddef.h>
 #include <errno.h>
 
 #include "ibverbs.h"
 
+
 HIDDEN int abi_ver;
 
 struct ibv_sysfs_dev {
@@ -85,6 +87,7 @@ static int find_sysfs_devs(void)
 	struct ibv_sysfs_dev *sysfs_dev = NULL;
 	char value[8];
 	int ret = 0;
+	struct dirent *buf;
 
 	snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
 		 ibv_get_sysfs_path());
@@ -93,7 +96,12 @@ static int find_sysfs_devs(void)
 	if (!class_dir)
 		return ENOSYS;
 
-	while ((dent = readdir(class_dir))) {
+	buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1);
+	if (buf == NULL) {
+		goto out_closedir;
+		ret = ENOMEM;
+	}
+	while (readdir_r(class_dir, buf, &dent) == 0 && dent) {
 		struct stat buf;
 
 		if (dent->d_name[0] == '.')
@@ -146,9 +154,11 @@ static int find_sysfs_devs(void)
 	}
 
  out:
+	free (buf);
 	if (sysfs_dev)
 		free(sysfs_dev);
 
+out_closedir:
 	closedir(class_dir);
 	return ret;
 }
@@ -290,26 +300,31 @@ static void read_config_file(const char *path)
 	fclose(conf);
 }
 
-static void read_config(void)
+static int read_config(void)
 {
 	DIR *conf_dir;
 	struct dirent *dent;
+	struct dirent *buf;
 	char *path;
 
 	conf_dir = opendir(IBV_CONFIG_DIR);
 	if (!conf_dir) {
 		fprintf(stderr, PFX "Warning: couldn't open config directory '%s'.\n",
 			IBV_CONFIG_DIR);
-		return;
+		return 0;
 	}
 
-	while ((dent = readdir(conf_dir))) {
+	buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1);
+	if (buf == NULL)
+		return ENOMEM;
+
+	while (readdir_r(conf_dir, buf, &dent) == 0 && dent) {
 		struct stat buf;
 
 		if (asprintf(&path, "%s/%s", IBV_CONFIG_DIR, dent->d_name) < 0) {
 			fprintf(stderr, PFX "Warning: couldn't read config file %s/%s.\n",
 				IBV_CONFIG_DIR, dent->d_name);
-			return;
+			return 0;
 		}
 
 		if (stat(path, &buf)) {
@@ -326,7 +341,9 @@ next:
 		free(path);
 	}
 
+	free(buf);
 	closedir(conf_dir);
+	return 0;
 }
 
 static struct ibv_device *try_driver(struct ibv_driver *driver,
@@ -471,7 +488,9 @@ HIDDEN int ibverbs_init(struct ibv_device ***list)
 
 	check_memlock_limit();
 
-	read_config();
+	ret = read_config();
+	if (ret)
+		return -ret;
 
 	ret = find_sysfs_devs();
 	if (ret)
Signature · 6 lines
1.6.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to a message not in the archive
Quoted 6 lines · Steven Dake

From: Steven Dake <sdake-AqqL/iNfm4CbU/[email protected]>

The readdir POSIX api is not thread safe. This presents problems in multithreaded programs that use the libibverbs APIs. This patch has been tested with a libibverbs application (http://www.corosync.org) on Mellanox MT26428 cards.

Well, that isn't true on glibc.

The buffer returned by readdir() is stored in the DIR * and is allocated during opendir().

The only time you hit thread safety issues is if multiple threads attempt to use the same DIR at once - glibc has internal locking to protect the DIR, but the buffer would be re-used.

Since libibverbs doesn't pass the DIR * between threads this usage is safe.

It isn't entirely clear to me if POSIX definition of readdir as non-rentrant applies to the general case or to the specific case I discuss above. Certainly, any implementation can do as glibc does and allocate the buffer in the DIR *.

But, you surely could not have seen a bug on Linux that was caused by this - so why was this patch prepared?

BTW, POSIX also says that NAME_MAX is optional, so you have traded one unlikely portability problem for another unlikely portability problem :)

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw
In reply to a message not in the archive
Quoted 4 lines

The readdir POSIX api is not thread safe. This presents problems in multithreaded programs that use the libibverbs APIs. This patch has been tested with a libibverbs application (http://www.corosync.org) on Mellanox MT26428 cards.

This is a bit vague. Is the problem that other threads may do readdir() while libibverbs initializes?

Quoted 4 lines

#include "ibverbs.h"

+
HIDDEN int abi_ver;

whitespace damage here.

Review

Quoted 1 line
(quoted diff) +1 -0
+	buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1);

cast of void* is not needed. Also it seems this could be alloca() and simplify things (avoid error checking and freeing).

Thanks,
Roland

Signature · 6 lines
Roland Dreier <[email protected]> || For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw
In reply to a message not in the archive
Quoted 20 lines · Jason Gunthorpe
Quoted 6 lines · Steven Dake

From: Steven Dake<sdake-AqqL/iNfm4CbU/[email protected]>

The readdir POSIX api is not thread safe. This presents problems in multithreaded programs that use the libibverbs APIs. This patch has been tested with a libibverbs application (http://www.corosync.org) on Mellanox MT26428 cards.

Well, that isn't true on glibc.

The buffer returned by readdir() is stored in the DIR * and is allocated during opendir().

The only time you hit thread safety issues is if multiple threads attempt to use the same DIR at once - glibc has internal locking to protect the DIR, but the buffer would be re-used.

Since libibverbs doesn't pass the DIR * between threads this usage is safe.

Quoted 8 lines

It isn't entirely clear to me if POSIX definition of readdir as non-rentrant applies to the general case or to the specific case I discuss above. Certainly, any implementation can do as glibc does and allocate the buffer in the DIR *.

But, you surely could not have seen a bug on Linux that was caused by this - so why was this patch prepared?

We redefine all the non-thread-safe posix calls in our package (such as readdir) to assert(0). This prevents their usage in our app. The reason we do this is to prevent plugin developers from writing plugins that use non-thread-safe posix APIs. Our app is portable, where the glibc semantics you describe above are not present. POSIX is blank on this issue, and was addressed in readdir_r.

Regards
-steve

Quoted 8 lines

BTW, POSIX also says that NAME_MAX is optional, so you have traded one unlikely portability problem for another unlikely portability problem :)

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw
In reply to a message not in the archive
Quoted 8 lines · Roland Dreier
Quoted 4 lines

The readdir POSIX api is not thread safe. This presents problems in multithreaded programs that use the libibverbs APIs. This patch has been tested with a libibverbs application (http://www.corosync.org) on Mellanox MT26428 cards.

This is a bit vague. Is the problem that other threads may do readdir() while libibverbs initializes?

Quoted 7 lines
Quoted 4 lines
  #include "ibverbs.h"

+
HIDDEN int abi_ver;

whitespace damage here.

thanks I'll fix

Quoted 5 lines
Quoted 1 line

+ buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1);

cast of void* is not needed. Also it seems this could be alloca() and simplify things (avoid error checking and freeing).

yes.. I can change if you like. Never been fond of alloca because of its limitation on error checking with stack overflow which is unlikely in this call path.

Regards
-steve

Quoted 3 lines

Thanks,
Roland

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to a message not in the archive
Quoted 9 lines · Steven Dake
Quoted 2 lines

But, you surely could not have seen a bug on Linux that was caused by this - so why was this patch prepared?

We redefine all the non-thread-safe posix calls in our package (such as readdir) to assert(0). This prevents their usage in our app. The reason we do this is to prevent plugin developers from writing plugins that use non-thread-safe posix APIs. Our app is portable, where the glibc semantics you describe above are not present. POSIX is blank on this issue, and was addressed in readdir_r.

Well, if you are already creating wrappers, wouldn't it be better to provide wrappers that provide the glibc functionality on platforms you port to that don't already do that, rather than forbid a perfectly functional, and thread safe, call?

FWIW, I've always considered readdir_r to be broken, you pass in a buffer without passing in a size and hope everything works out. Your proposed patch to libibverbs is also not-portable because it uses NAME_MAX, not pathconf.. Sigh POSIX.

I guess, as a general question, do you know of any libc implementations that actually use a static buffer for readdir and would require readdir_r to function properly?

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to a message not in the archive
Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Steven Dake
Quoted 2 lines

But, you surely could not have seen a bug on Linux that was caused by this - so why was this patch prepared?

We redefine all the non-thread-safe posix calls in our package (such as readdir) to assert(0). This prevents their usage in our app. The reason we do this is to prevent plugin developers from writing plugins that use non-thread-safe posix APIs. Our app is portable, where the glibc semantics you describe above are not present. POSIX is blank on this issue, and was addressed in readdir_r.

Quoted 5 lines

Well, if you are already creating wrappers, wouldn't it be better to provide wrappers that provide the glibc functionality on platforms you port to that don't already do that, rather than forbid a perfectly functional, and thread safe, call?

Not sure how to map a readdir to readdir_r on a thread unsafe system...
perhaps with thread keys. In any regard, seems pointless, readdir_r
is there and what POSIX specifies for this purpose.

Quoted 2 lines

FWIW, I've always considered readdir_r to be broken, you pass in a buffer without passing in a size and hope everything works out. Your

I also have objections to some POSIX standard APIs - however, using non-reentrant POSIX apis when reentrant POSIX APIs are available seems counterproductive.

Quoted 3 lines

proposed patch to libibverbs is also not-portable because it uses NAME_MAX, not pathconf.. Sigh POSIX.

On bsd/solaris/darwin/linux, NAME_MAX is defined. Not sure which other POSIX systems one would care about..

Quoted 3 lines

I guess, as a general question, do you know of any libc implementations that actually use a static buffer for readdir and would require readdir_r to function properly?

I have not looked at each libc's implementation to see if it matches the glibc behaviour. The resolution provided by glibc seems like an enhancement over POSIX, where the behaviour is unspecified.

Regards
-steve

Quoted 6 lines

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
In reply to a message not in the archive
Quoted 3 lines · Steven Dake

Not sure how to map a readdir to readdir_r on a thread unsafe system... perhaps with thread keys. In any regard, seems pointless, readdir_r is there and what POSIX specifies for this purpose.

Override opendir and allocate the buffer then and return a pointer to it through a custom 'DIR *'.

Quoted 6 lines
Quoted 2 lines

FWIW, I've always considered readdir_r to be broken, you pass in a buffer without passing in a size and hope everything works out. Your

I also have objections to some POSIX standard APIs - however, using non-reentrant POSIX apis when reentrant POSIX APIs are available seems counterproductive.

Well, if the non-reentrant ones are badly designed I'm not sure it is a good trade.. Ie Solaris's man pages say:

It is safe to use readdir() in a threaded application, so long as only one thread reads from the directory stream at any given time. The readdir() function is generally preferred over the readdir_r() function.

Also see

http://lists.grok.org.uk/pipermail/full-disclosure/2005-November/038295.html

The horribleness of readdir_r is well documented, and is partly why libc's advocate thread safe readdir() desipte the existence of readdir_r.

Quoted 5 lines
Quoted 2 lines

proposed patch to libibverbs is also not-portable because it uses NAME_MAX, not pathconf.. Sigh POSIX.

On bsd/solaris/darwin/linux, NAME_MAX is defined. Not sure which other POSIX systems one would care about..

If all you care able is bsd/solaris/darwin/linux then this is a non-problem, AFAIK they have sane libc's :) Ie I just checked and openbsd libc has been using a dynamic buffer allocated at opendir since 1996.

If you care about theortical portability then you have to worry about NAME_MAX too..

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw
In reply to a message not in the archive
Quoted 38 lines · Jason Gunthorpe
Quoted 3 lines · Steven Dake

Not sure how to map a readdir to readdir_r on a thread unsafe system... perhaps with thread keys. In any regard, seems pointless, readdir_r is there and what POSIX specifies for this purpose.

Override opendir and allocate the buffer then and return a pointer to it through a custom 'DIR *'.

Quoted 6 lines
Quoted 2 lines

FWIW, I've always considered readdir_r to be broken, you pass in a buffer without passing in a size and hope everything works out. Your

I also have objections to some POSIX standard APIs - however, using non-reentrant POSIX apis when reentrant POSIX APIs are available seems counterproductive.

Well, if the non-reentrant ones are badly designed I'm not sure it is a good trade.. Ie Solaris's man pages say:

It is safe to use readdir() in a threaded application, so long as only one thread reads from the directory stream at any given time. The readdir() function is generally preferred over the readdir_r() function.

Also see

http://lists.grok.org.uk/pipermail/full-disclosure/2005-November/038295.html

The horribleness of readdir_r is well documented, and is partly why libc's advocate thread safe readdir() desipte the existence of readdir_r.

Quoted 5 lines
Quoted 2 lines

proposed patch to libibverbs is also not-portable because it uses NAME_MAX, not pathconf.. Sigh POSIX.

On bsd/solaris/darwin/linux, NAME_MAX is defined. Not sure which other POSIX systems one would care about..

Quoted 8 lines

If all you care able is bsd/solaris/darwin/linux then this is a non-problem, AFAIK they have sane libc's :) Ie I just checked and openbsd libc has been using a dynamic buffer allocated at opendir since 1996.

If you care about theortical portability then you have to worry about NAME_MAX too..

Thanks I'll look into NAME_MAX for my server.

Regards
-steve

Quoted 1 line

Jason

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html