From nobody Fri Oct 2 10:07:49 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 273EA345EC8 for ; Sun, 2 Aug 2026 15:23:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785684230; cv=none; b=ZLsQG86mP1DsDKn1+i2pNMGej69Ul9uD5L5ne+3HkMxaDkFSDF+APRKF5YZprPkliK2jQbcqEL/yO/YkEcq0cPN4ynH63dI2XEwp9F2lBlIABOyYEgX6nTXIepQcxq9Qu1W6NZIw7Ven8aYiZviSMtNzAIvCE2pBCjdq8gKODxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785684230; c=relaxed/simple; bh=ToY9uHYPwnCExAaylf1jjhCH7i6dkAib+8j00Q4rQnc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Ll8VHJYKL06rm1NSwbXa7RRBuEzSK+OycjB5KFo/puw3iymjgCpQhR4Btih2VGQHyldldFS9IqyXrozMrMp1DIgeJVVTVwX71lg11DpzeXexuT+4vg9PXVPq8XD8/b4nE9kUXfX7mNuhMeQnaOYm5+q1UFgGEoo2fxZwglgBzzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Hgyc5fp4; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Hgyc5fp4" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2caed617615so32530335ad.3 for ; Sun, 02 Aug 2026 08:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785684227; x=1786289027; 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:content-type; bh=rUwniuzrkpCFpQcic8q1OxrQ7B0joRHxWX+7MTlOezo=; b=Hgyc5fp4+hebouc5ZSKkMT3ROXpEvcLGV6j+K+eFS0hIZILgqzeFNGoHlBo3Ik8XI4 L7rkcwDIvRHfV1Ckib7nLUEv7FTD7ozQFn35YAPucW5RrsoE5w40QjgzVpJLpKeERNb1 3AY0kolXuOP1rcW8Fwb08hF2AtxgeQRT3kkpcSNql/t92UUwVCbacNzh+AJykRdcVl62 YyDjASjIwtRS2HcLfZ8UBKzqtSHJBdG0nXtQhGGUJXYvGlavUJhB23d+9+AfihBxwFGG qpBFHyG/K0BmzvvCeoX0TaVRnD0gmLaC3DYDt+PFXxXHmLC6Ofqpr5F0byhpSz3aDbQI sQCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785684227; x=1786289027; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=rUwniuzrkpCFpQcic8q1OxrQ7B0joRHxWX+7MTlOezo=; b=WghCGGzlRrK/QRDR1SHFLVV/cY2/Lu7/yUebv9Tc7BQP79gWBmU+tKIQr9HIAkk6gW ZQVueDzMJwrOMD+S0YZy1z8TjV/G8rCu3IkfMVMALQPN+YlI+zglGI+Gw1bvcDODlGBP ruJk9KQeemQhPVvV2aAo4aDmYLJiWRClRVyh3q0BuY/TGKlkfg4MVVWavMscQ+eAMqBU 4HLIhrjBOt0DTZxThC+hFtx3BQRpqi5KFn3yof2opUEKLcg9/ABzPjNZ1bDMmr9MU13j PZnEARObq3PvnPrIywIns+6X3+mOWMm+lLvIm+rWUnuPqWY4qdlPMAsWTJqBuXOTuTVz uuZA== X-Forwarded-Encrypted: i=1; AHgh+RoMoI8eb892uBdkGlTP0gGg50tDUE1Vl4xaVk/jg+wsPktgrro8erBNT0X8kJ39yz0p+Bkrnn6vinahoZY=@vger.kernel.org X-Gm-Message-State: AOJu0Ywmab6TGTqJbHSGNjFEBhckLVgQO2L7x9W/3El8XMBgONCUd51b 8wkn7MKEpBFe3rzafcsSyhvaRTpKhdfpdUeMcJ/v/VpaW2K39iH+4E+Q X-Gm-Gg: AR+sD11HX6u+44SAGPDF3dHwkvsrs+vigJiu/j6zeBbobn7ylpQaeUJ1ymjtswrtdff 9OzBzo/owRPhZgi/W+qYllfao5vHcagkg/Wp1LliGtjYmvF9DMhdvPMCWaP136zSYy7mdPe8lyo WB7y9P68gM8F3xsXKFjCNLxdeinWxiAiLQfCaW11wNCBA6RNGj3v7wSyD6JgBjZm/lSDDXEjfjy 9IeOwRXhJ30yZzG8nlQ9B7SLRQJibiJP2bL1rDE4H8mVNkxsehDarkJo7J2lplivi0bshjkyQgW ms6Yk1RkYbQDeH4FEeCcWmSeQkM7I6HS44lIJz4Me1Qk5B1cDXrnsNtqVEZbJ3G16LAEAW5Mpgi fawtQDNjUAMe3LWcau6mMIFWDZa9Y/4dTTeamcJJl0BXghmPynAeCWdsRxujmsPsh7jN2fm/tIS 0uvfp9XIwIYG6l2eUK9bimpMJNu6/ddKzQUWHzNkBaPJCtk4FpF1LyMIwL4wuElMv1XYS84+uRX rIToIiNkBiTXkcRPh+Gj4ebEQQQiT566GcK+BQDmG4GBg== X-Received: by 2002:a17:903:38c4:b0:2c9:b8b7:5d27 with SMTP id d9443c01a7336-2d0521bc1a6mr71406475ad.1.1785684227415; Sun, 02 Aug 2026 08:23:47 -0700 (PDT) Received: from chcpu15.cse.ust.hk (191host116.mobilenet.cse.ust.hk. [143.89.191.116]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d04ae19ecfsm26640495ad.10.2026.08.02.08.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Aug 2026 08:23:46 -0700 (PDT) From: Qi Zhang To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Chengfeng Ye , Thorsten Blum , Andy Shevchenko , Randy Dunlap , Robert Olsson , Jamal Hadi Salim Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Qi Zhang Subject: [PATCH net] net: pktgen: use a consistent flow count Date: Sun, 2 Aug 2026 23:23:09 +0800 Message-Id: <20260802152309.821584-1-marsy12010123@gmail.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Chengfeng Ye pktgen_if_write() can update cflows while the packet generator thread is inside mod_cur_headers(). The latter first tests cflows, but f_pick() then reloads it when selecting a random flow. This allows the following interleaving: CPU 0 (kpktgend) CPU 1 (proc write) if (pkt_dev->cflows) // 10 pkt_dev->cflows =3D 0 get_random_u32_below(pkt_dev->cflows) get_random_u32_below(0) returns a full-width random value. Using that value as an index into the fixed-size flows array causes an out-of-bounds access. The kernel reported: BUG: unable to handle page fault for address: ffffc8fe2d2674bc #PF: supervisor read access in kernel mode Oops: Oops: 0000 [#1] SMP KASAN NOPTI CPU: 0 UID: 0 PID: 65 Comm: kpktgend_0 RIP: 0010:mod_cur_headers+0x16f8/0x2840 Call Trace: pktgen_thread_worker+0x305a/0x6bc0 kthread+0x2c6/0x3b0 ret_from_fork+0x36e/0x5a0 ret_from_fork_asm+0x1a/0x30 Read cflows once at the start of mod_cur_headers(), pass the snapshot to f_pick(), and use it for later flow-state decisions in the same packet. Publish proc updates with WRITE_ONCE(). Flow selection then always uses a nonzero count bounded by MAX_CFLOWS, while a concurrent update takes effect on a later packet. Fixes: 007a531b0a0c ("[PKTGEN]: Introduce sequential flows") Cc: stable@vger.kernel.org Signed-off-by: Chengfeng Ye Signed-off-by: Qi Zhang --- net/core/pktgen.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/net/core/pktgen.c b/net/core/pktgen.c index ee64f3012321..631cb1f55f6d 100644 --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -1632,7 +1632,7 @@ static ssize_t pktgen_if_write(struct file *file, if (value > MAX_CFLOWS) value =3D MAX_CFLOWS; =20 - pkt_dev->cflows =3D value; + WRITE_ONCE(pkt_dev->cflows, value); sprintf(pg_result, "OK: flows=3D%u", pkt_dev->cflows); return count; } @@ -2373,7 +2373,7 @@ static inline int f_seen(const struct pktgen_dev *pkt= _dev, int flow) return !!(pkt_dev->flows[flow].flags & F_INIT); } =20 -static inline int f_pick(struct pktgen_dev *pkt_dev) +static inline int f_pick(struct pktgen_dev *pkt_dev, unsigned int cflows) { int flow =3D pkt_dev->curfl; =20 @@ -2383,11 +2383,11 @@ static inline int f_pick(struct pktgen_dev *pkt_dev) pkt_dev->flows[flow].count =3D 0; pkt_dev->flows[flow].flags =3D 0; pkt_dev->curfl +=3D 1; - if (pkt_dev->curfl >=3D pkt_dev->cflows) + if (pkt_dev->curfl >=3D cflows) pkt_dev->curfl =3D 0; /*reset */ } } else { - flow =3D get_random_u32_below(pkt_dev->cflows); + flow =3D get_random_u32_below(cflows); pkt_dev->curfl =3D flow; =20 if (pkt_dev->flows[flow].count > pkt_dev->lflow) { @@ -2461,12 +2461,14 @@ static void set_cur_queue_map(struct pktgen_dev *pk= t_dev) */ static void mod_cur_headers(struct pktgen_dev *pkt_dev) { + unsigned int cflows; __u32 imn; __u32 imx; int flow =3D 0; =20 - if (pkt_dev->cflows) - flow =3D f_pick(pkt_dev); + cflows =3D READ_ONCE(pkt_dev->cflows); + if (cflows) + flow =3D f_pick(pkt_dev, cflows); =20 /* Deal with source MAC */ if (pkt_dev->src_mac_count > 1) { @@ -2582,7 +2584,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_de= v) pkt_dev->cur_saddr =3D htonl(t); } =20 - if (pkt_dev->cflows && f_seen(pkt_dev, flow)) { + if (cflows && f_seen(pkt_dev, flow)) { pkt_dev->cur_daddr =3D pkt_dev->flows[flow].cur_daddr; } else { imn =3D ntohl(pkt_dev->daddr_min); @@ -2611,7 +2613,7 @@ static void mod_cur_headers(struct pktgen_dev *pkt_de= v) pkt_dev->cur_daddr =3D htonl(t); } } - if (pkt_dev->cflows) { + if (cflows) { pkt_dev->flows[flow].flags |=3D F_INIT; pkt_dev->flows[flow].cur_daddr =3D pkt_dev->cur_daddr; --=20 2.43.0