From patchwork Wed Jul 24 22:52:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13741436 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 D942AC3DA63 for ; Wed, 24 Jul 2024 22:52:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D8D76B00A5; Wed, 24 Jul 2024 18:52:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 687C86B00A6; Wed, 24 Jul 2024 18:52:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 551216B00A7; Wed, 24 Jul 2024 18:52:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 365E06B00A5 for ; Wed, 24 Jul 2024 18:52:46 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E05E5C0C7A for ; Wed, 24 Jul 2024 22:52:45 +0000 (UTC) X-FDA: 82376147490.26.59FFBCB Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id 9C76F18000F for ; Wed, 24 Jul 2024 22:52:43 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BGDkWmUr; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721861510; 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=qDhJw1Y+KBgN25rma0Rt8X7KKfmJMg5s8TsdR1QZQCLcWjLPrI84I3XV86Mg7VXhu0lR6H Wh2/j+NP0dqPiDBSAYvd70MDvYCSqtbM4nVAFEqaovudbm94OHZWBbp32VBUoQ+duM15qi coHfDk3hpW0Ye32ZdPyCieS1nnuFEYE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721861510; a=rsa-sha256; cv=none; b=VGeEyPjYd1egTGlaVXB13n6vkKHxPwnbHZDEHzIXK5v0G3VIioPw7opdcOSaIfUEPgoghJ P8ezDYsdSh0KlSrgNkQ4VsytHvMoP8FahBZHWP8OGgfgrabxjsBK/XBUfb8tggOn9f6kZ8 +pMZGiOcH2KjiuXjsPqzFUJzMpXhCnU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BGDkWmUr; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id C9784CE12CE; Wed, 24 Jul 2024 22:52:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9582C4AF0B; Wed, 24 Jul 2024 22:52:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721861560; bh=kcQeVuVwnIJJkBHc/ynMBCpPQQtY5kY6QBTEhCYYFCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BGDkWmUrnZuRcU338NjBL7jISkv1hnIqu7EmRZtJYWqjfuNTDFVhavENhOlvKQQSC htiPnRUx56FVGLy6y9FmD0Mg9cXTodSpvRBXVhd9VbCcimJEMqZgr8XsaMut99P4EW 4O4k1eFKlMHBPhHkhHGx0nzwefPziyBydQetHHkbo5UjUelJlYxKHkwAK76DqlRH/A d7jK6YSLVcsQ37iktrEAyy9ZPcna3kEuhe9qqjN7Pw+EQtJF1vH5ZEE7sUzEKu0q8C l7GXLqDK7VXlh/YLNPFlW9fsLME0ruLo2ZVLvLoQy8qtAiJNmK4Sp92LI88/xdPMFc USYEDfusZiTQQ== 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, song@kernel.org, Andrii Nakryiko Subject: [PATCH v2 bpf-next 08/10] bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry Date: Wed, 24 Jul 2024 15:52:08 -0700 Message-ID: <20240724225210.545423-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240724225210.545423-1-andrii@kernel.org> References: <20240724225210.545423-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9C76F18000F X-Stat-Signature: optixr1war4z37rxkrpcwomrqxtucwhn X-Rspam-User: X-HE-Tag: 1721861563-247457 X-HE-Meta: U2FsdGVkX18KgkMuEZ9IIZudwdqrIPZ4ksxXmc86hgD7OMprAeB09DXMc6oEVSTDGyfqigVrf89x0dUc1oRJ2dVi2Y9g5jcDf9AGDnsv71ZwFSCI8XKzdycHxs56Yc9tceOOSW/tXTPC4+5kYXnGyHg4Gbal9ZxaqNVwD9pD+hXBgyoitykzQJAWOglCbhZVEvh5BozRS8+kvNLpPGeqvNeO/1hsVeCpOqTT7S4i9wmgqJMP6jrQw4PrRa3Gkmg9q9wgFl1Xn/axZf6mtp6vr87uc26aoSIyhehQbYpLsCAT3JgfwJYvVVobiRAh0E6hUBtC1KczO4936gcqH1qvVlY6QDpSU8hrWJP2crQq2JAJDxqXYMmPB09Qc+93wHDlIFD8n1yjOSOZtx33Z5wfgKixio8MFORbNosXOrDjZGWM1T4WKuwMZd+pubvqpite9NLaW0Y+pe58ZeSjTWMf5/vFggxgwIwSeSxqwZerVoCnW/WV03k2YQoQLxg5I1zsZa0xVDCBX8KAWaHpAn+/Ss7kzAQ1jy2HiY9wXAnP4wUYBJV6tFdhRoV2vCBjvycSW3AUPl9Mxi7VX6yvj3BH/hAkNOuTkobocsD8pWWuqpQKs9IrZy6H6qgxSGcSn6mt8TSt9RF3fHQmOVIZsode6YME3WvdX0wnhXsezrcVFtGUgUSAV7HaopQ6DRoI776NVLQCYhcXiipd+FNVNortvOYUSDiI8EdAq4APUbkxWGhpEseUH1EGxvOw/yymAWcEAIGzCJXa98E2fOmRAJXSueL10SeB+jyJ9siGKDszN0KByFPjCJsRDEGj1ScQ75L9rhqHivWpMV0FND9dBTyrtTaS/mcM98H/9V8knrKKp5fOY7b5TmWrHH1rnudebyrOwmroKQhJorkjqYkDPW3QfPRI+E4kiOUnEwE64pKu5FkJf9gO8OGp+TK6OUaJQrdPCgsxZ4YwTLGXwW2kxgD YE+B7E7L ZX6EB8W67MFJqIbWlZ73duFQTrr+CD+D0BKeblrjiiVsrpqIVZCxH97z7e+pzjanxPsPPd+MHlapMyYV89fwGOk69ZJNtG7r5r6sUXlF56pIlgSgnE45dX97/QKCzE3q4iYZTtuVrFENglTfuN7SNYBnsERtXVHzIznSJkLg56fipXHHuUnd6VcterPSUEGtIpF4ZrGppsWA+2yodw6Huo7lfrhYkrg6Y6wuZoH4q9ajZaCCvLTjK2ALKA+aHX8N+nVQ6ZuUE/mu8xoJucPk6fQ3e5Qfk2I88jawY 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);