From patchwork Thu Sep 28 17:29:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 819691 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-463125-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="GrflWljO"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3y31sZ48lVz9t48 for ; Fri, 29 Sep 2017 03:29:52 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=r2dlDl82O29ZCdGd9ZMH6qxSFPJry8rSG9emE0MR7Z0aV6PY4l hNqCM0wTJX+V9L6RE8eOFDLXGUz8xa/onmEf4O+xKAzU/BpUFJvYQ7rBzCyXrhHe hUlUCVDIBFfVtR++vgnh5l5qz2izPavulI+kAyhbI5chOYJBbWcGbCo7E= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=9bzdMGjbKy40yk9I3xS4k1P50X4=; b=GrflWljOoikkzIvnbopI MiaeIE7Dct2khFyPgLXoYbWwMGw4q3IBO6Ap4wNmDeDCbZBlaSskscqN2l6UTB6O VzUtqCDNd4Ped4T1pyue6ZeouCPexABsyWT+8uAYJIOIgu5lHIs8y+XYCMKoey+u 827WH6HZBPsUIkGJ7qkqG/U= Received: (qmail 119436 invoked by alias); 28 Sep 2017 17:29:41 -0000 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 Received: (qmail 119416 invoked by uid 89); 28 Sep 2017 17:29:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=unlock, tkoeniggccgnuorg, H*r:sk:tkoenig, U*tkoenig X-Spam-User: qpsmtpd, 2 recipients X-HELO: cc-smtpout2.netcologne.de Received: from cc-smtpout2.netcologne.de (HELO cc-smtpout2.netcologne.de) (89.1.8.212) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 28 Sep 2017 17:29:38 +0000 Received: from cc-smtpin3.netcologne.de (cc-smtpin3.netcologne.de [89.1.8.203]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id 441F9129A9; Thu, 28 Sep 2017 19:29:34 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin3.netcologne.de (Postfix) with ESMTP id 3F6A211DC1; Thu, 28 Sep 2017 19:29:34 +0200 (CEST) Received: from [78.35.161.68] (helo=cc-smtpin3.netcologne.de) by localhost with ESMTP (eXpurgate 4.1.9) (envelope-from ) id 59cd317e-02b7-7f0000012729-7f0000018bf3-1 for ; Thu, 28 Sep 2017 19:29:34 +0200 Received: from [192.168.178.20] (xdsl-78-35-161-68.netcologne.de [78.35.161.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by cc-smtpin3.netcologne.de (Postfix) with ESMTPSA; Thu, 28 Sep 2017 19:29:32 +0200 (CEST) To: "fortran@gcc.gnu.org" , gcc-patches From: Thomas Koenig Subject: [patch, libfortran] Fix thead sanitizer issue with libgfortran Message-ID: <43bc4547-abe8-312d-e66e-ad9e2d3034bd@netcologne.de> Date: Thu, 28 Sep 2017 19:29:32 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 Hello world, the attached patch fixes the problem reported in PR 66756: When opeing a file, the main lock for all units was acquired, the unit lock was acquired, and then the main lock was released and re-aquired. To the thread sanitizer, this is a lock-order inversion. One option would have been to simply close the bug, because this only occurs in opening a file, when the gfc_unit has not yet had a chance to escape to another thread. However, it appears that this causes trouble debugging parallel applications, hence this patch. What this patch does is to change the assumptions for insert_unit: Previously, this used to lock the newly created unit, and the caller had to unlock. Now, gfc_get_unit can do the locking after releasing the global lock. This gets rid of the thread sanitizer issue; the thread sanitizer output is clean. However, I would appreciate feedback about whether this approach (and my code) is correct. Regression-tested. Comments? Suggestions for improvements/other approaches? Close the PR as WONTFIX instead? OK for trunk? Regards Thomas 2017-09-28 Thomas Koenig PR fortran/66756 * io/fbuf.c (fbuf_destroy): Lock unit before freeing the buffer. * io/unit.c (insert_unit): Do not create lock and lock, move to (gfc_get_unit): here; lock after insert_unit has succeded. (init_units): Do not unlock unit locks for stdin, stdout and stderr. Index: io/fbuf.c =================================================================== --- io/fbuf.c (Revision 253162) +++ io/fbuf.c (Arbeitskopie) @@ -50,9 +50,11 @@ fbuf_destroy (gfc_unit *u) { if (u->fbuf == NULL) return; + __gthread_mutex_lock (&u->lock); free (u->fbuf->buf); free (u->fbuf); u->fbuf = NULL; + __gthread_mutex_unlock (&u->lock); } Index: io/unit.c =================================================================== --- io/unit.c (Revision 253162) +++ io/unit.c (Arbeitskopie) @@ -221,23 +221,14 @@ insert (gfc_unit *new, gfc_unit *t) return t; } +/* insert_unit()-- Create a new node, insert it into the treap. It is assumed + that the caller holds unit_lock. */ -/* insert_unit()-- Create a new node, insert it into the treap. */ - static gfc_unit * insert_unit (int n) { gfc_unit *u = xcalloc (1, sizeof (gfc_unit)); u->unit_number = n; -#ifdef __GTHREAD_MUTEX_INIT - { - __gthread_mutex_t tmp = __GTHREAD_MUTEX_INIT; - u->lock = tmp; - } -#else - __GTHREAD_MUTEX_INIT_FUNCTION (&u->lock); -#endif - __gthread_mutex_lock (&u->lock); u->priority = pseudo_random (); unit_root = insert (u, unit_root); return u; @@ -361,9 +352,20 @@ retry: if (created) { - /* Newly created units have their lock held already - from insert_unit. Just unlock UNIT_LOCK and return. */ +#ifdef __GTHREAD_MUTEX_INIT + { + __gthread_mutex_t tmp = __GTHREAD_MUTEX_INIT; + p->lock = tmp; + } +#else + __GTHREAD_MUTEX_INIT_FUNCTION (&p->lock); +#endif __gthread_mutex_unlock (&unit_lock); + + /* Nobody outside this address has seen this unit yet. We could safely + keep it unlocked until now. */ + + __gthread_mutex_lock (&p->lock); return p; } @@ -618,8 +620,6 @@ init_units (void) u->filename = strdup (stdin_name); fbuf_init (u, 0); - - __gthread_mutex_unlock (&u->lock); } if (options.stdout_unit >= 0) @@ -649,8 +649,6 @@ init_units (void) u->filename = strdup (stdout_name); fbuf_init (u, 0); - - __gthread_mutex_unlock (&u->lock); } if (options.stderr_unit >= 0) @@ -680,8 +678,6 @@ init_units (void) fbuf_init (u, 256); /* 256 bytes should be enough, probably not doing any kind of exotic formatting to stderr. */ - - __gthread_mutex_unlock (&u->lock); } /* Calculate the maximum file offset in a portable manner.