From patchwork Tue Nov 14 04:54:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 13454770 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BC7CBE4E for ; Tue, 14 Nov 2023 04:54:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="O0GmaRme" Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBBBD18D for ; Mon, 13 Nov 2023 20:54:57 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-6c415e09b1bso5187571b3a.0 for ; Mon, 13 Nov 2023 20:54:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699937697; x=1700542497; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TXzT+3Ak0JHrKHlBli6RvkJW17J45KrF6p/rzIgNF8c=; b=O0GmaRmeNQiQ2LvSTBr55IN7K1uE4P4WoSuezBpptmTBMkO7KBxQXRc28tRU9pgk2E q7IgDYpZ/erl+k5OizEdZNWvGrnF9Z+b3dgcctHSWOjxDgYRWCw2xyMoX+ABXNA05fiq soOmig84vJzWfaN7rl+EZNn5Y5o6ITrQ4xsxtXNOaLTd04DzeH8oLlkX/9kj/sskIIto uhXWwM8YSrypVeQYCS50dSVWw0TWQ3cok7VT/c1Tb6IHm93eGs+fYB9GsFxHo/NzAnKL mE6s6Ny9yxpW3UbYFIGxgh8JmySCg8Epw36T+oGlaCw8tMkO54plAxNx2qL8r3gZDxqE K1sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699937697; x=1700542497; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TXzT+3Ak0JHrKHlBli6RvkJW17J45KrF6p/rzIgNF8c=; b=IccKdvzfqhMMGF53PTDtFk+gMxz4Tm1nSOY65AOz+tM0zQPqXC0CkSRI6rpglfSocE bjsQDOj9Ywo/z4E3n5/SpK8g1pTccebhQG9hSSFHlETYH2a7eTAeRTBlkPKdEOFkzJ9D A72NRXYrotTstXmtj/tR+UPl+QG4XqsZd1dLt7wPxpZPgpBmugZgDJWLGgD+AAPk4N6s YuNXWX0M58bPtuo86DfCF1SptGyXl5Km0l4OjMPlurjgHajXJBWLuCdlFMkC4Q3ik2mw HyvpLaYEktZKA5bmCm6t26b0cMu9iNjujjzHgQm7cLVhY3XYO6Tzk7EBAwn3/3tn97N6 9MrA== X-Gm-Message-State: AOJu0YxcWyTcOyeJYPqQgvYJyNtqwi9iK7mR2Qj7DOnKUz0OyA6cm/u1 xZHd0dcXtsLIc+PmzncUPWY/oWAMy7q6dy9qaDk0P9zILdhLzbvvoVBHoTPlRt7/KjcY3k+pDKb SEk3XGZ20RXAc8Wk8EnCijGbDip49b33siJLxwfBZrdqERNRlYQ== X-Google-Smtp-Source: AGHT+IGWF7XSwqLBEK1wJwVpXO9lb1cDKDlPlaS4aLH7mH/s7E9StMeoY6zfvu0D4aRcQHugPQG5prA= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a05:6a00:93a4:b0:6c0:f86a:8d85 with SMTP id ka36-20020a056a0093a400b006c0f86a8d85mr2965314pfb.2.1699937697188; Mon, 13 Nov 2023 20:54:57 -0800 (PST) Date: Mon, 13 Nov 2023 20:54:52 -0800 In-Reply-To: <20231114045453.1816995-1-sdf@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231114045453.1816995-1-sdf@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231114045453.1816995-2-sdf@google.com> Subject: [PATCH bpf-next 1/2] netdevsim: don't accept device bound programs From: Stanislav Fomichev To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, Dipendra Khadka , syzbot+44c2416196b7c607f226@syzkaller.appspotmail.com X-Patchwork-Delegate: bpf@iogearbox.net Commit 2b3486bc2d23 ("bpf: Introduce device-bound XDP programs") introduced device-bound programs by largely reusing existing offloading infrastructure. This changed the semantics of 'prog->aux->offload' a bit. Now, it's non-null for both offloaded and device-bound programs. Instead of looking at 'prog->aux->offload' let's call bpf_prog_is_offloaded which should be true iff the program is offloaded and not merely device-bound. Cc: Dipendra Khadka Reported-by: syzbot+44c2416196b7c607f226@syzkaller.appspotmail.com Fixes: 2b3486bc2d23 ("bpf: Introduce device-bound XDP programs") Signed-off-by: Stanislav Fomichev Reviewed-by: Jakub Kicinski --- drivers/net/netdevsim/bpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/netdevsim/bpf.c b/drivers/net/netdevsim/bpf.c index f60eb97e3a62..608953d4f98d 100644 --- a/drivers/net/netdevsim/bpf.c +++ b/drivers/net/netdevsim/bpf.c @@ -93,7 +93,7 @@ static void nsim_prog_set_loaded(struct bpf_prog *prog, bool loaded) { struct nsim_bpf_bound_prog *state; - if (!prog || !prog->aux->offload) + if (!prog || !bpf_prog_is_offloaded(prog->aux)) return; state = prog->aux->offload->dev_priv; @@ -311,7 +311,7 @@ nsim_setup_prog_hw_checks(struct netdevsim *ns, struct netdev_bpf *bpf) if (!bpf->prog) return 0; - if (!bpf->prog->aux->offload) { + if (!bpf_prog_is_offloaded(bpf->prog->aux)) { NSIM_EA(bpf->extack, "xdpoffload of non-bound program"); return -EINVAL; } From patchwork Tue Nov 14 04:54:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 13454771 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFCC9BA45 for ; Tue, 14 Nov 2023 04:55:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xxvjB+WV" Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D5C5A4 for ; Mon, 13 Nov 2023 20:55:00 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5af592fed43so61731817b3.2 for ; Mon, 13 Nov 2023 20:55:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699937699; x=1700542499; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xHw9c1x/1MrkMxN4PZXPTW4efkpHjKlYOHaTk46Psr4=; b=xxvjB+WVyoR9xN2W63eGBy5PE1XSrzv4tQt7oOjmakVOLiV1qvNXZ9VNHArIY+CsJb FGDe6zlWZO7fU72ds1m5fHQxs884hYEwsYr2p1uT5IayTb14vOZEi+0nYJOPkWdXWFUM /mErUfAUSGtg5J71YfgH0NGGB4bLNL4jk3F+xG3i6GaBXUAT2Y2B6SH3i5AOPzfTZD4J 0CeAFPGE/Y5nJ3sD9qcHufPxvl2l2Jjk1GfzwUTCKGns9bbvQKksrJewMjAkfgJOshmU FwgmZWMTf/v/b1BMJNrY60ouD+XuC+VKu0+q6nUGDwOBymNAwtzdcHKk0diel0pgsS0E +suQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699937699; x=1700542499; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xHw9c1x/1MrkMxN4PZXPTW4efkpHjKlYOHaTk46Psr4=; b=JUBskt5BVgAG2Kihq4eRLiXevkoo4UVcPATLIo9o44c/dBsBzMev4mE8q6FJYjNBBe 9Oaq1VfVQJBzDrVoChmCVOqFA7Qmbx7u0S3eEYHsoJXBae3/a8pXeaJeeqv962LVoR3u H2zUAkt2eIKhkJmwEjzaRfvg/Ywe7KiUPsQkv00eDLzht1w1pH2sjd0jiuCHLT6bdIt2 GKtcC+4s8sIHMWjecQCuidJ38TLlD3AIadhqcAzBUw1VYz6k7cwmQmVHmrNaUVrwiK9Y uWelSI7dyFYwT6K7Bh1QpjjEGYyUHbTAydiTrzVozHZMj4B8EOi9gYl4/Aw8ieK8zQYw MsNQ== X-Gm-Message-State: AOJu0YzoGyQM7UxRmVZAe6cBTCxya/BLVOzHED17lDXLsRnVeh0fYfKm 5pkEbjUhaI/13vZZuES/A+F/60mnIFSfBFBxBaLSEsaJZbQTeAuMVbK+NqC/N/pp8pDpx1vnWZA HnFokgVB2s4NzIFcZapOZsaS9+RQXcCrR6UHbr7noxG5LF1FJSA== X-Google-Smtp-Source: AGHT+IG4r1voAGEi9crD9gREjLeW/8OoJQZBOb+nEPlqefEdITsoiFaKTw9vKoTI2Vczqput0kGzjDQ= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a25:aad4:0:b0:d9a:6007:223a with SMTP id t78-20020a25aad4000000b00d9a6007223amr189049ybi.8.1699937698880; Mon, 13 Nov 2023 20:54:58 -0800 (PST) Date: Mon, 13 Nov 2023 20:54:53 -0800 In-Reply-To: <20231114045453.1816995-1-sdf@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231114045453.1816995-1-sdf@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231114045453.1816995-3-sdf@google.com> Subject: [PATCH bpf-next 2/2] bpf: bring back removal of dev-bound id from idr From: Stanislav Fomichev To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Commit ef01f4e25c17 ("bpf: restore the ebpf program ID for BPF_AUDIT_UNLOAD and PERF_BPF_EVENT_PROG_UNLOAD") stopped removing program's id from idr when the offloaded/bound netdev goes away. I was supposed to take a look and check in [0], but apparently I did not. The purpose of idr removal is to avoid BPF_PROG_GET_NEXT_ID returning stale ids for the programs that have a dead netdev. This functionality is verified by test_offload.py, but we don't run this test in the CI. Introduce new bpf_prog_remove_from_idr which takes care of correctly dealing with potential double idr_remove() via separate skip_idr_remove flag in the aux. Verified by running the test manually: test_offload.py: OK 0: https://lore.kernel.org/all/CAKH8qBtyR20ZWAc11z1-6pGb3Hd47AQUTbE_cfoktG59TqaJ7Q@mail.gmail.com/ Fixes: ef01f4e25c17 ("bpf: restore the ebpf program ID for BPF_AUDIT_UNLOAD and PERF_BPF_EVENT_PROG_UNLOAD") Signed-off-by: Stanislav Fomichev --- include/linux/bpf.h | 2 ++ kernel/bpf/offload.c | 3 +++ kernel/bpf/syscall.c | 15 +++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4001d11be151..d2aa4b59bf1e 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1414,6 +1414,7 @@ struct bpf_prog_aux { bool xdp_has_frags; bool exception_cb; bool exception_boundary; + bool skip_idr_remove; /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto; /* function name for valid attach_btf_id */ @@ -2049,6 +2050,7 @@ void bpf_prog_inc(struct bpf_prog *prog); struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog); void bpf_prog_put(struct bpf_prog *prog); +void bpf_prog_remove_from_idr(struct bpf_prog *prog); void bpf_prog_free_id(struct bpf_prog *prog); void bpf_map_free_id(struct bpf_map *map); diff --git a/kernel/bpf/offload.c b/kernel/bpf/offload.c index 1a4fec330eaa..6f4fe492ee2a 100644 --- a/kernel/bpf/offload.c +++ b/kernel/bpf/offload.c @@ -112,6 +112,9 @@ static void __bpf_prog_offload_destroy(struct bpf_prog *prog) if (offload->dev_state) offload->offdev->ops->destroy(prog); + /* Make sure BPF_PROG_GET_NEXT_ID can't find this dead program */ + bpf_prog_remove_from_idr(prog); + list_del_init(&offload->offloads); kfree(offload); prog->aux->offload = NULL; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0ed286b8a0f0..bc813e03e2cf 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2083,10 +2083,19 @@ static int bpf_prog_alloc_id(struct bpf_prog *prog) return id > 0 ? 0 : id; } -void bpf_prog_free_id(struct bpf_prog *prog) +void bpf_prog_remove_from_idr(struct bpf_prog *prog) { unsigned long flags; + spin_lock_irqsave(&prog_idr_lock, flags); + if (!prog->aux->skip_idr_remove) + idr_remove(&prog_idr, prog->aux->id); + prog->aux->skip_idr_remove = 1; + spin_unlock_irqrestore(&prog_idr_lock, flags); +} + +void bpf_prog_free_id(struct bpf_prog *prog) +{ /* cBPF to eBPF migrations are currently not in the idr store. * Offloaded programs are removed from the store when their device * disappears - even if someone grabs an fd to them they are unusable, @@ -2095,10 +2104,8 @@ void bpf_prog_free_id(struct bpf_prog *prog) if (!prog->aux->id) return; - spin_lock_irqsave(&prog_idr_lock, flags); - idr_remove(&prog_idr, prog->aux->id); + bpf_prog_remove_from_idr(prog); prog->aux->id = 0; - spin_unlock_irqrestore(&prog_idr_lock, flags); } static void __bpf_prog_put_rcu(struct rcu_head *rcu)