Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp815812ybi; Wed, 3 Jul 2019 05:10:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqw8NBNRAqHYB7dXutfCcTdbbX3kGPIKkzW9pJLyn0yVvcVJ4fN+iOLvhY89iUgRGRi9zN+m X-Received: by 2002:a17:90a:2506:: with SMTP id j6mr12748996pje.129.1562155840808; Wed, 03 Jul 2019 05:10:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562155840; cv=none; d=google.com; s=arc-20160816; b=ZxWIbWxNqKAdA9nGvGCi3NNrkzW6vM1zyA7RbaIiimk6TPd1hEaECZOd3WSLaJwyyU cewmz3Ng6L46JnUKeglOitcxCF/qWKDjYy9SsHGpLWEB7QimhZ4zzIKAxHVU43bTeqec gI6Nxhl9Iv0saYt797ZbYK2jwdKkchuZK73ahH3y+UZygyxznaqAGOGhg84+YbD/UL/h c9Gpu5bgjsb7cbMuLhV1dOLrm/OzN8LElihIR6NhNq0epMMB/FznyAEqjXeZ4uL8v1at vazS3Sp35Bf2qvzg5HtBHDtWp2XEtN6Qc2fuFliJGM4NySCp2b1/1sCliIMbhiesZIaR MZ9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=EAaWlzGCbN7kuloDJblzBSkq9mB9Yb8loWEXFJ3x8FI=; b=wWln2eSFx/4RMMKNiO75w8t5lvNW/U0IGM5FaaZBeamIDvLS4zCATjAGhst7hTUObA 8j41CnKjo3XwYas7zhNTTO69m+wbTfJ01XSW3gsLPa3Mu9ih+1YW2ciLyU9A0tnCxT/n LAP99rH1MhmY+bSBxVvqXK/GN33efkw5LKrr8qUq1vjq65CP8NjLM584nsZRNsCJUxIG 1exdyFfuPA9fvMh9dvp8vpRMGtaHPCtnsuZrQeW8znfawXgD/E6Un700AmqlbRxVCmuS cZYcZORV7DroTg7N42RWcEnHSfBcOG1bn3kiprFiWGXdRCWqLzgbDE34Knd7/Cb5ClHi SUMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=sfKBvO91; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j6si2136566pll.162.2019.07.03.05.10.24; Wed, 03 Jul 2019 05:10:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=sfKBvO91; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726825AbfGCMJZ (ORCPT + 99 others); Wed, 3 Jul 2019 08:09:25 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:41893 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726457AbfGCMJZ (ORCPT ); Wed, 3 Jul 2019 08:09:25 -0400 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20190703120923euoutp0242b2169994fa9de22c3b9ea47b78d520~t5GPvko4Y2189021890euoutp02R for ; Wed, 3 Jul 2019 12:09:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20190703120923euoutp0242b2169994fa9de22c3b9ea47b78d520~t5GPvko4Y2189021890euoutp02R DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1562155763; bh=EAaWlzGCbN7kuloDJblzBSkq9mB9Yb8loWEXFJ3x8FI=; h=From:To:Cc:Subject:Date:References:From; b=sfKBvO91Rk8zEsNoNvXvmUKReQkWWNqQ5dFfL/mHD25v9Y7j0Jdwg9fa3K8kpBVW1 easiqLv9iCK2S3SiacnFiYA7qV8yQE7duQU9t+0JuOTPnUjvofT6+6dehovlupvqjl nOi+VM8LqVdrmSaRerm/WLfFujeFd5eYjA9LEydo= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20190703120922eucas1p201a9328a62ca5bea1866462f64378890~t5GPB9HYc3132631326eucas1p2l; Wed, 3 Jul 2019 12:09:22 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id 2D.00.04298.2FA9C1D5; Wed, 3 Jul 2019 13:09:22 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20190703120922eucas1p2d97e3b994425ecdd2dadd13744ac2a77~t5GOUX1Ih1247312473eucas1p2L; Wed, 3 Jul 2019 12:09:22 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20190703120921eusmtrp172fc4f66baa50e60c702a594de8995ac~t5GOAMQZh0630506305eusmtrp1a; Wed, 3 Jul 2019 12:09:21 +0000 (GMT) X-AuditID: cbfec7f2-f13ff700000010ca-71-5d1c9af2960e Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id DB.57.04140.1FA9C1D5; Wed, 3 Jul 2019 13:09:21 +0100 (BST) Received: from imaximets.rnd.samsung.ru (unknown [106.109.129.180]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20190703120921eusmtip23925ada6b9adf7ce371179cdbfec550d~t5GNS7MlP1386113861eusmtip2a; Wed, 3 Jul 2019 12:09:20 +0000 (GMT) From: Ilya Maximets To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, xdp-newbies@vger.kernel.org, "David S. Miller" , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Magnus Karlsson , Jonathan Lemon , Jakub Kicinski , Alexei Starovoitov , Daniel Borkmann , Ilya Maximets Subject: [PATCH bpf v2] xdp: fix race on generic receive path Date: Wed, 3 Jul 2019 15:09:16 +0300 Message-Id: <20190703120916.19973-1-i.maximets@samsung.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA0VSW0hTYRzv25lnx+HkNG9f8xINw4rSBB8OZFbkw3mIWFQPlmLHPHhJN9lx mgkmat7xsoqVLTUtXTNU5vAapcO0stSckVdaoTG7mU4tUaxtZ9Lb78r/x8eHIUKVkwhLkKbS cimVJEb53PaB9eFDy1U+UYctXf7Eyvo0j9jMbwWEpX8QJeofrCGEeiSPS4wXrPOIAXMeSnQ3 30UIY7caJTSGl1at1pPoKH8GjruQ+seTHLKrapZH1j9d4JDFE2MIqdMWoaSq5BNClum1gLTo /CTYBX5oLJ2UkEbLg8Iu8eOrlfWclDyPq6U9o2g2yN1ZDJwxiIfA6gKLUzHgY0JcA6D6TpWD rADYPD7CY4kFwO+vlNztyi/jksNoBPC2aZTDkj8ANrR95thSKH4Qvm7qBzbsjovgUmeHvYHg vQjsGaxGbIYbfhRW1H6wh7j4Xqh5lGM/IcCPwJq1945zu2FTay9iK0O8nAff1RlR1giH86YZ B3aDXwf1PBb7wKGbpY7ydfgxbwGw5UIAVYYtDmscg/pvw9YCZp20H7Z0B7HyCajRtAGbDHFX OPHD/kqIFSrbVQgrC2BhvpBN+8ONvkaExSI4+dPiWEDCF7lz9gVCPAp+aZzhVAC/qv+3agHQ Ai9awSTH0UywlE4PZK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tYnyoPqO7ec6bdp7mffMT2pjkjX+kOi1t 93jJvu6uhc+yc4FnvDcLiaZxRU96znKNNU2JpTgj0VCLuag4EQA7azZxgAIAAA== X-CMS-MailID: 20190703120922eucas1p2d97e3b994425ecdd2dadd13744ac2a77 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20190703120922eucas1p2d97e3b994425ecdd2dadd13744ac2a77 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20190703120922eucas1p2d97e3b994425ecdd2dadd13744ac2a77 References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Unlike driver mode, generic xdp receive could be triggered by different threads on different CPU cores at the same time leading to the fill and rx queue breakage. For example, this could happen while sending packets from two processes to the first interface of veth pair while the second part of it is open with AF_XDP socket. Need to take a lock for each generic receive to avoid race. Fixes: c497176cb2e4 ("xsk: add Rx receive functions and poll support") Signed-off-by: Ilya Maximets --- Version 2: * spin_lock_irqsave --> spin_lock_bh. include/net/xdp_sock.h | 2 ++ net/xdp/xsk.c | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index d074b6d60f8a..ac3c047d058c 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -67,6 +67,8 @@ struct xdp_sock { * in the SKB destructor callback. */ spinlock_t tx_completion_lock; + /* Protects generic receive. */ + spinlock_t rx_lock; u64 rx_dropped; }; diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index a14e8864e4fa..5e0637db92ea 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -123,13 +123,17 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) u64 addr; int err; - if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) - return -EINVAL; + spin_lock_bh(&xs->rx_lock); + + if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) { + err = -EINVAL; + goto out_unlock; + } if (!xskq_peek_addr(xs->umem->fq, &addr) || len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) { - xs->rx_dropped++; - return -ENOSPC; + err = -ENOSPC; + goto out_drop; } addr += xs->umem->headroom; @@ -138,13 +142,21 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) memcpy(buffer, xdp->data_meta, len + metalen); addr += metalen; err = xskq_produce_batch_desc(xs->rx, addr, len); - if (!err) { - xskq_discard_addr(xs->umem->fq); - xsk_flush(xs); - return 0; - } + if (err) + goto out_drop; + + xskq_discard_addr(xs->umem->fq); + xskq_produce_flush_desc(xs->rx); + spin_unlock_bh(&xs->rx_lock); + + xs->sk.sk_data_ready(&xs->sk); + return 0; + +out_drop: xs->rx_dropped++; +out_unlock: + spin_unlock_bh(&xs->rx_lock); return err; } @@ -765,6 +777,7 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol, xs = xdp_sk(sk); mutex_init(&xs->mutex); + spin_lock_init(&xs->rx_lock); spin_lock_init(&xs->tx_completion_lock); mutex_lock(&net->xdp.lock); -- 2.17.1