From nobody Sun Mar 20 05:20:52 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 16EEE1A1C822; Sun, 20 Mar 2022 05:20:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KLmLF411Bz4WBT; Sun, 20 Mar 2022 05:20:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647753653; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5l+6ydYXf/4CeK69HtY4lBlpnfMyt4dF7ojIUXc0KRU=; b=lLaqmVcs2TBTqS+DI0grDhHZt9WhewabV8K9fDnsETbndVVvKr5UigyAv18YIeIcfA6kM2 BxjlT8ZhxErxX8z8SlNCVL37znpILbudhXuEbJQwckVkP5JY8CDZRYOVW3V4gW1oWAOec2 G5m1wD3wzN7Hkb0WKi6ZsVDYiYZjo/Nzx8EvYP9NU0ZNL4NHUWGNQWJ9nBLlddqQj7n0bh 5yvxm8ruBvMGjlcKDL3/jWurUzag/5pkh4pNA2wO++K6wPqGVrPE6NId8tAHiAEd3BYHnF itEKjruAQfqN5rMqfT38nim/sMNYp1UNNOnTkiNUSgt5rh9QCeITunSiw/N+CA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 046D5213E2; Sun, 20 Mar 2022 05:20:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22K5KqIA029988; Sun, 20 Mar 2022 05:20:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22K5KqTu029987; Sun, 20 Mar 2022 05:20:52 GMT (envelope-from git) Date: Sun, 20 Mar 2022 05:20:52 GMT Message-Id: <202203200520.22K5KqTu029987@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: fb3644ab2afe - stable/12 - if_epair: fix race condition on multi-core systems List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: fb3644ab2afe777fdd2539bc996a390443f052f1 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647753653; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5l+6ydYXf/4CeK69HtY4lBlpnfMyt4dF7ojIUXc0KRU=; b=LOsYvv1ywoV2QvlmOWdciMmqCuQIspeDFKRZTxai0AY4jIaysCPMFUhxzCGhijSQb0TRcX mp9va4UyedP3OkeibdBdu9D7Hw2POOVF+wfdufb9tMYHBq3KL+Y4bCIcYXOP7Gj+Mrx2XE KQzf/SxNwVZaZZ+CK7LaK2B3lED6tBAxzt4nr6V3iFf0Vc49bUZFVYLDahBiJagyHbf1LF Wf4HAv0V5MZJaKn073n4goqYDxKL+ahNT6CblJk5Vf14OEtm9758zkNzxGi4yFMEmh+Vgj kCbxudKdNymWZDg3mG5xlS12iscgBRPwOFwtvXiCk60YHwsjMfvJ7LlZuqps+g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1647753653; a=rsa-sha256; cv=none; b=fiJnitPVJ4SMIcF697ztL7YQmga1jpcDTWslMWmiRM8Eex0/G+WkcgX5vyA//hS2Mcuz+d M9qmcOO+MZo7OTMgp6CzmhNQl+ooLsIpfKv38k9FUnHf95eRNvHmkPaB9QI5/7Jfrb8jYb STf+tlDhFssUOPtGpwywxY1fFik7W92vwnQniphcXRYvesiiWT82Ccxf5h6u4nZQq7gf5p pznUR64JQbck93ZyTbiS0UGz0hSy9YPCu/UTZF+Xf7zD3HnEo4sJJsuKWI7RI7B/RCrEEb hcsuXWURel8kam762JvHY624VpHeBGS5Ogp9S9Pl6PrGDGeBNE3qPGpLl/JQQA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=fb3644ab2afe777fdd2539bc996a390443f052f1 commit fb3644ab2afe777fdd2539bc996a390443f052f1 Author: Michael Gmelin AuthorDate: 2022-03-16 22:08:55 +0000 Commit: Kristof Provost CommitDate: 2022-03-20 00:24:51 +0000 if_epair: fix race condition on multi-core systems As an unwanted side effect of the performance improvements in 24f0bfbad57b9, epair interfaces stop forwarding traffic on higher load levels when running on multi-core systems. This happens due to a race condition in the logic that decides when to place work in the task queue(s) responsible for processing the content of ring buffers. In order to fix this, a field named state is added to the epair_queue structure. This field is used by the affected functions to signal each other that something happened in the underlying ring buffers that might require work to be scheduled in task queue(s), replacing the existing logic, which relied on checking if ring buffers are empty or not. epair_menq() does: - set BIT_MBUF_QUEUED - queue mbuf - if testandset BIT_QUEUE_TASK: enqueue task epair_tx_start_deferred() does: - swap ring buffers - process mbufs - clear BIT_QUEUE_TASK - if testandclear BIT_MBUF_QUEUED enqueue task PR: 262571 Reported by: Johan Hendriks MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D34569 (cherry picked from commit 66acf7685bcd8cf23b6c658a991637238a01859e) --- sys/net/if_epair.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sys/net/if_epair.c b/sys/net/if_epair.c index b95418e29071..64c9af521a39 100644 --- a/sys/net/if_epair.c +++ b/sys/net/if_epair.c @@ -105,11 +105,15 @@ static unsigned int next_index = 0; #define EPAIR_LOCK() mtx_lock(&epair_n_index_mtx) #define EPAIR_UNLOCK() mtx_unlock(&epair_n_index_mtx) +#define BIT_QUEUE_TASK 0 +#define BIT_MBUF_QUEUED 1 + struct epair_softc; struct epair_queue { int id; struct buf_ring *rxring[2]; volatile int ridx; /* 0 || 1 */ + volatile int state; /* taskqueue coordination */ struct task tx_task; struct epair_softc *sc; }; @@ -173,7 +177,8 @@ epair_tx_start_deferred(void *arg, int pending) } while (!atomic_fcmpset_int(&q->ridx, &ridx, nidx)); epair_if_input(sc, q, ridx); - if (! buf_ring_empty(q->rxring[nidx])) + atomic_clear_int(&q->state, (1 << BIT_QUEUE_TASK)); + if (atomic_testandclear_int(&q->state, BIT_MBUF_QUEUED)) taskqueue_enqueue(epair_tasks.tq[q->id], &q->tx_task); if_rele(sc->ifp); @@ -188,7 +193,6 @@ epair_menq(struct mbuf *m, struct epair_softc *osc) short mflags; struct epair_queue *q = NULL; uint32_t bucket; - bool was_empty; #ifdef RSS struct ether_header *eh; #endif @@ -240,14 +244,14 @@ epair_menq(struct mbuf *m, struct epair_softc *osc) #endif q = &osc->queues[bucket]; + atomic_set_int(&q->state, (1 << BIT_MBUF_QUEUED)); ridx = atomic_load_int(&q->ridx); - was_empty = buf_ring_empty(q->rxring[ridx]); ret = buf_ring_enqueue(q->rxring[ridx], m); if (ret != 0) { /* Ring is full. */ if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); m_freem(m); - goto done; + return (0); } if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); @@ -262,8 +266,7 @@ epair_menq(struct mbuf *m, struct epair_softc *osc) /* Someone else received the packet. */ if_inc_counter(oifp, IFCOUNTER_IPACKETS, 1); -done: - if (was_empty) + if (!atomic_testandset_int(&q->state, BIT_QUEUE_TASK)) taskqueue_enqueue(epair_tasks.tq[bucket], &q->tx_task); return (0); @@ -546,6 +549,7 @@ epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); q->ridx = 0; + q->state = 0; q->sc = sca; TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); } @@ -568,6 +572,7 @@ epair_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) q->rxring[0] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); q->rxring[1] = buf_ring_alloc(RXRSIZE, M_EPAIR, M_WAITOK, NULL); q->ridx = 0; + q->state = 0; q->sc = scb; TASK_INIT(&q->tx_task, 0, epair_tx_start_deferred, q); }