Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2318220rdg; Sun, 15 Oct 2023 23:41:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFGbv68b/MKkW3UuYdp1Pbrk2IL4g4u1oVjzaeZ39sznLo/Hpoj0TxSVrv48JoRKw/x10NV X-Received: by 2002:a05:6a21:3284:b0:16b:79b3:222b with SMTP id yt4-20020a056a21328400b0016b79b3222bmr33983680pzb.34.1697438503739; Sun, 15 Oct 2023 23:41:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697438503; cv=none; d=google.com; s=arc-20160816; b=EMkq01xzL1k/OqsxjKmgOJQbhlU47akdppWtlgm3rtvFtDurk/AfCeIYE2TGcy56bE 5EE4KrriotNDcG127oLuGwXSxkBnRjYivNMTdLhzMRoVb6vt6sMBg4/Q3vonE0Lhvvot v36QZxa31XmYijSYNHbeKncqcsw5Qqbr8zVpVvVXqhXI0sx4AN5TKvnZ/pjfYmqi+H/O iieZvDeISawxhJ8wlJLuyv+79SG2341xxVE9YlD62IPS6Q9cqpk6ibxY72z0O8vxeG35 /mEAciN9DZnv9chX7njUwYEFetbwxAgBNu7GRcPAnfdTxSs4w81FDNMseBK5AUIrxwBI AFrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=U2AgiFgpa6O47pkmBK6yizLzUM3j74/+Btppzhh7DTg=; fh=Hmofr0dp9o60Rhwi1TIoFPbtWNsxmbxQjMA7unyQwrE=; b=ePm+Z1N0BvM1E01ddokCijgarIwtftCNol5i+bVFtE7Ny6I52gr8tkBbu+rM6HFB4v iHhYj6H0sAPFK5KgVdXs/YAjYm9zYOEYQZMDBjVuJMtwPOlVk8oLRRaYrbrU8OYZkzos QPk2waZjIFGiupA+heOS3dQGNWtbppnLFzGCxvg7ZPly8Qw9WdqCDXVoklExCInNY3px jUrTq9EBJIXfq6UwJjNgcfaeA2WoTLc/8X9dK4STWvN6osfNggDBRJxCU+nX7J27kkb7 //Wyp2QSHfHdxrT+6Zx0Ln7HrSvPTUo9LhW6n4fa9Jic1Dzjvg7X2lTORVyoHPI2Req3 mmOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bhFTjkBs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id t24-20020a17090ae51800b00274b23c2867si5436841pjy.58.2023.10.15.23.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 23:41:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bhFTjkBs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id CCD4080ADF28; Sun, 15 Oct 2023 23:41:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231690AbjJPGlf (ORCPT + 99 others); Mon, 16 Oct 2023 02:41:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjJPGlc (ORCPT ); Mon, 16 Oct 2023 02:41:32 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22BD897; Sun, 15 Oct 2023 23:41:30 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id 6a1803df08f44-66d501d3ffbso1920166d6.1; Sun, 15 Oct 2023 23:41:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697438489; x=1698043289; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=U2AgiFgpa6O47pkmBK6yizLzUM3j74/+Btppzhh7DTg=; b=bhFTjkBsaH04PGaiBkmzQe/A+T5jKzXD6kjZSvuh4bOTJB6PPR2v/dz2b96UnoueB3 p+hjc1/8v1Lm4Vg+UmdHwlyNujaYu8NyKCFlRywK74SXZhyHXP6hz4xWj/f7Ir1X9kdh UuMEALuue/R4si2E4Uuls688KPGOWbrgKna0050vbiHRBZbmrlVS+Oeu+lxK0TM9zupT /SExgKzW8WJuYyB69ChApcrnqjdLmtg96qwYJtqumctwZ9sLuMPcYlmxzGX9UhIFhzwm 34NTCZwOTux9lyDXvCvn6fyr1esNuONarHo1/MnZhH8oyimFzxpSNPmiJkvMeXPJx1Vw X3mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697438489; x=1698043289; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=U2AgiFgpa6O47pkmBK6yizLzUM3j74/+Btppzhh7DTg=; b=aoIPmq+iq3oKxmk3ydDvUNQ/5GD3tutw82tD5BAJ61SnXZ+JEteR+IA2GPriykqZEf uD0TvXwq9MHok/jbI5RdCi34+dDWwLGb7sN2IAbmF69BsSsa7AmAzrvzw+JnChFYJNvh FerfVrsf0I6stAHsdJiOhUsU3ectPeRY54ezOkuTR6ToqHhYXicrSYugJ7pBcT2luA+d PdlBh3ZOastDiuGeegO1B37OQP90/SzelC1UffhCd7wV/cEKveqQ7Ynf42IlKylTlmAH w8PZRx13NR+fpUfXijIgWa/ig9/xlC83EiEJKgh5IgLCKxcTfEf7QLQtDhEdOHBLhSWJ y4Og== X-Gm-Message-State: AOJu0YzrGyFeer6cgcxXfK9Jl/tVNY4dTcBW+sL8Q3lsTzpi3v5X3C/D CrFQ/9hKEI1aPau5EXz2WjfYNAAc0QVuSOd63qk= X-Received: by 2002:ad4:5a13:0:b0:66d:5d31:999b with SMTP id ei19-20020ad45a13000000b0066d5d31999bmr1038478qvb.3.1697438489105; Sun, 15 Oct 2023 23:41:29 -0700 (PDT) MIME-Version: 1.0 References: <20231016031649.35088-1-huangjie.albert@bytedance.com> In-Reply-To: <20231016031649.35088-1-huangjie.albert@bytedance.com> From: Magnus Karlsson Date: Mon, 16 Oct 2023 08:41:17 +0200 Message-ID: Subject: Re: [PATCH v2 net-next] xsk: Avoid starving xsk at the end of the list To: Albert Huang Cc: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Magnus Karlsson , Jonathan Lemon , 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 15 Oct 2023 23:41:42 -0700 (PDT) On Mon, 16 Oct 2023 at 05:17, Albert Huang wrote: > > 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. Hi Albert. Yes you are correct that there is nothing hindering this to happen in the code at the moment, so let us fix it. > 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 I think something like MAX_PER_SOCKET_BUDGET would be clearer. > 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; Enough with a bool; > 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; > + } The problem here is that the fixed MAX_XSK_TX_BUDGET is only useful for the <= 2 socket case. If I have 3 sockets sharing a netdev/queue_id, the two first sockets can still starve the third one since the total budget per send is 32. You need to go through the list of sockets in the beginning to compute the MAX_XSK_TX_BUDGET to compute this dynamically before each call. Or cache this value somehow, in the pool for example. Actually, the refcount in the buf_pool will tell you how many sockets are sharing the same buf_pool. Try using that to form MAX_XSK_TX_BUDGET on the fly. Another simpler way of accomplishing this would be to just reorder the list every time. Put the first socket last in the list every time. The drawback of this is that you need to hold the xsk_tx_list_lock while doing this so might be slower. The per socket batch size would also be 32 and you would not receive "fairness" over a single call to sendto(). Would that be a problem for you? > + > 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 > >