From patchwork Thu Jun 21 17:36:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 139557 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp2368534lji; Thu, 21 Jun 2018 10:40:49 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLYsXMOsoKjIugUgeQPo5Bnuv6H9Z6PjqFEcLuzhYDFO5MBgqKDNi24suLAVaNT+vX9QCiS X-Received: by 2002:a37:2c87:: with SMTP id s129-v6mr21299975qkh.68.1529602849685; Thu, 21 Jun 2018 10:40:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529602849; cv=none; d=google.com; s=arc-20160816; b=0D05EHB2QRYgqA2HMWb+qQe+tb9EdjeXxnNSWIzpmMfewZK8SZhNdWkmF+wPbNBS+3 MoUwDlllXuRzzsk0Ax4uZ+WUmxvEYyChD8at9SjqOykmM2vHgTDkdkY0WkwzSgVzCd/r NZtWyI9Cjdeq08BBhBVXab5UPF0pPT2Ab6TIvWmDbvCZ98i627VhXT4xJoAwWjXEJPfU zrSPnaV3Na1eggywK0QHBDVIW5cVSU7G6JrykFycqQu+Nau+zSHH3bm4utNOexT+BzR/ BkQc3GE6mYRGor9UhuXwBev444dAbBY9K+KiWwqpicQlW61uxGH4xxNOlOcoDHq0K28N reKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=dAGvimUHj8i54umH7c4ck5t1ysicwOLXz6VvcrwBBVo=; b=FCX4uxGlEIIXmyxhsGaI55boqLoy50n6V7CJmQQA+SY42/HSt+rZLD0qTRZiD8EN3D iZjzBorYc1xMwEPvUR/wTjPUBAx8P9WPJONMZPvsypiuHMrna/bAhJBF6vZVMjWsPlhp K7z4SBLQGzJk1QMpWh0tsiUypyFiJEeLBtBkANrwRWimNQuMdH5D0ocWbV5V2xnZ/7Pk xXS1Cu54NRFmi9T+5r0YnBbCQ0qKHNmrsE4aYc+TkEftsu1wg2PiUMc1IGRpZ0y31PMo Y42BrKWZwPAjlESW4W8nkA6fNqm/9IWCqV7QyfcP+qxseLjV6Mjf0PxZF4EoLG7/BxcF lG2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=a3dng0iw; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id i34-v6si3108052qtb.318.2018.06.21.10.40.49 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 21 Jun 2018 10:40:49 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=a3dng0iw; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:56922 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW3Zh-0002xx-4g for patch@linaro.org; Thu, 21 Jun 2018 13:40:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54976) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW3Vn-0000rN-KS for qemu-devel@nongnu.org; Thu, 21 Jun 2018 13:36:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fW3Vm-0004O1-Oc for qemu-devel@nongnu.org; Thu, 21 Jun 2018 13:36:47 -0400 Received: from mail-pf0-x22f.google.com ([2607:f8b0:400e:c00::22f]:34434) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fW3Vm-0004NE-8g for qemu-devel@nongnu.org; Thu, 21 Jun 2018 13:36:46 -0400 Received: by mail-pf0-x22f.google.com with SMTP id a63-v6so1881541pfl.1 for ; Thu, 21 Jun 2018 10:36:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dAGvimUHj8i54umH7c4ck5t1ysicwOLXz6VvcrwBBVo=; b=a3dng0iwajn81tYrivszbJwfLRCggA4jVOIOOmZ+uQ8sgblEQzwWV8X3N9v9e3UWnX so36Y8C++rWMYjpPTonROLhcq9g727K1sPaz+0Zr7RzY9sKecjqPp9WKlxsw/f2TiSdd cn8DXkvqP6fdOrmvieuSyyFRreKASXWi0ZGyg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dAGvimUHj8i54umH7c4ck5t1ysicwOLXz6VvcrwBBVo=; b=Y8p+0d/ZI68j2Y9p6fcK43Tp2E8n6I4w5XqKnuVLGtanGqcGgvXQ9hN9ho0be7cOhV s9HDzmkRWbqGel1PAP9750/k3kl2lbrIuRYnnHoKRs07jZhOGt95sY/gx0bitghZeuZ7 d47QAMbm69lhZoIXYonz1P+2Y1UCOyvONNvbDSfQozwm1ijfjEDOrpE6WyM9UfHwtabk 4oEQI//E6FE/S66w4eMtAfMy1EPjWgnUjF6ZDgDHojL06fV68DtYOVdBoh4RnDd9X3LT dfK6oAyKN5GLUic47E9XwP0flnOE361+/FfUiP4OV5tG9RojvkGobjjzcIzymUjFYlyo gFNQ== X-Gm-Message-State: APt69E2OtJwpJBv/DBRL0IERFVB1u1dHMc1W9P8NboSj2szUpb47/p/+ vIiXlNQ0AUUANfUMqroPBDO+4YwMfBw= X-Received: by 2002:a63:8b44:: with SMTP id j65-v6mr23985916pge.203.1529602604780; Thu, 21 Jun 2018 10:36:44 -0700 (PDT) Received: from cloudburst.twiddle.net (mta-98-147-121-51.hawaii.rr.com. [98.147.121.51]) by smtp.gmail.com with ESMTPSA id e192-v6sm9642897pfg.187.2018.06.21.10.36.43 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Jun 2018 10:36:43 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 21 Jun 2018 07:36:35 -1000 Message-Id: <20180621173635.21537-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180621173635.21537-1-richard.henderson@linaro.org> References: <20180621173635.21537-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::22f Subject: [Qemu-devel] [PATCH 2/2] linux-user: Use pthread_rwlock_t for mmap_rd/wrlock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: cota@braap.org, laurent@vivier.eu, qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Change the implementation of these functions to use an actual reader/writer lock, allowing multiple simultaneous readers. Signed-off-by: Richard Henderson --- linux-user/mmap.c | 52 ++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 21 deletions(-) -- 2.17.1 diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 71b6bed5e2..2dc133515a 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -24,52 +24,62 @@ //#define DEBUG_MMAP -static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; -static __thread int mmap_lock_count; - -static void mmap_lock_internal(void) -{ - if (mmap_lock_count++ == 0) { - pthread_mutex_lock(&mmap_mutex); - } -} +static pthread_rwlock_t mmap_rwlock = PTHREAD_RWLOCK_INITIALIZER; +/* Bit 0 indicates reading; bit 1 indicates writing; bits 2+ are count-1. */ +static __thread int mmap_lock_held; void mmap_rdlock(void) { - mmap_lock_internal(); + if (likely(mmap_lock_held == 0)) { + pthread_rwlock_rdlock(&mmap_rwlock); + mmap_lock_held = 1; + } else { + /* can read-lock when write-lock held */ + mmap_lock_held += 4; + } } void mmap_wrlock(void) { - mmap_lock_internal(); + if (likely(mmap_lock_held == 0)) { + pthread_rwlock_rdlock(&mmap_rwlock); + mmap_lock_held = 2; + } else { + /* cannot upgrade a read-lock to a write-lock */ + assert((mmap_lock_held & 1) == 0); + mmap_lock_held += 4; + } } void mmap_unlock(void) { - if (--mmap_lock_count == 0) { - pthread_mutex_unlock(&mmap_mutex); + assert(mmap_lock_held > 0); + mmap_lock_held -= 4; + if (mmap_lock_held < 0) { + mmap_lock_held = 0; + pthread_rwlock_unlock(&mmap_rwlock); } } bool have_mmap_lock(void) { - return mmap_lock_count > 0 ? true : false; + return mmap_lock_held != 0; } /* Grab lock to make sure things are in a consistent state after fork(). */ void mmap_fork_start(void) { - if (mmap_lock_count) - abort(); - pthread_mutex_lock(&mmap_mutex); + assert(mmap_lock_held == 0); + pthread_rwlock_wrlock(&mmap_rwlock); } void mmap_fork_end(int child) { - if (child) - pthread_mutex_init(&mmap_mutex, NULL); - else - pthread_mutex_unlock(&mmap_mutex); + if (child) { + pthread_rwlock_init(&mmap_rwlock, NULL); + } else { + pthread_rwlock_unlock(&mmap_rwlock); + } } /* NOTE: all the constants are the HOST ones, but addresses are target. */