PATCH: Add memory barrier to malloc

H. J. Lu hjl@lucon.org
Sat May 3 19:56:00 GMT 2003


On Sat, May 03, 2003 at 10:40:37AM -0700, H. J. Lu wrote:
> We got corrupted linked list in malloc due to memory ordering issues
> on our ia64 MP machine. This URL
> 
> http://lse.sourceforge.net/locking/wmbdd.html
> 
> has some relevant info. We need to make memory barrier available
> to the whole glibc, even user application source codes. This patch
> adds memory-barrier.h to glibc internal headers and adds one memory
> barrier to malloc.
> 

I added nptl and fixed a typo.


H.J.
-------------- next part --------------
libc/

2003-05-02  H.J. Lu <hongjiu.lu@intel.com>

	* malloc/arena.c (arena_get2): Add WRITE_MEMORY_BARRIER.

	* thread-m.h: Include <memory-barrier.h> for glibc.
	(MEMORY_BARRIER): Provide default.
	(READ_MEMORY_BARRIER): Likewise.
	(WRITE_MEMORY_BARRIER): Likewise.

	* sysdeps/generic/memory-barrier.h: New file.

linuxthreads/

2003-05-02  H.J. Lu <hongjiu.lu@intel.com>

	* sysdeps/pthread/memory-barrier.h : New file.

nptl/

2003-05-02  H.J. Lu <hongjiu.lu@intel.com>

	* sysdeps/pthread/memory-barrier.h : New file.

--- libc/linuxthreads/sysdeps/pthread/memory-barrier.h.barrier	2003-05-03 12:35:12.000000000 -0700
+++ libc/linuxthreads/sysdeps/pthread/memory-barrier.h	2003-05-03 12:35:12.000000000 -0700
@@ -0,0 +1,38 @@
+/* Pthread memory barrier.
+
+   Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifndef _PTHREAD_MEMORY_BARRIER_H
+#define _PTHREAD_MEMORY_BARRIER_H 1
+
+#include <pt-machine.h>
+
+/* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
+   architecture doesn't need a memory barrier instruction (e.g. Intel
+   x86).  Still we need the compiler to respect the barrier and emit
+   all outstanding operations which modify memory.  Some architectures
+   distinguish between full, read and write barriers.  */
+
+#ifndef MEMORY_BARRIER
+#define MEMORY_BARRIER() asm ("" : : : "memory")
+#endif
+
+#include <sysdeps/generic/memory-barrier.h>
+
+#endif /* memory-barrier.h */
--- libc/malloc/arena.c.barrier	2002-12-26 16:36:41.000000000 -0800
+++ libc/malloc/arena.c	2003-05-03 12:35:12.000000000 -0700
@@ -758,6 +758,7 @@ arena_get2(a_tsd, size) mstate a_tsd; si
   /* Add the new arena to the global list.  */
   (void)mutex_lock(&list_lock);
   a->next = main_arena.next;
+  WRITE_MEMORY_BARRIER ();
   main_arena.next = a;
   (void)mutex_unlock(&list_lock);
 
--- libc/malloc/thread-m.h.barrier	2003-04-07 08:09:36.000000000 -0700
+++ libc/malloc/thread-m.h	2003-05-03 12:35:12.000000000 -0700
@@ -32,6 +32,7 @@
 #if defined(_LIBC) /* The GNU C library, a special case of Posix threads */
 
 #include <bits/libc-lock.h>
+#include <memory-barrier.h>
 
 #ifdef PTHREAD_MUTEX_INITIALIZER
 
@@ -317,4 +318,14 @@ typedef void *tsd_key_t;
 
 #endif /* defined(NO_THREADS) */
 
+#ifndef MEMORY_BARRIER
+#define MEMORY_BARRIER()
+#endif
+#ifndef READ_MEMORY_BARRIER
+#define READ_MEMORY_BARRIER() MEMORY_BARRIER()
+#endif
+#ifndef WRITE_MEMORY_BARRIER
+#define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
+#endif
+
 #endif /* !defined(_THREAD_M_H) */
--- libc/nptl/sysdeps/pthread/memory-barrier.h.barrier	2003-05-03 12:43:47.000000000 -0700
+++ libc/nptl/sysdeps/pthread/memory-barrier.h	2003-05-03 12:35:12.000000000 -0700
@@ -0,0 +1,38 @@
+/* Pthread memory barrier.
+
+   Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifndef _PTHREAD_MEMORY_BARRIER_H
+#define _PTHREAD_MEMORY_BARRIER_H 1
+
+#include <pt-machine.h>
+
+/* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
+   architecture doesn't need a memory barrier instruction (e.g. Intel
+   x86).  Still we need the compiler to respect the barrier and emit
+   all outstanding operations which modify memory.  Some architectures
+   distinguish between full, read and write barriers.  */
+
+#ifndef MEMORY_BARRIER
+#define MEMORY_BARRIER() asm ("" : : : "memory")
+#endif
+
+#include <sysdeps/generic/memory-barrier.h>
+
+#endif /* memory-barrier.h */
--- libc/sysdeps/generic/memory-barrier.h.barrier	2003-05-03 12:35:12.000000000 -0700
+++ libc/sysdeps/generic/memory-barrier.h	2003-05-03 12:35:12.000000000 -0700
@@ -0,0 +1,34 @@
+/* Generic memory barrier.
+
+   Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifndef _MEMORY_BARRIER_H
+#define _MEMORY_BARRIER_H 1
+
+#ifndef MEMORY_BARRIER
+#define MEMORY_BARRIER()
+#endif
+#ifndef READ_MEMORY_BARRIER
+#define READ_MEMORY_BARRIER() MEMORY_BARRIER()
+#endif
+#ifndef WRITE_MEMORY_BARRIER
+#define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
+#endif
+
+#endif /* memory-barrier.h */


More information about the Libc-alpha mailing list