From patchwork Tue Jul 9 20:42:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13728513 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1165BC2BD09 for ; Tue, 9 Jul 2024 20:43:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 942496B00A5; Tue, 9 Jul 2024 16:43:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8CAFB6B00A6; Tue, 9 Jul 2024 16:43:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 76C816B00A7; Tue, 9 Jul 2024 16:43:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5691F6B00A5 for ; Tue, 9 Jul 2024 16:43:24 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 102261A1A49 for ; Tue, 9 Jul 2024 20:43:24 +0000 (UTC) X-FDA: 82321389528.29.7088C4A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id BB5AA4001D for ; Tue, 9 Jul 2024 20:43:21 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OWv8PMY6; spf=pass (imf01.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720557787; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gsmzGpKK8QXJw069hvnjOrZZkhs0zSJy4R5UDJ6ztho=; b=Ke7//cbEghLr4YvVRmxlN1A0n/Ur5NUyE2LL1dTMGlXM1tJv+W/wDEFhJVbI1Bsh0CwAN4 hMhZga5iFgC5R2Z1NFrGbkiDWmie7NVtGISPJDglL7W1EhXLs1UTnpeZ607zTPSxZI/Bz3 ZWWNPPAkZUzoBo6aSNBqrzZigXQfGRQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OWv8PMY6; spf=pass (imf01.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720557787; a=rsa-sha256; cv=none; b=ebFn2t6eDfC09haYjiQd4KPFa60C7IxHdsejS/umZ5xGAf1DaAr9gBprtNLt2m52n6cyPq fXRqfQtJ+F/5r1y3UJAlZQbagLSJAzwnWb603X7LQuKTjWjhOk/qpg9UL+2+SrJS3zFgc+ Sk029SDzNvxa90C1I7S0kCxkx7ZqNMg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 14B4DCE130A; Tue, 9 Jul 2024 20:43:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21B81C4AF0B; Tue, 9 Jul 2024 20:43:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720557798; bh=kcQeVuVwnIJJkBHc/ynMBCpPQQtY5kY6QBTEhCYYFCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OWv8PMY6JEthzTzEwmP72e2DJAJOPaEwZAp7O4/fr0gT4l5X7qaTvQu3XTg4YIpdQ lMR7sM7uLJABy0s5ECMyu6ejI7msl8sdp+Eeu1fSIOFIaIWCoCNisri6EHyDysYv9O o920CW+s/KmvV054Pih5VtsyfsH90njm7nTcAep0DTi5xVKu2+oIj/fMKU/JdXjsKx VS1gKIxrPZuZ+aC0X2h8WoWopAhqzcNH02v1154WIwnjH9g2yK2NWxSiO8Vg1TAb90 8xip0AgsUPfI2frI7jwdw9sq+nllMMmi1OMXrgJiELAit4vboPOj6tnDn7RlekKP6T pqcgIxFZsiVzw== From: Andrii Nakryiko To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, adobriyan@gmail.com, shakeel.butt@linux.dev, hannes@cmpxchg.org, ak@linux.intel.com, osandov@osandov.com, Andrii Nakryiko Subject: [PATCH bpf-next 08/10] bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry Date: Tue, 9 Jul 2024 13:42:43 -0700 Message-ID: <20240709204245.3847811-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240709204245.3847811-1-andrii@kernel.org> References: <20240709204245.3847811-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: BB5AA4001D X-Stat-Signature: y1aa64t5esm7kuc7qhagwdd5q4mp648o X-HE-Tag: 1720557801-468947 X-HE-Meta: U2FsdGVkX19tNYT3wupSLsfuRDegw7HSmEDCNh//3i7c972i0wxVvFYZy7ZV2fE9ogISfKtdPhTzrGug8p8VOL9jXdf6fD5f7t32uWqbPf7QwqsOmSC3cYkvTnORWwwuTNXd1Vs385cU3TmtmpNMPvKyVuk063vtL4GD8QbxKVPytRC9HL4VLjm4nfa0NtQtp+CZTsbH7rnYZbmnqYyP0PYK5nL1ocAEX+3rns0cSfU0ocERZNl+zQyyZMo2O/LJm7IF5kdGlIqMTTCHSK6bqUehTyw+8+ufAYQewW4T2nADZHTD67fIMkcRfcM/7IMtfK1tQXMNnHLs0eI7MEG3Z8P+wRujx3RgCKjOOnIzXKxhXoBhK2RptiUi9Qm9isYz0C7uHRqtQcviP747cw/4LuNf4+uioZFYr00Mnp27U5XPkj8LQjyRPakNzkrxwQ53ipkOr1lbn57Z2BV9ayEtARBN29m+Hl0tJ55qmw5aTADEyD2ItN3s3AMlqqlhUJLThMLZamo3yqKoPoFfzlKVGvU3wA/4/j5P4NJA9sm4ZHoK7dBGFRyj2ei10LSVc6obXBi/CMRY5jjIXBGWbAxQby+uFkwV2Avb4KZX7FpD9SxeGJMddDDf2D9oLp+HhMKCqmcNE7oG3BTQAzr93YQ5l8AQcQQPymOYUyq4LUan695Hd6VrJO465i9IgZSSJq2CO6DON1uvYAIsOmz5yttwRgvbgFlsTkmLnvQ53rm2pnKUmSE4ZamUhYNM5DYWMalrzolNWfV1gUV5k9AvA2Jq2WBXfXAkM8aIA2WeylhUxb/unk357U4NdX6PXzeGJMZqRi6Vfz5qVBCNoX6FHEbhM/v1NNF6MXK/n2ODXAMimCt2qczWKJECBn4zN6yRKuypFaTlNuBHxhiuDF8ePQcl8ALDO9KJH9ZghEJl96hgJr4o/+Vdv+KC7DmD2HKOkGMb2tGfMoGozltXyxYQM8O oc5K8+LM ohDnN9J+1E2bCLmit9CYjMl0iQVkOr23WP8ZiAVpEdyLFfLPJP+GQQ5YjMGa1lBAQQE6zCDpaoQopL0NOR40Ah809kD+yacpqZ/sHF4ECpI9dDCk13mmzs1/tYsBVN6f4VnoWPp6CcnD0QB/FnjHFSLH65Xfh0e6wL/kivkRp8a8xHEV1Z2gI+9hy8Ef7GSHpPEkyh6Vh8Wn+ej5MvUiFbilbBq0+zlW5P8TiWg4H3VVFr1A4LySa82vM30MEd4tuq+iQWvpc9VMcmLEPAog++6vBZg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Change stack_map_get_build_id_offset() which is used to convert stack trace IP addresses into build ID+offset pairs. Right now this function accepts an array of u64s as an input, and uses array of struct bpf_stack_build_id as an output. This is problematic because u64 array is coming from perf_callchain_entry, which is (non-sleepable) RCU protected, so once we allows sleepable build ID fetching, this all breaks down. But its actually pretty easy to make stack_map_get_build_id_offset() works with array of struct bpf_stack_build_id as both input and output. Which is what this patch is doing, eliminating the dependency on perf_callchain_entry. We require caller to fill out bpf_stack_build_id.ip fields (all other can be left uninitialized), and update in place as we do build ID resolution. We make sure to READ_ONCE() and cache locally current IP value as we used it in a few places to find matching VMA and so on. Given this data is directly accessible and modifiable by user's BPF code, we should make sure to have a consistent view of it. Signed-off-by: Andrii Nakryiko --- kernel/bpf/stackmap.c | 49 +++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 770ae8e88016..6457222b0b46 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -124,8 +124,18 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr) return ERR_PTR(err); } +/* + * Expects all id_offs[i].ip values to be set to correct initial IPs. + * They will be subsequently: + * - either adjusted in place to a file offset, if build ID fetching + * succeeds; in this case id_offs[i].build_id is set to correct build ID, + * and id_offs[i].status is set to BPF_STACK_BUILD_ID_VALID; + * - or IP will be kept intact, if build ID fetching failed; in this case + * id_offs[i].build_id is zeroed out and id_offs[i].status is set to + * BPF_STACK_BUILD_ID_IP. + */ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, - u64 *ips, u32 trace_nr, bool user) + u32 trace_nr, bool user) { int i; struct mmap_unlock_irq_work *work = NULL; @@ -142,30 +152,28 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, /* cannot access current->mm, fall back to ips */ for (i = 0; i < trace_nr; i++) { id_offs[i].status = BPF_STACK_BUILD_ID_IP; - id_offs[i].ip = ips[i]; memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); } return; } for (i = 0; i < trace_nr; i++) { - if (range_in_vma(prev_vma, ips[i], ips[i])) { + u64 ip = READ_ONCE(id_offs[i].ip); + + if (range_in_vma(prev_vma, ip, ip)) { vma = prev_vma; - memcpy(id_offs[i].build_id, prev_build_id, - BUILD_ID_SIZE_MAX); + memcpy(id_offs[i].build_id, prev_build_id, BUILD_ID_SIZE_MAX); goto build_id_valid; } - vma = find_vma(current->mm, ips[i]); + vma = find_vma(current->mm, ip); if (!vma || build_id_parse_nofault(vma, id_offs[i].build_id, NULL)) { /* per entry fall back to ips */ id_offs[i].status = BPF_STACK_BUILD_ID_IP; - id_offs[i].ip = ips[i]; memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); continue; } build_id_valid: - id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i] - - vma->vm_start; + id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ip - vma->vm_start; id_offs[i].status = BPF_STACK_BUILD_ID_VALID; prev_vma = vma; prev_build_id = id_offs[i].build_id; @@ -216,7 +224,7 @@ static long __bpf_get_stackid(struct bpf_map *map, struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map); struct stack_map_bucket *bucket, *new_bucket, *old_bucket; u32 skip = flags & BPF_F_SKIP_FIELD_MASK; - u32 hash, id, trace_nr, trace_len; + u32 hash, id, trace_nr, trace_len, i; bool user = flags & BPF_F_USER_STACK; u64 *ips; bool hash_matches; @@ -238,15 +246,18 @@ static long __bpf_get_stackid(struct bpf_map *map, return id; if (stack_map_use_build_id(map)) { + struct bpf_stack_build_id *id_offs; + /* for build_id+offset, pop a bucket before slow cmp */ new_bucket = (struct stack_map_bucket *) pcpu_freelist_pop(&smap->freelist); if (unlikely(!new_bucket)) return -ENOMEM; new_bucket->nr = trace_nr; - stack_map_get_build_id_offset( - (struct bpf_stack_build_id *)new_bucket->data, - ips, trace_nr, user); + id_offs = (struct bpf_stack_build_id *)new_bucket->data; + for (i = 0; i < trace_nr; i++) + id_offs[i].ip = ips[i]; + stack_map_get_build_id_offset(id_offs, trace_nr, user); trace_len = trace_nr * sizeof(struct bpf_stack_build_id); if (hash_matches && bucket->nr == trace_nr && memcmp(bucket->data, new_bucket->data, trace_len) == 0) { @@ -445,10 +456,16 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, copy_len = trace_nr * elem_size; ips = trace->ip + skip; - if (user && user_build_id) - stack_map_get_build_id_offset(buf, ips, trace_nr, user); - else + if (user && user_build_id) { + struct bpf_stack_build_id *id_offs = buf; + u32 i; + + for (i = 0; i < trace_nr; i++) + id_offs[i].ip = ips[i]; + stack_map_get_build_id_offset(buf, trace_nr, user); + } else { memcpy(buf, ips, copy_len); + } if (size > copy_len) memset(buf + copy_len, 0, size - copy_len);