Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp537656ioo; Sat, 21 May 2022 06:30:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQdIaADoZBjtbdQb6AMUGSV9xreEMkyDSxgh+0VEWOc68i3ed3qArmh5B9fVie2mD4lzZV X-Received: by 2002:a65:6ccd:0:b0:3c6:2ba2:5f9a with SMTP id g13-20020a656ccd000000b003c62ba25f9amr12953836pgw.216.1653139820745; Sat, 21 May 2022 06:30:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653139820; cv=none; d=google.com; s=arc-20160816; b=rvzDJZiSLppmPu519HBzQKCXCZ08Kn2uc+KcFRU8oHiz64ItD+7RfgoVWoJcsdijHL TsTLuaBLl2eTyQZf/SGlqW2Mdd3+NEillyY7rIgItfpVR2sYOOKuLQ4UltkhmoD0RMn1 URKRc6w0Nr1X3lcDG5+1pFY0vOt+3X+BLcywNX6Deeh8AYO/dgk4as2d3tbhJMnmk4aM Mi1Dv1/Or/+yED5rsqeCyLhOrTohOe9bHXheJ5e1is8jJNVIkv/5kzoVfeZuMGp6r3mu qhIBQdD6DOo4wMxroFwATUCBQbFmntgwkNKqNU1wpEKy+4WcFRegwTRYBw1j3EyE9Dr3 mM6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=GFbPgQEtbW912BPIItjV08RciM4FEzthIck0qttGkpk=; b=uJu1992AJPAHHujHnTpCqRECFLUIDcq1cdgqhTQ35EPhuoIFXJ4NFGAX+nTCl+Ea7P WW9FdZ8CVaCHGnIYnKCBi5uQqbs4+MCHqz8t4uCtggC0o3EEoUGcz3hB7CVdRYxUpczQ tuJ/p0UB9ONcI8o7sCuF8EOlI/Y4b7dg1LJOeE9OvdEKdonlWbeOXc7a3iMkAjDaL8YP vRwBiF6a5nZBq8dZweKTGg87E0IGnVuD4v6OP+5fWtCw8eJ8YCVWnNb5npVAjvzmKxwI KUV6Gnsk3aWuxFHeSYz9VDIFFLar2fr2G+KmvQigBv41DzQkl0J2EkyIQLI9BdLGAIQg MrLg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w23-20020a1709026f1700b0015f3e6cc98csi2891295plk.128.2022.05.21.06.30.08; Sat, 21 May 2022 06:30:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242959AbiETS7u (ORCPT + 99 others); Fri, 20 May 2022 14:59:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237963AbiETS7s (ORCPT ); Fri, 20 May 2022 14:59:48 -0400 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6A0F195E92; Fri, 20 May 2022 11:59:47 -0700 (PDT) Received: by mail-oi1-f175.google.com with SMTP id w130so11041009oig.0; Fri, 20 May 2022 11:59:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GFbPgQEtbW912BPIItjV08RciM4FEzthIck0qttGkpk=; b=fDsPDHpCxd2z66DUW2lygsqMnwmm+Oemfn/HWNOJM5u3y+fwXKI7FC2OVjuDN/mdJL WGOIwyGxQin0Fs/iuoAB6cpUM117DT2VXt7Lwq/q9SUyjZG24V07OKFP8VCywKCIhwiq jHyUf1gOLSfJY5c5NRUV4ZL38Q21609njDFdgYm5TuKxLht69Hjp+KBQzZC49uwd3bmY nqyXw5/CTlR6pSEElBHjxqghCNN7TTrqEXgrOjIEc9D+o0aitpB2MZyI1ln4Dq15gEDi H+shatSm9R06b+e2fahalh/oVOIl1RUcORpFkdSLYhTOiTAojzg5Quryn7vNnKj+2wUR UtGg== X-Gm-Message-State: AOAM531qC9rJ2vQT+Bq1CEm7yopMmzl2B2nyvDFYSOhlmoZtfNNpx0pw a0oLDhgx84YcL4WlUueXchgjurDkJmYiB+PrGFY= X-Received: by 2002:a05:6808:2218:b0:326:bd8c:d044 with SMTP id bd24-20020a056808221800b00326bd8cd044mr6204978oib.92.1653073187030; Fri, 20 May 2022 11:59:47 -0700 (PDT) MIME-Version: 1.0 References: <20220422093833.340873-1-nakamura.shun@fujitsu.com> <20220422093833.340873-3-nakamura.shun@fujitsu.com> In-Reply-To: <20220422093833.340873-3-nakamura.shun@fujitsu.com> From: Namhyung Kim Date: Fri, 20 May 2022 11:59:35 -0700 Message-ID: Subject: Re: [PATCH 2/7] libperf: Introduce perf_{evsel, evlist}__open_opt with extensible struct opts To: Shunsuke Nakamura Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , linux-kernel , linux-perf-users Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On Fri, Apr 22, 2022 at 2:44 AM Shunsuke Nakamura wrote: > > Introduce perf_{evsel, evlist}__open_opt with extensible structure opts. > The mechanism of the extensible structure opts imitates > tools/lib/bpf/libbpf.h. Currently, only open_flags is supported for the > opts structure. > > Signed-off-by: Shunsuke Nakamura > --- [SNIP] > diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c > index 210ea7c06ce8..00c0cea43b52 100644 > --- a/tools/lib/perf/evsel.c > +++ b/tools/lib/perf/evsel.c > @@ -16,8 +16,12 @@ > #include > #include > #include > +#include > +#include > +#include > #include > #include > +#include "internal.h" > > void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr, > int idx) > @@ -26,6 +30,7 @@ void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr, > evsel->attr = *attr; > evsel->idx = idx; > evsel->leader = evsel; > + evsel->open_flags = 0; In general, you don't need to reset it to zero as it's allocated with zalloc(). > } > [SNIP] > diff --git a/tools/lib/perf/internal.h b/tools/lib/perf/internal.h > index 2c27e158de6b..4b91a087ed62 100644 > --- a/tools/lib/perf/internal.h > +++ b/tools/lib/perf/internal.h > @@ -20,4 +20,48 @@ do { \ > #define pr_debug2(fmt, ...) __pr(LIBPERF_DEBUG2, fmt, ##__VA_ARGS__) > #define pr_debug3(fmt, ...) __pr(LIBPERF_DEBUG3, fmt, ##__VA_ARGS__) > > +static inline bool libperf_is_mem_zeroed(const char *p, ssize_t len) > +{ > + while (len > 0) { > + if (*p) > + return false; > + p++; > + len--; > + } > + return true; > +} > + > +static inline bool libperf_validate_opts(const char *opts, > + size_t opts_sz, size_t user_sz, > + const char *type_name) > +{ > + if (user_sz < sizeof(size_t)) { > + pr_warning("%s size (%zu) is too small\n", type_name, user_sz); > + return false; > + } > + if (!libperf_is_mem_zeroed(opts + opts_sz, (ssize_t)user_sz - opts_sz)) { I don't think the cast is necessary since it'll be promoted again to size_t due to opts_sz. Instead, we can check if user_sz is greater than opts_sz explicitly and call the function. > + pr_warning("%s has non-zero extra bytes\n", type_name); > + return false; > + } > + return true; > +} > + > +#define offsetofend(TYPE, FIELD) \ > + (offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD)) > + > +#define OPTS_VALID(opts, type) \ > + (!(opts) || libperf_validate_opts((const char *)opts, \ > + offsetofend(struct type, \ > + type##__last_field), \ > + (opts)->sz, #type)) > +#define OPTS_HAS(opts, field) \ > + ((opts) && opts->sz >= offsetofend(typeof(*(opts)), field)) > +#define OPTS_GET(opts, field, fallback_value) \ > + (OPTS_HAS(opts, field) ? (opts)->field : fallback_value) It'd be nice if you add a blank line between the macros. Thanks, Namhyung > +#define OPTS_SET(opts, field, value) \ > + do { \ > + if (OPTS_HAS(opts, field)) \ > + (opts)->field = value; \ > + } while (0) > + > #endif /* __LIBPERF_INTERNAL_H */ > diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map > index 190b56ae923a..eeeb3075e092 100644 > --- a/tools/lib/perf/libperf.map > +++ b/tools/lib/perf/libperf.map > @@ -25,6 +25,7 @@ LIBPERF_0.0.1 { > perf_evsel__enable; > perf_evsel__disable; > perf_evsel__open; > + perf_evsel__open_opts; > perf_evsel__close; > perf_evsel__mmap; > perf_evsel__munmap; > @@ -36,6 +37,7 @@ LIBPERF_0.0.1 { > perf_evlist__new; > perf_evlist__delete; > perf_evlist__open; > + perf_evlist__open_opts; > perf_evlist__close; > perf_evlist__enable; > perf_evlist__disable; > -- > 2.25.1 >