Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2260250rdg; Sun, 15 Oct 2023 20:17:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG9yoKkMMiup4RMShGiG/32GXmg71EnS6nCUMd1SPVtXoNwtSr78dc/16wKtDd1TBKIxwYz X-Received: by 2002:a17:902:864c:b0:1bd:da96:dc70 with SMTP id y12-20020a170902864c00b001bdda96dc70mr31023115plt.49.1697426268242; Sun, 15 Oct 2023 20:17:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697426268; cv=none; d=google.com; s=arc-20160816; b=whofttCWLymK8mPf1Bt7KpRTo8nKyuDsChwFyYfTIH21CwJC76Xuculcjl8ZKkOcgd iu5mJo4QakAQkP4FqnSF+5FYxlj6IL/k9QKEpV+hYxjRjJ6+Kb8PIZqphlkHBVChtWBc m8TJ+L4MCMHpMor3DmfiuUc2snq9BPJ88jhMh+mt4cBcsOB0kHL8m8CzDwIeKX8pceaH L6LWudr8mfIv3nwtDey1/eVakuxASbUV+4VGmk2mwohE1wzdNdGHNR3SVNhodMUrqEj+ dFYepE65I1JiFMaZ3mbvb3knOGVm6WzRfQMCbK84/2uWT7kOE+kJgLTUYxRiaW7y2W+K u+YA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=+gcDF6obS9msoNGbrvp8VjI3eEmOOKndQkCumFLGk4Q=; fh=h3EIZLNVQAy/dhjgTT9kvDyfk/IsWLENcLVZR2NKiT4=; b=oSY3aCijQxnQqvhH9GIb3JY7LfOQoPhmRqAV5NUkC2JK+yj7z/5oCFvUb1zRQpfYqE wM9fIZoKB3CAHwY0KhPf8OwaaMhqkAKhAnPDwq9LWtw2u60pUCTX1Ti+NG7EGmRRYTyN OP1YU6zGG/ScmZeehb0CrmIpHyeqXGE9miLgcx98x3lIo0PwIyXlliVibHpJS4UYZ8ex I8MPvHlBJr/W3241gsJN1uyIaGy2tfozkER5wfrJ0frFfOW/e+/fpCGDviMeKgVXQox5 /6eHFzD3sJjvaZgJ1uvO4Y0dFiVYAq3R9ujgcWVR2E0skeyrtzAr6T8sVvFK4tr000KI VIdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=efevaxTD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id d13-20020a170902cecd00b001c77e00809csi7656826plg.453.2023.10.15.20.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 20:17:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=efevaxTD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 11E3C808AB99; Sun, 15 Oct 2023 20:17:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231253AbjJPDRf (ORCPT + 99 others); Sun, 15 Oct 2023 23:17:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230451AbjJPDRd (ORCPT ); Sun, 15 Oct 2023 23:17:33 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D926DA for ; Sun, 15 Oct 2023 20:17:08 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1c8a1541232so35263655ad.0 for ; Sun, 15 Oct 2023 20:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1697426228; x=1698031028; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+gcDF6obS9msoNGbrvp8VjI3eEmOOKndQkCumFLGk4Q=; b=efevaxTDxwJGJGS0dUk31db53gixwaHeSqy55hTLRJd/aVWej4JZAd7FjlCe78XrUu KFbc9UKoSoKfH/+rgsc/4sg5jS2BqBL1boHp5L9b7SnrNH/K8bUJ25R+5y2BKQjW2Mmu 5N83kD9Qxp5KzbvznX55REYGDQxRJKTzs28+EeGxqixG5yduhCmnGEZELYmhFoWpaUKJ IXWjyDEqI9Umt/4MWUSK2muG/M4v3WDI3HvB4lQiLoqvNC67LXsQ3RM0pEbQSzAY3kyh Q8rT53qDVNo/+HCx/me0NiOgSMGEib+ma27B9KxYXRC3wCrHT/+6liqO/WiPcH9JFex/ Ue4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697426228; x=1698031028; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+gcDF6obS9msoNGbrvp8VjI3eEmOOKndQkCumFLGk4Q=; b=onFnQCnATUMbNtUIQ+zzY7rgas3WGwTexQwsTWyFrYh7O6ZfvpGzbK0cUur96mUsZY gXrGiyA63iDm6ud4g7/rvvrX3t1QV05mwbpL3OZ8FFh1rq7lGbmeIpsvcyoUjgpsqjbm Bp4qujhYdAeiaANCoXW5w7mBK386juDog7dh+CDXDhoJ18VnL20w+KJrCfyoFfi3nAN+ acUzAsDpyX0JDQyuZzcCLY4k/ccYPMEBkIBgBdiHUNFAjfGx+dIhXw4qTiOdN3bfjgTN AS008Chz57g6VU2jMxnqCLyghRpd9F5DLzdiPGoOurNw1TWDnnhJ2X6epAtlG+SYgKBY rMNw== X-Gm-Message-State: AOJu0YzblCRrsQZK95K2wB4Wkwv0GTfDOxPMZrtJnCIeaccW+dUYixWi GLIy1Z8s7HrjZZXQPkdq1q+i9g== X-Received: by 2002:a17:902:f14c:b0:1c9:e0f9:a668 with SMTP id d12-20020a170902f14c00b001c9e0f9a668mr8430661plb.18.1697426227817; Sun, 15 Oct 2023 20:17:07 -0700 (PDT) Received: from C02FG34NMD6R.bytedance.net ([203.208.189.8]) by smtp.gmail.com with ESMTPSA id g12-20020a170902934c00b001b9da42cd7dsm7320172plp.279.2023.10.15.20.17.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 20:17:07 -0700 (PDT) From: Albert Huang To: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Magnus Karlsson , Jonathan Lemon Cc: Albert Huang , Maciej Fijalkowski , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 net-next] xsk: Avoid starving xsk at the end of the list Date: Mon, 16 Oct 2023 11:16:48 +0800 Message-Id: <20231016031649.35088-1-huangjie.albert@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 15 Oct 2023 20:17:44 -0700 (PDT) In the previous implementation, when multiple xsk sockets were associated with a single xsk_buff_pool, a situation could arise where the xsk_tx_list maintained data at the front for one xsk socket while starving the xsk sockets at the back of the list. This could result in issues such as the inability to transmit packets, increased latency, and jitter. To address this problem, we introduced a new variable called tx_budget_cache, which limits each xsk to transmit a maximum of MAX_XSK_TX_BUDGET tx descriptors. This allocation ensures equitable opportunities for subsequent xsk sockets to send tx descriptors. The value of MAX_XSK_TX_BUDGET is temporarily set to 16. Signed-off-by: Albert Huang --- include/net/xdp_sock.h | 6 ++++++ net/xdp/xsk.c | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index 69b472604b86..f617ff54e38c 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -44,6 +44,7 @@ struct xsk_map { struct xdp_sock __rcu *xsk_map[]; }; +#define MAX_XSK_TX_BUDGET 16 struct xdp_sock { /* struct sock must be the first member of struct xdp_sock */ struct sock sk; @@ -63,6 +64,11 @@ struct xdp_sock { struct xsk_queue *tx ____cacheline_aligned_in_smp; struct list_head tx_list; + /* Record the actual number of times xsk has transmitted a tx + * descriptor, with a maximum limit not exceeding MAX_XSK_TX_BUDGET + */ + u32 tx_budget_cache; + /* Protects generic receive. */ spinlock_t rx_lock; diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index f5e96e0d6e01..087f2675333c 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -413,16 +413,25 @@ EXPORT_SYMBOL(xsk_tx_release); bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc) { + u32 xsk_full_count = 0; struct xdp_sock *xs; rcu_read_lock(); +again: list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) { + if (xs->tx_budget_cache >= MAX_XSK_TX_BUDGET) { + xsk_full_count++; + continue; + } + if (!xskq_cons_peek_desc(xs->tx, desc, pool)) { if (xskq_has_descs(xs->tx)) xskq_cons_release(xs->tx); continue; } + xs->tx_budget_cache++; + /* This is the backpressure mechanism for the Tx path. * Reserve space in the completion queue and only proceed * if there is space in it. This avoids having to implement @@ -436,6 +445,14 @@ bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc) return true; } + if (unlikely(xsk_full_count > 0)) { + list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) { + xs->tx_budget_cache = 0; + } + xsk_full_count = 0; + goto again; + } + out: rcu_read_unlock(); return false; @@ -1230,6 +1247,7 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) xs->zc = xs->umem->zc; xs->sg = !!(xs->umem->flags & XDP_UMEM_SG_FLAG); xs->queue_id = qid; + xs->tx_budget_cache = 0; xp_add_xsk(xs->pool, xs); out_unlock: -- 2.20.1