Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2735969rdb; Mon, 5 Feb 2024 16:38:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4/FFxKCJpLg/FTUUXegX7o4YQioT9cQ5Gnm/Fl4Jbk//TzfDCU2Mt7iRqYcFDQ0hgTKZq X-Received: by 2002:a05:622a:190b:b0:42c:111a:ac7c with SMTP id w11-20020a05622a190b00b0042c111aac7cmr15282957qtc.34.1707179888542; Mon, 05 Feb 2024 16:38:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707179888; cv=pass; d=google.com; s=arc-20160816; b=LTQXPGednvYHVAnlA/Qy2a6gwEUsTDGSuNRcF8I50LxPMY3YIYaJdyicuk4AF2T+2e olOg5BIKWif6Xdi5ooCnC5MKo/12OMhMgZNFrl4U5bU9XbYj7+NvjlUQgbK7D2WVSOSJ GoJx6ufQWQg27bnotPhRMOZg7fUBLR9l7pk5+bgeR1XzohRC5PThgqPLnWsrpEdxiDfT Q9KwbIqaqtwSSzlSDRxYbEh09pxTj4NxwLZPBjS4DCxCSzdnxeoRGySYSGEnKhU+xrSw O4AgwrDsBoDIjgJ6WxVhKhwnYQRNxgHTvDCM3Oq5UgqjwmQR2uoskn/uuA+mHvFf9yx4 zV2w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=eLXwxn1c+Kh9cKSsiilLIy3CGMCH0CX3Zb7/Ic0eQqk=; fh=btTiD8ueCkOdqjgDbKhXvrzGcE7Aw50EkrUIzWJS9+0=; b=gu8z+J7c2qe9wVNgOfjj+OPhirWYEv5ak7i3gzDC8qCSZeJ7BARRZ5jzYTo8r5tajQ 5YYM1DJv73f3LMN5fH2ojwPVgat8feTm8OcW3jhpvup14gLhjdQ38xNv4U0FOWPvFjMx /cs9mt5qMgSMaeLPQuHyXVPYOOY7i67cNWzRleWZO1R5B75HzTQgRcmRlouOFT85RIKL XhaPr/B8YZsBsSmV94ew4hJgqOpqGy0tPPLUEbpQJeODmlnUK0aEGteYJYAG4uSfI6Z9 lXCNVe4GhOuU7QbkvTQvG2AQ/rmUcI98GXEpfwHNv0MIzDd0PkVSWqlhlmmL/taE14js fR9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-54128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUZLMm+RGIOtf746O4avSvwdXd7wM/G0I0aEY/VwKCGvLHPM9VuKdFxI5ERsqDmLg+4FIK6AtF9kVm2uV8eqrQ/xn4ktNQ8zqdWyItJJg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t1-20020a05622a148100b0042beb657032si1174411qtx.276.2024.02.05.16.38.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 16:38:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-54128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4132D1C24723 for ; Tue, 6 Feb 2024 00:38:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA9EC1361; Tue, 6 Feb 2024 00:38:01 +0000 (UTC) Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AADE9EDE; Tue, 6 Feb 2024 00:37:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707179881; cv=none; b=sjrDM2GZEmeb1lnR8/+OL4r+/Z/fkY3S/6shEB5g3H/6DjV7tNKZayuB0lzHsjKbYCDetpj4bBM/5rBjrsPcc8qArOSKmT34fOGSTCgaUTg7Zn9MOkroFhhPHc9sJ+FIoGij+P7+FoOpG5WUguyPMUd/WUc4xEAfSDmPVSWWhlc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707179881; c=relaxed/simple; bh=cgDHhAokoJ3JI9GUvK2dmNMWiCipmG7oxT8Xy10ZKZ8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=B9mypZjzWpEFVPffiBml1afPnmP6+T7oSk9T+2CFWOH/qIzHtY0PU5jXFPnhVPBFHTpHlJXE2c8cL/8Wjt539txQUvdYNG3QvBp5WD2zthLdD8xBUqUfaF+t3SEiJEqQoMDSLG48hvlSZIOhYIZJnyLfe3rM+KmB0r4y/RD/+zc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1d93b525959so544415ad.0; Mon, 05 Feb 2024 16:37:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707179879; x=1707784679; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eLXwxn1c+Kh9cKSsiilLIy3CGMCH0CX3Zb7/Ic0eQqk=; b=R5K2I0sn3g5NAgogBi6G4f0Z05Ce0uUUjNQ15vEJ5MhHoTJiMEX2C0FgN9F/Vlu2Yl uwT36mmYJY3FoRmm+4i1gsWnqbcmWubWLJEFMn1sqGM/cDB8FBIBUK9QBImbP1/MViJ4 alcntY9lD6Bs+47GDnId8DeABacqtH071HsZ3HZ4RsuokhwIDY7NCssHPVIOuFQ13+JH mC4lqj8r97JTbCEUMgRVsIRsNSI5YP/cm0Is8Z1EoZliHnph7SnBVjIDhqhzsu/0FP39 C2LtxeIAJo2455ijVLmnvPkFpBZvPmvpZvetr0wbIzz4ZlEHSIhiD1/Lg4CY6Cu5VYoE 3/bw== X-Gm-Message-State: AOJu0Yw6Gn3V9KF1FhNE0vg3UObjt6bIc/TA4fKcdQi5JIPzi+EFUwdk vdSDbQPIKWD0a25BCNm+GiQfV5x3mb/ntJ1SiL46qOGYf2oifB5kNf9LcwBjqosag/ihUV9ShpF otnWFxFXA+ONLKODWanAjjlsTc9w= X-Received: by 2002:a05:6a20:4310:b0:19c:9a00:1351 with SMTP id h16-20020a056a20431000b0019c9a001351mr211155pzk.25.1707179878934; Mon, 05 Feb 2024 16:37:58 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240103050635.391888-1-irogers@google.com> <20240103050635.391888-2-irogers@google.com> In-Reply-To: From: Namhyung Kim Date: Mon, 5 Feb 2024 16:37:47 -0800 Message-ID: Subject: Re: [PATCH v7 01/25] perf maps: Switch from rbtree to lazily sorted array for addresses To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Ian, Sorry for the late reply. On Thu, Feb 1, 2024 at 8:21=E2=80=AFPM Ian Rogers wrot= e: > > On Thu, Feb 1, 2024 at 6:48=E2=80=AFPM Namhyung Kim = 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 =3D 0; > > > + unsigned int n; > > > + > > > + down_write(maps__lock(dest)); > > > down_read(maps__lock(parent)); > > > > > > - maps__for_each_entry(parent, rb_node) { > > > - struct map *new =3D map__clone(rb_node->map); > > > + parent_maps_by_address =3D maps__maps_by_address(parent); > > > + n =3D maps__nr_maps(parent); > > > + if (maps__empty(dest)) { > > > + /* No existing mappings so just copy from parent to a= void reallocs in insert. */ > > > + unsigned int nr_maps_allocated =3D RC_CHK_ACCESS(pare= nt)->nr_maps_allocated; > > > + struct map **dest_maps_by_address =3D > > > + malloc(nr_maps_allocated * sizeof(struct map = *)); > > > + struct map **dest_maps_by_name =3D NULL; > > > > > > - if (new =3D=3D NULL) { > > > + if (!dest_maps_by_address) > > > err =3D -ENOMEM; > > > - goto out_unlock; > > > + else { > > > + if (maps__maps_by_name(parent)) { > > > + dest_maps_by_name =3D > > > + malloc(nr_maps_allocated * si= zeof(struct map *)); > > > + } > > > + > > > + RC_CHK_ACCESS(dest)->maps_by_address =3D dest= _maps_by_address; > > > + RC_CHK_ACCESS(dest)->maps_by_name =3D dest_ma= ps_by_name; > > > + RC_CHK_ACCESS(dest)->nr_maps_allocated =3D nr= _maps_allocated; > > > } > > > > > > - err =3D unwind__prepare_access(maps, new, NULL); > > > - if (err) > > > - goto out_unlock; > > > + for (unsigned int i =3D 0; !err && i < n; i++) { > > > + struct map *pos =3D parent_maps_by_address[i]= ; > > > + struct map *new =3D map__clone(pos); > > > > > > - err =3D maps__insert(maps, new); > > > - if (err) > > > - goto out_unlock; > > > + if (!new) > > > + err =3D -ENOMEM; > > > + else { > > > + err =3D unwind__prepare_access(dest, = new, NULL); > > > + if (!err) { > > > + dest_maps_by_address[i] =3D n= ew; > > > + if (dest_maps_by_name) > > > + dest_maps_by_name[i] = =3D map__get(new); > > > + RC_CHK_ACCESS(dest)->nr_maps = =3D 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 = =3D > > > + RC_CHK_ACCESS(parent)->last_search_by= _name_idx; > > > + maps__set_maps_by_name_sorted(dest, > > > + dest_maps_by_name && > > > + maps__maps_by_name_so= rted(parent)); > > > + } else { > > > + RC_CHK_ACCESS(dest)->last_search_by_name_idx = =3D 0; > > > + maps__set_maps_by_name_sorted(dest, false); > > > + } > > > + } else { > > > + /* Unexpected copying to a maps containing entries. *= / > > > + for (unsigned int i =3D 0; !err && i < n; i++) { > > > + struct map *pos =3D parent_maps_by_address[i]= ; > > > + struct map *new =3D map__clone(pos); > > > > > > - map__put(new); > > > + if (!new) > > > + err =3D -ENOMEM; > > > + else { > > > + err =3D unwind__prepare_access(dest, = new, NULL); > > > + if (!err) > > > + err =3D maps__insert(dest, ne= w); > > > > 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 =3D 0; > > > -out_unlock: > > > up_read(maps__lock(parent)); > > > + up_write(maps__lock(dest)); > > > return err; > > > }