From patchwork Tue Dec 6 18:41:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wan-Teh Chang X-Patchwork-Id: 1700 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp2312330vsa; Tue, 6 Dec 2016 10:41:56 -0800 (PST) X-Received: by 10.28.216.65 with SMTP id p62mr10760wmg.92.1481049716374; Tue, 06 Dec 2016 10:41:56 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v2si20793903wjh.194.2016.12.06.10.41.55; Tue, 06 Dec 2016 10:41:56 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5B3B6689ABC; Tue, 6 Dec 2016 20:41:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f52.google.com (mail-pg0-f52.google.com [74.125.83.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 785796897D6 for ; Tue, 6 Dec 2016 20:41:35 +0200 (EET) Received: by mail-pg0-f52.google.com with SMTP id x23so152028584pgx.1 for ; Tue, 06 Dec 2016 10:41:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id; bh=nfszx2pv3YHFYcgbgl5y0Pq0cmge3esoEyMB8HFkOng=; b=NmTTgN93k5vo7ST2uqCYvOJNRNcCrAym3rJ+9nnHU6D+0sFR5KRDZbNw0Q2XxO7JVx hHJOYgnRfkliBPBbaLYK/W2fjPcAlY5Z/6WhZGlZNwALRijHqjCtNW6J6bnNZTB0AS81 /MPEuS4XzVaizuhnpGFtXT/NPXRju8j0KjkVl4/Beb6DgGG35HE99c0SRvihB1ZAOrrY O49EBXooiOEF2CfkAp1SWaVz5HkS1FYxQIm7MOaUd6j39fsuFiPl/GFBBfu+1kbKNLWI dBbVVTYLmhZAR+Rf5AbGn6TUEOPUlvthuHlynpY9rirUL9RhLien1cH4AxOYF4L4cz8Q czvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=nfszx2pv3YHFYcgbgl5y0Pq0cmge3esoEyMB8HFkOng=; b=iSGlENBTG2BWKqN8euGallocG3//7VATj8WYo0JF81uD/GtBFNFlktcxSLWvvMPBPq 4bu4HtEVZxlhQL5G3yKuUUSVuk2GZhipiEgIBwtg0LfPsb+vWYO6gMQw7xUL81Qpz6iA LjWHeOaqiLlMuTQlLYHHOZBvrBaOtSp194g2Nf1ZGt0BgFM0p/fwDflF/IqVhOehg8UM Th97JjW72XUko5f+KuysjzrASR3ZkrM6p08OFwuTSpMsjhNbaMhWctq9zb0IuSzV0vEC 7pqQEfcyRyxZxmG8qCcYKJoNU1Rg/Ym5AE57YCUTmntkoIti/cBXRPu0kJgOm/SwXawI MjNg== X-Gm-Message-State: AKaTC02yEbySw/6zXTr9Pk39KSqW12DXD+jhYO/+p8eBXt7FmWZXpXXa1lA3QaeQp8xR/Ch+ X-Received: by 10.99.232.21 with SMTP id s21mr115060904pgh.19.1481049703813; Tue, 06 Dec 2016 10:41:43 -0800 (PST) Received: from wtc-desktop.mtv.corp.google.com ([100.98.5.45]) by smtp.googlemail.com with ESMTPSA id c22sm36759079pgn.12.2016.12.06.10.41.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Dec 2016 10:41:43 -0800 (PST) From: Wan-Teh Chang To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Dec 2016 10:41:25 -0800 Message-Id: <1481049685-76564-1-git-send-email-wtc@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Subject: [FFmpeg-devel] [PATCH] avutil: fix data race in av_get_cpu_flags() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Make the one-time initialization in av_get_cpu_flags() thread-safe. The static variable |cpu_flags| in libavutil/cpu.c is read and written using normal load and store operations. These are considered as data races. The fix is to use atomic load and store operations. The fix can be verified by running the libavutil/tests/cpu_init.c test program under ThreadSanitizer: ./configure --toolchain=clang-tsan make libavutil/tests/cpu_init libavutil/tests/cpu_init There should be no warnings from ThreadSanitizer. Co-author: Dmitry Vyukov of Google, which suggested the data race fix. Signed-off-by: Wan-Teh Chang --- libavutil/cpu.c | 12 +++++++----- libavutil/cpu.h | 2 -- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libavutil/cpu.c b/libavutil/cpu.c index 73317c4..16e0c92 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -17,6 +17,7 @@ */ #include +#include #include "cpu.h" #include "cpu_internal.h" @@ -44,7 +45,7 @@ #include #endif -static int cpu_flags = -1; +static atomic_int cpu_flags = ATOMIC_VAR_INIT(-1); static int get_cpu_flags(void) { @@ -82,22 +83,23 @@ void av_force_cpu_flags(int arg){ arg |= AV_CPU_FLAG_MMX; } - cpu_flags = arg; + atomic_store_explicit(&cpu_flags, arg, memory_order_relaxed); } int av_get_cpu_flags(void) { - int flags = cpu_flags; + int flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed); if (flags == -1) { flags = get_cpu_flags(); - cpu_flags = flags; + atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed); } return flags; } void av_set_cpu_flags_mask(int mask) { - cpu_flags = get_cpu_flags() & mask; + atomic_store_explicit(&cpu_flags, get_cpu_flags() & mask, + memory_order_relaxed); } int av_parse_cpu_flags(const char *s) diff --git a/libavutil/cpu.h b/libavutil/cpu.h index 4bff167..8499f0e 100644 --- a/libavutil/cpu.h +++ b/libavutil/cpu.h @@ -85,8 +85,6 @@ void av_force_cpu_flags(int flags); * Set a mask on flags returned by av_get_cpu_flags(). * This function is mainly useful for testing. * Please use av_force_cpu_flags() and av_get_cpu_flags() instead which are more flexible - * - * @warning this function is not thread safe. */ attribute_deprecated void av_set_cpu_flags_mask(int mask);