From patchwork Thu Jan 31 14:50:51 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: VandeVondele Joost X-Patchwork-Id: 217197 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 437252C007A for ; Fri, 1 Feb 2013 01:51:18 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1360248679; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=nswG11OpGbaNjT9fHqhE 6kL9LaM=; b=F4drUKzJXTMQ01drg4Om5hUNwKRHEbVe0jgpyfJoLl+NQ7KTRYb8 nlFDA0s7cUlaV+whtCZpRASZZLzt5r4RmdlF6EK3pSrwlU9rPJIMu6OhrbXNqlYQ 8EpCzjjz80NlmrCc2lfkos4oO+TG0sPmK/AQgoEsFLn3pH7z7TSzOBg= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=ESotBf2g/CVZn325M3PLqoL0UnNvcoZ0zycOJaYRJGoMqbeYG6UpD+fJ0CwTrf JfWgplZCebDn49l3cJk1uJaXg++AFuGI/gPoPXVHuGB5nijib/nIcqeUr3d6VUz3 s/tUTbe1Kb2XmR/e+4d32LByWTBH/6vUxOQzR1xr/l07A=; Received: (qmail 23741 invoked by alias); 31 Jan 2013 14:51:05 -0000 Received: (qmail 23729 invoked by uid 22791); 31 Jan 2013 14:51:03 -0000 X-SWARE-Spam-Status: No, hits=-3.4 required=5.0 tests=BAYES_00, KHOP_RCVD_UNTRUST, KHOP_SPAMHAUS_DROP, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from edge10.ethz.ch (HELO edge10.ethz.ch) (82.130.75.186) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 31 Jan 2013 14:50:55 +0000 Received: from CAS21.d.ethz.ch (172.31.51.111) by edge10.ethz.ch (82.130.75.186) with Microsoft SMTP Server (TLS) id 14.2.298.4; Thu, 31 Jan 2013 15:50:49 +0100 Received: from MBX11.d.ethz.ch ([fe80::25ef:802e:3bf1:31e5]) by CAS21.d.ethz.ch ([fe80::55ba:c4a5:d8a7:ab62%10]) with mapi id 14.02.0298.004; Thu, 31 Jan 2013 15:50:52 +0100 From: "VandeVondele Joost" To: "gcc-patches@gcc.gnu.org" CC: "dvyukov@gcc.gnu.org" , "jakub@redhat.com" Subject: Fix for PR55561 race condition in libgomp Date: Thu, 31 Jan 2013 14:50:51 +0000 Message-ID: <908103EDB4893A42920B21D3568BFD9308C78C5D@MBX11.d.ethz.ch> MIME-Version: 1.0 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org The attached patch fixes a race condition in libgomp. Based on the suggestions by Dmitry, and verified that it fixes the corresponding sanitizer warnings. 2012-12-30 Dmitry Vyukov PR libgomp/55561 * config/linux/wait.h (do_spin): Use atomic load for addr. * config/linux/ptrlock.c (gomp_ptrlock_get_slow): Use atomic for intptr and ptrlock. * onfig/linux/ptrlock.h (gomp_ptrlock_get): Use atomic load for ptrlock. Index: libgomp/config/linux/wait.h =================================================================== --- libgomp/config/linux/wait.h (revision 194730) +++ libgomp/config/linux/wait.h (working copy) @@ -51,7 +51,7 @@ static inline int do_spin (int *addr, in if (__builtin_expect (gomp_managed_threads > gomp_available_cpus, 0)) count = gomp_throttled_spin_count_var; for (i = 0; i < count; i++) - if (__builtin_expect (*addr != val, 0)) + if (__builtin_expect (__atomic_load_n (addr, MEMMODEL_RELAXED) != val, 0)) return 0; else cpu_relax (); Index: libgomp/config/linux/ptrlock.c =================================================================== --- libgomp/config/linux/ptrlock.c (revision 194730) +++ libgomp/config/linux/ptrlock.c (working copy) @@ -50,9 +50,9 @@ gomp_ptrlock_get_slow (gomp_ptrlock_t *p #endif do do_wait (intptr, 2); - while (*intptr == 2); + while (__atomic_load_n (intptr, MEMMODEL_RELAXED) == 2); __asm volatile ("" : : : "memory"); - return *ptrlock; + return (void *) __atomic_load_n (ptrlock, MEMMODEL_ACQUIRE); } void Index: libgomp/config/linux/ptrlock.h =================================================================== --- libgomp/config/linux/ptrlock.h (revision 194730) +++ libgomp/config/linux/ptrlock.h (working copy) @@ -48,8 +48,9 @@ static inline void *gomp_ptrlock_get (go { uintptr_t oldval; - if ((uintptr_t) *ptrlock > 2) - return *ptrlock; + uintptr_t v = (uintptr_t) __atomic_load_n (ptrlock, MEMMODEL_ACQUIRE); + if (v > 2) + return (void *) v; oldval = 0; if (__atomic_compare_exchange_n (ptrlock, &oldval, 1, false,