From patchwork Fri Mar 15 02:18:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13593001 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C46FA6FD1 for ; Fri, 15 Mar 2024 02:18:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469124; cv=none; b=Rp3NtgE3+NkEnRdo9olOHWBxE7m/156IzwlD/NfXXeCcTKs4ikxAEmc15DOdzEcAB9yjXN9WGrrAsQOjgZTuRGoL6xgywvEJa2+Qw9Rj+COCwlyJ3lif+LazVs+oCcLbzG3JFOqQj2yGKOPOvIqNTkdYLhWC6+TjGvGi3APUvr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469124; c=relaxed/simple; bh=d88y2F04DSKnoADFHmboH2wiThUSyoR+o38AjRueuto=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=krAlFzPmNz1Wzqi0GGUUYCg7u9co/7sYx6U0SYmWnTzzT8Q1iQjX4tEtqg9tHZONkp8lbjqI3QmoSsneyRurssBOEJM/ODTUxCeTMsEU2aTfPDBVqbaN4IQRsOqqC7fmeyeGTiJfU0+lpI8vnUtXyh+Ht8WNc69owCp8AW7Zl4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Lfc9TPxH; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Lfc9TPxH" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1dddaa02d22so9918015ad.2 for ; Thu, 14 Mar 2024 19:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710469121; x=1711073921; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ryZnmvBx3CZ9a33TjKjCtH+JryFtuyjLqRiWRXEiNMc=; b=Lfc9TPxHotfoeSWhTwhBC2DZl+szwZmzN432GPqWFJ0mKj/3hc8o11EU66rYXfVrif WYgrgWItBa/mLqU294lWOgzCM96HAg3T8Rx1itKdXNXAEEMTKNfA1umCjEsG3qUPCBIO imPsZFwU0JOlItP+mC8guI6i1fmsGqUYRvMNjMT24T4Qd7NjWryLBCmZX+oacoRmpgO7 GALyfDjcX60Z+DLjMao/35NuUwdMQYOyZWYnNXXpCMJe5fgtnPfvf+9c5Nru6Z/Y9TWW MCvgjkybb49AWQpdXeGCNRKwpAR+BcOwHNdirbgtygEjWE15SmNZ5kmGsuDZIzoWY+r4 /1FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710469121; x=1711073921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ryZnmvBx3CZ9a33TjKjCtH+JryFtuyjLqRiWRXEiNMc=; b=HO2M1y6zaxX18PDcYb2/+eOHR00zRGwlYnejGDXksRQKTVa9i+oTqcJpMZ8GEBPAHQ 27ofizoaavB/rNGtH9doh/TEbZl4lXFCkNFbFB/JYI+ql5URXgNr86UdlxtGVt479lfc EEXNFKkXOH+mgBEaxUbx+lKxSjZcGVM5noBt3Jj0v1+eeD1alKdft/Olnr4iK/7BCjGY tbpbThj4/8y1BXM7zoY2/c6VY1oLGZzIysCk9zGLGKEHujEOu5u+g/QFkXVaDyxA+1Pk Cs5CgwUQPCDx75EMVUIBdOmwsj+3pEfwpOYeH2sqovrk+W3IJsBIIGunIJ2L6KMqpwf5 I5Zw== X-Gm-Message-State: AOJu0YxOjFNEZkfFNYcrLJc0wYsDy9j+DN2QlAidgFo48AiTrB/IPn0t O/oC5hHHlv9dihxPjuleXFGh8j3g3KpI+dDePEhQc2ZezvfYxwlzuJCx3nhm X-Google-Smtp-Source: AGHT+IE9P9isnWik3MI63pTSR49o4wHdea/Q7GE65TBti6gV4omXFppc+XfK1iGqG5Cf0WVX/fWpeQ== X-Received: by 2002:a17:902:f691:b0:1dc:3261:ab7 with SMTP id l17-20020a170902f69100b001dc32610ab7mr2150914plg.49.1710469121273; Thu, 14 Mar 2024 19:18:41 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:400::5:12e]) by smtp.gmail.com with ESMTPSA id t10-20020a170902dcca00b001dd621111e2sm2470843pll.194.2024.03.14.19.18.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 14 Mar 2024 19:18:40 -0700 (PDT) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, eddyz87@gmail.com, kernel-team@fb.com Subject: [PATCH bpf 1/4] bpf: Clarify bpf_arena comments. Date: Thu, 14 Mar 2024 19:18:31 -0700 Message-Id: <20240315021834.62988-2-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240315021834.62988-1-alexei.starovoitov@gmail.com> References: <20240315021834.62988-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Clarify two bpf_arena comments, use existing SZ_4G #define, improve page_cnt check. Signed-off-by: Alexei Starovoitov --- kernel/bpf/arena.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 86571e760dd6..343c3456c8dd 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -38,7 +38,7 @@ /* number of bytes addressable by LDX/STX insn with 16-bit 'off' field */ #define GUARD_SZ (1ull << sizeof(((struct bpf_insn *)0)->off) * 8) -#define KERN_VM_SZ ((1ull << 32) + GUARD_SZ) +#define KERN_VM_SZ (SZ_4G + GUARD_SZ) struct bpf_arena { struct bpf_map map; @@ -110,7 +110,7 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr) return ERR_PTR(-EINVAL); vm_range = (u64)attr->max_entries * PAGE_SIZE; - if (vm_range > (1ull << 32)) + if (vm_range > SZ_4G) return ERR_PTR(-E2BIG); if ((attr->map_extra >> 32) != ((attr->map_extra + vm_range - 1) >> 32)) @@ -301,7 +301,7 @@ static unsigned long arena_get_unmapped_area(struct file *filp, unsigned long ad if (pgoff) return -EINVAL; - if (len > (1ull << 32)) + if (len > SZ_4G) return -E2BIG; /* if user_vm_start was specified at arena creation time */ @@ -322,7 +322,7 @@ static unsigned long arena_get_unmapped_area(struct file *filp, unsigned long ad if (WARN_ON_ONCE(arena->user_vm_start)) /* checks at map creation time should prevent this */ return -EFAULT; - return round_up(ret, 1ull << 32); + return round_up(ret, SZ_4G); } static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma) @@ -346,7 +346,7 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma) return -EBUSY; /* Earlier checks should prevent this */ - if (WARN_ON_ONCE(vma->vm_end - vma->vm_start > (1ull << 32) || vma->vm_pgoff)) + if (WARN_ON_ONCE(vma->vm_end - vma->vm_start > SZ_4G || vma->vm_pgoff)) return -EFAULT; if (remember_vma(arena, vma)) @@ -420,7 +420,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt if (uaddr & ~PAGE_MASK) return 0; pgoff = compute_pgoff(arena, uaddr); - if (pgoff + page_cnt > page_cnt_max) + if (pgoff > page_cnt_max - page_cnt) /* requested address will be outside of user VMA */ return 0; } @@ -447,7 +447,13 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt goto out; uaddr32 = (u32)(arena->user_vm_start + pgoff * PAGE_SIZE); - /* Earlier checks make sure that uaddr32 + page_cnt * PAGE_SIZE will not overflow 32-bit */ + /* Earlier checks made sure that uaddr32 + page_cnt * PAGE_SIZE - 1 + * will not overflow 32-bit. Lower 32-bit need to represent + * contiguous user address range. + * Map these pages at kern_vm_start base. + * kern_vm_start + uaddr32 + page_cnt * PAGE_SIZE - 1 can overflow + * lower 32-bit and it's ok. + */ ret = vm_area_map_pages(arena->kern_vm, kern_vm_start + uaddr32, kern_vm_start + uaddr32 + page_cnt * PAGE_SIZE, pages); if (ret) { @@ -510,6 +516,11 @@ static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt) if (!page) continue; if (page_cnt == 1 && page_mapped(page)) /* mapped by some user process */ + /* Optimization for the common case of page_cnt==1: + * If page wasn't mapped into some user vma there + * is no need to call zap_pages which is slow. When + * page_cnt is big it's faster to do the batched zap. + */ zap_pages(arena, full_uaddr, 1); vm_area_unmap_pages(arena->kern_vm, kaddr, kaddr + PAGE_SIZE); __free_page(page); From patchwork Fri Mar 15 02:18:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13593002 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13FEE7499 for ; Fri, 15 Mar 2024 02:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469127; cv=none; b=PmGrOfq382EMpRKlS2V0symvrGYeSnNYafMNT8BTWN8ZbXQyibbzSIGxwYMEwb1Q2u8zNh6TdSKPkhb1tl52/3Arj9b/7jAfyo7h03LQ/ZL2mw6Xe69V89+MoigsXPs1FbgMbtICQnAY3aahbOUUVe7Zsl5UU2LY27JzLnzE50A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469127; c=relaxed/simple; bh=Q9ogDrVBReMWJF93xhEiBRaSk+I0x49qVjP+LojR4bY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T6OXAnJJAL4mDmByCPhXVo5AsUL/zqF83yZg9Jynl8eSDGzW7g6GaUCiOHT5V1x6OlcjCG7AEXUdboUfhHFhVb7bYm5wpydi62dgJYV1Ii8jbORJTq/+nVfBey63ot0gqLwtz12hJGwiInPiFC1sKQcmIxjb1JGbXdrJD0dmETQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Zi5Y2QOJ; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Zi5Y2QOJ" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6e6eb891db9so1164599b3a.3 for ; Thu, 14 Mar 2024 19:18:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710469124; x=1711073924; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=17f+nEHPgSpLlniyWNzznar1mNYCf92rMKqwvSNOLng=; b=Zi5Y2QOJ5z4DGE2lEkT/PPqEpOLXoRSw/xujDEfaV0q1gKFggU5A0OyESi4vnmHjyh Hl6Tt/6jlSL33GyuofNnXWbM8IgyO4mx2eocNcwXb6EUV8vl3kuMvRVtKUWtYfui5u+w qqKBpQ/aTairDSjB+47vBEEP0CjXKUCEV1C1Dv0PcQu+zl0pWFZWpCqUYMjK2PshKjxc IymlAGnAj4jNOw/3wyG5Hyo2a3iYwltAaq6yoeE9xFauSos9C1V9oJUv1AO4Z2SJ2dub 3Fgk1rXC7IwtW2FPrM27H/e3F+6aUv8EnXe46jj/xuUTn3Soc4tGsJgVXKBkYcmJgeCr /img== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710469124; x=1711073924; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=17f+nEHPgSpLlniyWNzznar1mNYCf92rMKqwvSNOLng=; b=C5Gvhlq9I7cq1pspZl7+mTMPDHzuHuxj6S2Dxai54qaNu3KAK6fqXVoFTbVyQ0vqx0 CeUqXTNZML9lezh7UUr83NTEH0X0M29yoNCGxnE35MR4x9VytJu+bOPAwbUbdbv7Qi6S ET517WI2DZYZCLShTGBZgQqBYtSJ+6+nlGGoIRzx2+DWAQpTnBEuSI2myGtfFRFz3ZFf teV0+mpLFNXTAfFM0JgUJXhvQvc3fyn/YV5zYwk4sVhDAlvBqNMl46U66jtIva0NqnNQ wCXhG1N1yNWYOQoljcy/VrX/lTyiab1a8+psj8lZUEXcNZ50kCDHi674qHbQAfJdNjbO WGxQ== X-Gm-Message-State: AOJu0YxCHSdnSHAn5mASMRw5DNr945whVRhZB8RvyjM4w+GB64KLITY7 1mAKeOH80WoKdNqYAbAnNhuLUPElmwaS2hBjNvG9VLK7mDiBQius4hbqMzfK X-Google-Smtp-Source: AGHT+IGJ9nXaLzS/lLGz1jxPzJqQkolv5kHyDrH/luDRxTS0vnsYAZ23OGYyWxGcrxSSH90oW80QCQ== X-Received: by 2002:a05:6a00:23c6:b0:6e5:e7c5:9120 with SMTP id g6-20020a056a0023c600b006e5e7c59120mr3948190pfc.19.1710469124559; Thu, 14 Mar 2024 19:18:44 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:400::5:12e]) by smtp.gmail.com with ESMTPSA id gx20-20020a056a001e1400b006e6bcbccda9sm2232547pfb.59.2024.03.14.19.18.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 14 Mar 2024 19:18:44 -0700 (PDT) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, eddyz87@gmail.com, kernel-team@fb.com Subject: [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM Date: Thu, 14 Mar 2024 19:18:32 -0700 Message-Id: <20240315021834.62988-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240315021834.62988-1-alexei.starovoitov@gmail.com> References: <20240315021834.62988-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov The selftests use to tell LLVM about special pointers. For LLVM there is nothing "arena" about them. They are simply pointers in a different address space. Hence LLVM diff https://github.com/llvm/llvm-project/pull/85161 renamed: . macro __BPF_FEATURE_ARENA_CAST -> __BPF_FEATURE_ADDR_SPACE_CAST . global variables in __attribute__((address_space(N))) are now placed in section named ".address_space.N" instead of ".arena.N". Adjust libbpf, bpftool, and selftests to match LLVM. Signed-off-by: Alexei Starovoitov --- tools/bpf/bpftool/gen.c | 2 +- tools/lib/bpf/libbpf.c | 2 +- tools/testing/selftests/bpf/bpf_arena_common.h | 2 +- tools/testing/selftests/bpf/progs/arena_htab.c | 2 +- tools/testing/selftests/bpf/progs/arena_list.c | 10 +++++----- tools/testing/selftests/bpf/progs/verifier_arena.c | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 4fa4ade1ce74..540c0f2c4fda 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -121,7 +121,7 @@ static bool get_datasec_ident(const char *sec_name, char *buf, size_t buf_sz) int i, n; /* recognize hard coded LLVM section name */ - if (strcmp(sec_name, ".arena.1") == 0) { + if (strcmp(sec_name, ".addr_space.1") == 0) { /* this is the name to use in skeleton */ snprintf(buf, buf_sz, "arena"); return true; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index efab29b8935b..36e26f4f5997 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -498,7 +498,7 @@ struct bpf_struct_ops { #define KSYMS_SEC ".ksyms" #define STRUCT_OPS_SEC ".struct_ops" #define STRUCT_OPS_LINK_SEC ".struct_ops.link" -#define ARENA_SEC ".arena.1" +#define ARENA_SEC ".addr_space.1" enum libbpf_map_type { LIBBPF_MAP_UNSPEC, diff --git a/tools/testing/selftests/bpf/bpf_arena_common.h b/tools/testing/selftests/bpf/bpf_arena_common.h index bcf195c64a45..567491f3e1b5 100644 --- a/tools/testing/selftests/bpf/bpf_arena_common.h +++ b/tools/testing/selftests/bpf/bpf_arena_common.h @@ -32,7 +32,7 @@ */ #endif -#if defined(__BPF_FEATURE_ARENA_CAST) && !defined(BPF_ARENA_FORCE_ASM) +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM) #define __arena __attribute__((address_space(1))) #define cast_kern(ptr) /* nop for bpf prog. emitted by LLVM */ #define cast_user(ptr) /* nop for bpf prog. emitted by LLVM */ diff --git a/tools/testing/selftests/bpf/progs/arena_htab.c b/tools/testing/selftests/bpf/progs/arena_htab.c index b7bb712cacfd..1e6ac187a6a0 100644 --- a/tools/testing/selftests/bpf/progs/arena_htab.c +++ b/tools/testing/selftests/bpf/progs/arena_htab.c @@ -22,7 +22,7 @@ int zero = 0; SEC("syscall") int arena_htab_llvm(void *ctx) { -#if defined(__BPF_FEATURE_ARENA_CAST) || defined(BPF_ARENA_FORCE_ASM) +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) || defined(BPF_ARENA_FORCE_ASM) struct htab __arena *htab; __u64 i; diff --git a/tools/testing/selftests/bpf/progs/arena_list.c b/tools/testing/selftests/bpf/progs/arena_list.c index cd35b8448435..c0422c58cee2 100644 --- a/tools/testing/selftests/bpf/progs/arena_list.c +++ b/tools/testing/selftests/bpf/progs/arena_list.c @@ -30,13 +30,13 @@ int list_sum; int cnt; bool skip = false; -#ifdef __BPF_FEATURE_ARENA_CAST +#ifdef __BPF_FEATURE_ADDR_SPACE_CAST long __arena arena_sum; int __arena test_val = 1; struct arena_list_head __arena global_head; #else -long arena_sum SEC(".arena.1"); -int test_val SEC(".arena.1"); +long arena_sum SEC(".addr_space.1"); +int test_val SEC(".addr_space.1"); #endif int zero; @@ -44,7 +44,7 @@ int zero; SEC("syscall") int arena_list_add(void *ctx) { -#ifdef __BPF_FEATURE_ARENA_CAST +#ifdef __BPF_FEATURE_ADDR_SPACE_CAST __u64 i; list_head = &global_head; @@ -66,7 +66,7 @@ int arena_list_add(void *ctx) SEC("syscall") int arena_list_del(void *ctx) { -#ifdef __BPF_FEATURE_ARENA_CAST +#ifdef __BPF_FEATURE_ADDR_SPACE_CAST struct elem __arena *n; int sum = 0; diff --git a/tools/testing/selftests/bpf/progs/verifier_arena.c b/tools/testing/selftests/bpf/progs/verifier_arena.c index 5540b05ff9ee..969bc091060b 100644 --- a/tools/testing/selftests/bpf/progs/verifier_arena.c +++ b/tools/testing/selftests/bpf/progs/verifier_arena.c @@ -19,7 +19,7 @@ SEC("syscall") __success __retval(0) int basic_alloc1(void *ctx) { -#if defined(__BPF_FEATURE_ARENA_CAST) +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) volatile int __arena *page1, *page2, *no_page, *page3; page1 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); @@ -58,7 +58,7 @@ SEC("syscall") __success __retval(0) int basic_alloc2(void *ctx) { -#if defined(__BPF_FEATURE_ARENA_CAST) +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) volatile char __arena *page1, *page2, *page3, *page4; page1 = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); From patchwork Fri Mar 15 02:18:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13593003 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40D3A612E for ; Fri, 15 Mar 2024 02:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469130; cv=none; b=RM1COx9K/wF+ZJIxjYbzr3CV/FPdXPfsVf1PuG7DNdogA49OqLlm0xXWA0mxAFqaWGNMUXAxjAjMP6uEzEDAKJRYQKyU5iGPD73+LTIoRJdNxhut5buAzfFNgjnTiR5CELzYP1S8i+5CnF8f96HZ0AURgeMD4KVHoGyGHphjXbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469130; c=relaxed/simple; bh=Ma3gWJpDDdEY2PHAX9uxB0hodqqLz6SSTDTiqh0msx0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XCRY3Nnossb7djPNo2GZ/qThSofVnm+jQu4F0K468XyjyADiY7MYViCWve6eIkaR8t9VFeG3i5Hbxl9ckpoT2PK9u9IxvFsAbz82Isssp4aj003f8MGJyQjNq8FckCal5mCOHqxRYMxLVVq9qdacC5f9whUY8fOLWKEWJ7WYtao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AxQnZ4FY; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AxQnZ4FY" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1dd955753edso14126465ad.1 for ; Thu, 14 Mar 2024 19:18:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710469128; x=1711073928; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/Zx7uP+08rqU4RWjEF5NIqdDD1npSQ+tlO45Imu6axw=; b=AxQnZ4FYrv6QDxy0FwUy/DXiFR8elykny/BgLNc2+qjL87MyeTX0jVcLgYevnvEV2w v4TC0bQ/EhaXdJEOY0SVpYOUzu7E5ZcQm7isG9+XpiYpOXSKs1cfiugdtASlKP56QD2T tcvMqsKwccXhbvlv+SUo3okuY5kFhygpg0cMvwc2jvLVFbyIduXJ08vl22sUCZwfdZrm au0B+w1ZkTJFQThVYqBytFhGkgp0vQo159w0/KhPS5oAz+nDrXgG0C9IqE5sDzfK+bMt AeeKLoKLFanZT5/526yJAlstFescuK+QcHb6WHNBWXNmXNqvjsij3eocFjTvBV8um8dH EnxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710469128; x=1711073928; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Zx7uP+08rqU4RWjEF5NIqdDD1npSQ+tlO45Imu6axw=; b=pfV1eXBwNjvfHgF4dJdj17o4082dW+iWmy2umiseFMCEA9jmI0P+gT8XIe5DmqCGwl 1SXq5rerlMqyvvvN0bFzLLPapq3wM4cUXzMFsVEIxTWD+VlCQkqdLJe3xR99z2BexUut 71eyWyL83uOYkIyKczqFCaIMJU6UbBffXAsCyhpOJV7za03037PLZSxFWfSO/fXtVRSn okjm/n+SgCxLvxxyYnWg/6l+gMPCTcJC1bTiIrLmDLKZnVzWOMdOZVZXVx8jD/3uFZTQ pt2cnVRGu9CQMyxSxzY4cA0ZZ7p6bcUcpwX4EsPT7u77/nQiG+6eGd9M4ErcyEwVKJNI opHg== X-Gm-Message-State: AOJu0Yx6qo8+SoBBfMby1fXCKOdI7I0XSLCCClJCeMfnmPXHaUBhW8Hi O2PpV3X/oTBmJ9IhxX+hMi5XJ3liwiW0T+cOoZKGwmHcyJxlxSSYVsDl059x X-Google-Smtp-Source: AGHT+IGI+zFrOc/Rqmpv9XDOzSo1uZFmdk/W/vFxrG4YH/f6UafrgD2mx8n6tPB5wv1YL/7MErn6gg== X-Received: by 2002:a17:90b:2348:b0:29c:718b:7b8f with SMTP id ms8-20020a17090b234800b0029c718b7b8fmr1702476pjb.28.1710469127773; Thu, 14 Mar 2024 19:18:47 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:400::5:12e]) by smtp.gmail.com with ESMTPSA id u10-20020a17090282ca00b001ddddc8c41fsm2478072plz.157.2024.03.14.19.18.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 14 Mar 2024 19:18:47 -0700 (PDT) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, eddyz87@gmail.com, kernel-team@fb.com Subject: [PATCH bpf 3/4] selftests/bpf: Remove hard coded PAGE_SIZE macro. Date: Thu, 14 Mar 2024 19:18:33 -0700 Message-Id: <20240315021834.62988-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240315021834.62988-1-alexei.starovoitov@gmail.com> References: <20240315021834.62988-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Remove hard coded PAGE_SIZE. Add #include instead (that works on x86-64 and s390) and fallback to slow getpagesize() for aarch64. Signed-off-by: Alexei Starovoitov --- tools/testing/selftests/bpf/prog_tests/arena_htab.c | 8 +++++--- tools/testing/selftests/bpf/prog_tests/arena_list.c | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/arena_htab.c b/tools/testing/selftests/bpf/prog_tests/arena_htab.c index 0766702de846..d69fd2465f53 100644 --- a/tools/testing/selftests/bpf/prog_tests/arena_htab.c +++ b/tools/testing/selftests/bpf/prog_tests/arena_htab.c @@ -3,12 +3,14 @@ #include #include #include - +#include +#ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */ +#include +#define PAGE_SIZE getpagesize() +#endif #include "arena_htab_asm.skel.h" #include "arena_htab.skel.h" -#define PAGE_SIZE 4096 - #include "bpf_arena_htab.h" static void test_arena_htab_common(struct htab *htab) diff --git a/tools/testing/selftests/bpf/prog_tests/arena_list.c b/tools/testing/selftests/bpf/prog_tests/arena_list.c index e61886debab1..d15867cddde0 100644 --- a/tools/testing/selftests/bpf/prog_tests/arena_list.c +++ b/tools/testing/selftests/bpf/prog_tests/arena_list.c @@ -3,8 +3,11 @@ #include #include #include - -#define PAGE_SIZE 4096 +#include +#ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */ +#include +#define PAGE_SIZE getpagesize() +#endif #include "bpf_arena_list.h" #include "arena_list.skel.h" From patchwork Fri Mar 15 02:18:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13593004 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07E097475 for ; Fri, 15 Mar 2024 02:18:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469135; cv=none; b=KQYVmDQBzRLiWqJaSlOJWGj6BHvHBRKKWEKH0ZaMZSdSdJ3+7Aq61IlfeIS2tY1UUR+7NIpmEFzPnz74oqlSBP092GAiG6ldW8Q/hKmLQNX0NrWhjk6G8Z/0HboTGTngG8hjv2ajGheSd3GQ9KBaYAQ7mdpq/fX1lXZ9W/oA/JI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710469135; c=relaxed/simple; bh=fRGxwgZJgIun9WG2Q0eGtrpe2RH7X+Ujdo3jnRHJpIY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k0BOl8IIqaVXSj3Hm3WSMMh9EeQO+HQiYShSVgTiqNAbjvM9stA0Tj9Cd7tUVj4JmmDh830arVzxZRI7iKa65xX6dcCeUp10URm2YPEPkQ3M7grDeoF5wV/RrFCu5/VlgfDc50HS725JKVZlD0kNWGrLzmN/3XzkUz3Bgxf6fwk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QRl36ZQu; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QRl36ZQu" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6e6ca3fc613so1495853b3a.3 for ; Thu, 14 Mar 2024 19:18:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710469131; x=1711073931; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aw9HNpkPu3M15nlqCUXDEG9a1OTpQP39gAEymrsi8BA=; b=QRl36ZQuyQ7NNRLSZ+hvTeD+MFk/elkKcaWmaXVIqCByxNzyDoheBEwcHbOWobNut6 APwGGT/tED/HOTVLmmE+X3OHOl7bz/gn/BbtoXRw1O/WKYd+A7RwoVqXcaZ3n0rlzf85 UYRT8aqTlSWN5akpTUTXsY0igHqeUFSzTJVCUKc8Lfs/TlqOwKHAWvp2Nycel57wLT3q /JPXIDjUiPtNMCPn7LHIeJH1Z32FI3PVnuYihOHmFThKRg4LWWLCgXwy3R1C2LhHiaF0 Nol4S44cCpucBoa/fCcMISeprx3FxY3gEzRib7dsKHvc1KwMdeorBPXUW4dfJb1+UY3v RAVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710469131; x=1711073931; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aw9HNpkPu3M15nlqCUXDEG9a1OTpQP39gAEymrsi8BA=; b=bB6ZHO3PJCO61LEcOQUE5Bp8jLZDzW87nqnPMLGcdttmCLC7ucu76zZ19ZFxp264GG QhD+BOI8HkOezVTL0wkDWKyzVcaHFUB/hPnAT4A2g1Jq6WGMQCZhrSv+vf79qp/KHIGz sGHY2oY4DWe9nlzdIpxIgkSIZfXsD6fpTFrvsdSbdI9sy2VoOOJCrTGMRFCjwbtS9pr6 iJR+/z7J49PFE7ZccylKBmbjsANtOovXO8/eLEp/4z+q/nr3o+B9yGOCEiHXiKZAwJZP zDCuKJcnzJ4hpgDLkYNY+LVuObBDwiE+XmmX0a01Xm8L2aS6t4waVQHbOBRtzPnKA4Rt 2H3A== X-Gm-Message-State: AOJu0Yx7Qyk0MEhQ8rAsEusEHFlgbIHCt3HPBjaHQLXd2thMlE3CBJIt 3721WpYKVzSPMuRCeQGswzoDRsiWPnRe9Yf4VfobvUHs+tt3DmTjSNfOgqn2 X-Google-Smtp-Source: AGHT+IHVv7qMAuZXk6yisA4UZYvIHHSKJGRUPaOWUd9/fJij/X++y8wP21hB1Q8juuFBninv0yPwtA== X-Received: by 2002:a05:6a21:1a4:b0:1a3:4859:1ac5 with SMTP id le36-20020a056a2101a400b001a348591ac5mr2126755pzb.3.1710469131018; Thu, 14 Mar 2024 19:18:51 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:400::5:12e]) by smtp.gmail.com with ESMTPSA id qn8-20020a17090b3d4800b0029a4efc0231sm1695675pjb.56.2024.03.14.19.18.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 14 Mar 2024 19:18:50 -0700 (PDT) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, eddyz87@gmail.com, kernel-team@fb.com Subject: [PATCH bpf 4/4] selftests/bpf: Add arena test case for 4Gbyte corner case Date: Thu, 14 Mar 2024 19:18:34 -0700 Message-Id: <20240315021834.62988-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240315021834.62988-1-alexei.starovoitov@gmail.com> References: <20240315021834.62988-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Check that 4Gbyte arena can be allocated and overflow/underflow access in the first and the last page behaves as expected. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_arena_large.c | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_large.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index 985273832f89..c4f9f306646e 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -5,6 +5,7 @@ #include "cap_helpers.h" #include "verifier_and.skel.h" #include "verifier_arena.skel.h" +#include "verifier_arena_large.skel.h" #include "verifier_array_access.skel.h" #include "verifier_basic_stack.skel.h" #include "verifier_bitfield_write.skel.h" @@ -120,6 +121,7 @@ static void run_tests_aux(const char *skel_name, void test_verifier_and(void) { RUN(verifier_and); } void test_verifier_arena(void) { RUN(verifier_arena); } +void test_verifier_arena_large(void) { RUN(verifier_arena_large); } void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); } void test_verifier_bitfield_write(void) { RUN(verifier_bitfield_write); } void test_verifier_bounds(void) { RUN(verifier_bounds); } diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_large.c b/tools/testing/selftests/bpf/progs/verifier_arena_large.c new file mode 100644 index 000000000000..ef66ea460264 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_arena_large.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" +#include "bpf_arena_common.h" + +#define ARENA_SIZE (1ull << 32) + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, ARENA_SIZE / PAGE_SIZE); +} arena SEC(".maps"); + +SEC("syscall") +__success __retval(0) +int big_alloc1(void *ctx) +{ +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) + volatile char __arena *page1, *page2, *no_page, *page3; + void __arena *base; + + page1 = base = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + if (!page1) + return 1; + *page1 = 1; + page2 = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE - PAGE_SIZE, + 1, NUMA_NO_NODE, 0); + if (!page2) + return 2; + *page2 = 2; + no_page = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE, + 1, NUMA_NO_NODE, 0); + if (no_page) + return 3; + if (*page1 != 1) + return 4; + if (*page2 != 2) + return 5; + bpf_arena_free_pages(&arena, (void __arena *)page1, 1); + if (*page2 != 2) + return 6; + if (*page1 != 0) /* use-after-free should return 0 */ + return 7; + page3 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + if (!page3) + return 8; + *page3 = 3; + if (page1 != page3) + return 9; + if (*page2 != 2) + return 10; + if (*(page1 + PAGE_SIZE) != 0) + return 11; + if (*(page1 - PAGE_SIZE) != 0) + return 12; + if (*(page2 + PAGE_SIZE) != 0) + return 13; + if (*(page2 - PAGE_SIZE) != 0) + return 14; +#endif + return 0; +} +char _license[] SEC("license") = "GPL";