From patchwork Fri May 10 01:13:04 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: 13660771 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (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 36965137C26 for ; Fri, 10 May 2024 01:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303598; cv=none; b=m2+8FJ2DIJI2ZkGHwNyArHP0pEMp1ri45iVSBiafXfp5fwcbAR+SSFoeF8F+qctTgJiwsZzWQM8TjrfUuMOwdOSYGLlmsLZQtsWZMDfyxULDgHgFKeFEw7m28rc2ABMNWcAHKroa8oKrF73UG/N9zzVbmp/QCS2/+xI21her3OY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303598; c=relaxed/simple; bh=qqggGaC3A0xzhmB4jpLtjhbNT+5ro/jXBMnCfYwbGzY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=m3+gbaMjQZcnGVBLS04N8J0kuC87NngKj3aeQcGEvSxZ6T2YaReEtXgXoJd/7qiF7FSzLHOUXePpp567ZdSt4zWKxvLhZVppovwrwVX1eQrLfdlx2j0Jt61r/g4wVNejzrw91fI0ASyGH4k3rjQkx3Ixgh2CINSDkzgiir1IsTc= 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=GWi/Gus1; arc=none smtp.client-ip=209.85.210.43 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="GWi/Gus1" Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-6f09ed75e4cso797010a34.3 for ; Thu, 09 May 2024 18:13:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303596; x=1715908396; 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=CEJYwvgNo6RYhVquU9hRkea7EjQyUP61ZP8HQ5pKYfw=; b=GWi/Gus1Pp4Y1FccQfsaM/zZABMjIyr0PiCmr5Fy2bJLFerYPSSbGlVx/AC4iCDPgn 3emfQQC4/pRqrIbs/DtalfyoU2JMvuMw5RWBeXQKlOdr2i7sLyOnmD6FUDALPI+aVxFb iEYy7SWpSXF6mxB6vcgekp00n5ESiHuYmVgx9ALAphxTxBnTDR+7jITsddjkfgs6aR0E p384JdB/G0vH3f9DgFX0D2W3HKXSbpF43wEEuVsNMbUTcs0WKDnzRM9Tu0D+AyGoUMWX piHoDkpfM0Eo/F4XePtYppxNFQurGLG7Aqr/+YvQDfAFfvNUgou2egFxGLEo7P8b48Ll h+5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303596; x=1715908396; 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=CEJYwvgNo6RYhVquU9hRkea7EjQyUP61ZP8HQ5pKYfw=; b=bbehmTg/b2VpvIgN8rI+bbF6gvJj0MmWK5IM0wNybuo5pU1URml2Q4O0SKd0lGnYjs C+KudKkI2viYn2d3/MBiiRAI3jOSlTTXjOx9TPEwV8MeF5z8sVqrq5eobA/slkzT5ec4 NwQxflOx1R1fD7oZsizffn4xrt5Qr+IoUqmPIdQfrmW4v3yjd+lFWZDjhw8qFEi1V5DE KHg9NEN+YuTeSTysamJxrDosNTjCaia+UZ8ZA+STG4qmDYQ1v8jMuGltHxstjiVakgVi sAh/B8hTbTdQvtuXUmBXkRskY8ENrMMZlzliGRvnWn0LtVSdMMQmYq24GbdUQTVelqMn 9GqA== X-Gm-Message-State: AOJu0YwhPXcDkY10qtWln7QXt04vI31fiYzEWDIMIrI5jdm0pz6IpOWX r0wus2EMIrJELPqffiKbQJmvPnSYtJIL0GTWZvAJU7Jk6iATgeZvUBDYyw== X-Google-Smtp-Source: AGHT+IGwqmOHYV2Np2ZRI/pjFWxc5B3NdomIkcFzNI/SZXASbJCHdp3y3f5/HsXsVqh+6sTzKqcgMA== X-Received: by 2002:a05:6830:88:b0:6ee:3596:ea1a with SMTP id 46e09a7af769-6f0e92d00b3mr1242503a34.31.1715303596012; Thu, 09 May 2024 18:13:16 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13:15 -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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee , Eduard Zingerman Subject: [PATCH bpf-next v5 1/9] bpf: Remove unnecessary checks on the offset of btf_field. Date: Thu, 9 May 2024 18:13:04 -0700 Message-Id: <20240510011312.1488046-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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 9e3aba08984e..3cc8e68b5b73 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 Fri May 10 01:13:05 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: 13660772 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 612EF13A884 for ; Fri, 10 May 2024 01:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303599; cv=none; b=AJVZLK7JCXAu+4ngqfRAez7GoRSKys+Chxr7Bk3BIhmm3HrZE30K+s0LHHlW5gkYE1xX84xKEP5HJ+qDfVEPtQUoBcyicWpn6X17cfBDZhbSBUXsEgDA9qhBbkRWe8uqSAuLdGA6udoMJ08cvc2ylEttZZ3MsTTNBwlKiIqvxYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303599; c=relaxed/simple; bh=2xfkYFldYvZvPKiTXacO80MKDZrrQYMBUHBKmZTcdbM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=g+l25Brb2eWQLbL6xYxIkdrAafnhW+64OCePjyrVflLimDuPYCUkAgxgMpSPpkei557LjYiS15UsgGF7BUJcpsCgr7OeYjw1GSP9GoDCYiS9CPcfFZzG4UsZ1erCXdtf9ssx4OBgzsu0OB/RYDl1owwZC7lXa+gOUscmVjQ771o= 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=HeqvvWmh; arc=none smtp.client-ip=209.85.210.46 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="HeqvvWmh" Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6f055c08220so816577a34.0 for ; Thu, 09 May 2024 18:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303597; x=1715908397; 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=oByKvxTobDLu+nJ0pkp9fRC/p41vrPlk7tpSQ/k6wLY=; b=HeqvvWmhKN4i+dFoz9MO4Ob+K5QFhtTX6DxHea23i3b6MC/3CTwGahadLsfzHSK+mw wPzVaq559CmaFSt9pMvefyuOdm4uYNkEqUJCvMCST4rQIPH9AR5MsWDTZaJwoOwksmPI vVGYAuJ5xOV8LydCPX+eDg9J9FIc6zjQqTNfgCsUCSJHXwcGGH1QL/9h2AS+xbBFjaF8 kN9IDd3ZvclMSwQybyo8lDGY9e8dnpJOauGxxUtUcxrKxnj3N2FXI6JEhC/ft7V/KVy8 P0i3hUv7zM4i0+v0wzz9ITuokHdszG9rsSYSoQlLvyLM3nfvi3BZsGA2USMlV3JprVHT eR8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303597; x=1715908397; 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=oByKvxTobDLu+nJ0pkp9fRC/p41vrPlk7tpSQ/k6wLY=; b=jJApC7Im8SqmOAcGdbo3nYbGaynfq+/qbwzw4tC0jc35hDBPLnxPa7jYraWDi2zqkY iECQVLTARojpElC1MC11M6ktIWp1IrmFcPlduNm5gb24Q+NCQ+kwzFmBNceuPtsOMy61 7ccBLRwtb5sdSY26Z+R6DUWFykNJ5LaV78XzUA1o8e/cprCIpYkfJmMX4JkfRA6o4BHD OTaIgoKMuomNhFUgjxFdg8hC1wVKt6fOAb37FJrtm8T+KlvigHt7wvtAArWkOmtln0qb K4oI2GvcD2oYQyisNA7EAPKhmm20UQHSfcfCL4iwY5ufrvIS+/8nGGzOJ+Usx7641dr3 eGXg== X-Gm-Message-State: AOJu0YwoswKUeRy+EDnVgLl5+YS3+32y6j97oT9BH5XoSLJ3ryR9LP/2 YTycqGY0rE3NoSYuaROlluUMXhFB3E8rpqqRuQ5JlVXRhq3vA/XD/ALaUg== X-Google-Smtp-Source: AGHT+IFXZi+AjyOFMzgEmBVdUC7lncs/+YFgEK09Pe9+aYRUHcNIOuBYpIms6Wtr4MNkOKC1A/TDWQ== X-Received: by 2002:a05:6830:1e0f:b0:6f0:7504:616f with SMTP id 46e09a7af769-6f0e92a2e77mr1495895a34.30.1715303597133; Thu, 09 May 2024 18:13:17 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13: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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee , Eduard Zingerman Subject: [PATCH bpf-next v5 2/9] bpf: Remove unnecessary call to btf_field_type_size(). Date: Thu, 9 May 2024 18:13:05 -0700 Message-Id: <20240510011312.1488046-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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 3cc8e68b5b73..7cfbc06d8d1b 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 Fri May 10 01:13:06 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: 13660773 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) (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 4AACE13A88F for ; Fri, 10 May 2024 01:13:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303600; cv=none; b=K/96gpUQyDy+zlu++RQyP0N6I5lMoFCUJsIpT/RJREsAp9QqjlDR0VjodcGff4+0+kbAnuD8uA/IPrlVTJrt7fYYkIQ10q08+y64KE1H5oKqzz9TmEieI+8pDlTGJckr08q5aBsbaVekDVnymS+F8pl39x3BYooTPNQgXEK/QEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303600; c=relaxed/simple; bh=upVhmpmzSJmS20JqXVMvwrSbBc8DdAjci0We5oLNjcw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PD97X5hXB5Ygq5T8LdhOYiI069aElSwaEQPaNegJaAoEIixtdgM0LS3qlaRKyiL/8d6b9MIR+y9DIYrTfnnXwh3BHruHJWUdHFWIbSqHYW/uepI/eD3O5T25Oe56bLqwjowmLRQask/pEcRr17VjQ3DOZK+VlnEwSs6gs6a+T2A= 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=X0PYpzXm; arc=none smtp.client-ip=209.85.160.52 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="X0PYpzXm" Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-23eb1851c34so608180fac.0 for ; Thu, 09 May 2024 18:13:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303598; x=1715908398; 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=X0PYpzXmKqAYkCp5lxfRzlxKzk3w+EVO/To6tHt2S2B1aaGEltLYAfOkhUVuNQvyzx IGDJNRdUSkPbNox70zrRIwmCtN/ylOhe00qUdLNRV0OzWbfXvOenLnhbKuyC2dm6AXD/ Cwoui87nSyx71YAqX7XNbDqGfDChMacfBWKkSHEsXU/88aPhuuOijY0EO+CnOogmrmYz qoDNJdBF7My7JzfFU09DN9bSqI73z0xFFWHyjsORGpvkyaR6WyLhGXVULJYcpOfWND0y UDAgo9P5QCBudzKzzHDAKeKjSgrwo/fJfgEEFnFjmkkTyVdqKUPHxlwvpHNSkrEwvi48 44gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303598; x=1715908398; 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=ZmpdeODRVipQs0Gp3rFew64T2fUAkq7Vw4voYuff+g1nCGJC47YDBOfZ/jyik4TJlv GQLDazq1twzEB8+vq5z7MeFbhar2xhG2BIAiD4zDKZRxYIAmIYO4zEuVxkSfWzJxAam+ ACahRh8rTCbN2qelpQ6Ftb8WyPaSu+HuKNG2cztIvLzpIFn5cGk1QT54XyRTg4QKjyF9 fvVPk9TkdauhKvxh8nT1PpKE5Wy6B48B6PcTgAyL3bD8q89sRLk/1zmsxjveXW7xjeMW SI/n+Y3byAY4JObxgvyqw4hKMn0QMzUWlCv5GszoAIm6n21f343O6Vqq74AN+Z66EXre FfPw== X-Gm-Message-State: AOJu0YwuiGMgKuURLMG8ACNA2cYWuRcuANs3W7i2cPiitQupOOgDAI8h zOR6SkDWDqni2kuauqBbYwrW1hZq7QK7qGKctGRjFIQNh6Otm+oigWLzkA== X-Google-Smtp-Source: AGHT+IFwA2knURKD7BEEuWkeMYf1cDDh7ZR//eZ2tMgD2HzU2xmp1chYcDYfe8MHGgiNlcu3q3fmTA== X-Received: by 2002:a05:6870:15c4:b0:22a:508a:66e6 with SMTP id 586e51a60fabf-24172a5aa40mr1518114fac.17.1715303598141; Thu, 09 May 2024 18:13:18 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13:17 -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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee , Eduard Zingerman Subject: [PATCH bpf-next v5 3/9] bpf: refactor btf_find_struct_field() and btf_find_datasec_var(). Date: Thu, 9 May 2024 18:13:06 -0700 Message-Id: <20240510011312.1488046-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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 Fri May 10 01:13:07 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: 13660775 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (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 4738D13AA31 for ; Fri, 10 May 2024 01:13:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303602; cv=none; b=WP06qQAmAAZYyD2E04JLXQCUzTHHxv1SwfAKPfckSg+VULG/ymu0VWXdqhYitIHlVRILkr5bZ2Gh9y2PYQrf9PGD5jYp+nWHpkS2O49lrd7KRJN0aM4XAxH9wcCV2rVxMlR2GS5S2nsTYXhPPL2e3KjaVnEOTr0YWkMYjv+zjt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303602; c=relaxed/simple; bh=beUO5MCH98hk7B1W+3WxHbLxcpbk5N8JhsxgQEHPOpA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QA5NBzsFRulgYXvCU/GEruD1XicJea+jXeL14L6X/8Mh1K/UDOvl+9EXfSOd4VHSfmPGZzpaJPpx+L0AYF6w8vsy+07CQfrgDUAMrTZd5l7CJvMU/OQyY4FtZL0DzkpttHPWRmuw0RfHlEFw4VWAW9KjF4hxAgCnuru6CBcXxX4= 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=ROP2WRId; arc=none smtp.client-ip=209.85.210.45 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="ROP2WRId" Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-6f04ec17bacso968551a34.1 for ; Thu, 09 May 2024 18:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303599; x=1715908399; 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=ROP2WRIdGzkrjVTkE0YPH3AcofJu9gmOclhqS51qpU+UzKhe+GSj8z7CvdOTJfo2dC 6llcmhGy0XphNSoyFNxT+GNA+FvKvVIk/psAFRKKaQjIk1cuga3vYQbm6ZXLbzfe4wLn DgBa+8OESFInnkGTGJT8yU2a37TnVCBCvULBJWLFDljD5MJXFjY3Bs17XlxiU6/Pchqp emv4ZWXFIAhgHONln8j66E6FoAt73Gd8D+wFb2pSIzurE8ciBSQadi/cD/kr5kkksrPt jRGEtysA0xdNcuokira9Nro0HQP005M232wIAislI0sGE+OIjyUpj82wjigNsc1ldDSZ iwbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303599; x=1715908399; 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=sOgAlV32xGh5tprowM309sbyEu+bOnhoDEly+2np8iitY2XMquvSlVcQ/9YkU2p20Z aWiD/LSbMBsWSkQDYxXQ/6U5bO/FnCHecI2EUXhqm/vD4hkNuN9es3Viwrn+ulri0wBw pgmH2dVy9l1TXHObdpLmwfahJZaioEobakAJgtCJGY5RnmZ0iQEvdjNyH8PJg7ZLdAkG vKj/bTLYD9o4/GqN359qNxrhzo6QtNuf1cJADgM7RlNMc1hY9/KobFeOyoQZ+vWLDcIC fYG8QXnepGyIYAww6OLnSb6/Zsa35AwfkF41yJ9pElT2OvuPfCRKl/K8x5oaNVFdWE5b 7M0A== X-Gm-Message-State: AOJu0YyUSEa1R02DOL7zUQ/E6L7Gw3EDIxzo3n7xQO10Cup3J//M49kh VI7eOgTYmxymTfVFvHpjkvmrGfPVyOxyAxkQ5NkGMs1jOPLXvTD+2eFpSw== X-Google-Smtp-Source: AGHT+IECdL4r79GmJfG6MT6wYPicTclfHUAHaqdIJn+ZLdpDuSthi+IE0/XGlynoq4mUz3stipLFtg== X-Received: by 2002:a9d:7503:0:b0:6ee:ead5:25c with SMTP id 46e09a7af769-6f0e90f60e7mr1504580a34.1.1715303599203; Thu, 09 May 2024 18:13:19 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13: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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee , Eduard Zingerman Subject: [PATCH bpf-next v5 4/9] bpf: create repeated fields for arrays. Date: Thu, 9 May 2024 18:13:07 -0700 Message-Id: <20240510011312.1488046-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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 Fri May 10 01:13:08 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: 13660774 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (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 3E19813AA2E for ; Fri, 10 May 2024 01:13:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303602; cv=none; b=m0T/eo0i3LnhYGCCYPli3iWq7MNARmfoXkZ9CZEQg1PaJDadvg1howHJWbkcNCBxiOnPGJOCzol5NdkjEZY1LUUAST3196pprQRKqojJaJRKROPNF2MngGUJC3Fi0YXQAcQyee+4PxHrP5wuMCqM6cUNrEkdjyGIY0//rJMHSfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303602; c=relaxed/simple; bh=WIIY6y7bxxaCZ5svhHpHgrLd2cPGOQG9EuH0faAVJAc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cFm+VIJcNdVITFCmhoZOfspF7GbiqzUzmeR5OmqDrqXdr1tcoCezlQDgH9qBM0EHyhJVF6Lwmew7U6H6aj++5/PEmE+7CqdCp1glzNZFnaNYTHh/mRzA9J1u+nbKgDTkQJJ8umH56cVLJOVC+Uk8VKcyStiXgz0QERshx9U3dLc= 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=NF6n0HGK; arc=none smtp.client-ip=209.85.210.50 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="NF6n0HGK" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-6f06b81676aso869240a34.3 for ; Thu, 09 May 2024 18:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303600; x=1715908400; 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=GMjWdOewIK+UMq8dNEpTV+Ou7wl/z63UxnzhJFnwDoY=; b=NF6n0HGKDhGrOICjPG1O1W/YQgRtnYYobgJeyK58/gnyNzc4PGuQ+pgIWzAK7akXWC brOWn5haoLzTScnv36ty+snglst5AntcI+O81GO1WPSsPWRR3Y8tosMc9lzaqKECt830 ftAG9VeiNbFFC+IZjt7CrdWdL5DbC0+dDu2U3jcYSvMmZSsjjzvApZ8NVymQhLwxDor5 j5/x9Zi13+itj2pFw5F9eNVXvGcV1Db1+Up9K96rEUgSYK+bLtJefKbSlkGDsGaRt1Kc v56oGOpp628VSFAd0Xo0I5EsQi+8FROKV28+6mg9IJWPzw7mW8U7Y/yRoo4Vjrl97+iz V24Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303600; x=1715908400; 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=GMjWdOewIK+UMq8dNEpTV+Ou7wl/z63UxnzhJFnwDoY=; b=juUWySnyeWrMFEzR/Wb97pXIQg7QfM9CBxiSBEiGBCmX5e5VChFRjK62FTQiDfCf5R NIYzbZD06W/U8eTR98kFt1sWEIhuPtHq3pn3zYa+Bk3uFI+eFOTh2V8a3O1FwzOs+ghJ GrSvVcapB1W2yfSEkC8DyNgeSwlF1zHyOBognN3S6WceSBHTPYIifTw9GCUexdySi6IA xH6rsJQHtl0ItB/piZ+5UgvXwCdWSIRWPX389qooi+N1fyTKkC3hpAtwmwFPnZY7acx8 QZh4GwRC171UJUDdKucIgDsLo3RyyTNSIv5Kgq98H+plxqwEPgN+uHII4s9Iy5IQe19u 1M5Q== X-Gm-Message-State: AOJu0YxbdAJYQaMdADnvnEx9izqcgj9wdWcLn2blAQyof/bH2DXX5XjB r3NFiqOzeMSQ0TITH0v3WXWT7TOwltxw/nUupLmz3l5ElniK8MVeySclNg== X-Google-Smtp-Source: AGHT+IF0yg06fH2L3PTwWl3peOf7rdNz0wlfQO9YneJuHN0m3JOTPzMaiYk0oIGAlUjhhZ8bTuKKTw== X-Received: by 2002:a05:6830:1e61:b0:6f0:44d9:6a80 with SMTP id 46e09a7af769-6f0e92e9f02mr1388655a34.32.1715303600137; Thu, 09 May 2024 18:13:20 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13: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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 5/9] bpf: look into the types of the fields of a struct type recursively. Date: Thu, 9 May 2024 18:13:08 -0700 Message-Id: <20240510011312.1488046-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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. Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 93 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 4fefa27d5aea..e78e2e41467d 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3494,41 +3494,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, @@ -3557,6 +3599,17 @@ static int btf_find_field_one(const struct btf *btf, field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off), field_mask, seen_mask, &align, &sz); + /* Look into variables of struct types */ + if ((field_type == BPF_KPTR_REF || !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 +3658,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 Fri May 10 01:13:09 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: 13660776 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (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 3C48613A886 for ; Fri, 10 May 2024 01:13:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303603; cv=none; b=QtMk/R+uR9knzOSjrsxyreo44bQRnmwAtbVi1pveQs/TgFdR+fpXKk/dA51RjoNAIDywZ1M0AQKNYD9vt7a/a4KpA+8csjFlXxhqFJ9Y8imw7Gq88uinNUZPWgDF44q9O6oIiJ/WcFrinRo/JgfTvJCEuZFRxrPb3IXNnOlpaEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303603; c=relaxed/simple; bh=oEBCIVoFTOPMvF4duIpfQ87/eC0OSXnLw+AR/43RUT8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h2GqFFeywoEBhdsc9RJpLXmrlw4Sps5GPVkOiQIQfoZFJ7t2+LSzTexSo1ehi1XzF2h11C+3BRseXHzzxWxQgMy+qE1pOKXMHDWknxqRy5bh2oIfMfo+/mw5qGZW8KfYSm7mJrzpvDYQbckvqj5oWAWd3vgkokmZO+vYe7p+frI= 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=TsDphmXZ; arc=none smtp.client-ip=209.85.210.48 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="TsDphmXZ" Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-6f054c567e2so931246a34.2 for ; Thu, 09 May 2024 18:13:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303601; x=1715908401; 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=nHH9Xryb0FOuN83WbsuAD3l1qxS4FUWZMjvJox+s7Ww=; b=TsDphmXZtn1svsFkWTATewATzi+hZKcxL3AUTL8DhhGgUF3+mZyJ6XZQbhHtcILCbT Z0NUkT6AIbBRriR3C1XhMQ+YUaHt8PX3WJme7g440D1FunsTg5ygsU8v+5/dvTQflm0l n9zRLscOSqv+7VMZqfVe85g4VD8OBrimasLSk9UMN6boR69mB+xuUQvRnBVJtwaotY1w i0nTAu7BYHLLU1EdjlS42rz1b1toah2rWrTTBMPMIDAKDjBYKPbdNj5ERvHRGBIlexwi oaA0qN844qE4FZlDjIIHuSxj5o+S9lbjfbYSyvmSd+bisoKxKvdKUO6dZxSbN3H9h7K+ MVYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303601; x=1715908401; 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=nHH9Xryb0FOuN83WbsuAD3l1qxS4FUWZMjvJox+s7Ww=; b=rZxHnQYzI9vkJtfD9vKy4dC4D5PcuZ5Nmt0q7kOzmUGbF10LzuykdqQDgTdIZrygrM 5r7jCm4jfiVY3CWcX3YJsTRrv7GXca2UnAQBxRuzsNKfYV0XJquf3lyScs3+YkoQxTqN T3pqhrpI2UXw/k7RelQT2DDj5wqLYlzw8n95HZsm2j92BOT2dy44d2noRvnJhSfP3MqQ gLQKW3bpWbY0qgonsv9A+LLMoavCsOC+CUHYWRMRVsvwpx+yoGky/7ArH4A75931sDHm iBoxw27I6SbJZf1uWsTEJ3WIVjwrIsrFoO+exJWlVP/InCe5q3dNjSkg+s9QhDvsBwaE kxhw== X-Gm-Message-State: AOJu0Yxj+HWcCQXLYv189j/mP27gLGKxQl5kaFhQcqx7WwMsUXSDZg3e tt7cbuvGhkhaC/R/UhuP8g/kGdqaHsIJZzNhvtx0ewFvlGkdX8BH4tSS0g== X-Google-Smtp-Source: AGHT+IE7tJ84BTWhhrxFkmKpf59sf5wSCzNIri+gQieDg1AzTxug4xeBu2mAzj4xfEONBA+Jgnox/Q== X-Received: by 2002:a05:6870:2153:b0:23e:111:cf21 with SMTP id 586e51a60fabf-24172a7dd70mr1535957fac.13.1715303601148; Thu, 09 May 2024 18:13:21 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13:20 -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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 6/9] bpf: limit the number of levels of a nested struct type. Date: Thu, 9 May 2024 18:13:09 -0700 Message-Id: <20240510011312.1488046-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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. Signed-off-by: Kui-Feng Lee Acked-by: Eduard Zingerman --- 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 e78e2e41467d..e122e30f8cf5 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3534,7 +3534,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. * @@ -3545,11 +3546,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; @@ -3577,7 +3582,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; @@ -3606,7 +3612,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; } @@ -3667,7 +3673,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; @@ -3686,7 +3693,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; @@ -3696,7 +3703,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; @@ -3709,7 +3716,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; @@ -3722,9 +3730,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 Fri May 10 01:13:10 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: 13660777 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) (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 5BB6C13AA32 for ; Fri, 10 May 2024 01:13:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303604; cv=none; b=byd9xc85jn7cA7IrA+Fwg7wYXeSnvH98KnM0d9qJj274RnlemVHJ6UWQYYg5u1N5Nu5Ee9EsSIADApL5lvdQ9HJPKiJOB89utz7ecHunnDmUB/6l8YAvHeBip/DI4qknGpXBu79cYucgP9sa/BCWkmhw0Pf2L63K9Isdtey1R5w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303604; c=relaxed/simple; bh=Ma3Vu2MlOrX3+7gHJ0smej7Cz6EfF40UjlbMuQF7iyA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h2qEVkeAZlAK/tCFiE+bmhZvZsdnxi0Ip7qHJZ3s+E+NVNE/tAWwmWSpAjwjrzM13PsasGVpwTDRdK3oEYiKdTof2+uiy9K+kqKlf8QKYJtfj3P+QxjsgywtA4Kb53tloSioA3UoE39bDSYpMwNdttOwLNEjYJ3SHok5XHuERWY= 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=W3yPjMjl; arc=none smtp.client-ip=209.85.160.45 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="W3yPjMjl" Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-23e78ef3de7so813510fac.1 for ; Thu, 09 May 2024 18:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303602; x=1715908402; 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=WEkoRMBGu2GwDIk9DvRSHZhuDVYyTKX9czbFcw2JQz8=; b=W3yPjMjl0GSSR6S/TTMhIuPu8njddsfoX5mGzHo750Yp4V43BjAEa2++bno+W2dEHh PtIyv3XHv85IuaO+MMktJ92eXZuW5BTNzmr4FCFqgDwzxCFZCzrb0hZzgPAf82Wnpv4N RYn+ya12zFAAi3Afyvvu/wOzzhmBua9YTO6PlgR0gIs22pTcycyqWsTwhuuPLvtMmoWR wbFtSlHK1Hxt5wvvRpwNOmUIKrtubna431vu/83PnmbGLV+u5qnc+EgWm29jZbTjDUKw PNQUfu/grD2WnAlTxRu2MQHrgZqj5+/8TyXcMV8oyNWze58U7vPEEMV1VT1xmYHCId/u 9J9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303602; x=1715908402; 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=WEkoRMBGu2GwDIk9DvRSHZhuDVYyTKX9czbFcw2JQz8=; b=P3XvXZMC27eEQyqN+vD0RRi2X9LsFFt/7EO0zmfHRfUYQDaaIHg8cbQOT95NzB2NlF SwdR5aXD53c4JKn2esI3y2jWp/YBdhH7RFKLACO9D6iXzgCbtNnCRFfai3uKesUMRsay isIrrBFsxTt0gjeNKTFYZ0GtNAcYvpGQpgv0W/CbyQkant1ubbRARsr47M3fqSpmKrPq BgWnC3Uzsk3IB8+feDupxDnR5+SFEJjGzyu/ApZIJhxz/z6Yh70e3TzlbUT3SwHzmLyQ 36vgQp/0YLoikoQAB2dfWVzABfcV+zOE1wu/XQb7I1pDVI/qfavID4esoS3P7o6okO+V 81Vw== X-Gm-Message-State: AOJu0YwncGZGYe6MjWrZusr6pz2g/l8vxIlJ2GcQXxucLoQaDIDskzk5 68xCnk7NnQFLAj0VFdRi+gIuRLnQLearga0QuO8O6/uN0nFFUpf/5sYf9Q== X-Google-Smtp-Source: AGHT+IF0oGU2w+HRZ6H4ovlZ08e/X89Sw6xvJQPVGKpnO8XeqrtDFrdXgcolyeMNlUqRSAi5W1p/sA== X-Received: by 2002:a05:6871:54a:b0:21f:d2a2:7ff4 with SMTP id 586e51a60fabf-24172a9c787mr1815371fac.17.1715303602107; Thu, 09 May 2024 18:13:22 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13:21 -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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 7/9] selftests/bpf: Test kptr arrays and kptrs in nested struct fields. Date: Thu, 9 May 2024 18:13:10 -0700 Message-Id: <20240510011312.1488046-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/cpumask.c | 5 + .../selftests/bpf/progs/cpumask_success.c | 133 ++++++++++++++++++ 2 files changed, 138 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..0b6383fa9958 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -12,6 +12,25 @@ char _license[] SEC("license") = "GPL"; int pid, nr_cpus; +struct kptr_nested { + struct bpf_cpumask __kptr * mask; +}; + +struct kptr_nested_mid { + int dummy; + struct kptr_nested m; +}; + +struct kptr_nested_deep { + struct kptr_nested_mid ptrs[2]; +}; + +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) 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 +479,120 @@ 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); +} + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_nested_deep_rcu, struct task_struct *task, u64 clone_flags) +{ + return _global_mask_array_rcu(&global_mask_nested_deep.ptrs[0].m.mask, + &global_mask_nested_deep.ptrs[1].m.mask); +} + SEC("tp_btf/task_newtask") int BPF_PROG(test_cpumask_weight, struct task_struct *task, u64 clone_flags) { From patchwork Fri May 10 01:13:11 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: 13660779 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) (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 47BB013AA54 for ; Fri, 10 May 2024 01:13:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303606; cv=none; b=plEIOkBIn1FL3gfNLu9seCNMp+cpbc9Q12Zfn7OaWLRC008YS2dWUCBtqaW5v0YtXltz1T3BhD+jAZzQjXhuWbVUX5w1ecRASzOAgITB6VTB7puCaOV37yD/vcyJjp49S1dNbvdGcXg5sozAfrU/jPlUu11SeWytHnOdK4H8dic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303606; c=relaxed/simple; bh=184HSHS8K1pDQhxH155teo7Hn+PjBBhq6NMvMs9ANKc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fCvTOEXO0bx0Z6wOB/D1hIs4SP47nvoUs3QyHxiXRkJwGdOPL/EGRxt1QiqrnBsMG6KT3hJS/QvvFZE2uO2A6Xb6Ud3eGymablD8rG9NvFoW1TQecWBN61/vOMZt6FM7ZTKEndltfAuRdmNJfp90i73o0ivpeiWHgffZVx4KzvQ= 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=baLtnvEr; arc=none smtp.client-ip=209.85.160.52 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="baLtnvEr" Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-22e6b61d652so830538fac.0 for ; Thu, 09 May 2024 18:13:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303603; x=1715908403; 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=baLtnvErRVNdoe0m0WHOSCzEy/68LsMXkKEIFBWoT/73+nTVcXPsorCCICETzsctAv U4tUKcn25KkzWUnRRA6f4eEUjTK32p4YNcIc4vuYDUf5X+rtwDYSuR30qSxGO0dLlr87 f2MbFfP2mGufQUBnmT8lB8n0YrR8CYOWUAcvJe2DWiVfWKQLyXj4dRleGa8cLyTqcJyQ 6Rn4rS+TNFMwLPkQJinRv7WKx+AEoqaAyFEdN3mf+HYL0Rbw+woSlXsOdUX1J7OedVk1 3zSl89JN68sm9/n/TqpDhmDMwd+exvBBeSK7hi7aOi8SRBJusGqsXjg1QQ/S28gZc5Qj 05vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303603; x=1715908403; 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=V0JV3MSL0Uz9aDjVAUsDOqBlSactycNM/wMFqURGJOp+9og73FoLLKgm4gThbl90CE P2ZzkNEz+0AJRL5r+Gy531eVe271On2qWKA+VKwd4r3cV1Xp8EXwjAqAIYpGE+LoAQSf zsSRlV3BCbw2RyaqlUMrqK3dO5u75FcaxTgToSomj4PreUlzScXCDVvgeRRh2Rbcr9Al d99mNnTdHJW2RVSuJK9rU7P5b2GEUR/vXstrOMt3TZ1W6UfIwiPrl8rQxA6OoKNGoZSX d+BM+yhccoUAddUEB8TseKzQXi3jSjCyyEdtpOOVeclULnMSS7ddqL55wtl32uXjnFhv Ao4Q== X-Gm-Message-State: AOJu0Yzq7hkNnzraMOrIy0bhTgTRCfksNZXGuwryRBon8Y0bIIeP4M4y crtZxqm5Z9vspp4SP6Q7G9rNSxnIKFXah+ZwezYevfj1IA1FqScnDCoLmQ== X-Google-Smtp-Source: AGHT+IGNEaUqfIpH6yLWtWhfLulMJc7Cs7w5uNdPdjbj7ui5ocypNOXn2KgxVrsIkRfiYBC64IXAsQ== X-Received: by 2002:a05:6870:648f:b0:233:d4b3:9b30 with SMTP id 586e51a60fabf-24172a2371dmr1635109fac.1.1715303603078; Thu, 09 May 2024 18:13:23 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13:22 -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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 8/9] selftests/bpf: Test global bpf_rb_root arrays and fields in nested struct types. Date: Thu, 9 May 2024 18:13:11 -0700 Message-Id: <20240510011312.1488046-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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 Fri May 10 01:13:12 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: 13660778 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (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 465AE13AA53 for ; Fri, 10 May 2024 01:13:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303606; cv=none; b=E9iy/Oymn9B4hxRUB3ObaFDk5vwMQrit+evkbIE3gd2SNHWLPM6+1oHRbCdeIfvUhI+vBwUXqQ1waNDtp6xL58X1E5Mlqff5bTffHtAMiAOS4afZpT+wY45xpki1L456Zp74l5hSiEcG8x+fcLXgHmbg3PE0VynoYkttiEbo+WE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715303606; c=relaxed/simple; bh=T99aEuo7lB5fd7ns1ZsNRt0u99NUChuBc++AQ35NEN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BvZ+8M1shL3D+N7MloCtbBFuHpsQQE+3VjQuq+SLYzGDWI1/MZwvRloTLdxA8mJ3wv1frpPd3KSZE39K7GWYaFLxuoQbyA6Zf5BIIFT2CG0U6f5eG/iVXEdIWxI26lIN+pg0/gcRVFUTW9tQqDA936Wv1fsszf+1yxvcvBTnMoo= 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=QfOhWBoL; arc=none smtp.client-ip=209.85.210.42 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="QfOhWBoL" Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-6f040769fd3so965831a34.1 for ; Thu, 09 May 2024 18:13:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715303604; x=1715908404; 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=2cZSvfqk5wcnoVZE269hw105nH6/qA5LuJG4//0qzg8=; b=QfOhWBoLElOppsr3M2LIsYqJcr0TLkvf56kEVGYFD+Nti+5eNntE9uata+prbaCbcy Lih3/OIdP+qyTxK1E3nLO8xB393ErmYGMAFlzDUUQH+nCZ86gKtPiPNNpntskuBZ/78u a9FBlkTDU8Uf1kD/qpgp3ofXZMQAPGsmuKKl+/Pdzj3mmTzvMxVnu9DZtWsfN39PPvoA 1ZuFxKUFE+0nMRi0yaDLSIY+0PzvYlsd8FMYdMx6PvXOLZXeG5e5qqoQm60lnmj1Kwnh Uqq3rmaknnPf+ncm4lnzGTjsmfpUJlfm9nXx0qif24uxa76aUAs3HOB28E/QPALWSk6G Fl2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715303604; x=1715908404; 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=2cZSvfqk5wcnoVZE269hw105nH6/qA5LuJG4//0qzg8=; b=prQjAkSHOiaRTFZT5+OTVHd0XgqDq2ynupS8plVOSHDZv9OWQ3YvR/6X1P+3HX4TBG OuWZZ9OP8OqBcNWIP+Z+amK+uwnQEwjFAyQtFPm5ncGvznOor5eHJmqTwVWcpkRZzaQ+ PUsmJmmGPF/aGWcwXssnCk0dOAOQXc3a5Y0nxrHu444GB+xmypdpmVLGs0svcYN+/Rzu UdduageyxQFzWM2nOOICiQZczSa2U4ShNvJPSCG52sW2nQJ6ukRP6hiWVv63J85ObwAi IMnipXFD5YBIHAFgXaQ8JYT3m+F61TqojZ7VjiqjQei9ciecMuJqqENCW9d2s/K1k2w8 um7A== X-Gm-Message-State: AOJu0YxmGZjlIH2ncPQOShEQfqAc0dZimZkVjbDygKnIe8+X7bTzi3Bk L6pU8S0k1NCsLi9+AdQZ9x3MM7jBDj+y9PcSspB4K9fCcALc4p/AOA4XAw== X-Google-Smtp-Source: AGHT+IH8Yaj1t35h08RLcLTqNc1yIy7I1uKklObc6yYpn9bDZIkr3wGaZZ137t1fuVPqwJISsKhWYQ== X-Received: by 2002:a9d:4d12:0:b0:6f0:3677:8b9e with SMTP id 46e09a7af769-6f0e92ce9b5mr1356965a34.37.1715303604266; Thu, 09 May 2024 18:13:24 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-6f0e01a8b23sm476874a34.6.2024.05.09.18.13.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 18:13:23 -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 Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 9/9] selftests/bpf: Test global bpf_list_head arrays. Date: Thu, 9 May 2024 18:13:12 -0700 Message-Id: <20240510011312.1488046-10-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510011312.1488046-1-thinker.li@gmail.com> References: <20240510011312.1488046-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. 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) {