From patchwork Sat Dec 21 01:23:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ihor Solodrai X-Patchwork-Id: 13917581 Received: from mail-10628.protonmail.ch (mail-10628.protonmail.ch [79.135.106.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BDEC1BF37; Sat, 21 Dec 2024 01:23:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.28 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734744236; cv=none; b=IDi8vvwqq5LV0+3m4cDofiBu/9w41no5GD7+aV1XA1ID0JYNCPrMPSsHW8TaYoAvzNXAC/9vOKlbvd2lHK+83YVHrnRfEgs312cAyAb38GmdXlK+1tq1j2osYVQUbAQkxwiDxTwjM5n84B7ECoydnOFHgnG/vyxjzm+5NIMbVR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734744236; c=relaxed/simple; bh=KSSdXmtfzYajZw4qih4NRTECtFQ6oqSR8n4/XZVpEFw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PcQBwYxXrwxp51aN+24wYSOksrTByT9Mt22KskEu9XaCjswN1RXPM6OtCzgzdxgnKou6PiffVhM7I+1fdVR6Oa1jlrNzaf3Yz8ccZzoArWcPfeWO3UPDC591vkZjKKAVWGLtnqg8mHKbpG1OulK+SJHfPbaUOdQnNOh5S15NYgY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=XYA+SoSa; arc=none smtp.client-ip=79.135.106.28 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="XYA+SoSa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1734744232; x=1735003432; bh=Ny/6YZ1iIK837U/U3ch6VU4EDb2Pi2hXvZfadV22vLE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=XYA+SoSa8/dzMn13AMY2E+GWdQkObVjw7h8IdebvPSSEmyqH1hr+FG3+QmS9CMbEg sOo1WxNQ26vU/rPFiuI9Yv6krP9Y2Xky4wUEyYzsgfPc9SAGV6OG81uOmN+yIQeQmU bjo+1Alx6bInfS08vtIA4gxxSHr/MMg0wJVtlzwRsRo21DRm2dypuOIOfm42UXH2N6 O9AI/IsLrR1xebIv3+zz3ffdB5rGKRoQbyKZJbtufr1o1ai+WQhprcNk9M6/hFme3+ gbFq27qzjCe9romGiqw24S2htqaf/yoChuae+p13Rf8M0qCvAA1QBKMgWQJrYFtQyl jKFrCXiAKtMMw== Date: Sat, 21 Dec 2024 01:23:45 +0000 To: dwarves@vger.kernel.org From: Ihor Solodrai Cc: acme@kernel.org, alan.maguire@oracle.com, eddyz87@gmail.com, andrii@kernel.org, mykolal@fb.com, bpf@vger.kernel.org Subject: [PATCH dwarves v3 8/8] btf_encoder: clean up global encoders list Message-ID: <20241221012245.243845-9-ihor.solodrai@pm.me> In-Reply-To: <20241221012245.243845-1-ihor.solodrai@pm.me> References: <20241221012245.243845-1-ihor.solodrai@pm.me> Feedback-ID: 27520582:user:proton X-Pm-Message-ID: 96da67071566f230b425a7233e7d0eebb2407ac5 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 With multithreading moved entirely to the dwarf_loader, now there is only one btf_encoder. Hence there is no need to maintain a global list of encoders anymore. Signed-off-by: Ihor Solodrai --- btf_encoder.c | 106 ++++++++------------------------------------------ btf_encoder.h | 4 -- 2 files changed, 17 insertions(+), 93 deletions(-) diff --git a/btf_encoder.c b/btf_encoder.c index 7e03ba4..88e32e4 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -218,39 +218,6 @@ static struct elf_functions *elf_functions__find(const Elf *elf, const struct li return NULL; } - -static LIST_HEAD(encoders); -static pthread_mutex_t encoders__lock = PTHREAD_MUTEX_INITIALIZER; - -/* mutex only needed for add/delete, as this can happen in multiple encoding - * threads. Traversal of the list is currently confined to thread collection. - */ - -#define btf_encoders__for_each_encoder(encoder) \ - list_for_each_entry(encoder, &encoders, node) - -static void btf_encoders__add(struct btf_encoder *encoder) -{ - pthread_mutex_lock(&encoders__lock); - list_add_tail(&encoder->node, &encoders); - pthread_mutex_unlock(&encoders__lock); -} - -static void btf_encoders__delete(struct btf_encoder *encoder) -{ - struct btf_encoder *existing = NULL; - - pthread_mutex_lock(&encoders__lock); - /* encoder may not have been added to list yet; check. */ - btf_encoders__for_each_encoder(existing) { - if (encoder == existing) - break; - } - if (encoder == existing) - list_del(&encoder->node); - pthread_mutex_unlock(&encoders__lock); -} - #define PERCPU_SECTION ".data..percpu" /* @@ -868,39 +835,6 @@ static int32_t btf_encoder__add_var_secinfo(struct btf_encoder *encoder, size_t return gobuffer__add(&encoder->secinfo[shndx].secinfo, &si, sizeof(si)); } -int32_t btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoder *other) -{ - size_t shndx; - if (encoder == other) - return 0; - - for (shndx = 1; shndx < other->seccnt; shndx++) { - struct gobuffer *var_secinfo_buf = &other->secinfo[shndx].secinfo; - size_t sz = gobuffer__size(var_secinfo_buf); - uint16_t nr_var_secinfo = sz / sizeof(struct btf_var_secinfo); - uint32_t type_id; - uint32_t next_type_id = btf__type_cnt(encoder->btf); - int32_t i, id; - struct btf_var_secinfo *vsi; - - if (strcmp(encoder->secinfo[shndx].name, other->secinfo[shndx].name)) { - fprintf(stderr, "mismatched ELF sections at index %zu: \"%s\", \"%s\"\n", - shndx, encoder->secinfo[shndx].name, other->secinfo[shndx].name); - return -1; - } - - for (i = 0; i < nr_var_secinfo; i++) { - vsi = (struct btf_var_secinfo *)var_secinfo_buf->entries + i; - type_id = next_type_id + vsi->type - 1; /* Type ID starts from 1 */ - id = btf_encoder__add_var_secinfo(encoder, shndx, type_id, vsi->offset, vsi->size); - if (id < 0) - return id; - } - } - - return btf__add_btf(encoder->btf, other->btf); -} - static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, size_t shndx) { struct gobuffer *var_secinfo_buf = &encoder->secinfo[shndx].secinfo; @@ -1326,27 +1260,29 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder) } } -static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_encoding_inconsistent_proto) { struct btf_encoder_func_state **saved_fns, *s; - struct btf_encoder *e = NULL; - int i = 0, j, nr_saved_fns = 0; + int err = 0, i = 0, j, nr_saved_fns = 0; - /* Retrieve function states from each encoder, combine them + /* Retrieve function states from the encoder, combine them * and sort by name, addr. */ - btf_encoders__for_each_encoder(e) { - list_for_each_entry(s, &e->func_states, node) - nr_saved_fns++; + list_for_each_entry(s, &encoder->func_states, node) { + nr_saved_fns++; } if (nr_saved_fns == 0) - return 0; + goto out; saved_fns = calloc(nr_saved_fns, sizeof(*saved_fns)); - btf_encoders__for_each_encoder(e) { - list_for_each_entry(s, &e->func_states, node) - saved_fns[i++] = s; + if (!saved_fns) { + err = -ENOMEM; + goto out; + } + + list_for_each_entry(s, &encoder->func_states, node) { + saved_fns[i++] = s; } qsort(saved_fns, nr_saved_fns, sizeof(*saved_fns), saved_functions_cmp); @@ -1377,11 +1313,10 @@ static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto) /* Now that we are done with function states, free them. */ free(saved_fns); - btf_encoders__for_each_encoder(e) { - btf_encoder__delete_saved_funcs(e); - } + btf_encoder__delete_saved_funcs(encoder); - return 0; +out: + return err; } static void elf_functions__collect_function(struct elf_functions *functions, GElf_Sym *sym) @@ -2134,7 +2069,7 @@ int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf) int err; size_t shndx; - btf_encoder__add_saved_funcs(conf->skip_encoding_btf_inconsistent_proto); + btf_encoder__add_saved_funcs(encoder, conf->skip_encoding_btf_inconsistent_proto); for (shndx = 1; shndx < encoder->seccnt; shndx++) if (gobuffer__size(&encoder->secinfo[shndx].secinfo)) @@ -2541,7 +2476,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam if (encoder->verbose) printf("File %s:\n", cu->filename); - btf_encoders__add(encoder); } return encoder; @@ -2558,7 +2492,6 @@ void btf_encoder__delete(struct btf_encoder *encoder) if (encoder == NULL) return; - btf_encoders__delete(encoder); for (shndx = 0; shndx < encoder->seccnt; shndx++) __gobuffer__delete(&encoder->secinfo[shndx].secinfo); free(encoder->secinfo); @@ -2727,8 +2660,3 @@ out: encoder->cu = NULL; return err; } - -struct btf *btf_encoder__btf(struct btf_encoder *encoder) -{ - return encoder->btf; -} diff --git a/btf_encoder.h b/btf_encoder.h index 0081a99..0f345e2 100644 --- a/btf_encoder.h +++ b/btf_encoder.h @@ -27,10 +27,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam void btf_encoder__delete(struct btf_encoder *encoder); int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf); - int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct conf_load *conf_load); -struct btf *btf_encoder__btf(struct btf_encoder *encoder); - -int btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoder *other); #endif /* _BTF_ENCODER_H_ */