All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chris Wilson <chris@chris-wilson.co.uk>
To: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
	igt-dev@lists.freedesktop.org
Subject: Re: [igt-dev] [PATCH i-g-t] igt/sw_sync: Wrap threaded counter manipulation with mb
Date: Thu, 03 May 2018 20:40:53 +0100	[thread overview]
Message-ID: <152537645311.16610.651740206977321997@mail.alporthouse.com> (raw)
In-Reply-To: <152536655771.15663.15614570347092183928@jlahtine-desk.ger.corp.intel.com>

Quoting Joonas Lahtinen (2018-05-03 17:55:57)
> Quoting Chris Wilson (2018-05-02 12:28:07)
> > sw_sync/sync_multi_consumer_producer was communicating between threads
> > using the sw_sync ioctl and manipulating a shared volatile counter.
> > However, the ioctl itself does not imply a memory barrier, and so
> > different CPUs may see different states of the counter (the volatile
> > making GCC perform the operation in stages making the race even more
> > likely). Instead of using volatile, use locked operations to make the
> > counter manipulation thread-safe.
> > 
> > References: https://bugs.freedesktop.org/show_bug.cgi?id=106344
> > Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> > ---
> >  tests/sw_sync.c | 24 +++++++++++-------------
> >  1 file changed, 11 insertions(+), 13 deletions(-)
> > 
> > diff --git a/tests/sw_sync.c b/tests/sw_sync.c
> > index 20dfbbb98..c6867e097 100644
> > --- a/tests/sw_sync.c
> > +++ b/tests/sw_sync.c
> > @@ -43,7 +43,7 @@ IGT_TEST_DESCRIPTION("Test SW Sync Framework");
> >  typedef struct {
> >         int timeline;
> >         uint32_t thread_id;
> > -       volatile uint32_t * volatile counter;
> > +       uint32_t *counter;
> >         sem_t *sem;
> >  } data_t;
> >  
> > @@ -508,7 +508,7 @@ static void * test_sync_multi_consumer_thread(void *arg)
> >                 if (sync_fence_wait(fence, 1000) < 0)
> >                         return (void *) 1;
> >  
> > -               if (*(data->counter) != next_point)
> > +               if (READ_ONCE(*data->counter) != next_point)
> >                         return (void *) 1;
> >  
> >                 sem_post(data->sem);
> > @@ -524,7 +524,7 @@ static void test_sync_multi_consumer(void)
> >         pthread_t thread_arr[MULTI_CONSUMER_THREADS];
> >         sem_t sem;
> >         int timeline;
> > -       volatile uint32_t counter = 0;
> > +       uint32_t counter = 0;
> >         uintptr_t thread_ret = 0;
> >         data_t data;
> >         int i, ret;
> > @@ -552,7 +552,7 @@ static void test_sync_multi_consumer(void)
> >         {
> >                 sem_wait(&sem);
> >  
> > -               counter++;
> > +               __sync_fetch_and_add(&counter, 1);
> >                 sw_sync_timeline_inc(timeline, 1);
> >         }
> >  
> > @@ -567,8 +567,8 @@ static void test_sync_multi_consumer(void)
> >         close(timeline);
> >         sem_destroy(&sem);
> >  
> > -       igt_assert_f(counter == MULTI_CONSUMER_THREADS * MULTI_CONSUMER_ITERATIONS,
> > -                    "Counter has unexpected value.\n");
> > +       igt_assert_eq(counter,
> > +                     MULTI_CONSUMER_THREADS * MULTI_CONSUMER_ITERATIONS);
> >  
> >         igt_assert_f(thread_ret == 0, "A sync thread reported failure.\n");
> >  }
> > @@ -589,11 +589,9 @@ static void * test_sync_multi_consumer_producer_thread(void *arg)
> >                 if (sync_fence_wait(fence, 1000) < 0)
> >                         return (void *) 1;
> >  
> > -               if (*(data->counter) != next_point)
> > +               if (__sync_fetch_and_add(data->counter, 1) != next_point)
> >                         return (void *) 1;
> >  
> > -               (*data->counter)++;
> 
> This is a behavior change, the counter gets incremented always :(

But it doesn't matter, since the return 1 here is failure. And any
thread reporting failure is enough.
-Chris
_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

  reply	other threads:[~2018-05-03 19:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-02  9:28 [igt-dev] [PATCH i-g-t] igt/sw_sync: Wrap threaded counter manipulation with mb Chris Wilson
2018-05-02 15:16 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork
2018-05-02 19:40 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
2018-05-03 16:55 ` [igt-dev] [PATCH i-g-t] " Joonas Lahtinen
2018-05-03 19:40   ` Chris Wilson [this message]
2018-06-15  9:07     ` Chris Wilson
2018-05-14  8:06   ` Chris Wilson
2018-06-15 11:00 ` Joonas Lahtinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=152537645311.16610.651740206977321997@mail.alporthouse.com \
    --to=chris@chris-wilson.co.uk \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=joonas.lahtinen@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.