From patchwork Mon May 20 20:40:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668647 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (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 61EF2139583 for ; Mon, 20 May 2024 20:40:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237626; cv=none; b=OLeiEh/RHZKL78UKIOk7nRlIsLusA/lBB7sd42uo5V4WtugkCpwNgEiXMJxCgEXSA1jY0MIirfm2KSO3pCM8oYfKwR+DxMPDQx+raQR9aB9FPHNs0kbkGjTl2/nvWNyFlTFcrnjDBW4uOqjt8vmpbZGD1Idp73hRZDuA0ppUP54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237626; c=relaxed/simple; bh=qqggGaC3A0xzhmB4jpLtjhbNT+5ro/jXBMnCfYwbGzY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DuvZpRlzf/mOxO2wcDdABmyZc8RPHb8u0b9HcZD4/bzMGx9VxdjW5CXYE8xpwqH6KwqoPDGOxEXZPjW8Vv2DVHiunQAQDQnaF/EIC57VKNcDUCvAMHvZ6/sVzhoaQTvZajhGqWYc2ui4nxnFqbD56WvlFp+qswiDescNF2FO1pY= 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=Hna+16y2; arc=none smtp.client-ip=209.85.128.172 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="Hna+16y2" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-61be74097cbso29257207b3.1 for ; Mon, 20 May 2024 13:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237623; x=1716842423; 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=Hna+16y2aZeDbJD96z8tPpNLtew8E3nWnOcqBvGGW9yMjg6tTm0+aSvT5A+Z8faQKd v8hp3hMCvDxk2gyUovxS/SPLw4j9tLGuMI91AtsNbQxghcjxVlMKpNzF/rxFajaFEPXh O0dONf89xCO7SOUHGxUDWgPqZ+jisof9am0QOkHTeHf77adUDHO1yn8HBYHsaIPRINf2 6djubj/d+WguMC/O2iBpjOXRoCDfN1SGwE/NL4NrE7cNFoerY53KPW2ola3WXubX1iZW voaQpPV5y5+LewcKT+erU2uhyVnlNtTrLT5vHIruhVNLsv5DjpnWj88alH4kLm+He/43 VrpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237623; x=1716842423; 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=sp1QpfBh5C/HBBGdqKSgNqyZJSpVIf3av3Ie5cfU15TEhmYVhqZFFQzZ0IFmQ96Dfo LQIb6gYlZBHavHpIFfCPv/QmG+TldlnUSjAc+g7LbWCgpggbUUd2+EpbUXxmMzbE3LLZ EChMEdDY3L9ioflB+DxPtkbUSEW8yJrwBQ9HS/5Jq/hWQB2f4btd57K2FML5wX9M0Z4o 83ZVnS+rDK0laEuT5A0KX8fok6yX5JuoLexxMl8nJQnVEZDA3mIWtkrZpAzkxTrLUOAF ZBVgb7lg/vTIdodcXdSEPJBGxjbl0OLXUbwbe1z7eSmPubESttExtmw/5gkDFofbDUmH WClg== X-Gm-Message-State: AOJu0Yw7QeLSP3kdeORzdjUvFmMTqxPzlCLKBcWOR6c03LGGcdNypJZB BgdMFNoDhymOCp7EJ944neJ2q805lfSfyo8DRI6jJuxQ3e6M3sO8ErVAqw== X-Google-Smtp-Source: AGHT+IHY6/O6dqg+wfIx53DPTjADjP2trBupwXH6Fc9N99+e9neInMjYX6gTE+zzp/YQ0q+IFa2KVQ== X-Received: by 2002:a05:690c:700d:b0:611:9622:46e7 with SMTP id 00721157ae682-622b013a1a5mr293220397b3.48.1716237623159; Mon, 20 May 2024 13:40:23 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40: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, eddyz87@gmail.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v6 1/9] bpf: Remove unnecessary checks on the offset of btf_field. Date: Mon, 20 May 2024 13:40:10 -0700 Message-Id: <20240520204018.884515-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Mon May 20 20:40:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668648 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 7FFDC139588 for ; Mon, 20 May 2024 20:40:25 +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=1716237626; cv=none; b=YwsnXVA/HgQSRRMgnLhUyD8qicQVB+WJNLmriHeI4k3hDXCwiVKRkonwdbvQKcafhioBWes4kwHUtqw8EZtvKBnuC3WOsAWIjfT+esjVkMtDRdtqHXHYiacykYw7oTrbEP7+h9jfz3LsQzi/mlqaPaxf1eAKjmRcUKIaHQt0jmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237626; c=relaxed/simple; bh=2xfkYFldYvZvPKiTXacO80MKDZrrQYMBUHBKmZTcdbM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qm3CNqylEEVgfXHWxSsYWWBExNiKcbs6guowcbi9Mwvn3yNZ/kMcQlwVaygS18RPPYI2qxokp1WnoOuagnwoLILuOa++GrmlawVTy5xp1RQZTku6EKHDwGdSVxzhyRNv+fxjZqjEcvPvV+OqcU5IP+r77vPZVeiGgbPsP+HZqE4= 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=OL3dAuW2; 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="OL3dAuW2" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-61e01d5ea74so29914737b3.2 for ; Mon, 20 May 2024 13:40:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237624; x=1716842424; 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=OL3dAuW2AIaW6eOfgHz0Wc5FThASsUDXoOv9XUaGCAG5akLryFRFQMOcjEpFrgZF2j zmDULiz5G/Rp7HHzUcNtT5e9UzuUv8BeMdJ53NBg1RKoAotHMg0VfLljpV4FZVc8wOx1 ZoqI/Z4G6fsGIJ8kW1gbayVPpFxdadYBQlZZ1TmDxHQ0n7SZ29qmAU9xlV8BnmsI7UAS LtSVlDDUrI/g4+er41b3gE8fjbIYK6uoQCbWM0u7KXNhADCB72epBFWlMjtyPPvTEAnz WQ6z8uQbmUF7tqbN4aBsXhIvUuZS8yr3BFScpzDNKXIucmgNLvfxnUaQUoo3NkUgiOzg e++Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237624; x=1716842424; 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=pfq/RlQjXrDhN0YpnrCCb3n2cjlZzNShXjAvcXJtOpIiI7L2IY+dfYapOOzzW227HB twPiJ9skW3o5ZqwyY+ePlgvf81AfnI9SqxG8kNFF4V8VYiWVodpDE5uJf2doQ+ZP+iCT OZxX9rknlV+clMPlr7PHiXl3VjQPUP6JVVP2NmCh2/rcOPYIZyEDcVXrbgfNZDMm5Efq CzWEFHzBkGdpzauXJKfKMjBY406DUq5Zf7f68j1oS84Um7Rg0/Y9JsmulqZrW58jW5RL b7NPPOborn9JIqDXIzUoj9k3ocj7aOO2Zt5oMRWrNrOttPfkE+ZsjqvPiCUacrWPP8gw ABKQ== X-Gm-Message-State: AOJu0YzYYqFHfuRZQ/xC9nouL2Gxwz2ZIWRoGIxrjIZxlP7BBmSZ1f4E Bb6FElFop54d+1QibOYaEr+O/UyJe7p3rHfoSzd7axzdeA2lAdqSfQ4/Zw== X-Google-Smtp-Source: AGHT+IHAAh1je8VXT4vZQMx91Uztc9/y+BH7jWigqym0DefDM+Nhke5vYJOrWBCAyxkTn3gR0o9xSA== X-Received: by 2002:a0d:dfd7:0:b0:614:719a:501c with SMTP id 00721157ae682-622aff921e8mr276106967b3.14.1716237624353; Mon, 20 May 2024 13:40:24 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:24 -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 v6 2/9] bpf: Remove unnecessary call to btf_field_type_size(). Date: Mon, 20 May 2024 13:40:11 -0700 Message-Id: <20240520204018.884515-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Mon May 20 20:40:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668649 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 03528139596 for ; Mon, 20 May 2024 20:40:26 +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=1716237628; cv=none; b=kVLWNro8dfMlhiqWtQ+S4LjmE2gXit11LDBdATsSac1ArY28VSWy8jKWHj+V9wKBeZ0jyyWW2/ggobJ0dBCECDgngE3ZpmUi5kQzmVwcQGpzanwHU49ozHzmD1FaHddeqUhtzslELo3JQibGbP3rLYFnnPrmk8Al+TtASx9FkM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237628; c=relaxed/simple; bh=upVhmpmzSJmS20JqXVMvwrSbBc8DdAjci0We5oLNjcw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rFPef5KbGPT6LqESWEJ0EL0i+C/M3CYJ7xOa+V8FaS7G7rx1Ghe+bh418YFRvQmT9dtPv09Zg2NUaAOvy/r400FbL3tlVuH4S2Yc3+4TOdpi8Xv/njLhbhIbu1jrzHh+KvMOFVmFHe7aE+l1TMcegpjiIthk7CpRNgs3mAf9vkk= 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=GK71bz8i; 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="GK71bz8i" Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-61b4cbb8834so26033047b3.0 for ; Mon, 20 May 2024 13:40:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237625; x=1716842425; 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=GK71bz8itYKiBVSSS91U8CAaet5qrV740fcyY89GZCLbNlE3uD8VAtuDLECDcwx7k2 qUdthA7Y8tj/ongYUFCQWAnitB/O+N0iBMcKWH7Aagoere6WqbtQ8IOSUV2BOTazY/vn bku3iri44qE5gjiUNFnmg3bEQP0nyQbM/5xA9yVqHrne7ZR2t/CYS1oo28k4n1Pvc1bH g1jWGC0PZ7qOfbNM9t5e4enCyTlSXaNnXGWU4GynUwKhaDd4Zj3UDaBx/SQI4Q2SBbAt 2MkJ9SdLQ9UYSvTaDhQpLeuZJqX/kkp3GyKN23nHK6KJ+67GwGAgrQeEX7UTCTbxrpvO buTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237625; x=1716842425; 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=aAVCugLLHDYfg5MaoxHgJE+SmQ6qr/7Q3rC5o7sWGy2S4OBdSrvv+iqWcZPh3+dsuw syxHttZsUELSkzOUtGW163WgH6tDJw2ry6luhvJXYURY1mk80VXnWIx5ehJXYvPShuDR Zwz0K0eJGUukb+1UmSRUCVnajKa35PcXQPVceYAv+CacTevKd8RZsttMv2nFZ/sKtA9t 4TyfLXkJuSZc90lGRG0pnVN8F510RxUp7MsYZaEXpBQGfAUwxYtgONWADZSNBEN2+V8d DwIqSplUgMVJW1blZPI3gflbPIGeuigt1VC54bwMQpJ396YboQXpGJNA7QQqhR1cDTh4 tyMg== X-Gm-Message-State: AOJu0YxdGQl9NODK+ThJu6y3Wjt1OLK6dvcO0YMl1Y6T66n1uCJjk0lP 8S8xuZFgzls3n00hEEpgkPRmb35oMOOG7VTY8GTdmy6/WM7QyRn5dZi8eQ== X-Google-Smtp-Source: AGHT+IHR5wYW/JJtHqUVcYWKMqkysQ/fGUuXXQalCDjb6ZJawbyms6y+NqeBqp0wGs28HvOabg2UGg== X-Received: by 2002:a05:690c:6e0d:b0:615:1413:95c1 with SMTP id 00721157ae682-622affa71c6mr329972777b3.19.1716237625584; Mon, 20 May 2024 13:40:25 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:25 -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 v6 3/9] bpf: refactor btf_find_struct_field() and btf_find_datasec_var(). Date: Mon, 20 May 2024 13:40:12 -0700 Message-Id: <20240520204018.884515-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Mon May 20 20:40:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668650 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 09294139CF6 for ; Mon, 20 May 2024 20:40:27 +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=1716237629; cv=none; b=HUxx0rYL/hvorB2SPEdY34NZq5pGWxyPbboW81v9TfivZHFSkMOfeFi4AyX6Nc07zmkmZWv97SFNVfia0Ar1zw16lhx08RNa798tYUvnEh0GezbF9g2fvcLxYAXUmxNduPaayyf1mb9jiAWlTrYLW5HqHkJGEXXNR4qy/1TY/SQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237629; c=relaxed/simple; bh=beUO5MCH98hk7B1W+3WxHbLxcpbk5N8JhsxgQEHPOpA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oYETVRHMZBYqBDcKHgi6RRx13G1yzKNTnqK2zuVVwiEf71WzdsVaBsGO0dlSg2Eb8y2hblLnxtnDE3VGTRHqrkqe7tVJhFnOFFTlkmCVML1jgvwn+uXbMZ5gKFcd935bGxy/mcNMpkrwA9oinpjqnvBauDebs5rvnHm9JZGd6e4= 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=SKlOcvJp; 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="SKlOcvJp" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-61804067da0so28365497b3.0 for ; Mon, 20 May 2024 13:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237627; x=1716842427; 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=SKlOcvJpehxBS1jSMKLMpCIrS+HPtDF+NA2Q9As1lmIowftQQNY0Hvk8GsAgygi/fA mM7obL3R6wpKLacVKfWFstSc0dzJcM3llkx7XO1g0axcJ6MMspxI1cbpEUbacIDfLurX F/Z05JU6Uy7aInKdgv6x/wG1TDRAiMg4XBa/zz6ypjV96tCnDL+SC+OYPiV2wv+Zku6n +m97IW0IC+TsGt4ucY0eBfq2ARafleeKjiVY0hxrek7GkDj4+XYPtJkupKHia7Ld05/M pjNariJ2s9kekag9UdUAjLRNaN94ePQVh67i/3WXAxTH+zNyTNe/fpbZB7r2b/xTc7LS 3ltQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237627; x=1716842427; 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=U4Aj0cO65J2GsENZ0WForVwROc+rW8mPzao2OiegikGzaGWq0sIYpmthZ8qlGqmKZ/ qT40w7oATku2Wt5SbFinTZzM3Prp+F1qg4e2S3Wy8WbrasRcZPSL28kvYfY0S1o3tExI 8hLYDnvuEpjrUZXgv6hIQVNIr+U+2pATCgsGDQTY0MfxzloLVFgfh65K6HqViuJCKXWp NOlWm3bhvQhZzW57kONf8yIcW0nmAs+GPgYZ90bcWr0V9k8kzplJnYn/qjDptUPaR/sP BTcZWspW1Fmef37D1CLHEMpkEvOHQ3A4NkGVsXfbgzdVgtDPMF0zwPXgR5ik6rmLA5zG ajIw== X-Gm-Message-State: AOJu0Yx9SOlPf4uJZi8dKWMJi9n8jqVkfd60t/vw3KZsC7uK0BNAhU0V tqcph+Nv4nV9aO6O8SjtX8+5E1q3ozS/285u58ioMMY0eD14t6pSGG3BIA== X-Google-Smtp-Source: AGHT+IGzYtgMolj3fUhtd/IYGfsJK+QG/yxIWiegtbzEP7sE1ccEuDVJ8olZPgaagrVU7qj1kd5koQ== X-Received: by 2002:a81:91cb:0:b0:61b:3346:eb60 with SMTP id 00721157ae682-622aff9a7b4mr277295757b3.22.1716237626885; Mon, 20 May 2024 13:40:26 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:26 -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 v6 4/9] bpf: create repeated fields for arrays. Date: Mon, 20 May 2024 13:40:13 -0700 Message-Id: <20240520204018.884515-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Mon May 20 20:40:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668651 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 6DE67139D13 for ; Mon, 20 May 2024 20:40:29 +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=1716237631; cv=none; b=T5fb+Z0FGWcNmsem56Ky4nhOUpffjQ5wrPYrZWQNSdp1MBmcFiN4o9ZLBm05WGbRMruLmbeR4ZoPKGM1vYQZ4cgittfK+TnCuM3V5bni4Zihu9RWhndq8ZUXUMnGBU7FcYLM+LaVWuVyW+riXyZMX0mJwsRyoIwE4eGFva4MypI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237631; c=relaxed/simple; bh=WIIY6y7bxxaCZ5svhHpHgrLd2cPGOQG9EuH0faAVJAc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P1gMv7dr5529oogRtyjShkOf/fV0oRqz+1TNwdr8dDpx3YKGa/vrLudmw9H15o7u2fjxKttLVTlSQAoCXGfHDoJNa2TdWlua+73tO0JSN5k4Tx307uvBOT1ORJNSQ3+uICspO1tc+nFoPIgjOg2tzZFBKdTzcNeCK5ilOQJEEFw= 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=IKaxFnVg; 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="IKaxFnVg" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-61816fc256dso29575847b3.0 for ; Mon, 20 May 2024 13:40:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237628; x=1716842428; 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=IKaxFnVgRLWpyoBh1zxRUJMf1NSk5Xs/wWnpRY3HziVgFr6mqfrIXNhdp+eh3YI8sS XfTRarXGfvHJaUF4AwayUFNrKtdQ2X0AeHrBern6bDEBVPHD0md4LBEnKNrP1duH3zKP ASBTUuERr0cVenEfakmaRVQFR6WUrsTYJkj+Z8MfKRJbynFSa/fUIakJCbFNxPtpadrg e8YwEBUVj6umRL4d43V1ZK1s+FFglpYcRyT9bc3Lkj1qv4rZtnliFQPf/1U4IMMP5rx6 UtI+bDJVbx+70D/5WzJBZG5rJ8sdgWMumMMPMBYJUCQid9uDPge7OFH5Wi0ZwGqsU7x1 8qQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237628; x=1716842428; 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=m2cOQbPOCFR4f4adCV44RWvx4mYihI68jDKHbSOGwQ9Y1ZuB7xzIkh3AL52oWSmm8l 8kzTU4xACJmr5oR2AujSg2t84UrbQwFHR+xJuTWdTC13C5DOiv4jWk5lSNA9Sz0aHmKI ROGlu6mPJp7M4NFakTo75CBr6y+uz4k8jv4w2Gwb3mwRmJdGpSazjR2XDQl542Mj1+5A Ygz6XgMEv9dbPf+ZAS8xjFHG5GJMZvi3aqD6rqLk5IgTCw/FV9mZ2mdjU0edtnI/acXA jJ3sibe661+HJvlkEPb3IFAJD/6LeK1CRLJgrtDlhH/LoLXWj5wEqILop/9KmghCEX5n k/2g== X-Gm-Message-State: AOJu0YxZI8HtwDW4ZJo0rwQry5fYmn7C8xO8jAVyrfkf0gt4trARh2tO 7Bgk0x8/2T3f+ozIPZg9bXw1rW51biDNVcSvWaWIFM/3qVhdoQnCzPHDrA== X-Google-Smtp-Source: AGHT+IHn4ydVAtbOtdnyaqZNNK8lm1IntAgfdHxixqMSVLOoexsRAGcAE/VcF5zLPBpW27uNqY9AtQ== X-Received: by 2002:a05:690c:6109:b0:61a:e5c9:9fc1 with SMTP id 00721157ae682-622affc747cmr320314397b3.1.1716237627998; Mon, 20 May 2024 13:40:27 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:27 -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 v6 5/9] bpf: look into the types of the fields of a struct type recursively. Date: Mon, 20 May 2024 13:40:14 -0700 Message-Id: <20240520204018.884515-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Acked-by: Eduard Zingerman --- 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 Mon May 20 20:40:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668652 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (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 8088413A244 for ; Mon, 20 May 2024 20:40:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237633; cv=none; b=c4Z6I7ro9d3HRa82Zzhf8Uj0gfJMUsIlEasVLgwsFOGm6Jhkt2TvBrRDR2ts5hyDOWmXpu7n8fYsb398fm+oxCwxtGhgyjKX+1wHzw0nXyC/LoRZFRd9JQYz/Yrx44z1q8XqyeK0HERCAi8pi4prD9vjg14YNcMQO3XiZwm2ykk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237633; c=relaxed/simple; bh=07OA5mz2CKxyyTX9ZC3rbmToCVoTB2+Qp4mhwkQl0N0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fzcGn6uql0JVuJeLuhzhsMa7N+7/tQZX0GM5JsF0XkWuxOOKGJb+8n4C1f4robHaHI7KYldqEu1aPrZbnBMuWYT6fYJcME7ZAQA18goPai9OYC2iGdZyzdsnJZZ3+Gm6XzKsKFqUgjxdfbl579skgBfI8Fn7wwcgcPbLsw7+h0E= 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=J4FSCjJA; arc=none smtp.client-ip=209.85.128.175 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="J4FSCjJA" Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-620390308e5so28227497b3.3 for ; Mon, 20 May 2024 13:40:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237629; x=1716842429; 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=Y2loRtf+J0AsjQ+vogzU7bqnrDe3kMPAQWv4ggs8hcM=; b=J4FSCjJAKptY9K12Ac05giTs1HDl7ca/g02I/9/sBHoTjPs37iK8xsXGsQ7Hy/LqYv HMz2wq6I+aTCbCHOLfIjbFeX8W6qajX0ikiRyFi5sWQ9PSTMRpEno6lqEcM3uvddRQvk /pdmciWyozWfIxMGHuVfvMe6I/mHKB0EkhR/284YamSrR8eTLhMO4tBkRCFDTUjkNhMi XTo/6pSyda/RZqG6jlOkr2DY2F69vdVcX0vyZUW8FOOxS5BvVoZ6u8+449IZAIXGtpnm RxrMKITFDzcuX1HuCtABtU0wBK/qWZqi6xP6AJPeI7SjSPi1rLwZMyXmBKKKJPirqHkd P5PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237629; x=1716842429; 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=Y2loRtf+J0AsjQ+vogzU7bqnrDe3kMPAQWv4ggs8hcM=; b=U/PYURro9tzBa5ZDhbJf/ou1UWl3BazPrF84SkE4n0ExrAD/dkcXxfxoCNbfsklRzv ybZCYntJoG1/MgHh1diSYvywNlVe5H2ir9ZNgOPwfZ/XgZAwgPq2CBJUtKbvHtzfFEqU K6fWHCky+e5aJCPQJVY3JsD5nLk91SXKhBm0Lwki/6CyDdHCrnzLMkf2xRjlWi37uH2v ytlKMuD1fBLbmy/r7FHHPYE5AoaUe3pGnKxp7e5YuPZck8KHdwZtp0xLXTDK2od2bF0/ 3dCS1oINMNfMdMnPmrqfEPrSKFB0gBil9+CfOMYSgkuilu0EDtuT9R9Y+lRjYj7HU0qZ 2/OA== X-Gm-Message-State: AOJu0YzNXXoKRmszkNpqF57m0y4eGugQCJvyLgshAz7Yd+TTMO4x3pfa g3eT6HBBWHFKFzWbRE0quylboXLkMulzjbW3Me+OKvTu34q8oB4Up2Xuyw== X-Google-Smtp-Source: AGHT+IGI42YZ3NyH+ihuUFd8YIvLTw3NJJ6BW3gA2vOWv9SNYSP8Bb9FDX1iQqgdftcVjFvJgSO6XA== X-Received: by 2002:a81:e40c:0:b0:620:2cfb:7a0e with SMTP id 00721157ae682-622b001f352mr268324627b3.40.1716237629621; Mon, 20 May 2024 13:40:29 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:28 -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 v6 6/9] bpf: limit the number of levels of a nested struct type. Date: Mon, 20 May 2024 13:40:15 -0700 Message-Id: <20240520204018.884515-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 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 Mon May 20 20:40:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668653 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (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 A971D1386D2 for ; Mon, 20 May 2024 20:40:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237634; cv=none; b=tcPRy9tI8R7DRNUYMa62dJmziPVgMoBFH35EZzM6y4vRWCYhkhnnCfXrJeYLn+rGsdUyBalbjhqrfE/oW+zvpTe9F3xiv9t56y5xcb5kqmWjusKX2CiIA5R1YLa6KKYxCkooN+VSe+luD5Q93NZFbxVqfYXd7eJGXF1k5PGpoMI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237634; c=relaxed/simple; bh=r0+JAmKGIFR8lsu6SQjcnUQHdWtYmi7Ng4M01Jx//2I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YEbFHM0jjn1oNNfgGVA2RnSrEzqJ6Jepf69Y4TCLOS05yHZmQPHpI9jJu637tiDux6huiGQYKvZTjkFG2yx89fzlLiEViwHMI4hYx5ofVD6F34E8Q4CC6K01DQl1cO2q+gVsTQh91cgFr5vI/IiM6aX7Iv7+ocd6mD+plvlGCEE= 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=ms8gZjyK; arc=none smtp.client-ip=209.85.128.172 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="ms8gZjyK" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-61bed738438so29454977b3.2 for ; Mon, 20 May 2024 13:40:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237631; x=1716842431; 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=Ko5jJMQ3mtxVdqS7Q2cBb4KtYtlBJOAJ7SQeOBSmR7c=; b=ms8gZjyKKO2vG4TuJryrtiLvBu1OnshrebxOS7TzCahUU+9ghkRuhBGFdoEdKxlHPu Pa/BLo/oJNC9XbblLqfOhohGeCFz+4/ECMO0+kDA+gqhi3G0ylvjLeYkfvIhwGjMx1n/ zdWhSZg99QKEpW5Xi7CTR4qOPagggA0tKJVVGP6rvVKKFvv2ZqyRTZ+vG7bOUxTEI+f6 +MP8EwcM+Z+vVFMx7dQb50H/v41qYfVSgRdG83+4PAAANfMLvK4zSRtkCW2BYW6j37PV ihijaMMEFNm1n6jgIyRDKskmxBP5qYFab4tfd0/RNFwwsVjG4Li6ar3xQ41eNI5w1pTf Ua7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237631; x=1716842431; 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=Ko5jJMQ3mtxVdqS7Q2cBb4KtYtlBJOAJ7SQeOBSmR7c=; b=QTSQC/dPtBBZ5rYR8APhd60ielx1PxaIzNMSS88dFRMSCHdLO4d0MgfYHCq67v+Mbk 3JWO0jMKEx2sHVYxnkm7T12v+oHfOl6F8Hnr9TmrhWcAUmn1tVFRg71WcvS7vYlAj6/f HqxqisiVJdYIqM1DJeQFDIt5h8Y8FVtX+pDiu7vM//OozsirTVV8A1WAvOwK8PtGGfM+ CxT7PVDJ+sYsEgQZSDPoWu4xENPoFs+tpsQ7TV/iliBfaMsc1+Z3JQYiWTkCL2lB/hPL egkwxl19HYdKkMNcxAcIeDCuXnhOhIYlPOgjoQGnnUtZpGF/MFlbVo84/qoC/rEfG/T3 UDYQ== X-Gm-Message-State: AOJu0YzyzXjEFqkpsvo1c7vC6K3pDnZsXXHZu0LO4LdEND07MdKpM35E v4mWYXzFcNWgzXC5E2s61MzY3ilnCJFpnaE0KsQ2Nma2hjHxdSg4QMkqWQ== X-Google-Smtp-Source: AGHT+IH2LYPLjFqtbStLgZYtXLmdk9hPJOZ0oqM47dIUUCM/n08rcJ1+BUoVBh4nxhgKzl9ZB6niIQ== X-Received: by 2002:a0d:ea0b:0:b0:618:ce0e:b915 with SMTP id 00721157ae682-622affc3bc9mr302045377b3.27.1716237630787; Mon, 20 May 2024 13:40:30 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:30 -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 v6 7/9] selftests/bpf: Test kptr arrays and kptrs in nested struct fields. Date: Mon, 20 May 2024 13:40:16 -0700 Message-Id: <20240520204018.884515-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Acked-by: Eduard Zingerman --- .../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 Mon May 20 20:40:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668654 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 72ADC139D12 for ; Mon, 20 May 2024 20:40:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237635; cv=none; b=YGc2Q0UvsFO9T+oQN1OsNOgC6z7wJWorNm38369z3CUlHQpKh/87sJqasv3XzUJ3wCsFd7c26ArDcxcITUZijQXcJc1NTwzzqkmKI0gcD5h0TvTv/enkHb9iP0gKy3bePClO+WdTaAKTHN3CAcXs4Gd4BrbND5pMg+U//3iBGIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237635; c=relaxed/simple; bh=184HSHS8K1pDQhxH155teo7Hn+PjBBhq6NMvMs9ANKc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cMeKscYORPvssQl25n38WjMDA186EKys1FmNKAImg8KksZsiZvBoCoilLWYXdJFy6ooqSZRRfIMzfyxy2eyGq2nuf3/m/pd7G6BvfxI8eFhMUmdNcW5QKAJqL+WZBAzwqNkFBbOqMHiuuDKFKj/CCX82sEUza8jKNX9fnTH9fQo= 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=jkuy0knb; arc=none smtp.client-ip=209.85.128.171 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="jkuy0knb" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-622f5a0badcso32782757b3.2 for ; Mon, 20 May 2024 13:40:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237632; x=1716842432; 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=jkuy0knbgMjXeVY7+5zsr4Dx1s/xXaAjLpDXQtS9nhfneOlvGcmRR2rM0ytwJYrFgJ Y54U8aLQNhHq3B+vydk5hZy7Gd1mv00RBwUmKZImtboN1jzx7im8CUrS8oIaZNdVKM8H phxJkQyTowRN9RDDBjyf565UcD/ANxh2knEnDwB1qfufL19LNEKmVm/ffT4DDKSAaNDa R8gsUAFq4/Vu1mWhQFpKPSnTj+gXb5+6hkEdAktirDLSjUkioHOZOsyArSxVGBb1YlDk 23LNZy3fRd85qW7TARkxe1Xm5sZRGWEdUfJ2Cm6RKGWtLI2lu5A6lTcshVt2Qxv0RgJa 5PuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237632; x=1716842432; 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=QebLf5ykk2Ym1fVgkUiMgcm1WBxLDsKs4W8Hi3iLw4OShZDn5TKzCQLyFefwjnlaW4 XALqbgaYzg6Q7DiXij0kJC5PVdocmoHf6Qhe1XGzGqfd/hxzDwyizy8LPdfLR+kCvVv1 WPcRDe8ehieOyfK2/T1S9YrFUMlAISjcMpTYW2Q0B3kYCcyI0/qI2eZ8N29C77GqEsv3 T9mBTcm2bur7eTMd3aWxbpRsEJcUJV2ReP9NFUK4ca0WJvJZqmzyzwJxqvBkBAjP+pEK SdDfEydY0Qi+RVm0DmIouUK7zpa7iI9e1IiPLrKgZ6+d1ICqhaEjDIn5x5uv7KQoV/eH c+JA== X-Gm-Message-State: AOJu0Yxi7OLdWHWAuItpPBGR0NcFxAiqWp3ghB0TpSJe2f6Ei7bi4eEw 91j7fyTs4fCY5xJBW/3nmQEampkdZsLF/8A7+LPaMGkpmx8xLyKe02UfzQ== X-Google-Smtp-Source: AGHT+IEK/8q9+X8rqZHRBt2CzIKXato2rrJ+wtlyCHrq9T9c4m1Wdw4lDr00qvcsqW8twTisKco06Q== X-Received: by 2002:a0d:e6c9:0:b0:61a:d6ce:487a with SMTP id 00721157ae682-622affe1e3cmr288196167b3.19.1716237632202; Mon, 20 May 2024 13:40:32 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:31 -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 v6 8/9] selftests/bpf: Test global bpf_rb_root arrays and fields in nested struct types. Date: Mon, 20 May 2024 13:40:17 -0700 Message-Id: <20240520204018.884515-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Mon May 20 20:40:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinker Li X-Patchwork-Id: 13668655 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 A7CBD13A24A for ; Mon, 20 May 2024 20:40:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237636; cv=none; b=VA1Dg7VOxNbrunZO6bHHgIF3tyRF0aQUGbKTkaZtw7dO9OFkzUlyPQM5UCL3H9bkqZv0gxgVziVD73S7ggiDnle5ArsQ0y+vOZPWa93SYICmw/w2imhDcsR03sFDe3IDO+C09H90+pl1I2Olb6pnPrsQpgyUe9tjGFFIUecN2i0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716237636; c=relaxed/simple; bh=T99aEuo7lB5fd7ns1ZsNRt0u99NUChuBc++AQ35NEN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HXwt05OV4+DBAGCqFVw6VKf8ziJBqimN+qtX+aygipgcQ4ZqymNdiSbNTWeFoSVVAxg9IPASHoT38D74TRQOUafD5mL2PCF96sRxEEhaYdSrSfjfBQR173XW19Wz+1miU/1+R9yHfMfu+m1FXwEcms5UtRKDcK6MjfHjI6pj67Y= 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=Rnw3AeEq; arc=none smtp.client-ip=209.85.128.176 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="Rnw3AeEq" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-61df903aa05so31609767b3.3 for ; Mon, 20 May 2024 13:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716237633; x=1716842433; 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=Rnw3AeEqcBVkvOVg07zNf8PyZVJ75USQRJXvARQzhPWxMNEL4llgEXYu+PXF7K+xei hrFCzGHU2lKIu4grWlkoj9UG22YcyyMG0dOIf+7f/YbxSUmh/1VAak/7jR+1ahKq5hh8 guATp96+XQP84rWeqTi5FUeD6uhhgiFlSTDUP/nzveLVAYLtRj44XI8knmfks1nE42A1 3+TgkvJ0v8LCiq+ferTf6qAYSCqvMIn64+x9V/piCQog1FHD4f7lJ05iPDljyiLwTObn yjjYQLmzCn5qoODldXO+BanZFs1LvCapV+e/andyqrDq/ns6AMOKr6H8GSGBdcQbEBD4 qwNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716237633; x=1716842433; 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=rCssTzYfinHf4T5c5kQzTqkElgvxZW5rcheRFLrKkfADcCS6OiiGsVjzZgN4YCfqoZ 0S2g5nMG/HODE4KN1qatmkNjZvJEjZrjRjRcDHLQRO/aEULVuBXHJoAp5fPSoa5IAqxX wYE/wKa0CKTyBYQkWJ4mb4uCZlG04VPa9LVhqE8TpsNkFzhnTAKh7slHt3KEzWIxiK11 +y+tJ3cMX7kCDVz0KG0brFGAJ059wppUgTbmDRHkOOPbyLizrAywdjkmCiWq/H3h6phr QgjO827KtuePIpUHOwRkPUJyZOsa2I87Rmr/5YmYsKqqAyPmdtG0xQZxmqWhGG8HYNGK 2RMw== X-Gm-Message-State: AOJu0YxzlBBqcLD0pHIipGFAkAIp5Uum0QeTfWo/ZM1PI8A6lvsVqbs7 CQJTwLwiUkxg69AiG4pe/jaM5PZ77G+LMgRvQyIq3DeEPPVCkcb0+pbAXg== X-Google-Smtp-Source: AGHT+IF7IpFuzRhjpHu2dLYJiW5TDyFmo7l88wdXmy537KbnjyUCqcWHS+y8ddjklI+PPg3xAFFolQ== X-Received: by 2002:a81:ae5f:0:b0:61d:fd3e:8e8f with SMTP id 00721157ae682-622affa8b29mr405852377b3.25.1716237633377; Mon, 20 May 2024 13:40:33 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:764d:6809:5ff0:b5b6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6209e381afdsm49684267b3.127.2024.05.20.13.40.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 13:40:33 -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 v6 9/9] selftests/bpf: Test global bpf_list_head arrays. Date: Mon, 20 May 2024 13:40:18 -0700 Message-Id: <20240520204018.884515-10-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520204018.884515-1-thinker.li@gmail.com> References: <20240520204018.884515-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 Acked-by: Eduard Zingerman --- .../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) {