From 0de39b6724fbb79d27cf85b015cf8f0edb0567a9 Mon Sep 17 00:00:00 2001 From: Andres Rodriguez Date: Tue, 1 Nov 2016 11:59:17 -0400 Subject: [PATCH] Fix hsaKmtOpen incorrectly doing nothing in some fork scenarios Currently, if a process' parent called hsaKmtOpen, the child will be unable to open a connection to KFD, since kfd_open_count will be > 0. When forking, the refcount should be reset, in order to allow the child to re-open /dev/kfd. Change-Id: Ia4b78f6bacc4f82e8ac724e5f488a3eff5084007 --- src/openclose.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/openclose.c b/src/openclose.c index 72baf7c997..5a55a3a50b 100644 --- a/src/openclose.c +++ b/src/openclose.c @@ -37,6 +37,19 @@ static const char kfd_device_name[] = "/dev/kfd"; static const char tmp_file[] = "/var/lock/.amd_hsa_thunk_lock"; int amd_hsa_thunk_lock_fd = 0; +static bool is_forked_child(void) +{ + static pid_t open_pid = -1; + pid_t my_pid = getpid(); + + if (open_pid == -1 || open_pid != my_pid) { + open_pid = my_pid; + return true; + } + + return false; +} + HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) @@ -47,6 +60,13 @@ hsaKmtOpenKFD(void) pthread_mutex_lock(&hsakmt_mutex); + /* If the process has forked, the child process must re-initialize + * it's connection to KFD. Any references tracked by kfd_open_count + * belong to the parent + */ + if (is_forked_child()) + kfd_open_count = 0; + if (kfd_open_count == 0) { fd = open(kfd_device_name, O_RDWR | O_CLOEXEC);