Re: [PATCH v7 01/25] perf maps: Switch from rbtree to lazily sorted array for addresses

From: Namhyung Kim
Date: Mon Feb 05 2024 - 19:38:07 EST


Hi Ian,

Sorry for the late reply.

On Thu, Feb 1, 2024 at 8:21 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> On Thu, Feb 1, 2024 at 6:48 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
[SNIP]
> > > +int maps__copy_from(struct maps *dest, struct maps *parent)
> > > +{
> > > + /* Note, if struct map were immutable then cloning could use ref counts. */
> > > + struct map **parent_maps_by_address;
> > > + int err = 0;
> > > + unsigned int n;
> > > +
> > > + down_write(maps__lock(dest));
> > > down_read(maps__lock(parent));
> > >
> > > - maps__for_each_entry(parent, rb_node) {
> > > - struct map *new = map__clone(rb_node->map);
> > > + parent_maps_by_address = maps__maps_by_address(parent);
> > > + n = maps__nr_maps(parent);
> > > + if (maps__empty(dest)) {
> > > + /* No existing mappings so just copy from parent to avoid reallocs in insert. */
> > > + unsigned int nr_maps_allocated = RC_CHK_ACCESS(parent)->nr_maps_allocated;
> > > + struct map **dest_maps_by_address =
> > > + malloc(nr_maps_allocated * sizeof(struct map *));
> > > + struct map **dest_maps_by_name = NULL;
> > >
> > > - if (new == NULL) {
> > > + if (!dest_maps_by_address)
> > > err = -ENOMEM;
> > > - goto out_unlock;
> > > + else {
> > > + if (maps__maps_by_name(parent)) {
> > > + dest_maps_by_name =
> > > + malloc(nr_maps_allocated * sizeof(struct map *));
> > > + }
> > > +
> > > + RC_CHK_ACCESS(dest)->maps_by_address = dest_maps_by_address;
> > > + RC_CHK_ACCESS(dest)->maps_by_name = dest_maps_by_name;
> > > + RC_CHK_ACCESS(dest)->nr_maps_allocated = nr_maps_allocated;
> > > }
> > >
> > > - err = unwind__prepare_access(maps, new, NULL);
> > > - if (err)
> > > - goto out_unlock;
> > > + for (unsigned int i = 0; !err && i < n; i++) {
> > > + struct map *pos = parent_maps_by_address[i];
> > > + struct map *new = map__clone(pos);
> > >
> > > - err = maps__insert(maps, new);
> > > - if (err)
> > > - goto out_unlock;
> > > + if (!new)
> > > + err = -ENOMEM;
> > > + else {
> > > + err = unwind__prepare_access(dest, new, NULL);
> > > + if (!err) {
> > > + dest_maps_by_address[i] = new;
> > > + if (dest_maps_by_name)
> > > + dest_maps_by_name[i] = map__get(new);
> > > + RC_CHK_ACCESS(dest)->nr_maps = i + 1;
> > > + }
> > > + }
> > > + if (err)
> > > + map__put(new);
> > > + }
> > > + maps__set_maps_by_address_sorted(dest, maps__maps_by_address_sorted(parent));
> > > + if (!err) {
> > > + RC_CHK_ACCESS(dest)->last_search_by_name_idx =
> > > + RC_CHK_ACCESS(parent)->last_search_by_name_idx;
> > > + maps__set_maps_by_name_sorted(dest,
> > > + dest_maps_by_name &&
> > > + maps__maps_by_name_sorted(parent));
> > > + } else {
> > > + RC_CHK_ACCESS(dest)->last_search_by_name_idx = 0;
> > > + maps__set_maps_by_name_sorted(dest, false);
> > > + }
> > > + } else {
> > > + /* Unexpected copying to a maps containing entries. */
> > > + for (unsigned int i = 0; !err && i < n; i++) {
> > > + struct map *pos = parent_maps_by_address[i];
> > > + struct map *new = map__clone(pos);
> > >
> > > - map__put(new);
> > > + if (!new)
> > > + err = -ENOMEM;
> > > + else {
> > > + err = unwind__prepare_access(dest, new, NULL);
> > > + if (!err)
> > > + err = maps__insert(dest, new);
> >
> > Shouldn't it be __maps__insert()?
>
> On entry the read lock is taken on parent but no lock is taken on dest
> so the locked version is used.

I think you added the writer lock on dest.

Thanks,
Namhyung

>
> > > + }
> > > + map__put(new);
> > > + }
> > > }
> > > -
> > > - err = 0;
> > > -out_unlock:
> > > up_read(maps__lock(parent));
> > > + up_write(maps__lock(dest));
> > > return err;
> > > }