Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751503AbbGNEDA (ORCPT ); Tue, 14 Jul 2015 00:03:00 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:36730 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750729AbbGNEC7 (ORCPT ); Tue, 14 Jul 2015 00:02:59 -0400 Message-ID: <55A4891D.9070600@huawei.com> Date: Tue, 14 Jul 2015 11:59:25 +0800 From: "Wangnan (F)" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: Arnaldo Carvalho de Melo CC: , , , , , Subject: Re: [PATCH 06/39] bpf tools: Create eBPF maps defined in an object file References: <1436445342-1402-1-git-send-email-wangnan0@huawei.com> <1436445342-1402-7-git-send-email-wangnan0@huawei.com> <20150713195453.GF2885@kernel.org> In-Reply-To: <20150713195453.GF2885@kernel.org> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.111.66.109] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4033 Lines: 156 On 2015/7/14 3:54, Arnaldo Carvalho de Melo wrote: > Em Thu, Jul 09, 2015 at 12:35:09PM +0000, Wang Nan escreveu: > > > >> +static int >> +bpf_object__create_maps(struct bpf_object *obj) >> +{ >> + unsigned int i; >> + size_t nr_maps; >> + int *pfd; >> + >> + nr_maps = obj->maps_buf_sz / sizeof(struct bpf_map_def); >> + if (!obj->maps_buf || !nr_maps) { >> + pr_debug("don't need create maps for %s\n", >> + obj->path); >> + return 0; >> + } >> + >> + obj->map_fds = malloc(sizeof(int) * nr_maps); >> + if (!obj->map_fds) { >> + pr_warning("realloc perf_bpf_map_fds failed\n"); >> + return -ENOMEM; >> + } >> + obj->nr_map_fds = nr_maps; >> + >> + /* fill all fd with -1 */ >> + memset(obj->map_fds, 0xff, sizeof(int) * nr_maps); > Huh? Each entry is an int... If you want to set it all to -1, why don't > you do it as: > > memset(obj->map_fds, -1, sizeof(int) * nr_maps); > > That way we can even remove that comment :) Doing it, please let me know > if this has some magic twist that makes it be right as-is, if so, please > provide a better comment. Yes, it should be better. Thank you, > - Arnaldo > >> + >> + pfd = obj->map_fds; >> + for (i = 0; i < nr_maps; i++) { >> + struct bpf_map_def def; >> + >> + def = *(struct bpf_map_def *)(obj->maps_buf + >> + i * sizeof(struct bpf_map_def)); >> + >> + *pfd = bpf_create_map(def.type, >> + def.key_size, >> + def.value_size, >> + def.max_entries); >> + if (*pfd < 0) { >> + size_t j; >> + int err = *pfd; >> + >> + pr_warning("failed to create map: %s\n", >> + strerror(errno)); >> + for (j = 0; j < i; j++) >> + zclose(obj->map_fds[j]); >> + obj->nr_map_fds = 0; >> + zfree(&obj->map_fds); >> + return err; >> + } >> + pr_debug("create map: fd=%d\n", *pfd); >> + pfd++; >> + } >> + >> + zfree(&obj->maps_buf); >> + obj->maps_buf_sz = 0; >> + return 0; >> +} >> + >> static int bpf_object__collect_reloc(struct bpf_object *obj) >> { >> int i, err; >> @@ -671,6 +736,42 @@ struct bpf_object *bpf_object__open_buffer(void *obj_buf, >> return __bpf_object__open("[buffer]", obj_buf, obj_buf_sz); >> } >> >> +int bpf_object__unload(struct bpf_object *obj) >> +{ >> + size_t i; >> + >> + if (!obj) >> + return -EINVAL; >> + >> + for (i = 0; i < obj->nr_map_fds; i++) >> + zclose(obj->map_fds[i]); >> + zfree(&obj->map_fds); >> + obj->nr_map_fds = 0; >> + >> + return 0; >> +} >> + >> +int bpf_object__load(struct bpf_object *obj) >> +{ >> + if (!obj) >> + return -EINVAL; >> + >> + if (obj->loaded) { >> + pr_warning("object should not be loaded twice\n"); >> + return -EINVAL; >> + } >> + >> + obj->loaded = true; >> + if (bpf_object__create_maps(obj)) >> + goto out; >> + >> + return 0; >> +out: >> + bpf_object__unload(obj); >> + pr_warning("failed to load object '%s'\n", obj->path); >> + return -EINVAL; >> +} >> + >> void bpf_object__close(struct bpf_object *obj) >> { >> size_t i; >> @@ -679,6 +780,7 @@ void bpf_object__close(struct bpf_object *obj) >> return; >> >> bpf_object__elf_finish(obj); >> + bpf_object__unload(obj); >> >> zfree(&obj->maps_buf); >> >> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h >> index 6e75acd..3e69600 100644 >> --- a/tools/lib/bpf/libbpf.h >> +++ b/tools/lib/bpf/libbpf.h >> @@ -30,6 +30,10 @@ struct bpf_object *bpf_object__open_buffer(void *obj_buf, >> size_t obj_buf_sz); >> void bpf_object__close(struct bpf_object *object); >> >> +/* Load/unload object into/from kernel */ >> +int bpf_object__load(struct bpf_object *obj); >> +int bpf_object__unload(struct bpf_object *obj); >> + >> /* >> * We don't need __attribute__((packed)) now since it is >> * unnecessary for 'bpf_map_def' because they are all aligned. >> -- >> 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/