From mboxrd@z Thu Jan 1 00:00:00 1970 From: Song Liu Subject: Re: [PATCH bpf-next 2/3] bpf: emit RECORD_MMAP events for bpf prog load/unload Date: Wed, 19 Sep 2018 23:44:19 +0000 Message-ID: <3FB6EC86-700C-4C80-A467-55B6753B07FE@fb.com> References: <20180919223935.999270-1-ast@kernel.org> <20180919223935.999270-3-ast@kernel.org> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Cc: "David S . Miller" , "daniel@iogearbox.net" , "peterz@infradead.org" , "acme@kernel.org" , "netdev@vger.kernel.org" , Kernel Team To: Alexei Starovoitov Return-path: Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:37260 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725834AbeITF05 (ORCPT ); Thu, 20 Sep 2018 01:26:57 -0400 In-Reply-To: <20180919223935.999270-3-ast@kernel.org> Content-Language: en-US Content-ID: <7A2616537430C84895903D72E15F5BFF@namprd15.prod.outlook.com> Sender: netdev-owner@vger.kernel.org List-ID: > On Sep 19, 2018, at 3:39 PM, Alexei Starovoitov wrote: >=20 > use perf_event_mmap_bpf_prog() helper to notify user space > about JITed bpf programs. > Use RECORD_MMAP perf event to tell user space where JITed bpf program was= loaded. > Use empty program name as unload indication. >=20 > Signed-off-by: Alexei Starovoitov > --- > kernel/bpf/core.c | 22 ++++++++++++++++++++-- > 1 file changed, 20 insertions(+), 2 deletions(-) >=20 > diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c > index 3f5bf1af0826..ddf11fdafd36 100644 > --- a/kernel/bpf/core.c > +++ b/kernel/bpf/core.c > @@ -384,7 +384,7 @@ bpf_get_prog_addr_region(const struct bpf_prog *prog, > *symbol_end =3D addr + hdr->pages * PAGE_SIZE; > } >=20 > -static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym) > +static char *bpf_get_prog_name(const struct bpf_prog *prog, char *sym) > { > const char *end =3D sym + KSYM_NAME_LEN; >=20 > @@ -402,9 +402,10 @@ static void bpf_get_prog_name(const struct bpf_prog = *prog, char *sym) > sym +=3D snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); > sym =3D bin2hex(sym, prog->tag, sizeof(prog->tag)); > if (prog->aux->name[0]) > - snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); > + sym +=3D snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); > else > *sym =3D 0; > + return sym; > } >=20 > static __always_inline unsigned long > @@ -480,23 +481,40 @@ static bool bpf_prog_kallsyms_verify_off(const stru= ct bpf_prog *fp) >=20 > void bpf_prog_kallsyms_add(struct bpf_prog *fp) > { > + unsigned long symbol_start, symbol_end; > + char buf[KSYM_NAME_LEN], *sym; > + > if (!bpf_prog_kallsyms_candidate(fp) || > !capable(CAP_SYS_ADMIN)) > return; >=20 > + bpf_get_prog_addr_region(fp, &symbol_start, &symbol_end); > + sym =3D bpf_get_prog_name(fp, buf); > + sym++; /* sym - buf is the length of the name including trailing 0 */ > + while (!IS_ALIGNED(sym - buf, sizeof(u64))) > + *sym++ =3D 0; nit: This logic feels a little weird to me. How about we wrap the extra log= ic in a separate function: size_t bpf_get_prog_name_u64_aligned(const struct bpf_prog fp, char *buf) where the return value is the u64 aligned size.=20 Other than this=20 Acked-by: Song Liu > spin_lock_bh(&bpf_lock); > bpf_prog_ksym_node_add(fp->aux); > spin_unlock_bh(&bpf_lock); > + perf_event_mmap_bpf_prog(symbol_start, symbol_end - symbol_start, > + buf, sym - buf); > } >=20 > void bpf_prog_kallsyms_del(struct bpf_prog *fp) > { > + unsigned long symbol_start, symbol_end; > + /* mmap_record.filename cannot be NULL and has to be u64 aligned */ > + char buf[sizeof(u64)] =3D {}; > + > if (!bpf_prog_kallsyms_candidate(fp)) > return; >=20 > spin_lock_bh(&bpf_lock); > bpf_prog_ksym_node_del(fp->aux); > spin_unlock_bh(&bpf_lock); > + bpf_get_prog_addr_region(fp, &symbol_start, &symbol_end); > + perf_event_mmap_bpf_prog(symbol_start, symbol_end - symbol_start, > + buf, sizeof(buf)); > } >=20 > static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr) > --=20 > 2.17.1 >=20