From nobody@FreeBSD.org  Sun May 17 07:51:19 2009
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 89062106567B
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 May 2009 07:51:19 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 7913F8FC16
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 May 2009 07:51:19 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id n4H7pJGm009459
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 May 2009 07:51:19 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id n4H7pJ9B009456;
	Sun, 17 May 2009 07:51:19 GMT
	(envelope-from nobody)
Message-Id: <200905170751.n4H7pJ9B009456@www.freebsd.org>
Date: Sun, 17 May 2009 07:51:19 GMT
From: Mikolaj Golub <to.my.trociny@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Memory leak in gcclibs/libgomp
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         134604
>Category:       gnu
>Synopsis:       [libgcc] [patch] Memory leak in gcclibs/libgomp
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          suspended
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun May 17 08:00:07 UTC 2009
>Closed-Date:    
>Last-Modified:  Thu Aug 26 12:01:38 UTC 2010
>Originator:     Mikolaj Golub
>Release:        
>Organization:
>Environment:
>Description:
#include <omp.h>

int n = 4, m = 2;

int main () {
        for (;;) {
                int i;

#pragma omp parallel for num_threads(m)
                for(i = 0; i < 1; i++) {}

#pragma omp parallel for num_threads(n)
                for(i = 0; i < 1; i++) {}

        }

        return 0;
}

When run on freebsd, this test shows constant growth of virtual memory usage.
Memory growth is observed only when m != n.

There is some discussion of this problem on freebsd-hackers:

http://lists.freebsd.org/pipermail/freebsd-hackers/2009-May/028552.html

The problem has also been reported to gcc mainstream:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40174
>How-To-Repeat:
Compile the test shown above with -fopenmp and run.
>Fix:
The problem is in libgomp/team.c.  gomp_thread_start() does sem_init()
but sem_destroy() is never called.

Attached patch solves the problem for me.

Patch attached with submission follows:

--- contrib/gcclibs/libgomp/team.c.orig	2009-05-16 17:32:57.000000000 +0300
+++ contrib/gcclibs/libgomp/team.c	2009-05-16 19:16:37.000000000 +0300
@@ -164,9 +164,12 @@ new_team (unsigned nthreads, struct gomp
 static void
 free_team (struct gomp_team *team)
 {
+  int i;
   free (team->work_shares);
   gomp_mutex_destroy (&team->work_share_lock);
   gomp_barrier_destroy (&team->barrier);
+  for(i = 1; i < team->nthreads; i++)
+    gomp_sem_destroy (team->ordered_release[i]);
   gomp_sem_destroy (&team->master_release);
   free (team);
 }


>Release-Note:
>Audit-Trail:

From: Mikolaj Golub <to.my.trociny@gmail.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: gnu/134604: Memory leak in gcclibs/libgomp
Date: Sun, 17 May 2009 11:35:05 +0300

 Sorry, I have forgotten to mention this. I am running 7.2-STABLE but I think
 CURRENT is affected too.
 
 -- 
 Mikolaj Golub

From: Mikolaj Golub <to.my.trociny@gmail.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: gnu/134604: Memory leak in gcclibs/libgomp
Date: Thu, 21 May 2009 22:17:28 +0300

 --=-=-=
 
 This bug has been fixed in upstream for gcc 4.4/4.5.
 
 The developer has pointed out that correct place to call sem_destroy() is on
 returning from gomp_thread_start(). So the patch for libgomp from RELENG_7
 (gcc 4.2.1) would be even simpler then provided previously.
 
 -- 
 Mikolaj Golub
 
 
 --=-=-=
 Content-Disposition: inline; filename=team.c.patch.txt
 
 --- contrib/gcclibs/libgomp/team.c.orig	2009-05-16 17:32:57.000000000 +0300
 +++ contrib/gcclibs/libgomp/team.c	2009-05-21 22:04:24.000000000 +0300
 @@ -127,6 +127,7 @@ gomp_thread_start (void *xdata)
        while (local_fn);
      }
  
 +  gomp_sem_destroy (&thr->release);
    return NULL;
  }
  
 
 --=-=-=--
State-Changed-From-To: open->suspended 
State-Changed-By: arundel 
State-Changed-When: Thu Aug 26 11:58:05 UTC 2010 
State-Changed-Why:  
To fix this issue GPL3 licensed code would have to be imported into the FreeBSD 
source tree, which isn't possible due to licensing issues. 
Thus suspend this PR for now. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=134604 
>Unformatted:
