From patchwork Fri Nov 15 21:44:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Brown X-Patchwork-Id: 1195867 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-513691-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Ttq7Mu3P"; 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 47FBhd3f0rz9s4Y for ; Sat, 16 Nov 2019 08:44:49 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; q=dns; s= default; b=O8DxuBCFtTLAT58y4ZkVkLokFmWryjWGDlJFii9KWTiAFzP7yN5XR E/wA/PK2QuK/jLP33Udwvbs4tp3cRn6cjcJG7EfBKWk75mJtypyaF2AOFfKtmuha jyxC+/uMsQ2zqtL3Un1qDmmannr54sTbtceRa4epwu0SHQ9ZmBZDQA= 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 :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=ZaTa1Q9TDarsBzdjgufU2s//P9w=; b=Ttq7Mu3Pbvxwk4PifKN+qiUnN7iK Ospa4yO1/YTCQsLdw/Qlsa1mO4Jusv0MN4m9Zs/OmifoIFnDKYqKeWQcuNPnV0lL azaNgUo6aRsUpXHmbN6xrtK2y4r3qjHtW3vez4/TyAhIFFliZyaCyfLCoCJgKG9Q gW+5WpvEW8xHlzY= Received: (qmail 50785 invoked by alias); 15 Nov 2019 21:44:28 -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 50736 invoked by uid 89); 15 Nov 2019 21:44:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.1 spammy=luckily, agent X-HELO: esa1.mentor.iphmx.com Received: from esa1.mentor.iphmx.com (HELO esa1.mentor.iphmx.com) (68.232.129.153) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Nov 2019 21:44:26 +0000 IronPort-SDR: fj71YvpHVVGtoDzkxcWLh5htLqt9GMdByLGpm0xC+5dJZ8CRY/5eMta87831HsYkmlo/OtxXrc CQQUWX30OZahhPo7PTMf+SdxKNgBZxYTqtSxWu4ttmC/Du86U0UfeBsRwfPz/0wFAjPPjU8X4D zXHj3AmsPn7NkxzBccdEHrpy3etXz6JoOATO9FHxIfWdZspSox1NYR0I11B2scMdFR5vaG7O9X 9zCPiJLj/uYMQgMCFjf+lOkQg2URhbpdCy2pFCkKjRZgbp2OwUcu1rZCtsVWjgooc2+JNtGE44 bHY= Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa1.mentor.iphmx.com with ESMTP; 15 Nov 2019 13:44:24 -0800 IronPort-SDR: hQ3OsxW2eKHeEUKsihevUQg2yCsAc92oZ6545giF//UOf4xU9eVNKtqO8J9vMaBVg9KHGbEREI iymgXEIAdenMD9kYI/bpetrki+MxUJIpzFlDrhBK0i37x8mGbHRcU2frnihhGCdsMWjBQG2Apz VhiCv5oFdafpJ17iWZrtB3QQ1sy8yoxKOjGsmUR0n38c2S9aCh3KoiY2HMvZopi86zF74nZ1Kq /fDQBw9rpseOpPmoWfu7Zbiy7UP6yZ7N/69v8eM/S1X9Nim77albjDESsnzuaM+pm1vdcBe9UW SfM= From: Julian Brown To: CC: , , , Andrew Stubbs Subject: [PATCH 08/13] Fix host-to-device copies from rodata for AMD GCN Date: Fri, 15 Nov 2019 13:44:01 -0800 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-IsSubscribed: yes It appears that the hsa_memory_copy API routine has problems copying from read-only data: in the libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-8.c test, a "const" variable cannot be successfully copied to the target. I think the problem is with read-only page mappings (in the HSA runtime). Luckily (?), if the copy fails, the API call fails with a HSA_STATUS_ERROR return code, which we can detect and use to trigger a workaround. I've also added return-code checks to several other uses of the hsa_memory_copy API routine, in an attempt to avoid silent runtime failures. (A previous fix for a similar problem, removed before upstream submission, does not appear to apply to this exact situation.) OK? Julian ChangeLog libgomp/ * plugin/plugin-gcn.c (hsa_memory_copy_wrapper): New. (copy_data, GOMP_OFFLOAD_host2dev): Use above function. (GOMP_OFFLOAD_dev2host, GOMP_OFFLOAD_dev2dev): Check hsa_memory_copy return code. --- libgomp/plugin/plugin-gcn.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/libgomp/plugin/plugin-gcn.c b/libgomp/plugin/plugin-gcn.c index 7b95a4cef8f..eb016e3fcd6 100644 --- a/libgomp/plugin/plugin-gcn.c +++ b/libgomp/plugin/plugin-gcn.c @@ -2940,6 +2940,29 @@ maybe_init_omp_async (struct agent_info *agent) = GOMP_OFFLOAD_openacc_async_construct (agent->device_id); } +/* A wrapper that works around an issue in the HSA runtime with host-to-device + copies from read-only pages. */ + +static void +hsa_memory_copy_wrapper (void *dst, const void *src, size_t len) +{ + hsa_status_t status = hsa_fns.hsa_memory_copy_fn (dst, src, len); + + if (status == HSA_STATUS_SUCCESS) + return; + + /* It appears that the copy fails if the source data is in a read-only page. + We can't detect that easily, so try copying the data to a temporary buffer + and doing the copy again if we got an error above. */ + + void *src_copy = malloc (len); + memcpy (src_copy, src, len); + status = hsa_fns.hsa_memory_copy_fn (dst, (const void *) src_copy, len); + free (src_copy); + if (status != HSA_STATUS_SUCCESS) + GOMP_PLUGIN_error ("memory copy failed"); +} + /* Copy data to or from a device. This is intended for use as an async callback event. */ @@ -2950,7 +2973,7 @@ copy_data (void *data_) GCN_DEBUG ("Async thread %d:%d: Copying %zu bytes from (%p) to (%p)\n", data->aq->agent->device_id, data->aq->id, data->len, data->src, data->dst); - hsa_fns.hsa_memory_copy_fn (data->dst, data->src, data->len); + hsa_memory_copy_wrapper (data->dst, data->src, data->len); if (data->free_src) free ((void *) data->src); free (data); @@ -3643,7 +3666,9 @@ GOMP_OFFLOAD_dev2host (int device, void *dst, const void *src, size_t n) { GCN_DEBUG ("Copying %zu bytes from device %d (%p) to host (%p)\n", n, device, src, dst); - hsa_fns.hsa_memory_copy_fn (dst, src, n); + hsa_status_t status = hsa_fns.hsa_memory_copy_fn (dst, src, n); + if (status != HSA_STATUS_SUCCESS) + GOMP_PLUGIN_error ("memory copy failed"); return true; } @@ -3654,7 +3679,7 @@ GOMP_OFFLOAD_host2dev (int device, void *dst, const void *src, size_t n) { GCN_DEBUG ("Copying %zu bytes from host (%p) to device %d (%p)\n", n, src, device, dst); - hsa_fns.hsa_memory_copy_fn (dst, src, n); + hsa_memory_copy_wrapper (dst, src, n); return true; } @@ -3675,7 +3700,9 @@ GOMP_OFFLOAD_dev2dev (int device, void *dst, const void *src, size_t n) GCN_DEBUG ("Copying %zu bytes from device %d (%p) to device %d (%p)\n", n, device, src, device, dst); - hsa_fns.hsa_memory_copy_fn (dst, src, n); + hsa_status_t status = hsa_fns.hsa_memory_copy_fn (dst, src, n); + if (status != HSA_STATUS_SUCCESS) + GOMP_PLUGIN_error ("memory copy failed"); return true; }