Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1121418rdh; Fri, 24 Nov 2023 05:50:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGQ9Hz6np4A6Z12jPqw++BVhkMrBeiqJl/39dPXtqr9lH5zhMFkUAfJzk01L0jEuO2vGbLY X-Received: by 2002:a17:90a:1a10:b0:281:35e:82a1 with SMTP id 16-20020a17090a1a1000b00281035e82a1mr2616686pjk.48.1700833831858; Fri, 24 Nov 2023 05:50:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700833831; cv=none; d=google.com; s=arc-20160816; b=W38QgWfCkq+5bVSshTFukjfHwS0LsYQVmcTM43XSe8cxncautjwidLLFmmbVTS04tT hc6ycYGLcGKnP/gyp1Z+r3FAgyuC36sWetsWV4NWixbdOjfL3uQfDGPKgyms0yXHXoSc 9MTdR03653rXV6qXBX9kTZI+BDNyRI/bM0QQMnu4txMxxsANDeo5fZuUOG7X+esrNQI/ znmOSO6Rx9WcGjzU53Ijk2mzVFuWMNhKfbQCyiejGUNUmNXwe7eyZFaynSMovuc3Dd3w h1RC6dSNUOLc5Fw/5+ATi+WfQ0xly98gbvPdwEit/qYzmUvk9ds6jiV1YbR3krPNEsnW w26g== 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=6I7CJB0Cp8tuYLriqcIjeTAsEGcMeXyDXVh80cPjfPM=; fh=Db27VihW5Tby++LHCMGglIT5dy5t2kh+NCUx9ODM9wE=; b=kPEfm3orTtJmuWupE1eAJIN9SWdaAHfXspxF/JDseVS1dIWG/bQdqJq2ef0opxZofD gZAvLaPLd/jTucVTHHISYHe26DD7h3EP9cCYn8iX2ycd4y6Tz043WoidqI9TY1MpuCjM AbW767qSWZRNxl9DrndCLomVO4VFvXLlNtOafV4LKZwTGM/wYJMW/hmjgky0lO+yeqkB obX7P9UdHUBbymPa9Ph2JYm1vVQc46+jPBA/OsAliTFsPfEtD4VKhYjLJb3lDsCY1kNT DzzGjW4RrndDYRdDdZqNWtEw0GiAYdxNmiiMki7ho4IplrdvwlSMfRH9NA9iiUyiwlQX 0ndg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=GpGQcg9b; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id a22-20020a17090a8c1600b002859d376d2csi85027pjo.29.2023.11.24.05.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 05:50:31 -0800 (PST) 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=@gmail.com header.s=20230601 header.b=GpGQcg9b; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id BA74B807F2BD; Fri, 24 Nov 2023 05:50:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230446AbjKXNuL (ORCPT + 99 others); Fri, 24 Nov 2023 08:50:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229742AbjKXNuK (ORCPT ); Fri, 24 Nov 2023 08:50:10 -0500 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E237D41; Fri, 24 Nov 2023 05:50:16 -0800 (PST) Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3b83fdab870so151335b6e.0; Fri, 24 Nov 2023 05:50:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700833815; x=1701438615; 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=6I7CJB0Cp8tuYLriqcIjeTAsEGcMeXyDXVh80cPjfPM=; b=GpGQcg9bodbd+Wnp/O+gz37+ltHLLWa+Lh72rJFdbNBbJlns6yigoGp6xlKxFbn3gh psZM//O5IELvcUr6tLWXyzEK+LrrPYKIspSUDmgSjk3rV6f3ozoI4DE0f835S4UmpryR CQBzjBeqy4R+nz1/Nz5RcstYrwf+dRMG2BJf/w3pwUL88v0PkAaqaWZh2j/CWjS8PepN MY9VFxim1jfYSfAJNwNwpJXajoig3mMRIfqByT4+Fl1+55olU7dunJZ4nNhevtHBiCo/ 25XJzq0I6ir2hT9ZbWIJmkTjoNGWkZnALGO9Oemx5zHYyJVHuzXMeXMXkIg3rBHbfCLs fsPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700833815; x=1701438615; 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=6I7CJB0Cp8tuYLriqcIjeTAsEGcMeXyDXVh80cPjfPM=; b=Mvrrnqin1fJfGuw6kaf8Do9XyQg4L3RFwW+dQ/lr7dEbYKLD2sQWBNluz6lZOTu9Ew VVoSqtdtSgBDPRVVFIvOd/xWQ1f/hUw7HDKfe1bQAtKbp2XoDA107K0Mq1jGLB63T9cO 8qyZ9bEGzbrQshPnHja0sGrfTPGlAf6QFKyUybw/5IZKtoA/D5k6x2jA24M0SXkNbwfu JUJm7rDS93Pv/vzkBkLkv1wntyOObIOl3OWzsIFKwfOikUGntYxZGLa/HJ0Ff6AP+ioc g/QL4efl0CBx/Vkk4ZvO5zZ/j+k1z8sJ1TOGJQCfnanVjUzlTZz/TTJxG7FoFtr5/Qrl xOJQ== X-Gm-Message-State: AOJu0YwUoeiDij9aiSnI1N022lLaqdArExaAESin90nIy7dHOY8dsBOu wm/x8NQ4QjorMDcwdw2iuVURciJDaVZaxdmsy7c= X-Received: by 2002:a9d:6a59:0:b0:6d7:ea8a:f0c1 with SMTP id h25-20020a9d6a59000000b006d7ea8af0c1mr3497137otn.3.1700833815295; Fri, 24 Nov 2023 05:50:15 -0800 (PST) MIME-Version: 1.0 References: <20231124070005.GA10393@libra05> In-Reply-To: <20231124070005.GA10393@libra05> From: Magnus Karlsson Date: Fri, 24 Nov 2023 14:50:04 +0100 Message-ID: Subject: Re: xdp/xsk.c: missing read memory barrier in xsk_poll() To: Yewon Choi Cc: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , "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, threeearcat@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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]); Fri, 24 Nov 2023 05:50:27 -0800 (PST) On Fri, 24 Nov 2023 at 08:00, Yewon Choi wrote: > > Hello, > > We found some possibility of missing read memory barrier in xsk_poll(), > so we would like to ask to check it. > > commit e6762c8b adds two smp_rmb() in xsk_mmap(), which are paired with > smp_wmb() in XDP_UMEM_REG and xsk_init_queue each. The later one is > added in order to prevent reordering between reading of q and reading > of q->ring. > One example in simplied code is: > > xsk_mmap(): > if (offset == XDP_PGOFF_RX_RING) { > q = READ_ONCE(xs->rx); > } > ... > if (!q) > return -EINVAL; > > /* Matches the smp_wmb() in xsk_init_queue */ > smp_rmb(); > ... > return remap_vmalloc_range(vma, q->ring, 0); > > Also, the similar logic exists in xsk_poll() without smp_rmb(). > > xsk_poll(): > ... > if (xs->rx && !xskq_prod_is_empty(xs->rx)) > mask |= EPOLLIN | EPOLLRDNORM; > if (xs->tx && xsk_tx_writeable(xs)) > mask |= EPOLLOUT | EPOLLWRNORM; > > xskq_prod_is_empty(): > return READ_ONCE(q->ring->consumer) && ... > > To be consistent, I think that smp_rmb() is needed between > xs->rx and !xsq_prod_is_empty() and the same applies for xs->tx. > > Could you check this please? > If a patch is needed, we will send them. Yes, you are correct that the current code would need an smp_rmb(). However, an unbound socket should never be allowed to enter the xsk_poll() code in the first place since it is pointless to poll a socket that has not been bound. This error was introduced in the commit below: commit 1596dae2f17ec5c6e8c8f0e3fec78c5ae55c1e0b Author: Maciej Fijalkowski Date: Wed Feb 15 15:33:09 2023 +0100 xsk: check IFF_UP earlier in Tx path When an AF_XDP socket has been bound, it is guaranteed to have been set up in the correct way and a memory barrier has already been executed in the xsk_bind call. It would be great if you could submit a patch, but I suggest that you do something like this instead of introducing an smp_rmb(): if (xsk_check_common(xs)) goto out; : : if (xs->rx && !xskq_prod_is_empty(xs->rx)) mask |= EPOLLIN | EPOLLRDNORM; if (xs->tx && xsk_tx_writeable(xs)) mask |= EPOLLOUT | EPOLLWRNORM; out: rcu_read_unlock(); return mask; Thank you for spotting this! /Magnus > > Best Regards, > Yewon Choi >