[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <b60c7731b8a84e01a77fea55c31a77b9@AcuMS.aculab.com>
Date: Thu, 29 Feb 2024 21:59:05 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Ian Rogers' <irogers@...gle.com>, Namhyung Kim <namhyung@...nel.org>
CC: Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>, Mark Rutland
<mark.rutland@....com>, Alexander Shishkin
<alexander.shishkin@...ux.intel.com>, Jiri Olsa <jolsa@...nel.org>, "Adrian
Hunter" <adrian.hunter@...el.com>, Oliver Upton <oliver.upton@...ux.dev>,
"Yang Jihong" <yangjihong1@...wei.com>, "linux-kernel@...r.kernel.org"
<linux-kernel@...r.kernel.org>, "linux-perf-users@...r.kernel.org"
<linux-perf-users@...r.kernel.org>, "bpf@...r.kernel.org"
<bpf@...r.kernel.org>
Subject: RE: [PATCH v1 4/6] perf threads: Move threads to its own files
From: Ian Rogers
> Sent: 27 February 2024 07:24
>
> On Mon, Feb 26, 2024 at 11:07 PM Namhyung Kim <namhyung@...nel.org> wrote:
> >
> > On Tue, Feb 13, 2024 at 10:37 PM Ian Rogers <irogers@...gle.com> wrote:
> > >
> > > Move threads out of machine and move thread_rb_node into the C
> > > file. This hides the implementation of threads from the rest of the
> > > code allowing for it to be refactored.
> > >
> > > Locking discipline is tightened up in this change.
> >
> > Doesn't look like a simple code move. Can we split the locking
> > change from the move to make the reviewer's life a bit easier? :)
>
> Not sure I follow. Take threads_nr as an example.
>
> The old code is in machine.c, so:
> -static size_t machine__threads_nr(const struct machine *machine)
> -{
> - size_t nr = 0;
> -
> - for (int i = 0; i < THREADS__TABLE_SIZE; i++)
> - nr += machine->threads[i].nr;
> -
> - return nr;
> -}
>
> The new code is in threads.c:
> +size_t threads__nr(struct threads *threads)
> +{
> + size_t nr = 0;
> +
> + for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
> + struct threads_table_entry *table = &threads->table[i];
> +
> + down_read(&table->lock);
> + nr += table->nr;
> + up_read(&table->lock);
> + }
> + return nr;
> +}
>
> So it is a copy paste from one file to the other. The only difference
> is that the old code failed to take a lock when reading "nr" so the
> locking is added. I wanted to make sure all the functions in threads.c
> were properly correct wrt locking, semaphore creation and destruction,
> etc. We could have a broken threads.c and fix it in the next change,
> but given that's a bug it could make bisection more difficult.
> Ultimately I thought the locking changes were small enough to not
> warrant being on their own compared to the advantages of having a sane
> threads abstraction.
The lock is pretty much entirely pointless.
All it really does is slow the code down.
The most you could want is:
nr += READ_ONCE(table->nr);
to avoid any hypothetical data tearing.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Powered by blists - more mailing lists