Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp787599lql; Mon, 11 Mar 2024 18:54:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWNjMnUARJGNNbPqVTCxB73ExtshYcWBboVbs0Df/Mif7zCesgdeV2Pvf4ThgtNmthknjXGu+duu6dltj1Y3KXxBT2QmpXqTTjA4DTZuQ== X-Google-Smtp-Source: AGHT+IE1bcK3HaRmM1Es8Gv0P67b7Xeq2Ax1wV22ZmWdgv76vX6Dc1pLDJi/Ua/fTtGkQ+N07zoj X-Received: by 2002:a50:cd9c:0:b0:567:80c7:33bf with SMTP id p28-20020a50cd9c000000b0056780c733bfmr7418083edi.7.1710208452041; Mon, 11 Mar 2024 18:54:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710208452; cv=pass; d=google.com; s=arc-20160816; b=gdWNnmpT1KghytqVMARHvpJ/QxrXDSDoc3actvi3rpj0Q21sf9CmvC3camKCHG9OYz Py7CrA5vEf78gte/Z69ARu7ffdo+9rsS+Smkgq7qvvwGN1PlHQFV8+IkanXm131rJH78 5UWdFTfJh7crOr7ql6k8cGMZZLqq+vLMRL+6F8tHlyv5EotHoDtKlTyipH6XaBkuUr4T fipBax9MwEE/baw0RVHykyQBo+5f6OEg199tUJAzdKN5EH4moUt39oiLSPv+YavW2jBv 0l7JSIL+AwRkEY95IItk37/UzJ0ZdLwdEad7jxV5B1TBxLNj7zqkeb4b3hnSuj2YnGMk Du4w== 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:dkim-signature; bh=m+8U24REJsycoMY3gapLXd6No4Kdp5va5OvGqVZaNtY=; fh=nUO3dZwW51FL4F2rQCVzyLpGEUkq+iZ9XbSnzhWG6v4=; b=ywc2fRs4MPLX7NhmY1DO1rv271lB3EScO/IASqLfKyJcVr+jaqKe/CYktTvCz1ZBvl e39QgJjbAqDeRWEJEEuolx79i+T4YrTYApAajGRkqef9hIcY5M17KCtBuOzPV+B47L84 fI+UU9OR3XuxLIsj4zVl9BjTJXf9w3Do7ectbqKxnCSZVJB/ARnfJkXsxggbVBQs1u8U PUOerhDVWc69SuALPjyMDbEmgagX5BnPkL//Wk9jF2Ym+R3pH/i+UC25xTeCV975Z0Nn 9m+l1e7Ks3Hc4ce1ZvPkUqCxq8k6Z/Stliv9JAzbflEvQ7MpmeNOVH6+fHrbdAAzlANd hy7g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=gqzO+a1j; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-99748-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99748-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id eo5-20020a056402530500b0056830d7fd0asi3164932edb.74.2024.03.11.18.54.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 18:54:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99748-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=gqzO+a1j; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-99748-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99748-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9A23A1F21CCB for ; Tue, 12 Mar 2024 01:54:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91AFD6FB9; Tue, 12 Mar 2024 01:53:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="gqzO+a1j" Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (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 1CBB61C17 for ; Tue, 12 Mar 2024 01:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710208438; cv=none; b=Vn7L0c6KPiaiIbWKWl9Hq3dzg+2Zhg5qQJvymlvKjYsKJIa/yuDs7y30fziomhQkJdtpkPwU1WYtoURkThAPXXLZz7NfmJot5EUQuACBqkcRigf/sXidCD3+KkpUlL83norMeHfUpotx8MRPBB/3lzK/fSvXiW4VEMRVfmYvZSk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710208438; c=relaxed/simple; bh=I3fTBn/wVvCv8wJz3S0c7XFDmGuGRN+xuP6W+JUCPFI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Ux6xq5QWKApmj7XyOLzd3TXHCTbxX47hGI/0GLPiZZ4WKlJuBukoYvF6qyxI9E5Czyk6VWIgCjJfvWKIs3bNLezvseypMj4VquCKl2XE+eBYcRzBaHcuI+vKs1AoYppkBohVHb5iL3e4WYfZJpoxN5u2dEBmNCIHM+Mr/jyYQ7Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=gqzO+a1j; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5dc949f998fso4625370a12.3 for ; Mon, 11 Mar 2024 18:53:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1710208436; x=1710813236; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=m+8U24REJsycoMY3gapLXd6No4Kdp5va5OvGqVZaNtY=; b=gqzO+a1jjHOI/w0pMfToI1QKtDcRnyMXI1L4htcnM9oM3BxUP5MzM64TykQ1A/w61B 6SN/UpkjdOP/nLhZYMxzhkf8I1bt098WSz2tbezMnBmbXzK1vSkzxRCr8zwY23epb+Hq DwyNxPr4uAglsLiod8M5wN3XSVQztL9k2Tp8h+TsVRhyLfTqLN4yAscHjE9s4XdONMG6 rQI98uDiDheGtIYYe3ikSJZFDL+DpK7Fof38hYjjh7hhvmvNvRVBJ9z6xdbZtgnGTfww 0s9ah7zPLeQ/eP8dnV6Ocnt06QcFG5Mz3K5dRAaYWlLI7UWaVi0l8kTd77ldtBLe7SAP DjMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710208436; x=1710813236; 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=m+8U24REJsycoMY3gapLXd6No4Kdp5va5OvGqVZaNtY=; b=hOT38En+hg8VmuB1Exs/ZWlMvfvqGWziVugbFbogOxxMVuOXUzxWN/O52HfIK4xaWd 7vUcSjlPdjQwBdRNxqfsM5Hqdxwd1ZQiJuy1jvPS/NzzzxO9z+J0iL3ic1mbhCYtQuW+ lyPDx4VTxvOrJjHxvpLd2yDPKTdqm06cS2IGJUIqbd3/Ptglc8vWxusKljaCGxU7VBLe A+/sCgygfMX0IuUfcmcD0roH2g5HJmk0yhj4n0fT3rt8fnVDycUL0TgeRVdrwXv2uBrU GxhbA/FDvuoNQy05VPhlNWZ8Ha/q0CtkX1lHoNFimBIMQo/sRmVvJZBNLpHbWt5QoIz+ E7ow== X-Forwarded-Encrypted: i=1; AJvYcCUpd/bz0Z9onJ3hHASGOoBYjCNWrWQuVJhGxoN7K8GZz/FTJ3dHUIq44YoU3P75pAvftsgydXMCtg+wrQBynQ3N0GtGIAczSCZBR+nO X-Gm-Message-State: AOJu0YwcSw1L0CdfkAAAgAoP8Fjr8LJ0Jc8Ws+fiCLQkDxOwQRRBPOMK 0phgQAm0dsezZBQ0kF0RHA8LYU7OUCvkLn9pNS29sobXQXIrDPhGwQY7Y5WuePXocaNWJfu6bEO FUUlkJ4tmzA9iTK/KRhdetZkDme5lLTeZae/JlQ== X-Received: by 2002:a05:6a20:429a:b0:1a1:4a4c:9f5e with SMTP id o26-20020a056a20429a00b001a14a4c9f5emr646029pzj.49.1710208436407; Mon, 11 Mar 2024 18:53:56 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240311093526.1010158-1-dongmenglong.8@bytedance.com> <20240311093526.1010158-3-dongmenglong.8@bytedance.com> In-Reply-To: From: =?UTF-8?B?5qKm6b6Z6JGj?= Date: Tue, 12 Mar 2024 09:53:45 +0800 Message-ID: Subject: Re: [External] Re: [PATCH bpf-next v2 2/9] bpf: refactor the modules_array to ptr_array To: Alexei Starovoitov Cc: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Eddy Z , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , David Ahern , Dave Hansen , X86 ML , Steven Rostedt , Mathieu Desnoyers , Quentin Monnet , bpf , linux-arm-kernel , LKML , linux-riscv , linux-s390 , Network Development , linux-trace-kernel@vger.kernel.org, "open list:KERNEL SELFTEST FRAMEWORK" , linux-stm32@st-md-mailman.stormreply.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Mar 12, 2024 at 9:49=E2=80=AFAM Alexei Starovoitov wrote: > > On Mon, Mar 11, 2024 at 2:34=E2=80=AFAM Menglong Dong > wrote: > > > > Refactor the struct modules_array to more general struct ptr_array, whi= ch > > is used to store the pointers. > > > > Meanwhiles, introduce the bpf_try_add_ptr(), which checks the existing = of > > the ptr before adding it to the array. > > > > Seems it should be moved to another files in "lib", and I'm not sure wh= ere > > to add it now, and let's move it to kernel/bpf/syscall.c for now. > > > > Signed-off-by: Menglong Dong > > --- > > include/linux/bpf.h | 10 +++++++++ > > kernel/bpf/syscall.c | 37 +++++++++++++++++++++++++++++++ > > kernel/trace/bpf_trace.c | 48 ++++++---------------------------------- > > 3 files changed, 54 insertions(+), 41 deletions(-) > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index 0f677fdcfcc7..997765cdf474 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -304,6 +304,16 @@ struct bpf_map { > > s64 __percpu *elem_count; > > }; > > > > +struct ptr_array { > > + void **ptrs; > > + int cnt; > > + int cap; > > +}; > > + > > +int bpf_add_ptr(struct ptr_array *arr, void *ptr); > > +bool bpf_has_ptr(struct ptr_array *arr, struct module *mod); > > +int bpf_try_add_ptr(struct ptr_array *arr, void *ptr); > > + > > static inline const char *btf_field_type_name(enum btf_field_type type= ) > > { > > switch (type) { > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > > index f63f4da4db5e..4f230fd1f8e4 100644 > > --- a/kernel/bpf/syscall.c > > +++ b/kernel/bpf/syscall.c > > @@ -479,6 +479,43 @@ static void bpf_map_release_memcg(struct bpf_map *= map) > > } > > #endif > > > > +int bpf_add_ptr(struct ptr_array *arr, void *ptr) > > +{ > > + void **ptrs; > > + > > + if (arr->cnt =3D=3D arr->cap) { > > + arr->cap =3D max(16, arr->cap * 3 / 2); > > + ptrs =3D krealloc_array(arr->ptrs, arr->cap, sizeof(*pt= rs), GFP_KERNEL); > > + if (!ptrs) > > + return -ENOMEM; > > + arr->ptrs =3D ptrs; > > + } > > + > > + arr->ptrs[arr->cnt] =3D ptr; > > + arr->cnt++; > > + return 0; > > +} > > + > > +bool bpf_has_ptr(struct ptr_array *arr, struct module *mod) > > Don't you need 'void *mod' here? > Oops, it should be void *ptr here, my mistake~ > > +{ > > + int i; > > + > > + for (i =3D arr->cnt - 1; i >=3D 0; i--) { > > + if (arr->ptrs[i] =3D=3D mod) > > + return true; > > + } > > + return false; > > +} > > ... > > > - kprobe_multi_put_modules(arr.mods, arr.mods_cnt); > > - kfree(arr.mods); > > + kprobe_multi_put_modules((struct module **)arr.ptrs, ar= r.cnt); > > Do you really need to type cast? Compiler doesn't convert void** > automatically? Yeah, the compiler reports errors without this casting.