From patchwork Mon Mar 28 09:45:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chung-Lin Tang X-Patchwork-Id: 602427 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qYTYr4L69z9s5Q for ; Mon, 28 Mar 2016 20:46:07 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=jUAmkZeu; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; q=dns; s=default; b=RxexJG8jd35k7qFIL/5e9UZc9X0r0odN7qiiohaj9s0FUseDaj VroNVGzp705X+YS5RXtuexhAUFXLCarEnKvcClC7NgSmfl6rrlKA36jAARHhWopr ak5jLUnGTPt5V4OSxPBMjeO7y7fjA788GeFyXO810mwcUo4rbEN+93ayw= 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:from :subject:to:cc:message-id:date:mime-version:content-type; s= default; bh=NAoQ9WfyeRlTFWs5CneLrklB7LI=; b=jUAmkZeuoszc6wsHtVbz jh8dOyO8J9C+9nZ+7lvgJFI0UCatsBmeeLO2bIK7pz0BsuvY1zHWF1hENEanfY78 ZTX7tsxSBJJn6re97xNDXNYVXQKB6jkgACB5DZ3uwOHUk5ihhpQ49ri8M44dmZIC qesr6/oGE6vsm+OSwW95kRQ= Received: (qmail 30798 invoked by alias); 28 Mar 2016 09:45:59 -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 30787 invoked by uid 89); 28 Mar 2016 09:45:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=2016-03-28, regressions, H*Ad:U*thomas, Hx-languages-length:2489 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 28 Mar 2016 09:45:48 +0000 Received: from svr-orw-fem-06.mgc.mentorg.com ([147.34.97.120]) by relay1.mentorg.com with esmtp id 1akTk0-0001wo-03 from ChungLin_Tang@mentor.com ; Mon, 28 Mar 2016 02:45:44 -0700 Received: from [0.0.0.0] (147.34.91.1) by SVR-ORW-FEM-06.mgc.mentorg.com (147.34.97.120) with Microsoft SMTP Server id 14.3.224.2; Mon, 28 Mar 2016 02:45:43 -0700 From: Chung-Lin Tang Subject: [PATCH, libgomp] Fix deadlock in acc_set_device_type To: gcc-patches , Jakub Jelinek CC: Thomas Schwinge Message-ID: <56F8FD46.1060400@codesourcery.com> Date: Mon, 28 Mar 2016 17:45:42 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 MIME-Version: 1.0 X-IsSubscribed: yes Hi Jakub, there's a path for deadlock on acc_device_lock when going through the acc_set_device_type() OpenACC library function. Basically, the gomp_init_targets_once() function should not be called with that held. The attached patch moves it appropriately. Also in this patch, there are several cases in acc_* functions where gomp_init_targets_once() is guarded by a test of !cached_base_dev. Since that function already uses pthread_once() to call gomp_target_init(), and technically cached_base_dev is protected by acc_device_lock, the cleanest way should be to simply drop those "if(!cached_base_dev)" tests. Tested libgomp without regressions on an nvptx offloaded system, is this okay for trunk? Thanks, Chung-Lin 2016-03-28 Chung-Lin Tang * oacc-init.c (acc_init): Remove !cached_base_dev condition on call to gomp_init_targets_once(). (acc_set_device_type): Remove !cached_base_dev condition on call to gomp_init_targets_once(), move call to before acc_device_lock acquire, to avoid deadlock. (acc_get_device_num): Remove !cached_base_dev condition on call to gomp_init_targets_once(). (acc_set_device_num): Likewise. Index: oacc-init.c =================================================================== --- oacc-init.c (revision 234502) +++ oacc-init.c (working copy) @@ -433,8 +433,7 @@ goacc_attach_host_thread_to_device (int ord) void acc_init (acc_device_t d) { - if (!cached_base_dev) - gomp_init_targets_once (); + gomp_init_targets_once (); gomp_mutex_lock (&acc_device_lock); @@ -498,11 +497,10 @@ acc_set_device_type (acc_device_t d) struct gomp_device_descr *base_dev, *acc_dev; struct goacc_thread *thr = goacc_thread (); + gomp_init_targets_once (); + gomp_mutex_lock (&acc_device_lock); - if (!cached_base_dev) - gomp_init_targets_once (); - cached_base_dev = base_dev = resolve_device (d, true); acc_dev = &base_dev[goacc_device_num]; @@ -563,8 +561,7 @@ acc_get_device_num (acc_device_t d) if (d >= _ACC_device_hwm) gomp_fatal ("unknown device type %u", (unsigned) d); - if (!cached_base_dev) - gomp_init_targets_once (); + gomp_init_targets_once (); gomp_mutex_lock (&acc_device_lock); dev = resolve_device (d, true); @@ -584,8 +581,7 @@ acc_set_device_num (int ord, acc_device_t d) struct gomp_device_descr *base_dev, *acc_dev; int num_devices; - if (!cached_base_dev) - gomp_init_targets_once (); + gomp_init_targets_once (); if (ord < 0) ord = goacc_device_num;