From patchwork Thu Nov 16 23:07:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Lewycky X-Patchwork-Id: 6130 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp6394606jah; Thu, 16 Nov 2017 15:08:25 -0800 (PST) X-Google-Smtp-Source: AGs4zMbZywcDxRR+ThQsmzQGdTR7nLUjNuxr90YDXBxPim4Xf1XbrdwSGyHMOK5/sbRClZPC/mQx X-Received: by 10.223.176.121 with SMTP id g54mr2709364wra.165.1510873705537; Thu, 16 Nov 2017 15:08:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510873705; cv=none; d=google.com; s=arc-20160816; b=ULCgUSqMij/4nhBqRRJPJf4sx69k6rBnq4a91pJtIuoOcI0jrPEGzchdTXuSgwXS1c VIHBaEWWGm1VUz/XgqZKo7bnC17fEPNHRgNMCq0R2l0JhSmqapVJbEqJDXs06u6ZfL9Q K98g56aFSlxyu0y5Zj9kN9AYbrmHWpX+v1jNuaWxU97obIZ4oLr4pCJ0kmpAb4b/PGY2 EeM+lCEYU8y/026ce5rEu75/K9Gmnjv9PK9yY3OAA3gBJpFM+AgvFOI8E/euPBmoyhs6 0scLW2CSjLtf5zQhc2DwDYQKx+//BUYcnsq+COJ9Cli2t6vjF/574kDp3h0/G/mGbxC2 y8kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:delivered-to:arc-authentication-results; bh=99Pwf9R6VS3UrEQg/FlzzTDNn0fAYq2zNDIFKT/ZPUw=; b=TVahHsj0ipEOTViHrTpu+vj8gdFrisgKDS8swMCMn+u8M5W0d3VsaSqnfo1Z5Me4ow qy3sJ93OOMdPUYzQk8Xy5Qyy8paEgqf049zvgGYL5a+h7rfPeKKECIhGZpXUGRt6EmOo pHsxclYc4JhB3MR2Q/3xbqIqrsN/76zbIDY5NBISAV+j5FpNQGzbfKZ8KwAWYEZFfn6N VNj3VtIL2Z/s2L9T3NFJ9o2nM0Ntj8r/HptBZnjv32JYBz4deOKvZwWzMXBxejqbKuIf J6s7blYSyGgMCrnmdctvoIWF/JBtJ/MBmAWBG2wRE0Gn4rHuMboRvD6WvBidOI6l80QA PLJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=kki2nm4y; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u4si1768823wmb.268.2017.11.16.15.08.24; Thu, 16 Nov 2017 15:08:25 -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 header.s=20161025 header.b=kki2nm4y; 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 6B2DB689C6F; Fri, 17 Nov 2017 01:08:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yb0-f177.google.com (mail-yb0-f177.google.com [209.85.213.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 73B436809AC for ; Fri, 17 Nov 2017 01:08:01 +0200 (EET) Received: by mail-yb0-f177.google.com with SMTP id y14so32859ybi.0 for ; Thu, 16 Nov 2017 15:08:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=WCW6oxe2CB0oulSCxgSkkNDLJmO08fABLv6bB3p++9k=; b=kki2nm4yaEmrrUvqE1nQPE4ncUP1FKESzeY0sWrK9GrKDI5yYbHexCqA1mi/TnX4TR EwpYpsAubUcr9UbuxJVab28gtrI77y1h42pPxvmCR2fitaHToopAROsxP5J8sp0+ZanX b1o2+mNXwD8scJYNdH13TTvE5V08rfpqjItW3g041g6UyvKk/omy/SHpgtEyq1Y/V/sK nQDrvfDcJ97rvsu3+sIxQaJU4tJV59z1J4dA0meefFRBcmgCjXkD9yt/a8wtX6/I8mfi ZqFk81GWLfBaVuYjesQtzr4TcrDE+jZmOqWJLnksCX24hLmMXFkq/6iSniA8+hEqMI+3 80Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=WCW6oxe2CB0oulSCxgSkkNDLJmO08fABLv6bB3p++9k=; b=DNASvCglD15aBF/uRLIm8tK+ygSSnQtXdne8D3NYMhEWhi71hI4GIfI/xeImTyooZu Z5VGgMs2c9KSaQjKYLZmAKyBuRdN+HC3hkLBdnIuOVBF3jIyZb/xDmJS4e7pz1dk0V3e BIaGOdDGl9bhRBlZPliuYH3p+1RyBi+y8bcz24RN92+F1258jBYWyEE7rO0eruHfx0iM Lm5TnQPpE4xWkv36bx6wC3weeFBWNDQkgDi6tXThlAKXO4yzaShA3CAmJocsHH2SdxcA 5JYyLpk/hOHLPPx9vGwZxF9OV+W4t5FMhzpqwsLuAZy6bJOmXdFBUBrEnr6OqIA992FJ sVeQ== X-Gm-Message-State: AJaThX5iaU1RlwBiOYaipMYd5yZ42gU788iiesdAZygTSfOPeFekYuOf xdJeKNUJ6xcik7i9uZp5TQ2gicVZD88cG6JlAsAQgU8g X-Received: by 10.37.4.129 with SMTP id 123mr1926820ybe.194.1510873695505; Thu, 16 Nov 2017 15:08:15 -0800 (PST) MIME-Version: 1.0 Received: by 10.37.190.140 with HTTP; Thu, 16 Nov 2017 15:07:54 -0800 (PST) In-Reply-To: <20171116223642.GJ6009@nb4> References: <20171116223642.GJ6009@nb4> From: Nick Lewycky Date: Thu, 16 Nov 2017 15:07:54 -0800 Message-ID: To: FFmpeg development discussions and patches Subject: Re: [FFmpeg-devel] [PATCH] libavcodec/mpeg12dec: Use atomic addition for value updated in multiple threads. 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Sorry! Let's try an attachment then. On 16 November 2017 at 14:36, Michael Niedermayer wrote: > On Thu, Nov 16, 2017 at 12:41:32PM -0800, Nick Lewycky wrote: >> I initially discovered a signed integer overflow on this line. Since >> this value is updated in multiple threads, I use an atomic update and >> as it happens atomic addition is defined to wrap around. However, >> there's still a potential bug in that the error_count may wrap around >> and equal zero again causing problems down the line. >> >> --- >> libavcodec/mpeg12dec.c | 3 ++- >> 1 file changed, 2 insertions(+), 1 deletion(-) >> >> diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c >> index d5bc5f21b2..b7c3b5106e 100644 >> --- a/libavcodec/mpeg12dec.c >> +++ b/libavcodec/mpeg12dec.c >> @@ -28,6 +28,7 @@ >> #define UNCHECKED_BITSTREAM_READER 1 >> #include >> >> +#include "libavutil/atomic.h" >> #include "libavutil/attributes.h" >> #include "libavutil/imgutils.h" >> #include "libavutil/internal.h" >> @@ -2476,7 +2477,7 @@ static int decode_chunks(AVCodecContext *avctx, >> AVFrame *picture, >> &s2->thread_context[0], NULL, >> s->slice_count, sizeof(void *)); >> for (i = 0; i < s->slice_count; i++) >> - s2->er.error_count += >> s2->thread_context[i]->er.error_count; >> + >> avpriv_atomic_int_add_and_fetch(&s2->er.error_count, >> s2->thread_context[i]->er.error_count); >> } > > This patch is corrupted by newlines > > [...] > > -- > Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB > > Dictatorship naturally arises out of democracy, and the most aggravated > form of tyranny and slavery out of the most extreme liberty. -- Plato > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel > From cd8ed0ee35853ae089df3d904846879ce4f00c4a Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 16 Nov 2017 11:50:38 -0800 Subject: [PATCH] libavcodec/mpeg12dec: Use atomic addition for value updated in multiple threads. --- libavcodec/mpeg12dec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index d5bc5f21b2..b7c3b5106e 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -28,6 +28,7 @@ #define UNCHECKED_BITSTREAM_READER 1 #include +#include "libavutil/atomic.h" #include "libavutil/attributes.h" #include "libavutil/imgutils.h" #include "libavutil/internal.h" @@ -2476,7 +2477,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, &s2->thread_context[0], NULL, s->slice_count, sizeof(void *)); for (i = 0; i < s->slice_count; i++) - s2->er.error_count += s2->thread_context[i]->er.error_count; + avpriv_atomic_int_add_and_fetch(&s2->er.error_count, s2->thread_context[i]->er.error_count); } ret = slice_end(avctx, picture); -- 2.15.0.448.gf294e3d99a-goog