Skip to contentSkip to conversation

ipc: unlock on failure

PATCH Superseded Aaron Conole 5 participants 8 messages May 6, 2019 – May 9, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipc: unlock on failure

Reported by Coverity.

Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>
---
 lib/librte_eal/common/eal_common_proc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_proc.c +3 -1
index d23728604..3498e6b07 100644
@@ -1005,8 +1005,10 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		/* unlocks the mutex while waiting for response,
 		 * locks on receive
 		 */
-		if (mp_request_sync(path, req, reply, &end))
+		if (mp_request_sync(path, req, reply, &end)) {
+			pthread_mutex_unlock(&pending_requests.lock);
 			goto err;
+		}
 	}
 	pthread_mutex_unlock(&pending_requests.lock);
 	/* unlock the directory */
Signature · 1 line
2.19.1
#1 lore raw
Quoted 29 lines · Aaron Conole

Reported by Coverity.

Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>
---
 lib/librte_eal/common/eal_common_proc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_proc.c +3 -1
index d23728604..3498e6b07 100644
@@ -1005,8 +1005,10 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
                /* unlocks the mutex while waiting for response,
                 * locks on receive
                 */
-               if (mp_request_sync(path, req, reply, &end))
+               if (mp_request_sync(path, req, reply, &end)) {
+                       pthread_mutex_unlock(&pending_requests.lock);
                        goto err;
+               }
        }
        pthread_mutex_unlock(&pending_requests.lock);
        /* unlock the directory */
Quoted 3 lines

--
2.19.1

Unfortunately, I think this is more complex than this.
We will leave a flock() pending and leak some resources from opendir().

Signature · 1 line
David Marchand
#2 lore raw
Quoted 29 lines · David Marchand

On Mon, May 6, 2019 at 3:11 PM Aaron Conole <[email protected]> wrote:

Reported by Coverity.

Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>

---

  lib/librte_eal/common/eal_common_proc.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/librte_eal/common/eal_common_proc.c b/lib/librte_eal/common/eal_common_proc.c index d23728604..3498e6b07 100644
--- a/lib/librte_eal/common/eal_common_proc.c
+++ b/lib/librte_eal/common/eal_common_proc.c
@@ -1005,8 +1005,10 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,

                 /* unlocks the mutex while waiting for response,
                  * locks on receive
                  */
 -               if (mp_request_sync(path, req, reply, &end))
 +               if (mp_request_sync(path, req, reply, &end)) {
 +                       pthread_mutex_unlock(&pending_requests.lock);
                         goto err;
 +               }
         }
         pthread_mutex_unlock(&pending_requests.lock);
         /* unlock the directory */
Quoted 5 lines

--
2.19.1

Unfortunately, I think this is more complex than this.
We will leave a flock() pending and leak some resources from opendir().

I completely forgot about it, while double checking the mp_request_sync

You're right, we leak the dirfd, still.

Okay, I think see a better way to write this.

#3 lore raw

PATCH v2 1/1 ipc: unlock on failure

In reply to Aaron Conole

Reported by Coverity.

Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>
---
 lib/librte_eal/common/eal_common_proc.c | 34 +++++++++++++------------
 1 file changed, 18 insertions(+), 16 deletions(-)
lib/librte_eal/common/eal_common_proc.c +18 -16
index d23728604..1a2259fe3 100644
@@ -934,7 +934,7 @@ int __rte_experimental
 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		const struct timespec *ts)
 {
-	int dir_fd, ret = 0;
+	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
 	struct timeval now;
@@ -947,7 +947,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	reply->msgs = NULL;
 
 	if (check_input(req) != 0)
-		goto err;
+		goto end;
 
 	if (internal_config.no_shconf) {
 		RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
@@ -957,7 +957,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	if (gettimeofday(&now, NULL) < 0) {
 		RTE_LOG(ERR, EAL, "Failed to get current time\n");
 		rte_errno = errno;
-		goto err;
+		goto end;
 	}
 
 	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
@@ -969,9 +969,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		pthread_mutex_lock(&pending_requests.lock);
 		ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
 		pthread_mutex_unlock(&pending_requests.lock);
-		if (ret)
-			goto err;
-		return ret;
+		goto end;
 	}
 
 	/* for primary process, broadcast request, and collect reply 1 by 1 */
@@ -979,7 +977,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	if (!mp_dir) {
 		RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
 		rte_errno = errno;
-		goto err;
+		goto end;
 	}
 
 	dir_fd = dirfd(mp_dir);
@@ -987,9 +985,8 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	if (flock(dir_fd, LOCK_SH)) {
 		RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
 			mp_dir_path);
-		closedir(mp_dir);
 		rte_errno = errno;
-		goto err;
+		goto close_end;
 	}
 
 	pthread_mutex_lock(&pending_requests.lock);
@@ -1006,21 +1003,26 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		 * locks on receive
 		 */
 		if (mp_request_sync(path, req, reply, &end))
-			goto err;
+			goto unlock_end;
 	}
+	ret = 0;
+
+unlock_end:
 	pthread_mutex_unlock(&pending_requests.lock);
 	/* unlock the directory */
 	flock(dir_fd, LOCK_UN);
 
+close_end:
 	/* dir_fd automatically closed on closedir */
 	closedir(mp_dir);
-	return ret;
 
-err:
-	free(reply->msgs);
-	reply->nb_received = 0;
-	reply->msgs = NULL;
-	return -1;
+end:
+	if (ret) {
+		free(reply->msgs);
+		reply->nb_received = 0;
+		reply->msgs = NULL;
+	}
+	return ret;
 }
 
 int __rte_experimental
Signature · 1 line
2.19.1
Reviewed-by on v2 1/1
#4 lore raw
Quoted 3 lines · Aaron Conole

Reported by Coverity.

Coverity issue: 340076

Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Quoted 114 lines
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>
---
 lib/librte_eal/common/eal_common_proc.c | 34 +++++++++++++------------
 1 file changed, 18 insertions(+), 16 deletions(-)
lib/librte_eal/common/eal_common_proc.c +2 -2
index d23728604..1a2259fe3 100644
@@ -934,7 +934,7 @@ int __rte_experimental
 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
                const struct timespec *ts)
 {
-       int dir_fd, ret = 0;
+       int dir_fd, ret = -1;
        DIR *mp_dir;
        struct dirent *ent;
        struct timeval now;
@@ -947,7 +947,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
        reply->msgs = NULL;
 
        if (check_input(req) != 0)
-               goto err;
+               goto end;
 
        if (internal_config.no_shconf) {

RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is
disabled\n");

lib/librte_eal/common/eal_common_proc.c +1 -1
@@ -957,7 +957,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
        if (gettimeofday(&now, NULL) < 0) {
                RTE_LOG(ERR, EAL, "Failed to get current time\n");
                rte_errno = errno;
-               goto err;
+               goto end;
        }
 
        end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
lib/librte_eal/common/eal_common_proc.c +1 -3
@@ -969,9 +969,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
                pthread_mutex_lock(&pending_requests.lock);
                ret = mp_request_sync(eal_mp_socket_path(), req, reply,
 &end);
                pthread_mutex_unlock(&pending_requests.lock);
-               if (ret)
-                       goto err;
-               return ret;
+               goto end;
        }
        /* for primary process, broadcast request, and collect reply 1 by

1 */

lib/librte_eal/common/eal_common_proc.c +1 -1
@@ -979,7 +977,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
        if (!mp_dir) {
                RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
 mp_dir_path);
                rte_errno = errno;
-               goto err;
+               goto end;
        }
        dir_fd = dirfd(mp_dir);
lib/librte_eal/common/eal_common_proc.c +1 -2
@@ -987,9 +985,8 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
        if (flock(dir_fd, LOCK_SH)) {
                RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
                        mp_dir_path);
-               closedir(mp_dir);
                rte_errno = errno;
-               goto err;
+               goto close_end;
        }
 
        pthread_mutex_lock(&pending_requests.lock);
lib/librte_eal/common/eal_common_proc.c +12 -7
@@ -1006,21 +1003,26 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
                 * locks on receive
                 */
                if (mp_request_sync(path, req, reply, &end))
-                       goto err;
+                       goto unlock_end;
        }
+       ret = 0;
+
+unlock_end:
        pthread_mutex_unlock(&pending_requests.lock);
        /* unlock the directory */
        flock(dir_fd, LOCK_UN);
 
+close_end:
        /* dir_fd automatically closed on closedir */
        closedir(mp_dir);
-       return ret;
 
-err:
-       free(reply->msgs);
-       reply->nb_received = 0;
-       reply->msgs = NULL;
-       return -1;
+end:
+       if (ret) {
+               free(reply->msgs);
+               reply->nb_received = 0;
+               reply->msgs = NULL;
+       }
+       return ret;
 }
 
Quoted 4 lines

int __rte_experimental
--
2.19.1

LGTM.

Signature · 1 line
David Marchand
Tested-by on v2 1/1
#5 lore raw
In reply to Aaron Conole
Quoted 9 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Aaron Conole
Sent: Monday, May 6, 2019 3:48 PM
To: [email protected]
Cc: Lipiec, Herakliusz <[email protected]>; [email protected]; Burakov, Anatoly <[email protected]>; David Marchand <[email protected]>
Subject: [dpdk-dev] [PATCH v2] ipc: unlock on failure

Quoted 8 lines

Reported by Coverity.

Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>

---

This silences ASAN as well. :)

Thanks,
D.

#7 lore raw

09/05/2019 13:50, Burakov, Anatoly:

Quoted 6 lines · Aaron Conole
Quoted 1 line

Reported by Coverity.

Maybe have a more descriptive commit message?

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Fixes a2a06860b8c4 ("ipc: fix memory leak on request failure")
Cc Herakliusz Lipiec <[email protected]>
Cc Anatoly Burakov <[email protected]>

---

Applied, thanks