Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2878088rwb; Mon, 15 Aug 2022 13:09:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR5R8ht8/TVKouLRypZNCCExlUXrLTZ42/tNCz/HCvQQA4lfGYdFXfub/AFWwmfCN7Z8J1M3 X-Received: by 2002:a05:6402:f22:b0:43e:8623:d32c with SMTP id i34-20020a0564020f2200b0043e8623d32cmr16089259eda.265.1660594144796; Mon, 15 Aug 2022 13:09:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594144; cv=none; d=google.com; s=arc-20160816; b=09QYtP+wF13VCegkfRaHhgQjC4qrfmiQWYpktwwpZyqOjoNmGzKX59zhBQ9rXwxoYo 1sHPyiWyC10Yycq1QwoSJN5QDKn0HQqTDufx//M7OOUqcMDUru5UUR6VvtWlXS7HejCH mfJcu9w596Ch55KOhhFsGLnmR7boC6ZcXe8HGGukX6oo8Ix2jf44IdqxJPMH1rv2cH5I S2QzrHfiwXOkmd6UG/4rAyM3A+zsAN1TdpNTgGFkDpFWo1mSrek5euWm6TR1F+jKlgcP 8W08W+LWESQLtB9T31rEt+zFELdocqHSVXRX7kibKJB3WwKww68s11ACvbdf5Pp7YXdK 2CFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mcpKMPxKQ2ZuUc0m2ETQlH6yWVr/LKp5RpA/ZPD0oRs=; b=nBwdpgHl4X66nO7nBni3UMIAIDkrD384oDyX+vPfuJKiUIJl9m2EsiHTsCmOs7Fei6 LwmiwunzmDHc267ep84YxqrMDchJ94ptse7bwGTnvkvGtiixXGKUVcwsRhNoAuJmIrXe S1GxnwtphBi/2qsicemdi5aR+GikfSGtXSDKh/DTnH4ZAXJybIlQpbOiSjiZYIScT8pF WGboBImvDVxpWCjOACPyRctetyCOO51c7ukO+5Un8ruJfYmZRiMQeV/AonYVnByxpQGi /vuyJT7ihH5TA+UUFm1w+YGU0qSfAMtqJRJssU5Oll3PR3GCL4HafY2bwwhUQCVZICyl LXwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sB14TNc6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q6-20020a056402518600b0043c2919cfd3si9878367edd.602.2022.08.15.13.08.38; Mon, 15 Aug 2022 13:09:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sB14TNc6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243494AbiHOSyI (ORCPT + 99 others); Mon, 15 Aug 2022 14:54:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244518AbiHOSvW (ORCPT ); Mon, 15 Aug 2022 14:51:22 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77A504598B; Mon, 15 Aug 2022 11:29:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 067E9B81062; Mon, 15 Aug 2022 18:29:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F782C433D6; Mon, 15 Aug 2022 18:29:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588152; bh=IH/eqnSii+jb589bXR/fw04LvgxuSLSD1NZCXm8mjqk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sB14TNc6JkJSXKH9ZktVllYm7j6HHnlwNPkBX5aUAE1Z6xVIumZIhBkEdueSZKs8m SuUzbzQjGNkEyTexRlxNixNJ3MnvS0dwDsKDzvezO6jRQKtGqobbEkmBvz/7iQbBCk dYPDzTtmzSYpjwuRlkBGvIZWqKHYvrbmDQQbdnWA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maciej Fijalkowski , Daniel Borkmann , Magnus Karlsson , Sasha Levin Subject: [PATCH 5.15 311/779] selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0 Date: Mon, 15 Aug 2022 19:59:15 +0200 Message-Id: <20220815180350.586778586@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Maciej Fijalkowski [ Upstream commit 39e940d4abfabb08b6937a315546b24d10be67e3 ] Currently, xsk_socket__delete frees BPF resources regardless of ctx refcount. Xdpxceiver has a test to verify whether underlying BPF resources would not be wiped out after closing XSK socket that was bound to interface with other active sockets. From library's xsk part perspective it also means that the internal xsk context is shared and its refcount is bumped accordingly. After a switch to loading XDP prog based on previously opened XSK socket, mentioned xdpxceiver test fails with: not ok 16 [xdpxceiver.c:swap_xsk_resources:1334]: ERROR: 9/"Bad file descriptor which means that in swap_xsk_resources(), xsk_socket__delete() released xskmap which in turn caused a failure of xsk_socket__update_xskmap(). To fix this, when deleting socket, decrement ctx refcount before releasing BPF resources and do so only when refcount dropped to 0 which means there are no more active sockets for this ctx so BPF resources can be freed safely. Fixes: 2f6324a3937f ("libbpf: Support shared umems between queues and devices") Signed-off-by: Maciej Fijalkowski Signed-off-by: Daniel Borkmann Acked-by: Magnus Karlsson Link: https://lore.kernel.org/bpf/20220629143458.934337-5-maciej.fijalkowski@intel.com Signed-off-by: Sasha Levin --- tools/lib/bpf/xsk.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c index a27b3141463a..42b8437b0535 100644 --- a/tools/lib/bpf/xsk.c +++ b/tools/lib/bpf/xsk.c @@ -1164,8 +1164,6 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, goto out_mmap_tx; } - ctx->prog_fd = -1; - if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) { err = __xsk_setup_xdp_prog(xsk, NULL); if (err) @@ -1246,7 +1244,10 @@ void xsk_socket__delete(struct xsk_socket *xsk) ctx = xsk->ctx; umem = ctx->umem; - if (ctx->prog_fd != -1) { + + xsk_put_ctx(ctx, true); + + if (!ctx->refcount) { xsk_delete_bpf_maps(xsk); close(ctx->prog_fd); if (ctx->has_bpf_link) @@ -1265,8 +1266,6 @@ void xsk_socket__delete(struct xsk_socket *xsk) } } - xsk_put_ctx(ctx, true); - umem->refcount--; /* Do not close an fd that also has an associated umem connected * to it. -- 2.35.1