From patchwork Thu May 23 17:41:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672078 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.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 8C45F12E75 for ; Thu, 23 May 2024 17:42:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486133; cv=none; b=Ak+ooFTXG1TfLsIvwZD/VbyLLnojTaheq75a7obGwPBOWxpIXfKeE1spjIGZ1y41ybtS32WgjI3rTLD8RbQbiFOEn2s/BhoKZ0UQECzGYM0z7iQveaauSkUdQWIKZrw4me3GbirSozwa20BtoFTHBAle4ln/I1hZXqMTY7J/XGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486133; c=relaxed/simple; bh=lSwhg2euaILyL0bcxrmxhLOF+u20nsB/9HP8mjIYgt4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=biK/8moNN4ChoCBPDRsAffhdlOVhIcaZoPMT+hpNnVg5yTG6acSwq6KO5ToyhZdlr+XVPA3zrejF0VRkneUj84coXuMWlfwaGefXY4g7F+iOxr6sWlUOc9fAesXkDRTTjikYrKzGq8Uu99YNBSFXis+BAydn/H2HwUcNk59OEmA= 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=dPcQSust; arc=none smtp.client-ip=209.85.128.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="dPcQSust" Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-627dfbcf42aso23439737b3.1 for ; Thu, 23 May 2024 10:42:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486130; x=1717090930; 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=7JuzWBhGt4+85t6yr8ntSpXISasRyZbrXWnkL7PGYWU=; b=dPcQSustaZM4B5srULOnS3isLq8qkDw1g6Y6zgqupmPfAQwtQeIfHQ7c6xFULyWb/+ BZVfw21jKP8k9eX4r6a9kQ47SfuUo98pAMOIkfrsua8wwNkfKfc7t3QTMZOYv9Pp74D7 k3PhQBJbJ7UOlq58OOjUHeNAddvQmKTs6iqJpy94/Y3C576ZDYJ9WXMjazW+P9qbILej QMBgCEJycSMctCeGg/21iD0vUbJhMBiU643nf+i300zSFCZ14qYgdkubxOm8KsewnEIZ bB9qDJzuQbbKhu2TaHeb6l56xO0T5Wuf+pEP4kCAiwywLUAnbT0B+fK0EAD6Wi1y8BVm rz4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486130; x=1717090930; 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=7JuzWBhGt4+85t6yr8ntSpXISasRyZbrXWnkL7PGYWU=; b=pUdB99ggxB01vOmlD2MAC4nZeNgjMzTTCxaQypE4qM8MTGXkMEsEIU68zWx2dSZLDT /z3/mvZ8hiHfqZDFE/MpcGQJugbdBmH4Zjbbq/ZrG7JLLK9GTnzLUg1GLJi884yoCeHl 9XoxU8C9zw5wkvOkAKmowtbIcRA+42NHVUgRVOoaTJQuMpdShvt3V1U0oWUf4Y4XPtFL k5aUwEc5QuAm989Q9a4nnPX7Ec5ikrvfwKaOGj8JV7IIj0fu5HeSIAIpzX0MCW0DTS1B wAjZKZZ2Xvs8OzQ4bB86XWnA3v8qwbuvJyU2kBmJJXG5ZEJgoX/XG0DREOCGf1yOVbpG WVAw== X-Gm-Message-State: AOJu0Yx0vYfvlQpgJv3QDbgazxwlJnaAQe6R31i+fHUAiuMStGWIBYkf kP1+j51NtsInLGb/twHnKBEqxHMRsU+vu5t/3j0vRPnBaSt5JhH0/blsjQ== X-Google-Smtp-Source: AGHT+IE12MVqlkD01VYZT1yvdmKRMheIxt1Vb///SHEvs5LlUJd2oeRMCrTtDMtuKeyXEyCMsG3skQ== X-Received: by 2002:a81:8457:0:b0:61a:d3ac:5b51 with SMTP id 00721157ae682-627e488cfc3mr52974757b3.38.1716486128992; Thu, 23 May 2024 10:42:08 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:07 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 1/9] bpf: Remove unnecessary checks on the offset of btf_field. Date: Thu, 23 May 2024 10:41:54 -0700 Message-Id: <20240523174202.461236-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 reg_find_field_offset() always return a btf_field with a matching offset value. Checking the offset of the returned btf_field is unnecessary. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/verifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 77da1f438bec..57c0c255bf4c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11640,7 +11640,7 @@ __process_kf_arg_ptr_to_graph_node(struct bpf_verifier_env *env, node_off = reg->off + reg->var_off.value; field = reg_find_field_offset(reg, node_off, node_field_type); - if (!field || field->offset != node_off) { + if (!field) { verbose(env, "%s not found at offset=%u\n", node_type_name, node_off); return -EINVAL; } From patchwork Thu May 23 17:41:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672079 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.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 DCC977604F for ; Thu, 23 May 2024 17:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486134; cv=none; b=qlAFOEszf43530A/pOcw6j/p0pb6Uj7rhJ2ZPKPqwCZ2C5ZX2SlhpvFTo6WSKUq33JbFfv7G8PeWdD/WwA6+CRksDRFpp3LelZIepExz/jJmGoqaNDtBNOT09HgTTRNZb1zRPHJECbxHZaPBGxoHU54Z6GtgfAu1dckPnWI0lA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486134; c=relaxed/simple; bh=7gprEONVxlTPxtpiE/RSBBqr7chsjcIUU2wLyzf2a3g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rj2t4VJonEqm9iG06k9fFgnEJYgxSYUva34/ulhwijaETEnLirzUTJz9EVBg1UC3jaV+mLqXyalLibO/27+DVKflwwfSI7xWdeioARYBicO0hBMgBEYyg2438HTQ/Opc2G4kc7ADXesWT5/h6VvHWNnnspYjEJ7GYOC7lhYJ1q4= 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=abUD1GuK; arc=none smtp.client-ip=209.85.128.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="abUD1GuK" Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-627efad69b4so19023457b3.3 for ; Thu, 23 May 2024 10:42:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486131; x=1717090931; 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=KQXWK1Yh+km0U2s78YJTSe+dMvqdnLtgISVHfad/We4=; b=abUD1GuK6o4omhCIwiJxJoXNBgIvoY2e3nvCo9Rcea1cFeQdXdPh6z5sn3quZC5220 0+IoBiYdHuWFRVVFspOKyVqvJ1nPCqCDXX3eEjtU/LvqhdMiBB0+vGYkBrMEIZVD+Vtt Tk2DZqvP4b4b40tUPK6W3R+OQ+OjYqrSoIaD2qBxwFqAF8nKWyc84rpuaeeAdyWyNlyF WnCWe7+RPeegIYW7CwGoGj7VwvBt5MFu2+qYj2LDUK7pExCb6x0LP7SsLXxUZFqWDTKN J41yx2w8PGHL4h1pdxswkdSDHrpZCDK+SwlzGI79luL8P4QHiyg39BLhFXEx845WpQX4 P21A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486131; x=1717090931; 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=KQXWK1Yh+km0U2s78YJTSe+dMvqdnLtgISVHfad/We4=; b=CjWfKFe/dlPE7+MCS1QUu/LWIKWcbuxMaDnDfmGcpKUfC/Ty8SLQnUH2364A6HRZTG yGqd5fBJkju8hQmgLqWZ+FHr8lWBy01srdrbLAjRUA4EvGSpOFQLEXys79zmina8+eL9 7u8Ss2SFS1iDGRVVsAk+63hPe3hCbUz+54i4TytvxaHvcmC9gbaKRXzK+EDkC0XqoAb8 B06mWKrdOFuArjxLJlzBEynQpJR1YJFUty/CIyIXLDWyGV+XlMkliZh0snvKBcvo9hB2 CHdA6YWW/8Is4G2TC421sFpZfrPqYQgc/lvUSabDv1iMGwxej81VEOAftv2aL4XicHpo IbJg== X-Gm-Message-State: AOJu0Yx1cEV6NlCuw/4bAdxuNsxOUtUWHS7zDoQQmUhxjwtjqeayyS56 UTlFLNknYsLkV5QvKoJrVJq349wNT3RKFD5Z8MgdpG16+1rN4wwL8EIqqQ== X-Google-Smtp-Source: AGHT+IHBs3b6AO/VdPJe9ijNbLUAjPqBHAxXVxvJYFRATtrFbxfi3WeJi9smmYT+lpa6JXttkTxBQA== X-Received: by 2002:a0d:d5c7:0:b0:627:d0e8:1775 with SMTP id 00721157ae682-627e47152a1mr58886807b3.39.1716486130157; Thu, 23 May 2024 10:42:10 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:09 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 2/9] bpf: Remove unnecessary call to btf_field_type_size(). Date: Thu, 23 May 2024 10:41:55 -0700 Message-Id: <20240523174202.461236-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 field->size has been initialized by bpf_parse_fields() with the value returned by btf_field_type_size(). Use it instead of calling btf_field_type_size() again. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 2 +- kernel/bpf/verifier.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 821063660d9f..226138bd139a 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6693,7 +6693,7 @@ int btf_struct_access(struct bpf_verifier_log *log, for (i = 0; i < rec->cnt; i++) { struct btf_field *field = &rec->fields[i]; u32 offset = field->offset; - if (off < offset + btf_field_type_size(field->type) && offset < off + size) { + if (off < offset + field->size && offset < off + size) { bpf_log(log, "direct access to %s is disallowed\n", btf_field_type_name(field->type)); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 57c0c255bf4c..81a3d2ced78d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5448,7 +5448,7 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, * this program. To check that [x1, x2) overlaps with [y1, y2), * it is sufficient to check x1 < y2 && y1 < x2. */ - if (reg->smin_value + off < p + btf_field_type_size(field->type) && + if (reg->smin_value + off < p + field->size && p < reg->umax_value + off + size) { switch (field->type) { case BPF_KPTR_UNREF: From patchwork Thu May 23 17:41:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672080 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.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 D6C3476034 for ; Thu, 23 May 2024 17:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486134; cv=none; b=nml997aVr0l0yk5trSf1IPnjVVVc8zlK5nOKYqrLLvRIVk7A1Zw49yKdRQOe9s3puRnhGiw2fIwBbZ2zJ4X5DLW5Z+iqyDSOlXZCyynpgMus+MuMrERY2y0Y4wxjblQN6hA3JKehOEAKQOqCzh1AFbDd/wIDccEZVYYW+v+3t48= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486134; c=relaxed/simple; bh=upVhmpmzSJmS20JqXVMvwrSbBc8DdAjci0We5oLNjcw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fjZHBSVCkcNvEHCBOezbuaxA9dswW9y9H0KZLifPdkMriOXjAe3l2MpW0UvvE0DsB/5vQRu3P4JhBcKOKeTimw7gcEQDjYn4XUaLUhyH8+p3ymR7hEDlT2uJ5+U96p8U7Dyovh/jqTRIANoryFJMS3jIvLASSq7R5ASexLSK0Ys= 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=RHonMHX7; arc=none smtp.client-ip=209.85.128.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="RHonMHX7" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6203f553e5fso62146987b3.1 for ; Thu, 23 May 2024 10:42:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486131; x=1717090931; 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=w+O2RZhOQRKcgUwqgGuv+62tvwQlJ69c+KC5kITkKYo=; b=RHonMHX7MEZBVq7ZUAJMGRch+duty2Ttzvh+FW9/T+o2H70rhuxfceVb02fpw1fqW0 yF1UNlGfMhYywJxUVvTX4rO0CBePs20pfECCvo3Cj+q83IM0ByaIYOG0THTS9JaKTxmT kW+gVEsQjiCfOydBXZZGf/wl1nKQzaIXGbdkoo6ok4OPQKPpTGRKCUqQBOwv1WzQQj3q pPv7rgXroiyhMo/t50rEk0EO7zchYGsKxC6b/Y7wsOynekF4EVds+ke/bXGFux/PmFwI 6xpmMSHEem9lkt/BABbpJgxnWYFKLc+MbxoiyV/KPsIsIDABhqKKviUVusKPIP52cw1O SUZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486131; x=1717090931; 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=w+O2RZhOQRKcgUwqgGuv+62tvwQlJ69c+KC5kITkKYo=; b=WkukxbwIOZCpR7z19ltQr3tCPTfw+zi/1xE+yVDSNfmRPlPvBBRyWsPweJapff7vDl dzWEIhtaVL85vSjpem9dwke10gyWh5/kDqklH8oEboNOyFsG8gRdMft6i6Cky/fZP85f iQLIw5BFlkR/qqkrNiS5CW/ZpJJYIUOFgLwA3SWv4sQWQPk29ZyYefP5pWyvOpyPbJAU Sl6Q4+ySJc83vJsCNnJIQ2FBJ82JXbhcKcNRhH8rlXjcwKgH0mJnmjTe2sgSltA35w68 8qhWaxBmIbdLNmH1Wl9H3s5JNYDxUFyUMekYjI/182f2CMMQvCBVAe49wF85wyDubohb AjHA== X-Gm-Message-State: AOJu0YyDXtGq5bJepjWyyMhbsDRUlfcwbyyip0P4vgXD0EbOMHZEwX/Q cuUp1RbikKg9507aStqKkTNRfu4QvRA0gUeltDxK4ekk7vLGtp1cRj5o5g== X-Google-Smtp-Source: AGHT+IGswrBEivxJssocldEQmuzZiMYEkmF1O4SL/5xj5MD3AYOI58M3rTCgoqrB18ssiCpD3IwMAg== X-Received: by 2002:a81:ab52:0:b0:618:8f69:df36 with SMTP id 00721157ae682-627e4685a7bmr58669077b3.23.1716486131375; Thu, 23 May 2024 10:42:11 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:11 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 3/9] bpf: refactor btf_find_struct_field() and btf_find_datasec_var(). Date: Thu, 23 May 2024 10:41:56 -0700 Message-Id: <20240523174202.461236-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 Move common code of the two functions to btf_find_field_one(). Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 180 +++++++++++++++++++++-------------------------- 1 file changed, 79 insertions(+), 101 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 226138bd139a..2ce61c3a7e28 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3494,72 +3494,95 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name +static int btf_find_field_one(const struct btf *btf, + const struct btf_type *var, + const struct btf_type *var_type, + int var_idx, + u32 off, u32 expected_size, + u32 field_mask, u32 *seen_mask, + struct btf_field_info *info, int info_cnt) +{ + int ret, align, sz, field_type; + struct btf_field_info tmp; + + field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), + field_mask, seen_mask, &align, &sz); + if (field_type == 0) + return 0; + if (field_type < 0) + return field_type; + + if (expected_size && expected_size != sz) + return 0; + if (off % align) + return 0; + + switch (field_type) { + case BPF_SPIN_LOCK: + case BPF_TIMER: + case BPF_WORKQUEUE: + case BPF_LIST_NODE: + case BPF_RB_NODE: + case BPF_REFCOUNT: + ret = btf_find_struct(btf, var_type, off, sz, field_type, + info_cnt ? &info[0] : &tmp); + if (ret < 0) + return ret; + break; + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + ret = btf_find_kptr(btf, var_type, off, sz, + info_cnt ? &info[0] : &tmp); + if (ret < 0) + return ret; + break; + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + ret = btf_find_graph_root(btf, var, var_type, + var_idx, off, sz, + info_cnt ? &info[0] : &tmp, + field_type); + if (ret < 0) + return ret; + break; + default: + return -EFAULT; + } + + if (ret == BTF_FIELD_IGNORE) + return 0; + if (!info_cnt) + return -E2BIG; + + return 1; +} + static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, int info_cnt) { - int ret, idx = 0, align, sz, field_type; + int ret, idx = 0; const struct btf_member *member; - struct btf_field_info tmp; u32 i, off, seen_mask = 0; for_each_member(i, t, member) { const struct btf_type *member_type = btf_type_by_id(btf, member->type); - field_type = btf_get_field_type(__btf_name_by_offset(btf, member_type->name_off), - field_mask, &seen_mask, &align, &sz); - if (field_type == 0) - continue; - if (field_type < 0) - return field_type; - off = __btf_member_bit_offset(t, member); if (off % 8) /* valid C code cannot generate such BTF */ return -EINVAL; off /= 8; - if (off % align) - continue; - - switch (field_type) { - case BPF_SPIN_LOCK: - case BPF_TIMER: - case BPF_WORKQUEUE: - case BPF_LIST_NODE: - case BPF_RB_NODE: - case BPF_REFCOUNT: - ret = btf_find_struct(btf, member_type, off, sz, field_type, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - ret = btf_find_kptr(btf, member_type, off, sz, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - ret = btf_find_graph_root(btf, t, member_type, - i, off, sz, - idx < info_cnt ? &info[idx] : &tmp, - field_type); - if (ret < 0) - return ret; - break; - default: - return -EFAULT; - } - if (ret == BTF_FIELD_IGNORE) - continue; - if (idx >= info_cnt) - return -E2BIG; - ++idx; + ret = btf_find_field_one(btf, t, member_type, i, + off, 0, + field_mask, &seen_mask, + &info[idx], info_cnt - idx); + if (ret < 0) + return ret; + idx += ret; } return idx; } @@ -3568,66 +3591,21 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, int info_cnt) { - int ret, idx = 0, align, sz, field_type; + int ret, idx = 0; const struct btf_var_secinfo *vsi; - struct btf_field_info tmp; u32 i, off, seen_mask = 0; for_each_vsi(i, t, vsi) { const struct btf_type *var = btf_type_by_id(btf, vsi->type); const struct btf_type *var_type = btf_type_by_id(btf, var->type); - field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), - field_mask, &seen_mask, &align, &sz); - if (field_type == 0) - continue; - if (field_type < 0) - return field_type; - off = vsi->offset; - if (vsi->size != sz) - continue; - if (off % align) - continue; - - switch (field_type) { - case BPF_SPIN_LOCK: - case BPF_TIMER: - case BPF_WORKQUEUE: - case BPF_LIST_NODE: - case BPF_RB_NODE: - case BPF_REFCOUNT: - ret = btf_find_struct(btf, var_type, off, sz, field_type, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - ret = btf_find_kptr(btf, var_type, off, sz, - idx < info_cnt ? &info[idx] : &tmp); - if (ret < 0) - return ret; - break; - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - ret = btf_find_graph_root(btf, var, var_type, - -1, off, sz, - idx < info_cnt ? &info[idx] : &tmp, - field_type); - if (ret < 0) - return ret; - break; - default: - return -EFAULT; - } - - if (ret == BTF_FIELD_IGNORE) - continue; - if (idx >= info_cnt) - return -E2BIG; - ++idx; + ret = btf_find_field_one(btf, var, var_type, -1, off, vsi->size, + field_mask, &seen_mask, + &info[idx], info_cnt - idx); + if (ret < 0) + return ret; + idx += ret; } return idx; } From patchwork Thu May 23 17:41:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672081 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (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 BD9807D3E6 for ; Thu, 23 May 2024 17:42:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486135; cv=none; b=HvSMTEha/anxnilxXqk+SkkSHSbSefZ7ZtBJqkQVm7JVoAr3uwB6UrhTDnU6WkQyeY3/wfr5hfJZg+CE4icsL16m657z84mt/AcmxZsE9kZtz9TciL/DputLRWEGbDoFe3ZrwtM/YDYzgl4CRzhbmBglYAjYtemrQuEQ6WIBEyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486135; c=relaxed/simple; bh=beUO5MCH98hk7B1W+3WxHbLxcpbk5N8JhsxgQEHPOpA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IxbcNn/6zClRWzivdOQNkuB7X+NDE27aenZbJnnwXUS0WNEdiY3ujC+L/1OWfhL6D5xlp1V/CBDpra5PXgwGHYGcoM17tbKryqbnj0dIW12GhmXQYfvWL4hLuGb7B7i7s6b+sox4rma3PavnN4/vSk5p+oyc+fXsLQD4Y+jfEok= 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=EaN66oFB; arc=none smtp.client-ip=209.85.128.169 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="EaN66oFB" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-622f5a0badcso71562207b3.2 for ; Thu, 23 May 2024 10:42:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486132; x=1717090932; 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=EScPTFIUsU9lafWPj5+nn2qz08FusFYqcC6FL2ky40Q=; b=EaN66oFBIKLT3YmYjPlWtKK10aeMXe/gso3oEMRZvswHlAWbUSjAeMkDmxAUFW+S0I CH7RRajf481VV0qTLk44fJnQ7YQQLlHq6jJ2PMik8IUkzRwzgiF5CCRAvoHPc9rsrmTP nhbBJHHnoNhtIN7K1BgiH1LmFO+efNlDnjZcMCgfbAZp1g/2Pb/FYqBA9vFvNpWeod/9 4r++wSOht3obdaUohA104P9Mo2vTWd3ALzBESNg+HO+dB7O1byekCGMzTjSaDv2pgyOo ZrNOR6pgiVJ73n4McPkwjvOhyUCrgNuxirXghp8pRUbi9eMf0hrzo5o+rs5Ob5HynmFj /gug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486132; x=1717090932; 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=EScPTFIUsU9lafWPj5+nn2qz08FusFYqcC6FL2ky40Q=; b=gu3eLBNUqkt9nuEEpBR7pjNk4KIXax6pgayW6WJ+1TYZTO8Wp1KEvq5rc7fNxLEACK 3hweVju1YbSx+wzrrF4FbvA3w9gL4q1VLKsamHZE7xUBwVMwnxX6qbZZxVR3CjJCxgfO BLajj3JgtodZXiZbXf+K8PQ8DIIEflKkJPj1TR1opUg/VVEK8rsJCQzqC3RiJC22Pokc T/fb7mRbmIrL0V8x29dr36lsgpPbPGXEZJJXrpeGKbXwew8X2lvGzanYaokGemOPllFE p9ib11wB+00IsZUhQYlLF9exPPV1XeAoDnR9tBuP0L6K12XiHW7fX1NHhuY77oACz0lv cFEA== X-Gm-Message-State: AOJu0YysokYPyTLE5r24ltJSc3GJZE6x/2Hito+HRlYkC5RTLzK15HOr d+GDee1gcIKypepmZA3CNPUQKs0yk8i9NVouFDGdlc9Hcmfg9NB1Tfs6rQ== X-Google-Smtp-Source: AGHT+IETJm205xsc1AU3XN//m7LSVClYTGRWlP1fJl+p85GPknn3LzBWNCeaG0A9sGjjwNNyQmJJ/g== X-Received: by 2002:a05:690c:60c7:b0:627:a382:a0fa with SMTP id 00721157ae682-627e4880150mr66236787b3.52.1716486132572; Thu, 23 May 2024 10:42:12 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:12 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 4/9] bpf: create repeated fields for arrays. Date: Thu, 23 May 2024 10:41:57 -0700 Message-Id: <20240523174202.461236-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 The verifier uses field information for certain special types, such as kptr, rbtree root, and list head. These types are treated differently. However, we did not previously support these types in arrays. This update examines arrays and duplicates field information the same number of times as the length of the array if the element type is one of the special types. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 62 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 2ce61c3a7e28..4fefa27d5aea 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3494,6 +3494,41 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name +/* Repeat a field for a specified number of times. + * + * Copy and repeat the first field for repeat_cnt + * times. The field is repeated by adding the offset of each field + * with + * (i + 1) * elem_size + * where i is the repeat index and elem_size is the size of an element. + */ +static int btf_repeat_field(struct btf_field_info *info, + u32 repeat_cnt, u32 elem_size) +{ + u32 i; + u32 cur; + + /* Ensure not repeating fields that should not be repeated. */ + switch (info[0].type) { + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + break; + default: + return -EINVAL; + } + + cur = 1; + for (i = 0; i < repeat_cnt; i++) { + memcpy(&info[cur], &info[0], sizeof(info[0])); + info[cur++].off += (i + 1) * elem_size; + } + + return 0; +} + static int btf_find_field_one(const struct btf *btf, const struct btf_type *var, const struct btf_type *var_type, @@ -3504,6 +3539,21 @@ static int btf_find_field_one(const struct btf *btf, { int ret, align, sz, field_type; struct btf_field_info tmp; + const struct btf_array *array; + u32 i, nelems = 1; + + /* Walk into array types to find the element type and the number of + * elements in the (flattened) array. + */ + for (i = 0; i < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); i++) { + array = btf_array(var_type); + nelems *= array->nelems; + var_type = btf_type_by_id(btf, array->type); + } + if (i == MAX_RESOLVE_DEPTH) + return -E2BIG; + if (nelems == 0) + return 0; field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), field_mask, seen_mask, &align, &sz); @@ -3512,7 +3562,7 @@ static int btf_find_field_one(const struct btf *btf, if (field_type < 0) return field_type; - if (expected_size && expected_size != sz) + if (expected_size && expected_size != sz * nelems) return 0; if (off % align) return 0; @@ -3552,10 +3602,14 @@ static int btf_find_field_one(const struct btf *btf, if (ret == BTF_FIELD_IGNORE) return 0; - if (!info_cnt) + if (nelems > info_cnt) return -E2BIG; - - return 1; + if (nelems > 1) { + ret = btf_repeat_field(info, nelems - 1, sz); + if (ret < 0) + return ret; + } + return nelems; } static int btf_find_struct_field(const struct btf *btf, From patchwork Thu May 23 17:41:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672082 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 0E82D7E59A for ; Thu, 23 May 2024 17:42:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486136; cv=none; b=bCTTidPOr80G3lRTA9+ptTFtey93u3NrQkBBMMkvXVuLBywo1Cd+4zU6FSUxhBEB5q9GzeTtfaYoKbQak1/zWTHTmjhxVbuZ9BNiEeNXgJPfIYkil0IlW/wIUUTFlbO+pU7lOvxib1tyqDjtHMX1oPWrjZmX21SLE5suhWZG2dQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486136; c=relaxed/simple; bh=f04j8SdsNpNXGMvRaqa/0k9JqXQZUvTALaVd1buz13E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KfkxYvlStrUqnMmOG+24ScEiHNXuEwD/5GgHqJ5EU0uMXDvRwNP9G7Al47D6oH+VmT4R2e1uVAEbwJ2RvCpXqWJnMw8vR8pLo95uXat7SRH6A+ZeDmi/p/+IRdB6JMExQt1xZSny3ubN3hdd3dHpNVpN5LenU4Bjp8upTFu8AsY= 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=MroN+ott; arc=none smtp.client-ip=209.85.128.174 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="MroN+ott" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-627efad69b4so19023877b3.3 for ; Thu, 23 May 2024 10:42:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486134; x=1717090934; 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=2zxGdAc8OkC+Ohn5y0sNY+gVO/Nxy7mPn5wFOc9WrDY=; b=MroN+otttNOwkCg2NqQ93OAFbg91TzH+6gBv/+rf/4hkaYjbXMNFNcNY3Xe5nBNcNQ QrFG7+/3DILz1ng3NvyPRDo1ht3nRy6xWfvmrSplNMN96Lu4gxmmQga/QbChT+IOCxO+ AAsnZIG1sYd4xTopAjRKAAStnuvg60vovs3npFd7mGvMZYfi1z9ERLxtDVCD3sOguv+N zUDTafjg4yj3KZiDdVihNAzKeC/gn5Y9YvdXIuBsW+Wx0WB7egjFGKxO6r6RqzEpoTQx wokwGudkhK8oW95YRPiViPIc+jdaB4mE+YyXVD6v+8FpdjKvvcJCnpACRrhxG4N3NKqF 4VQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486134; x=1717090934; 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=2zxGdAc8OkC+Ohn5y0sNY+gVO/Nxy7mPn5wFOc9WrDY=; b=TvdMPZzm7CPl7FgECsnJZlrBRxOHpD1vjiAWYjIWjTpOJq0axglDfr4ARvuHVp2xT6 rEUMb7pMBbMt39f4fSRacLtPfSq//OybSQnD4fJ+nEob4mIQDtnS2Vocp3WTtMWi9YT7 hKKvM5AUwZZaZ17QoHgwXXv5Hp5qk1Kzu801E4tK8WC25zBKqDcLOXeFGv5gjLZdnLe+ H3EeHmyKgCNiwBd9D3lJMfgkUM+MR8vkpoVSzgGK3FtVOR4E5whQQbPSoRpBrKNK2bsT tqS2v1B549fl9SuFcK6atVzdaAgZ3cOakF6wnu61TauK05YA2V/XuuKPQdUmMzHoaa9T kG5g== X-Gm-Message-State: AOJu0YxCnA85uO3WffvkPW1fZdpTCBmhEhlSGHNA8UKPsZnBGfRN8BMH rrV5KXi++jVh9n1SRP7JpCVlgVKDqMMVfuCyhIjZKwgBewT9mCM/93ePlw== X-Google-Smtp-Source: AGHT+IGBpVGAaXUX+yEpSk1LnJ+tQC9AT0ZvFHh/yGh7QQ16fr0MvvKN2i4wGJWDEfWqgJmWnx0jgw== X-Received: by 2002:a81:b64e:0:b0:627:ecd3:6223 with SMTP id 00721157ae682-627ecd364femr52227827b3.35.1716486133799; Thu, 23 May 2024 10:42:13 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:13 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 5/9] bpf: look into the types of the fields of a struct type recursively. Date: Thu, 23 May 2024 10:41:58 -0700 Message-Id: <20240523174202.461236-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 The verifier has field information for specific special types, such as kptr, rbtree root, and list head. These types are handled differently. However, we did not previously examine the types of fields of a struct type variable. Field information records were not generated for the kptrs, rbtree roots, and linked_list heads that are not located at the outermost struct type of a variable. For example, struct A { struct task_struct __kptr * task; }; struct B { struct A mem_a; } struct B var_b; It did not examine "struct A" so as not to generate field information for the kptr in "struct A" for "var_b". This patch enables BPF programs to define fields of these special types in a struct type other than the direct type of a variable or in a struct type that is the type of a field in the value type of a map. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 100 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 23 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 4fefa27d5aea..5e2b231a9af4 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3442,10 +3442,12 @@ btf_find_graph_root(const struct btf *btf, const struct btf_type *pt, goto end; \ } -static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, +static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_type, + u32 field_mask, u32 *seen_mask, int *align, int *sz) { int type = 0; + const char *name = __btf_name_by_offset(btf, var_type->name_off); if (field_mask & BPF_SPIN_LOCK) { if (!strcmp(name, "bpf_spin_lock")) { @@ -3481,7 +3483,7 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, field_mask_test_name(BPF_REFCOUNT, "bpf_refcount"); /* Only return BPF_KPTR when all other types with matchable names fail */ - if (field_mask & BPF_KPTR) { + if (field_mask & BPF_KPTR && !__btf_type_is_struct(var_type)) { type = BPF_KPTR_REF; goto end; } @@ -3494,41 +3496,83 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask, #undef field_mask_test_name -/* Repeat a field for a specified number of times. +/* Repeat a number of fields for a specified number of times. * - * Copy and repeat the first field for repeat_cnt - * times. The field is repeated by adding the offset of each field - * with + * Copy the fields starting from the first field and repeat them for + * repeat_cnt times. The fields are repeated by adding the offset of each + * field with * (i + 1) * elem_size * where i is the repeat index and elem_size is the size of an element. */ -static int btf_repeat_field(struct btf_field_info *info, - u32 repeat_cnt, u32 elem_size) +static int btf_repeat_fields(struct btf_field_info *info, + u32 field_cnt, u32 repeat_cnt, u32 elem_size) { - u32 i; + u32 i, j; u32 cur; /* Ensure not repeating fields that should not be repeated. */ - switch (info[0].type) { - case BPF_KPTR_UNREF: - case BPF_KPTR_REF: - case BPF_KPTR_PERCPU: - case BPF_LIST_HEAD: - case BPF_RB_ROOT: - break; - default: - return -EINVAL; + for (i = 0; i < field_cnt; i++) { + switch (info[i].type) { + case BPF_KPTR_UNREF: + case BPF_KPTR_REF: + case BPF_KPTR_PERCPU: + case BPF_LIST_HEAD: + case BPF_RB_ROOT: + break; + default: + return -EINVAL; + } } - cur = 1; + cur = field_cnt; for (i = 0; i < repeat_cnt; i++) { - memcpy(&info[cur], &info[0], sizeof(info[0])); - info[cur++].off += (i + 1) * elem_size; + memcpy(&info[cur], &info[0], field_cnt * sizeof(info[0])); + for (j = 0; j < field_cnt; j++) + info[cur++].off += (i + 1) * elem_size; } return 0; } +static int btf_find_struct_field(const struct btf *btf, + const struct btf_type *t, u32 field_mask, + struct btf_field_info *info, int info_cnt); + +/* Find special fields in the struct type of a field. + * + * This function is used to find fields of special types that is not a + * global variable or a direct field of a struct type. It also handles the + * repetition if it is the element type of an array. + */ +static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t, + u32 off, u32 nelems, + u32 field_mask, struct btf_field_info *info, + int info_cnt) +{ + int ret, err, i; + + ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt); + + if (ret <= 0) + return ret; + + /* Shift the offsets of the nested struct fields to the offsets + * related to the container. + */ + for (i = 0; i < ret; i++) + info[i].off += off; + + if (nelems > 1) { + err = btf_repeat_fields(info, ret, nelems - 1, t->size); + if (err == 0) + ret *= nelems; + else + ret = err; + } + + return ret; +} + static int btf_find_field_one(const struct btf *btf, const struct btf_type *var, const struct btf_type *var_type, @@ -3555,8 +3599,18 @@ static int btf_find_field_one(const struct btf *btf, if (nelems == 0) return 0; - field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), + field_type = btf_get_field_type(btf, var_type, field_mask, seen_mask, &align, &sz); + /* Look into variables of struct types */ + if (!field_type && __btf_type_is_struct(var_type)) { + sz = var_type->size; + if (expected_size && expected_size != sz * nelems) + return 0; + ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask, + &info[0], info_cnt); + return ret; + } + if (field_type == 0) return 0; if (field_type < 0) @@ -3605,7 +3659,7 @@ static int btf_find_field_one(const struct btf *btf, if (nelems > info_cnt) return -E2BIG; if (nelems > 1) { - ret = btf_repeat_field(info, nelems - 1, sz); + ret = btf_repeat_fields(info, 1, nelems - 1, sz); if (ret < 0) return ret; } From patchwork Thu May 23 17:41:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672083 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.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 1CB187F486 for ; Thu, 23 May 2024 17:42:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486138; cv=none; b=lPEtf2Ejb4fOFtw5T8p1NHQpaVPUK/G9HSwaq4Cf43C9CSfNyS1vOrewAtngKiN6DgBuYYSUToTHSiI8393fPmEZKFx0Dbkj1zZxKTBOsbBbDG9x34kSF/9+qPPeWBQxh/Hg8c+yJEDknvvrVyD9oz684fV8mAZNGRP9R9zzbqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486138; c=relaxed/simple; bh=AfPVNo1WOUDkeqpBoOWizg53TqtuNzGhAMFb5lNqTho=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=up58SyqkBCJOdBqh6aj+V+8LUIdi8J3A456OVzNFVNIVYysFNyJpymWbdlKL7XygPrl1noPQmhy/1DwzAaN5dvVcHKlvU2MvgMkRQlrMGrscJ4QTiFf6zqGYBzmG7mal3UHKN69hlD/zS7O5Q9XXCoBQ0NldncYKu4ESHhgJ0b4= 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=knyrCRTa; arc=none smtp.client-ip=209.85.128.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="knyrCRTa" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-62a080a171dso6547b3.0 for ; Thu, 23 May 2024 10:42:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486136; x=1717090936; 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=xtdQr/zEufaVdZNG//MSA6qPzIpwLHsz9vFprtP/cME=; b=knyrCRTaZJGZLB5Yh1LOXaOMxjYA1hNHUIk8ybchLH2RnXGHBMJ5AT83M9Qgo3ucY0 2OQkWPnWYFMVRBYniDrVIQ7cxhrt1ziAtiDLQXJlcLbj8ohKAltEL+6BvIGaO2+J333T 0HsiS/HDDQW57TabCgb8XoMHSiUZr7AUVDd7b9MVXIkRrn/5kBJZGUO/UKrC23/7BpC+ u9a7EUjD/nLKkuxly6J4LczWAghxZGdtcNkhakQ51agWk1F4827zpGUomrtnPNFf1aLG 8GXD7XfaSF7gdVaN6C18oTKRts3GSb1LJPBhIzShOQ8jzZ9Facm41cVoABO2i7AS2krW 9b8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486136; x=1717090936; 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=xtdQr/zEufaVdZNG//MSA6qPzIpwLHsz9vFprtP/cME=; b=d4/iBDDtdxIa0PXvJzBS32kfgU/zzyBuPrucgSubGMSAP7sS7eLchIY0/0G9O6LAbL o75vwJsvNkpafYyHcRWTnaMlc8LvbwO9z4M3u3QcW7OCnXFxXHqH3Vqfp8YuPOtBWlo2 no4qxb5wf4A1rBwD1iu3mzdx3/BzwCVKPfaSrukNLofB13feFICkv0OJJMlz1EqNEzOp Mo4uZx3tygxEc6kem1lnd+/plWWm2VTOic8AH78qjvSc9S6+QOYOeG00Y7hJPIbHCT9x Rqxxaw+Fn2U6TUc4l4xsgVwFax3FaMICVF7mGHHy3HohJTcRJLeqWVzAApPvI1Gln/wN l2ng== X-Gm-Message-State: AOJu0Yzg5YWl+M0jS9BL1pQ261GNl++HKiVJlHrhjTSC7yudwbxtI9Wn L105CP16lBl11LpINuODg0pMVaMGds9F7MQ+fenkS4wI5vwVYREQax0W2g== X-Google-Smtp-Source: AGHT+IF06h4TqOrTWk9YihojP1RVpznBghkflUAsJCptmHVlEBPHzvZaH7qD2sV8rNh7E5s9+YXlbg== X-Received: by 2002:a0d:c3c4:0:b0:61a:cea1:3c63 with SMTP id 00721157ae682-627e482ebe1mr57925677b3.47.1716486135853; Thu, 23 May 2024 10:42:15 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:14 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 6/9] bpf: limit the number of levels of a nested struct type. Date: Thu, 23 May 2024 10:41:59 -0700 Message-Id: <20240523174202.461236-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 Limit the number of levels looking into struct types to avoid running out of stack space. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 5e2b231a9af4..7928d920056f 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3536,7 +3536,8 @@ static int btf_repeat_fields(struct btf_field_info *info, static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, - struct btf_field_info *info, int info_cnt); + struct btf_field_info *info, int info_cnt, + u32 level); /* Find special fields in the struct type of a field. * @@ -3547,11 +3548,15 @@ static int btf_find_struct_field(const struct btf *btf, static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t, u32 off, u32 nelems, u32 field_mask, struct btf_field_info *info, - int info_cnt) + int info_cnt, u32 level) { int ret, err, i; - ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt); + level++; + if (level >= MAX_RESOLVE_DEPTH) + return -E2BIG; + + ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt, level); if (ret <= 0) return ret; @@ -3579,7 +3584,8 @@ static int btf_find_field_one(const struct btf *btf, int var_idx, u32 off, u32 expected_size, u32 field_mask, u32 *seen_mask, - struct btf_field_info *info, int info_cnt) + struct btf_field_info *info, int info_cnt, + u32 level) { int ret, align, sz, field_type; struct btf_field_info tmp; @@ -3607,7 +3613,7 @@ static int btf_find_field_one(const struct btf *btf, if (expected_size && expected_size != sz * nelems) return 0; ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask, - &info[0], info_cnt); + &info[0], info_cnt, level); return ret; } @@ -3668,7 +3674,8 @@ static int btf_find_field_one(const struct btf *btf, static int btf_find_struct_field(const struct btf *btf, const struct btf_type *t, u32 field_mask, - struct btf_field_info *info, int info_cnt) + struct btf_field_info *info, int info_cnt, + u32 level) { int ret, idx = 0; const struct btf_member *member; @@ -3687,7 +3694,7 @@ static int btf_find_struct_field(const struct btf *btf, ret = btf_find_field_one(btf, t, member_type, i, off, 0, field_mask, &seen_mask, - &info[idx], info_cnt - idx); + &info[idx], info_cnt - idx, level); if (ret < 0) return ret; idx += ret; @@ -3697,7 +3704,7 @@ static int btf_find_struct_field(const struct btf *btf, static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, u32 field_mask, struct btf_field_info *info, - int info_cnt) + int info_cnt, u32 level) { int ret, idx = 0; const struct btf_var_secinfo *vsi; @@ -3710,7 +3717,8 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t, off = vsi->offset; ret = btf_find_field_one(btf, var, var_type, -1, off, vsi->size, field_mask, &seen_mask, - &info[idx], info_cnt - idx); + &info[idx], info_cnt - idx, + level); if (ret < 0) return ret; idx += ret; @@ -3723,9 +3731,9 @@ static int btf_find_field(const struct btf *btf, const struct btf_type *t, int info_cnt) { if (__btf_type_is_struct(t)) - return btf_find_struct_field(btf, t, field_mask, info, info_cnt); + return btf_find_struct_field(btf, t, field_mask, info, info_cnt, 0); else if (btf_type_is_datasec(t)) - return btf_find_datasec_var(btf, t, field_mask, info, info_cnt); + return btf_find_datasec_var(btf, t, field_mask, info, info_cnt, 0); return -EINVAL; } From patchwork Thu May 23 17:42:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672084 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 61B54763F2 for ; Thu, 23 May 2024 17:42:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486140; cv=none; b=qvakpS9532JzM1YJtDNkBqGeXw9T0D8ZNhaHSMHyvfM+TTyDSjN3FvIkt5FI/IJjqbMMsBMlxwlxuehH69MnnSKNJ4Jb0pUe06D2BwRDVt/TTTTfSYUWm0aAwisVSmW9S3jqjRdiiBTrJ/CPVobUcN0XAIEOxJ93+DJSvEW7fs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486140; c=relaxed/simple; bh=6ddzF50xdfvUTLbRPmh03d817gxeSkpTT82lJoW/bdU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C+Ufo6p+Ex/AILyWNN6ylBhcfLh7pjl81t38NTvpxlu3s4mScjIO1nmhrcGDUIumBpzwIxh3i69iXlxlTTL233N5kA517nuovuKYAnQw+KcisdrwCH5+LE1i0tVq6FP+deGcsmAiiaddIBWFwiZm8f5g/dkGZzLS7Fuo+ufH/Bg= 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=l2Ln+t11; arc=none smtp.client-ip=209.85.128.180 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="l2Ln+t11" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-6114c9b4d83so57967707b3.3 for ; Thu, 23 May 2024 10:42:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486137; x=1717090937; 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=54DvYQpAAB02Xh88RZICeBNDEtLg9JnAjUy2/MHSl1w=; b=l2Ln+t11/LzlK/VRGklKAib8BYhcj+tZdGyYr8FOapHBFnZeSZKxxGfBcKtarUX5L2 JOVM4mPwMupkxZfB8FXqEqPgDxSemVHtR/qZe2RfdqzW9dfjRj/PADGV6Smt0+UNQs4V wQlhcFErOA1GCje+pJZsDdKVMmmPsa70QPES6mhyn/OsXDyoqgsWyEUAhDQqi72kmx2a PZmVoxUNZDt7irzw730fu8W8R7ctxWKpl4VZ09jsPnXBrQabY18VUyynAwG8MNIXJKAx 53Qy881aPg09rmT4gwYxLM4ohrIPriKC7UlzwVqQfVzO79Ikyp9vQX6Kt7xcVnGgbDE4 E33Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486137; x=1717090937; 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=54DvYQpAAB02Xh88RZICeBNDEtLg9JnAjUy2/MHSl1w=; b=wN1Oqryy2shAFiz/rpo8MOX9e+U2u3mov4OOVGocQrcHTMQ2qfL4d3bS42lvkDXJcG QMj6+HP8m8MqsnNEPF/GTpC6Z/3IEqkevSXtNQ8tA1c3NaGaMn1LaogIq6NGWdBKVHdy Am+TmD8eYs1othDi635h5JBx8gzW8SKjLAzzOKx1b+Cu2n4TshnRA20FCFNYjsNujRHS 1nxHAdHYIor7mu67j2VDrsRgjKXEc5sagGSE+LSPouzEplCbdfKoqLdC+YfOuG9dOsFW olqV8GMuv7KAY7P2okyg0XuxANJR7o/SznIcxYwoyD6/hf10YVM8ZeQacQD1mXdPVCfP oRJg== X-Gm-Message-State: AOJu0YzGtRKt2XbCUeP8t3I1iRAymKfMv3q7T29Q2tECI4FX48LW/T+W ci2s9rpMlwaJ9PN09e9mPhqzvWjgB/PpiDCCf9cmH12aTwvq3P6+H1oIWA== X-Google-Smtp-Source: AGHT+IHr9UIkxAVA+O7zsOGJSdI/4CFIOyqP6AmaYw1m3gbrkfMGeEKGj6rsgUlDGiZzkqqKrAR2dA== X-Received: by 2002:a0d:d415:0:b0:627:972f:bac3 with SMTP id 00721157ae682-627e4688f57mr62096587b3.8.1716486137051; Thu, 23 May 2024 10:42:17 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:16 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 7/9] selftests/bpf: Test kptr arrays and kptrs in nested struct fields. Date: Thu, 23 May 2024 10:42:00 -0700 Message-Id: <20240523174202.461236-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 Make sure that BPF programs can declare global kptr arrays and kptr fields in struct types that is the type of a global variable or the type of a nested descendant field in a global variable. An array with only one element is special case, that it treats the element like a non-array kptr field. Nested arrays are also tested to ensure they are handled properly. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/cpumask.c | 5 + .../selftests/bpf/progs/cpumask_success.c | 171 ++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index ecf89df78109..2570bd4b0cb2 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -18,6 +18,11 @@ static const char * const cpumask_success_testcases[] = { "test_insert_leave", "test_insert_remove_release", "test_global_mask_rcu", + "test_global_mask_array_one_rcu", + "test_global_mask_array_rcu", + "test_global_mask_array_l2_rcu", + "test_global_mask_nested_rcu", + "test_global_mask_nested_deep_rcu", "test_cpumask_weight", }; diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index 7a1e64c6c065..fd8106831c32 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -12,6 +12,31 @@ char _license[] SEC("license") = "GPL"; int pid, nr_cpus; +struct kptr_nested { + struct bpf_cpumask __kptr * mask; +}; + +struct kptr_nested_pair { + struct bpf_cpumask __kptr * mask_1; + struct bpf_cpumask __kptr * mask_2; +}; + +struct kptr_nested_mid { + int dummy; + struct kptr_nested m; +}; + +struct kptr_nested_deep { + struct kptr_nested_mid ptrs[2]; + struct kptr_nested_pair ptr_pairs[3]; +}; + +private(MASK) static struct bpf_cpumask __kptr * global_mask_array[2]; +private(MASK) static struct bpf_cpumask __kptr * global_mask_array_l2[2][1]; +private(MASK) static struct bpf_cpumask __kptr * global_mask_array_one[1]; +private(MASK) static struct kptr_nested global_mask_nested[2]; +private(MASK_DEEP) static struct kptr_nested_deep global_mask_nested_deep; + static bool is_test_task(void) { int cur_pid = bpf_get_current_pid_tgid() >> 32; @@ -460,6 +485,152 @@ int BPF_PROG(test_global_mask_rcu, struct task_struct *task, u64 clone_flags) return 0; } +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_one_rcu, struct task_struct *task, u64 clone_flags) +{ + struct bpf_cpumask *local, *prev; + + if (!is_test_task()) + return 0; + + /* Kptr arrays with one element are special cased, being treated + * just like a single pointer. + */ + + local = create_cpumask(); + if (!local) + return 0; + + prev = bpf_kptr_xchg(&global_mask_array_one[0], local); + if (prev) { + bpf_cpumask_release(prev); + err = 3; + return 0; + } + + bpf_rcu_read_lock(); + local = global_mask_array_one[0]; + if (!local) { + err = 4; + bpf_rcu_read_unlock(); + return 0; + } + + bpf_rcu_read_unlock(); + + return 0; +} + +static int _global_mask_array_rcu(struct bpf_cpumask **mask0, + struct bpf_cpumask **mask1) +{ + struct bpf_cpumask *local; + + if (!is_test_task()) + return 0; + + /* Check if two kptrs in the array work and independently */ + + local = create_cpumask(); + if (!local) + return 0; + + bpf_rcu_read_lock(); + + local = bpf_kptr_xchg(mask0, local); + if (local) { + err = 1; + goto err_exit; + } + + /* [, NULL] */ + if (!*mask0 || *mask1) { + err = 2; + goto err_exit; + } + + local = create_cpumask(); + if (!local) { + err = 9; + goto err_exit; + } + + local = bpf_kptr_xchg(mask1, local); + if (local) { + err = 10; + goto err_exit; + } + + /* [, ] */ + if (!*mask0 || !*mask1 || *mask0 == *mask1) { + err = 11; + goto err_exit; + } + +err_exit: + if (local) + bpf_cpumask_release(local); + bpf_rcu_read_unlock(); + return 0; +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_array[0], &global_mask_array[1]); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_array_l2_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_array_l2[0][0], &global_mask_array_l2[1][0]); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_nested[0].mask, &global_mask_nested[1].mask); +} + +/* Ensure that the field->offset has been correctly advanced from one + * nested struct or array sub-tree to another. In the case of + * kptr_nested_deep, it comprises two sub-trees: ktpr_1 and kptr_2. By + * calling bpf_kptr_xchg() on every single kptr in both nested sub-trees, + * the verifier should reject the program if the field->offset of any kptr + * is incorrect. + * + * For instance, if we have 10 kptrs in a nested struct and a program that + * accesses each kptr individually with bpf_kptr_xchg(), the compiler + * should emit instructions to access 10 different offsets if it works + * correctly. If the field->offset values of any pair of them are + * incorrectly the same, the number of unique offsets in btf_record for + * this nested struct should be less than 10. The verifier should fail to + * discover some of the offsets emitted by the compiler. + * + * Even if the field->offset values of kptrs are not duplicated, the + * verifier should fail to find a btf_field for the instruction accessing a + * kptr if the corresponding field->offset is pointing to a random + * incorrect offset. + */ +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_deep_rcu, struct task_struct *task, u64 clone_flags) +{ + int r, i; + + r = _global_mask_array_rcu(&global_mask_nested_deep.ptrs[0].m.mask, + &global_mask_nested_deep.ptrs[1].m.mask); + if (r) + return r; + + for (i = 0; i < 3; i++) { + r = _global_mask_array_rcu(&global_mask_nested_deep.ptr_pairs[i].mask_1, + &global_mask_nested_deep.ptr_pairs[i].mask_2); + if (r) + return r; + } + return 0; +} + SEC("tp_btf/task_newtask") int BPF_PROG(test_cpumask_weight, struct task_struct *task, u64 clone_flags) { From patchwork Thu May 23 17:42:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672085 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (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 B416D7604F for ; Thu, 23 May 2024 17:42:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486141; cv=none; b=qP3Jl8QJECjP4h3CprsjIxKAHkoMGaXQS8A9QszYFbX+cwSjTAgf4sUtCr6v5RUMafNGB7UvZSckKsShfkcSisuOmTg1DEs1OVieZxtPHGDRZSKmyBqeEB2y5ZQy/wT0Sx8IVchWu20grK7eggAa4iVXCn2LVxwl4dif9KQj/uI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486141; c=relaxed/simple; bh=184HSHS8K1pDQhxH155teo7Hn+PjBBhq6NMvMs9ANKc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KosXkrYTWieaDPyUZ/MkrCp8+Jmj4npc8B49PuTkUHN4D785MZq22gG807kTuSb4nc5EuaSSO2motwLhqxmA66VNsw6XSHh7xjBTsF/B+TR3vyhnZvjvAlbN/xzdq3sAQlLNnAUkqqYOPL9UU0VPJNytITJik7K5fXHOQ6zCpCs= 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=K/d2U2Yx; arc=none smtp.client-ip=209.85.128.169 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="K/d2U2Yx" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-6209e8a0386so22112287b3.0 for ; Thu, 23 May 2024 10:42:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486138; x=1717090938; 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=EWwzY90xebc6YceOSeyo6ErhBa/XYlP1DhvcruTgZXw=; b=K/d2U2YxL1MTweoFLR7OCTVg2Y+/0HBw2yjHS+62snc5m/dGuPQlWmOodrCnCAKFuW 0pWFfT3oYaZlDzi8yWx5i7gE9naNYVRGPTXE+37uYB5EhbBIA+b75xeUE7mzw9BPbr1Y BfYdh5TPKa+HXCYaCIzddnmDvR5fYicVjXKiYCBd1DQBbyKcRfIh/1Vb077p/QEtZtAa 0PQK9CP5+UUI1wTLzVbR384GftrrFg0zlMy9No9xsfO8eBun+VNiZZm/lfUtqsvwAAe9 EGZ7Lum+dH3ny6+wvWxWf1qI0AEp4pYSzU4XrkwwTbYSXJhAQG2vsejismy1jW+jmpyn ULPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486138; x=1717090938; 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=EWwzY90xebc6YceOSeyo6ErhBa/XYlP1DhvcruTgZXw=; b=Cj2Xj2txqXAIusz7QIexExoYVIDNp+H+Ev7vsqWjw7cr02HMasz7LM9kNa1E7tTNKO BuT/uGmfz5dMPUgmt1VhGeCIAfoRW6d5S2Tf6UswvQjdvGTDHw6aYrKkGv2GUunE/SbU Kzi21HYyQzNAn9pn7Gj6ZMDl9YkKErkMLEEV2VXTxDDjTlzitfmOO9nBa/KIJ2GdFaRW 69lfQ9d0y6OY7kosYIhkQpgm+BaH2HuxitdMTAyGlM12erM5wCavRkjwNDMUzeo4/0wz 2GuF/nEkm3rAXwtkdVOu7ZoBm0n+3M+lCWi2hl9jisLgQgPjLC2nCrKC3ajnHPkOiFll 2xow== X-Gm-Message-State: AOJu0YyFh4Gc5XFM2gr2vy2ZDZiSjwHOpxqjiWq2rSIHgBIcuG+Tlgti K0M+YZghBPv5jsZgMJl8eEVeQTNNqXjogXH2brdEDVV94Kq1qQ5uQgQkOA== X-Google-Smtp-Source: AGHT+IGesTXvFrr9FSaAdQ4WOCpn6OhKuXgebsv0UAokcWW7xZ065jxPKkJkHTKDFyYsf/o5YIurUA== X-Received: by 2002:a0d:d78d:0:b0:61a:d30f:a9c4 with SMTP id 00721157ae682-627e4664c64mr78739867b3.8.1716486138553; Thu, 23 May 2024 10:42:18 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:18 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 8/9] selftests/bpf: Test global bpf_rb_root arrays and fields in nested struct types. Date: Thu, 23 May 2024 10:42:01 -0700 Message-Id: <20240523174202.461236-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 Make sure global arrays of bpf_rb_root and fields of bpf_rb_root in nested struct types work correctly. Signed-off-by: Kui-Feng Lee --- .../testing/selftests/bpf/prog_tests/rbtree.c | 47 +++++++++++ tools/testing/selftests/bpf/progs/rbtree.c | 77 +++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/rbtree.c b/tools/testing/selftests/bpf/prog_tests/rbtree.c index e9300c96607d..9818f06c97c5 100644 --- a/tools/testing/selftests/bpf/prog_tests/rbtree.c +++ b/tools/testing/selftests/bpf/prog_tests/rbtree.c @@ -31,6 +31,28 @@ static void test_rbtree_add_nodes(void) rbtree__destroy(skel); } +static void test_rbtree_add_nodes_nested(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + struct rbtree *skel; + int ret; + + skel = rbtree__open_and_load(); + if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load")) + return; + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_nodes_nested), &opts); + ASSERT_OK(ret, "rbtree_add_nodes_nested run"); + ASSERT_OK(opts.retval, "rbtree_add_nodes_nested retval"); + ASSERT_EQ(skel->data->less_callback_ran, 1, "rbtree_add_nodes_nested less_callback_ran"); + + rbtree__destroy(skel); +} + static void test_rbtree_add_and_remove(void) { LIBBPF_OPTS(bpf_test_run_opts, opts, @@ -53,6 +75,27 @@ static void test_rbtree_add_and_remove(void) rbtree__destroy(skel); } +static void test_rbtree_add_and_remove_array(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + struct rbtree *skel; + int ret; + + skel = rbtree__open_and_load(); + if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load")) + return; + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_and_remove_array), &opts); + ASSERT_OK(ret, "rbtree_add_and_remove_array"); + ASSERT_OK(opts.retval, "rbtree_add_and_remove_array retval"); + + rbtree__destroy(skel); +} + static void test_rbtree_first_and_remove(void) { LIBBPF_OPTS(bpf_test_run_opts, opts, @@ -104,8 +147,12 @@ void test_rbtree_success(void) { if (test__start_subtest("rbtree_add_nodes")) test_rbtree_add_nodes(); + if (test__start_subtest("rbtree_add_nodes_nested")) + test_rbtree_add_nodes_nested(); if (test__start_subtest("rbtree_add_and_remove")) test_rbtree_add_and_remove(); + if (test__start_subtest("rbtree_add_and_remove_array")) + test_rbtree_add_and_remove_array(); if (test__start_subtest("rbtree_first_and_remove")) test_rbtree_first_and_remove(); if (test__start_subtest("rbtree_api_release_aliasing")) diff --git a/tools/testing/selftests/bpf/progs/rbtree.c b/tools/testing/selftests/bpf/progs/rbtree.c index b09f4fffe57c..a3620c15c136 100644 --- a/tools/testing/selftests/bpf/progs/rbtree.c +++ b/tools/testing/selftests/bpf/progs/rbtree.c @@ -13,6 +13,15 @@ struct node_data { struct bpf_rb_node node; }; +struct root_nested_inner { + struct bpf_spin_lock glock; + struct bpf_rb_root root __contains(node_data, node); +}; + +struct root_nested { + struct root_nested_inner inner; +}; + long less_callback_ran = -1; long removed_key = -1; long first_data[2] = {-1, -1}; @@ -20,6 +29,9 @@ long first_data[2] = {-1, -1}; #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) private(A) struct bpf_spin_lock glock; private(A) struct bpf_rb_root groot __contains(node_data, node); +private(A) struct bpf_rb_root groot_array[2] __contains(node_data, node); +private(A) struct bpf_rb_root groot_array_one[1] __contains(node_data, node); +private(B) struct root_nested groot_nested; static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b) { @@ -71,6 +83,12 @@ long rbtree_add_nodes(void *ctx) return __add_three(&groot, &glock); } +SEC("tc") +long rbtree_add_nodes_nested(void *ctx) +{ + return __add_three(&groot_nested.inner.root, &groot_nested.inner.glock); +} + SEC("tc") long rbtree_add_and_remove(void *ctx) { @@ -109,6 +127,65 @@ long rbtree_add_and_remove(void *ctx) return 1; } +SEC("tc") +long rbtree_add_and_remove_array(void *ctx) +{ + struct bpf_rb_node *res1 = NULL, *res2 = NULL, *res3 = NULL; + struct node_data *nodes[3][2] = {{NULL, NULL}, {NULL, NULL}, {NULL, NULL}}; + struct node_data *n; + long k1 = -1, k2 = -1, k3 = -1; + int i, j; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + nodes[i][j] = bpf_obj_new(typeof(*nodes[i][j])); + if (!nodes[i][j]) + goto err_out; + nodes[i][j]->key = i * 2 + j; + } + } + + bpf_spin_lock(&glock); + for (i = 0; i < 2; i++) + for (j = 0; j < 2; j++) + bpf_rbtree_add(&groot_array[i], &nodes[i][j]->node, less); + for (j = 0; j < 2; j++) + bpf_rbtree_add(&groot_array_one[0], &nodes[2][j]->node, less); + res1 = bpf_rbtree_remove(&groot_array[0], &nodes[0][0]->node); + res2 = bpf_rbtree_remove(&groot_array[1], &nodes[1][0]->node); + res3 = bpf_rbtree_remove(&groot_array_one[0], &nodes[2][0]->node); + bpf_spin_unlock(&glock); + + if (res1) { + n = container_of(res1, struct node_data, node); + k1 = n->key; + bpf_obj_drop(n); + } + if (res2) { + n = container_of(res2, struct node_data, node); + k2 = n->key; + bpf_obj_drop(n); + } + if (res3) { + n = container_of(res3, struct node_data, node); + k3 = n->key; + bpf_obj_drop(n); + } + if (k1 != 0 || k2 != 2 || k3 != 4) + return 2; + + return 0; + +err_out: + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { + if (nodes[i][j]) + bpf_obj_drop(nodes[i][j]); + } + } + return 1; +} + SEC("tc") long rbtree_first_and_remove(void *ctx) { From patchwork Thu May 23 17:42:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13672086 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 09CA2763F2 for ; Thu, 23 May 2024 17:42:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486142; cv=none; b=qHffClVBSMFIJ/emP9gVv9eL9ciLOQJIgCHYKNVFeSNxdE0XBGIhJPNs4LJNelAInUUaOnBsi1/jCQg4Mvv+sdflZptfc+RuvqRArpreFSKCyfJUzXCZerwr0AbuSB2pEUivNhtZyqbr7VYnaZhLUVgrqF2VSXher40Bzj7Hn70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716486142; c=relaxed/simple; bh=pnfwEmhJaGJnnF5pQds6sqgpSop4ID4OueH53SaCbNw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ItSbxpNwmroAvUoEOk+tYjgP1phYZJPCZenEA9A4GsMEn6NG7RFaYwQnX6KRlJImlA7vJYYMkl8QVLNZBtHPhxjCdKFmqI6tKzCOFqVpBmvBD0ISuTUU4oYggjm8GZ7FI9sG9uYVkg/kD3+vP64d0fFxbKGAKXLCrgcbSH7pPr4= 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=e2D63vQw; arc=none smtp.client-ip=209.85.128.180 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="e2D63vQw" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-627f5b7b75bso9804717b3.0 for ; Thu, 23 May 2024 10:42:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716486140; x=1717090940; 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=/3VSzMOsCVcU1vd0IqZVW+SxoJr/0Bn4dYKhvg3D3Hk=; b=e2D63vQwayuA7Xumn+hRbd14sVxU9mU+Qkf6tJC0lgJv8/iznRVxmEovs4A9YSpN/x 9mLTg73pykCT49N9ct49x9mlorGbsm9sqMJxSlP4n0KF+lZMiawcj2SawVUFL3D0J/mV w0xmMdLOd/WZRrP/4Y04kkm9/y06DwmmovrLvpgqNTu1ObGJfH5M+uyyaU7pIrA0Aksv g/4V1pvFCYYZwvF58fPjxpLp7Ho8p35ZKAxDPWTwRn4ZiMd3cr1bQz4VUiXbn+cA+ymQ lfAFcbKI/FtTF/nrw0AwkaEPNw1YQHrXFUIZBAgZqflaAPfQMqp9eMmRexi/NB2S47vB yniQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716486140; x=1717090940; 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=/3VSzMOsCVcU1vd0IqZVW+SxoJr/0Bn4dYKhvg3D3Hk=; b=xUjjsMNR8xakLZnrLRSoQrzYZeBZKhXCrW89z7JPitLLK5hUXT2kcb5c/qD7TFqqa3 4E/jrrm3pvn0QNCXx/Ol6vONxNJYfhbfytd6l/cjaeBH8dGsONZ/uEUml5g+Ym22abo9 F/zM5wvZD9ASjqFE8A06H50inPeXh46V2NIZKejKBoHYAkorKAUv2RlrMNfWm6z7xpwL MuKM67nJnfB6gRiPtWRPAgIBWFWzih2AYG51n5uBHeHcUY+Vk1mQKDPpmCRR/MpmBh7I Hdbj6EPVLX6kL1jNEF35rlToKb9dq74rP0PAP7IXLsLy3VTXDKrK8L4vOshLhFKSO0lV 0CDQ== X-Gm-Message-State: AOJu0YzxE0BrYgCzRaFPrLWcDm789cTIiyBQGpM/dbOsSzLRpJ+JPb35 FwbKtwMkla+B29QYgHaV2P1H2H6NDAB5n///GtD+2V8Ih1duzhnve7t2zQ== X-Google-Smtp-Source: AGHT+IGa0ExyWcX7eFVT9z0nWb503IWwGU073NA0AJ+N3/BtbHiWr2NT6MIgCf4qIYCkogdQOpC+2Q== X-Received: by 2002:a05:690c:c99:b0:618:88d1:f15f with SMTP id 00721157ae682-627fb1ca4c5mr23859287b3.0.1716486139899; Thu, 23 May 2024 10:42:19 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:a2b5:fcfb:857c:2908]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e2514bbsm63652277b3.42.2024.05.23.10.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 10:42:19 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v7 9/9] selftests/bpf: Test global bpf_list_head arrays. Date: Thu, 23 May 2024 10:42:02 -0700 Message-Id: <20240523174202.461236-10-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240523174202.461236-1-thinker.li@gmail.com> References: <20240523174202.461236-1-thinker.li@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 Make sure global arrays of bpf_list_heads and fields of bpf_list_heads in nested struct types work correctly. Acked-by: Eduard Zingerman Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/linked_list.c | 12 ++++++ .../testing/selftests/bpf/progs/linked_list.c | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c index 2fb89de63bd2..77d07e0a4a55 100644 --- a/tools/testing/selftests/bpf/prog_tests/linked_list.c +++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c @@ -183,6 +183,18 @@ static void test_linked_list_success(int mode, bool leave_in_map) if (!leave_in_map) clear_fields(skel->maps.bss_A); + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.global_list_push_pop_nested), &opts); + ASSERT_OK(ret, "global_list_push_pop_nested"); + ASSERT_OK(opts.retval, "global_list_push_pop_nested retval"); + if (!leave_in_map) + clear_fields(skel->maps.bss_A); + + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.global_list_array_push_pop), &opts); + ASSERT_OK(ret, "global_list_array_push_pop"); + ASSERT_OK(opts.retval, "global_list_array_push_pop retval"); + if (!leave_in_map) + clear_fields(skel->maps.bss_A); + if (mode == PUSH_POP) goto end; diff --git a/tools/testing/selftests/bpf/progs/linked_list.c b/tools/testing/selftests/bpf/progs/linked_list.c index 26205ca80679..f69bf3e30321 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.c +++ b/tools/testing/selftests/bpf/progs/linked_list.c @@ -11,6 +11,22 @@ #include "linked_list.h" +struct head_nested_inner { + struct bpf_spin_lock lock; + struct bpf_list_head head __contains(foo, node2); +}; + +struct head_nested { + int dummy; + struct head_nested_inner inner; +}; + +private(C) struct bpf_spin_lock glock_c; +private(C) struct bpf_list_head ghead_array[2] __contains(foo, node2); +private(C) struct bpf_list_head ghead_array_one[1] __contains(foo, node2); + +private(D) struct head_nested ghead_nested; + static __always_inline int list_push_pop(struct bpf_spin_lock *lock, struct bpf_list_head *head, bool leave_in_map) { @@ -309,6 +325,32 @@ int global_list_push_pop(void *ctx) return test_list_push_pop(&glock, &ghead); } +SEC("tc") +int global_list_push_pop_nested(void *ctx) +{ + return test_list_push_pop(&ghead_nested.inner.lock, &ghead_nested.inner.head); +} + +SEC("tc") +int global_list_array_push_pop(void *ctx) +{ + int r; + + r = test_list_push_pop(&glock_c, &ghead_array[0]); + if (r) + return r; + + r = test_list_push_pop(&glock_c, &ghead_array[1]); + if (r) + return r; + + /* Arrays with only one element is a special case, being treated + * just like a bpf_list_head variable by the verifier, not an + * array. + */ + return test_list_push_pop(&glock_c, &ghead_array_one[0]); +} + SEC("tc") int map_list_push_pop_multiple(void *ctx) {