diff mbox series

[FFmpeg-devel,2/3] swscale/swscale: Fix races when using unaligned strides/data

Message ID AM7PR03MB6660AC1E45A4490D61118BCA8FDE9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit f440c422b70ce76f225a34ebf168215a432e8d88
Headers show
Series [FFmpeg-devel,1/3] libswscale/options: Add parent_log_context_offset to AVClass | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 18, 2021, 3:43 a.m. UTC
In this case the current code tries to warn once; to do so, it uses
ordinary static ints to store whether the warning has already been
emitted. This is both a data race (and therefore undefined behaviour)
as well as a race condition, because it is really possible for multiple
threads to be the one thread to emit the warning. This is actually
common since the introduction of the new multithreaded scaling API.

This commit fixes this by using atomic integers for the state;
furthermore, these are not static anymore, but rather contained
in the user-facing SwsContext (i.e. the parent SwsContext in case
of slice-threading).

Given that these atomic variables are not intended for synchronization
at all (but only for atomicity, i.e. only to output the warning once),
the atomic operations use memory_order_relaxed.

This affected nv12, nv21, yuv420, yuv420p10, yuv422, yuv422p10 and
yuv444 filter-overlay FATE-tests.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
Of course, one could also use static atomic integers.

 libswscale/swscale.c          | 12 ++++++------
 libswscale/swscale_internal.h |  4 ++++
 libswscale/utils.c            |  2 ++
 3 files changed, 12 insertions(+), 6 deletions(-)

Comments

Michael Niedermayer Sept. 19, 2021, 2:47 p.m. UTC | #1
On Sat, Sep 18, 2021 at 05:43:25AM +0200, Andreas Rheinhardt wrote:
> In this case the current code tries to warn once; to do so, it uses
> ordinary static ints to store whether the warning has already been
> emitted. This is both a data race (and therefore undefined behaviour)
> as well as a race condition, because it is really possible for multiple
> threads to be the one thread to emit the warning. This is actually
> common since the introduction of the new multithreaded scaling API.
> 
> This commit fixes this by using atomic integers for the state;
> furthermore, these are not static anymore, but rather contained
> in the user-facing SwsContext (i.e. the parent SwsContext in case
> of slice-threading).
> 
> Given that these atomic variables are not intended for synchronization
> at all (but only for atomicity, i.e. only to output the warning once),
> the atomic operations use memory_order_relaxed.
> 
> This affected nv12, nv21, yuv420, yuv420p10, yuv422, yuv422p10 and
> yuv444 filter-overlay FATE-tests.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> Of course, one could also use static atomic integers.

should be ok

thx

[...]
diff mbox series

Patch

diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index b9c9647fcb..040172752f 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -312,12 +312,12 @@  static int swscale(SwsContext *c, const uint8_t *src[],
 
     if (dstStride[0]&15 || dstStride[1]&15 ||
         dstStride[2]&15 || dstStride[3]&15) {
-        static int warnedAlready = 0; // FIXME maybe move this into the context
-        if (flags & SWS_PRINT_INFO && !warnedAlready) {
+        SwsContext *const ctx = c->parent ? c->parent : c;
+        if (flags & SWS_PRINT_INFO &&
+            !atomic_exchange_explicit(&ctx->stride_unaligned_warned, 1, memory_order_relaxed)) {
             av_log(c, AV_LOG_WARNING,
                    "Warning: dstStride is not aligned!\n"
                    "         ->cannot do aligned memory accesses anymore\n");
-            warnedAlready = 1;
         }
     }
 
@@ -326,11 +326,11 @@  static int swscale(SwsContext *c, const uint8_t *src[],
         || dstStride[0]&15 || dstStride[1]&15 || dstStride[2]&15 || dstStride[3]&15
         || srcStride[0]&15 || srcStride[1]&15 || srcStride[2]&15 || srcStride[3]&15
     ) {
-        static int warnedAlready=0;
+        SwsContext *const ctx = c->parent ? c->parent : c;
         int cpu_flags = av_get_cpu_flags();
-        if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
+        if (HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) &&
+            !atomic_exchange_explicit(&ctx->stride_unaligned_warned,1, memory_order_relaxed)) {
             av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speed loss\n");
-            warnedAlready=1;
         }
     }
 
diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h
index 0d60dd2e6f..e6e7b934b6 100644
--- a/libswscale/swscale_internal.h
+++ b/libswscale/swscale_internal.h
@@ -21,6 +21,8 @@ 
 #ifndef SWSCALE_SWSCALE_INTERNAL_H
 #define SWSCALE_SWSCALE_INTERNAL_H
 
+#include <stdatomic.h>
+
 #include "config.h"
 #include "version.h"
 
@@ -672,6 +674,8 @@  typedef struct SwsContext {
     unsigned int xyz_scratch_allocated;
 
     unsigned int dst_slice_align;
+    atomic_int   stride_unaligned_warned;
+    atomic_int   data_unaligned_warned;
 } SwsContext;
 //FIXME check init (where 0)
 
diff --git a/libswscale/utils.c b/libswscale/utils.c
index 84a29c4dc7..fcfba971c6 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -1112,6 +1112,8 @@  SwsContext *sws_alloc_context(void)
     if (c) {
         c->av_class = &ff_sws_context_class;
         av_opt_set_defaults(c);
+        atomic_init(&c->stride_unaligned_warned, 0);
+        atomic_init(&c->data_unaligned_warned,   0);
     }
 
     return c;