From patchwork Wed Dec 7 02:16:13 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: 1705 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.86 with SMTP id o83csp63476vsa; Tue, 6 Dec 2016 18:23:00 -0800 (PST) X-Received: by 10.28.180.214 with SMTP id d205mr321953wmf.131.1481077380444; Tue, 06 Dec 2016 18:23:00 -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 a17si6381130wma.103.2016.12.06.18.23.00; Tue, 06 Dec 2016 18:23:00 -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 ACAB7689F44; Wed, 7 Dec 2016 04:22:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io0-f181.google.com (mail-io0-f181.google.com [209.85.223.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E6E34689E5A for ; Wed, 7 Dec 2016 04:22:41 +0200 (EET) Received: by mail-io0-f181.google.com with SMTP id c21so632882969ioj.1 for ; Tue, 06 Dec 2016 18:22:52 -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=YJA8RyzbG+FCSFagg5K4Ebtl0JC1SRq14JslFF+SJjE=; b=KqiwDVCZKB28rPyhf4PTQpfufM5244JnLB75EH/mauHhMZ9WETidK4P3FfU8sDpJX0 RYdXBQTRT6QS6u2Jff4AUAYBvOVAixiKu7pi5HEtbI086pZoCHuHrbgQg/L6BGv5YCqJ QTi1Ehyo8Z3kvokaTCNYkAf+OaoWRm8yomy5LNczZM2JajjfmCmB/EzPNkipzKpi5urA sp2FXfslB13D6FoF9H7PS2yW3nEqeCpkzl6GkBBcfcdAm4Bwct8hlEpNyzZ7NTPP9H+y x66dqHJWGDSCygItbaxhEtW20H0AxW1Wlgdapk8+TdAFTUZO52k0zaCt4Zx2uMac/7bn rPfg== 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=YJA8RyzbG+FCSFagg5K4Ebtl0JC1SRq14JslFF+SJjE=; b=ddpmJHr9rEciE5wHpX3cf3n/iKDDKCcPxEKtTAE037uBHZCVKdPqvbrxZ6PQJf5Fcc WaURICLhBepS7if4sxn7jCRyoe/wW6OSV5XQPDgjvo9iH+vh9myI5qiPzJjsx4VFZTSt Oqc0mns4Yp4qKRlxR6xgIjDjQAqqIRqOqIi2OEzZr5tKxvqstc4l6aLDbrmUWIMyLhw4 09SfvFdzY24kOsoPgdJuF5aJ33BqtWC8YSM2edOrLnKMeDFuiK/urUBI0+xCDpxNlsEx KT4K0qmiukdHMpGBD0VOoCEK89ELmOI7fbE/dcqRZxhEBDpXsq1nqk4j3TDh7Cxtx8SU 5/dA== X-Gm-Message-State: AKaTC02l4O8ahbxX5cpX89pF5CoTc0Nz8TGHVzAW6asIOxKqtPGTXLGPVLI2V0D+ii24iuEF X-Received: by 10.107.138.10 with SMTP id m10mr16827421iod.188.1481076991408; Tue, 06 Dec 2016 18:16:31 -0800 (PST) Received: from wtc-desktop.mtv.corp.google.com ([100.98.5.45]) by smtp.googlemail.com with ESMTPSA id y125sm2563759ity.13.2016.12.06.18.16.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Dec 2016 18:16:30 -0800 (PST) From: Wan-Teh Chang To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Dec 2016 18:16:13 -0800 Message-Id: <1481076973-11855-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, who 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);