From patchwork Mon May 14 11:51:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandipan Das X-Patchwork-Id: 912898 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40kzq64c9Bz9s08 for ; Mon, 14 May 2018 22:02:58 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40kzq63SH7zF3HF for ; Mon, 14 May 2018 22:02:58 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=linux.vnet.ibm.com (client-ip=148.163.158.5; helo=mx0a-001b2d01.pphosted.com; envelope-from=sandipan@linux.vnet.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40kzZ24hSJzF3X1 for ; Mon, 14 May 2018 21:51:38 +1000 (AEST) Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4EBnOd5012773 for ; Mon, 14 May 2018 07:51:35 -0400 Received: from e06smtp11.uk.ibm.com (e06smtp11.uk.ibm.com [195.75.94.107]) by mx0b-001b2d01.pphosted.com with ESMTP id 2hy8mguekq-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 14 May 2018 07:51:35 -0400 Received: from localhost by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 14 May 2018 12:51:33 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp11.uk.ibm.com (192.168.101.141) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 14 May 2018 12:51:31 +0100 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4EBpUOC2163138; Mon, 14 May 2018 11:51:30 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6BC72A404D; Mon, 14 May 2018 12:43:03 +0100 (BST) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6D15FA4053; Mon, 14 May 2018 12:43:02 +0100 (BST) Received: from localhost.localdomain (unknown [9.40.193.84]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Mon, 14 May 2018 12:43:02 +0100 (BST) From: Sandipan Das To: ast@fb.com, daniel@iogearbox.net Subject: [RFC][PATCH bpf v3 5/5] tools: bpftool: resolve call addresses without using imm field Date: Mon, 14 May 2018 17:21:18 +0530 X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180514115118.8149-1-sandipan@linux.vnet.ibm.com> References: <20180514115118.8149-1-sandipan@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18051411-0040-0000-0000-0000045942B4 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18051411-0041-0000-0000-000020FD64AB Message-Id: <20180514115118.8149-5-sandipan@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-05-14_02:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805140122 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: netdev@vger.kernel.org, naveen.n.rao@linux.vnet.ibm.com, linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" Currently, we resolve the callee's address for a JITed function call by using the imm field of the call instruction as an offset from __bpf_call_base. If bpf_jit_kallsyms is enabled, we further use this address to get the callee's kernel symbol's name. For some architectures, such as powerpc64, the imm field is not large enough to hold this offset. So, instead of assigning this offset to the imm field, the verifier now assigns the subprog id. Also, a list of kernel symbol addresses for all the JITed functions is provided in the program info. We now use the imm field as an index for this list to lookup a callee's symbol's address and resolve its name. Suggested-by: Daniel Borkmann Signed-off-by: Sandipan Das --- tools/bpf/bpftool/prog.c | 33 +++++++++++++++++++++++++++++++++ tools/bpf/bpftool/xlated_dumper.c | 24 +++++++++++++++++------- tools/bpf/bpftool/xlated_dumper.h | 2 ++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 9bdfdf2d3fbe..0ba947c7deec 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -428,6 +428,8 @@ static int do_dump(int argc, char **argv) bool opcodes = false; bool visual = false; unsigned char *buf; + __u64 *addrs = NULL; + __u32 nr_addrs = 0; __u32 *member_len; __u64 *member_ptr; ssize_t n; @@ -496,11 +498,27 @@ static int do_dump(int argc, char **argv) return -1; } + if (info.nr_jited_funcs) { + nr_addrs = info.nr_jited_funcs; + addrs = (__u64 *) malloc(nr_addrs * sizeof(__u64)); + if (!addrs) { + p_err("mem alloc failed"); + free(buf); + close(fd); + return -1; + } + } + memset(&info, 0, sizeof(info)); *member_ptr = ptr_to_u64(buf); *member_len = buf_size; + if (nr_addrs) { + info.jited_funcs = ptr_to_u64(addrs); + info.nr_jited_funcs = nr_addrs; + } + err = bpf_obj_get_info_by_fd(fd, &info, &len); close(fd); if (err) { @@ -513,6 +531,11 @@ static int do_dump(int argc, char **argv) goto err_free; } + if (info.nr_jited_funcs > nr_addrs) { + p_err("too many addresses returned"); + goto err_free; + } + if ((member_len == &info.jited_prog_len && info.jited_prog_insns == 0) || (member_len == &info.xlated_prog_len && @@ -558,6 +581,12 @@ static int do_dump(int argc, char **argv) dump_xlated_cfg(buf, *member_len); } else { kernel_syms_load(&dd); + + if (info.nr_jited_funcs) { + dd.jited_funcs = (u64 *) info.jited_funcs; + dd.nr_jited_funcs = info.nr_jited_funcs; + } + if (json_output) dump_xlated_json(&dd, buf, *member_len, opcodes); else @@ -566,10 +595,14 @@ static int do_dump(int argc, char **argv) } free(buf); + if (nr_addrs) + free(addrs); return 0; err_free: free(buf); + if (nr_addrs) + free(addrs); return -1; } diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c index 7a3173b76c16..70943612b667 100644 --- a/tools/bpf/bpftool/xlated_dumper.c +++ b/tools/bpf/bpftool/xlated_dumper.c @@ -178,8 +178,12 @@ static const char *print_call_pcrel(struct dump_data *dd, snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), "%+d#%s", insn->off, sym->name); else - snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), - "%+d#0x%lx", insn->off, address); + if (address) + snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), + "%+d#0x%lx", insn->off, address); + else + snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), + "%+d", insn->off); return dd->scratch_buff; } @@ -200,14 +204,20 @@ static const char *print_call(void *private_data, const struct bpf_insn *insn) { struct dump_data *dd = private_data; - unsigned long address = dd->address_call_base + insn->imm; - struct kernel_sym *sym; + unsigned long address = 0; + struct kernel_sym *sym = NULL; - sym = kernel_syms_search(dd, address); - if (insn->src_reg == BPF_PSEUDO_CALL) + if (insn->src_reg == BPF_PSEUDO_CALL) { + if (dd->nr_jited_funcs) { + address = dd->jited_funcs[insn->imm]; + sym = kernel_syms_search(dd, address); + } return print_call_pcrel(dd, sym, address, insn); - else + } else { + address = dd->address_call_base + insn->imm; + sym = kernel_syms_search(dd, address); return print_call_helper(dd, sym, address); + } } static const char *print_imm(void *private_data, diff --git a/tools/bpf/bpftool/xlated_dumper.h b/tools/bpf/bpftool/xlated_dumper.h index b34affa7ef2d..6afc03a5fbad 100644 --- a/tools/bpf/bpftool/xlated_dumper.h +++ b/tools/bpf/bpftool/xlated_dumper.h @@ -47,6 +47,8 @@ struct kernel_sym { struct dump_data { unsigned long address_call_base; + unsigned long *jited_funcs; + unsigned int nr_jited_funcs; struct kernel_sym *sym_mapping; __u32 sym_count; char scratch_buff[SYM_MAX_NAME + 8];