From patchwork Wed May 6 21:33:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 219741 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E0FFC28CBC for ; Wed, 6 May 2020 21:33:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DC332070B for ; Wed, 6 May 2020 21:33:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="Al5Tclrd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729876AbgEFVdX (ORCPT ); Wed, 6 May 2020 17:33:23 -0400 Received: from mail.zx2c4.com ([192.95.5.64]:53575 "EHLO mail.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729152AbgEFVdW (ORCPT ); Wed, 6 May 2020 17:33:22 -0400 Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTP id c1797ad1; Wed, 6 May 2020 21:20:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=mail; bh=SZ+7gT7++HRheT9GPfZjo5R/I cw=; b=Al5TclrdIGH9fhTsPxxbGYXp+n6d/V8wc/+vREbb/+HERF5JSlC7kQ956 eklLV8PeKlCUvtwU4rdnc8l23k8EWLxCRrSS0JYI22wffzeNBMW/m2r5wKLgW18/ aij0aapfounXSseM/maB0mPq1iyvvQKlJAOHPf2+phh1ww/Gg8GRQIEcLExkAurO 0ogMni6APDIfX/WLlxoWBNvIWX6xobbkjGTeADBFk+gL2fh1nn8o4TYy9e0ikHBh T+jTB/MQsKJSayKaMCG1JyMbE/SWf0W0Pg1CP4hDTvu0CxyWp16z7nSyGtRJWrBu LH883eilUopBipcCCuccoIlMiz+2Q== Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 4ffbcace (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 6 May 2020 21:20:36 +0000 (UTC) From: "Jason A. Donenfeld" To: davem@davemloft.net, netdev@vger.kernel.org Cc: "Jason A. Donenfeld" Subject: [PATCH net 1/5] wireguard: selftests: use normal kernel stack size on ppc64 Date: Wed, 6 May 2020 15:33:02 -0600 Message-Id: <20200506213306.1344212-2-Jason@zx2c4.com> In-Reply-To: <20200506213306.1344212-1-Jason@zx2c4.com> References: <20200506213306.1344212-1-Jason@zx2c4.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org While at some point it might have made sense to be running these tests on ppc64 with 4k stacks, the kernel hasn't actually used 4k stacks on 64-bit powerpc in a long time, and more interesting things that we test don't really work when we deviate from the default (16k). So, we stop pushing our luck in this commit, and return to the default instead of the minimum. Signed-off-by: Jason A. Donenfeld --- tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config b/tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config index 990c510a9cfa..f52f1e2bc7f6 100644 --- a/tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config +++ b/tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config @@ -10,3 +10,4 @@ CONFIG_CMDLINE_BOOL=y CONFIG_CMDLINE="console=hvc0 wg.success=hvc1" CONFIG_SECTION_MISMATCH_WARN_ONLY=y CONFIG_FRAME_WARN=1280 +CONFIG_THREAD_SHIFT=14 From patchwork Wed May 6 21:33:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 219740 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CC1CC38A2A for ; Wed, 6 May 2020 21:33:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40B642076D for ; Wed, 6 May 2020 21:33:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="hAPSsmxq" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729947AbgEFVd2 (ORCPT ); Wed, 6 May 2020 17:33:28 -0400 Received: from mail.zx2c4.com ([192.95.5.64]:53575 "EHLO mail.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729199AbgEFVdY (ORCPT ); Wed, 6 May 2020 17:33:24 -0400 Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTP id d6bef07c; Wed, 6 May 2020 21:20:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=mail; bh=kYi8KCe/dSke//200kwKYMPwC Ws=; b=hAPSsmxqfO8x9h3iAXlk7tm1bfYteu7CjlUzD/d4RzRQ2CY4lr/6UZTyW 2/j3U/J6CpFDzho4nHB1+lJ93YE2k2w3sk4J4pqtCj5b6A2ZxnIg7JBRat9On8b8 teVQoSy121G9ZLGIlds08OkGofss9C/mltHCTMmlDLUb0UjQ8MHYwFgs5SlRysdC eXftaIrtM03zltfmM62ifNtftuukgTsTzv3/W6y9GkesPsorx42WU1XSYkifUA6S RImB/EJNxvp7d1kJQ3oDyUBkw+U0Z8kMnkhM2U0yhI0aQAJ1EmvoDJpvi5mKHHsZ w3cE8TKGcVZxoJH4YWL741A3NkDfg== Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 01b67c00 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 6 May 2020 21:20:39 +0000 (UTC) From: "Jason A. Donenfeld" To: davem@davemloft.net, netdev@vger.kernel.org Cc: "Jason A. Donenfeld" , Sultan Alsawaf , Wang Jian Subject: [PATCH net 3/5] wireguard: send/receive: cond_resched() when processing worker ringbuffers Date: Wed, 6 May 2020 15:33:04 -0600 Message-Id: <20200506213306.1344212-4-Jason@zx2c4.com> In-Reply-To: <20200506213306.1344212-1-Jason@zx2c4.com> References: <20200506213306.1344212-1-Jason@zx2c4.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Users with pathological hardware reported CPU stalls on CONFIG_ PREEMPT_VOLUNTARY=y, because the ringbuffers would stay full, meaning these workers would never terminate. That turned out not to be okay on systems without forced preemption, which Sultan observed. This commit adds a cond_resched() to the bottom of each loop iteration, so that these workers don't hog the core. Note that we don't need this on the napi poll worker, since that terminates after its budget is expended. Suggested-by: Sultan Alsawaf Reported-by: Wang Jian Fixes: e7096c131e51 ("net: WireGuard secure network tunnel") Signed-off-by: Jason A. Donenfeld --- drivers/net/wireguard/receive.c | 2 ++ drivers/net/wireguard/send.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/drivers/net/wireguard/receive.c b/drivers/net/wireguard/receive.c index 267f202f1931..2566e13a292d 100644 --- a/drivers/net/wireguard/receive.c +++ b/drivers/net/wireguard/receive.c @@ -516,6 +516,8 @@ void wg_packet_decrypt_worker(struct work_struct *work) &PACKET_CB(skb)->keypair->receiving)) ? PACKET_STATE_CRYPTED : PACKET_STATE_DEAD; wg_queue_enqueue_per_peer_napi(skb, state); + if (need_resched()) + cond_resched(); } } diff --git a/drivers/net/wireguard/send.c b/drivers/net/wireguard/send.c index 3e030d614df5..dc3079e17c7f 100644 --- a/drivers/net/wireguard/send.c +++ b/drivers/net/wireguard/send.c @@ -281,6 +281,8 @@ void wg_packet_tx_worker(struct work_struct *work) wg_noise_keypair_put(keypair, false); wg_peer_put(peer); + if (need_resched()) + cond_resched(); } } @@ -304,6 +306,8 @@ void wg_packet_encrypt_worker(struct work_struct *work) } wg_queue_enqueue_per_peer(&PACKET_PEER(first)->tx_queue, first, state); + if (need_resched()) + cond_resched(); } } From patchwork Wed May 6 21:33:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 219739 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2FBCC28CBC for ; Wed, 6 May 2020 21:33:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C45932076D for ; Wed, 6 May 2020 21:33:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="lacZfV2b" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729977AbgEFVdc (ORCPT ); Wed, 6 May 2020 17:33:32 -0400 Received: from mail.zx2c4.com ([192.95.5.64]:53575 "EHLO mail.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729152AbgEFVd1 (ORCPT ); Wed, 6 May 2020 17:33:27 -0400 Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 009af1fd; Wed, 6 May 2020 21:20:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=mail; bh=iKaIY/IfgB+5XH5ZbMMcDObHv hc=; b=lacZfV2bP5Hv2l9t4Z0D5x+8oUs+uHtosILJsV3+1DXmj43ZQOZn8MYu9 56c9qBxHSKJ98b+/VGFmPZ9BM/uExi6F+iUQkymg1cR86rILAuXDHBbQ0UF2f8zE d7rTykxxOLOrJ5iFJ8dklRBUJcVj2obQXv6DZdsjLa0x33rulMZ7jwKdGUKVQZ8b ZtVUtuaJAlx+ZsNQJcJ2hu8SnmHuu6hIpkEfrTMWrQyAvAGtqCD0zuntzZ6jaWhc qT8klRKLzTN9FrrNzQ0ZWl7HwSoqe0VwwP/UmcwAHeqa0oxbnIsjhoa9ybFsMLd1 2ihKzq59qBrWu4JCegQ30ahJtoNbQ== Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 70d827dd (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 6 May 2020 21:20:41 +0000 (UTC) From: "Jason A. Donenfeld" To: davem@davemloft.net, netdev@vger.kernel.org Cc: "Jason A. Donenfeld" , Sultan Alsawaf Subject: [PATCH net 5/5] wireguard: send/receive: use explicit unlikely branch instead of implicit coalescing Date: Wed, 6 May 2020 15:33:06 -0600 Message-Id: <20200506213306.1344212-6-Jason@zx2c4.com> In-Reply-To: <20200506213306.1344212-1-Jason@zx2c4.com> References: <20200506213306.1344212-1-Jason@zx2c4.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org It's very unlikely that send will become true. It's nearly always false between 0 and 120 seconds of a session, and in most cases becomes true only between 120 and 121 seconds before becoming false again. So, unlikely(send) is clearly the right option here. What happened before was that we had this complex boolean expression with multiple likely and unlikely clauses nested. Since this is evaluated left-to-right anyway, the whole thing got converted to unlikely. So, we can clean this up to better represent what's going on. The generated code is the same. Suggested-by: Sultan Alsawaf Signed-off-by: Jason A. Donenfeld --- drivers/net/wireguard/receive.c | 13 ++++++------- drivers/net/wireguard/send.c | 15 ++++++--------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/drivers/net/wireguard/receive.c b/drivers/net/wireguard/receive.c index 2566e13a292d..3bb5b9ae7cd1 100644 --- a/drivers/net/wireguard/receive.c +++ b/drivers/net/wireguard/receive.c @@ -226,21 +226,20 @@ void wg_packet_handshake_receive_worker(struct work_struct *work) static void keep_key_fresh(struct wg_peer *peer) { struct noise_keypair *keypair; - bool send = false; + bool send; if (peer->sent_lastminute_handshake) return; rcu_read_lock_bh(); keypair = rcu_dereference_bh(peer->keypairs.current_keypair); - if (likely(keypair && READ_ONCE(keypair->sending.is_valid)) && - keypair->i_am_the_initiator && - unlikely(wg_birthdate_has_expired(keypair->sending.birthdate, - REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT))) - send = true; + send = keypair && READ_ONCE(keypair->sending.is_valid) && + keypair->i_am_the_initiator && + wg_birthdate_has_expired(keypair->sending.birthdate, + REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT); rcu_read_unlock_bh(); - if (send) { + if (unlikely(send)) { peer->sent_lastminute_handshake = true; wg_packet_send_queued_handshake_initiation(peer, false); } diff --git a/drivers/net/wireguard/send.c b/drivers/net/wireguard/send.c index dc3079e17c7f..6687db699803 100644 --- a/drivers/net/wireguard/send.c +++ b/drivers/net/wireguard/send.c @@ -124,20 +124,17 @@ void wg_packet_send_handshake_cookie(struct wg_device *wg, static void keep_key_fresh(struct wg_peer *peer) { struct noise_keypair *keypair; - bool send = false; + bool send; rcu_read_lock_bh(); keypair = rcu_dereference_bh(peer->keypairs.current_keypair); - if (likely(keypair && READ_ONCE(keypair->sending.is_valid)) && - (unlikely(atomic64_read(&keypair->sending.counter.counter) > - REKEY_AFTER_MESSAGES) || - (keypair->i_am_the_initiator && - unlikely(wg_birthdate_has_expired(keypair->sending.birthdate, - REKEY_AFTER_TIME))))) - send = true; + send = keypair && READ_ONCE(keypair->sending.is_valid) && + (atomic64_read(&keypair->sending.counter.counter) > REKEY_AFTER_MESSAGES || + (keypair->i_am_the_initiator && + wg_birthdate_has_expired(keypair->sending.birthdate, REKEY_AFTER_TIME))); rcu_read_unlock_bh(); - if (send) + if (unlikely(send)) wg_packet_send_queued_handshake_initiation(peer, false); }