Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp623051pxv; Thu, 15 Jul 2021 11:47:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3Bnbrvw07oDF0yNeV3OG9/RF+8S3IRme9ABtY/9G/zs5UlBLzlYDcQaKgXwMq4+ztLcg5 X-Received: by 2002:a5d:91cc:: with SMTP id k12mr4215611ior.103.1626374879708; Thu, 15 Jul 2021 11:47:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626374879; cv=none; d=google.com; s=arc-20160816; b=AonM/X2sfsA21+nZ9MOgqHxsFNWI38evKPG9Yp9DGIqekYrz6VI9ENIKdlruRXCRoZ Ad7Mc3VlgeaIR2aJjtJwm4AOAUH73nlfRIWeQ4HLAQ45++ACU5crRopoVhqvHwVNh/fo 79GI1TotVBUSBi9kyCZwPIVhaOoCwhi2bz9zqQvwNFQcVs3U6y7CyGcEFiB3bhivPM4+ /YM6CeXRgZjxpumM54HiZShatK1uPkAIAQ2ioX6P+AfPrqUSnnRySXbaPhfk1f7EbKwx EqrMKutmVVGhhRkFNvC+qcaJkI1CJywfGxcwQhgUOqsYwrvvQgAWsM5lsbbGtPKgxyHv j+Iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TJio3D2xyWHpWrt23QnyKoYKgOuLmTj7q2v2bUHMFxg=; b=jsV0LFElwfy9tTCzcJZt6y2jyXtZU+8y6UbxkGo9JyK14h91cDOHjR1olBFMJqvdEj NOx+ukftdFNuWX1sXPU86eXXWUfjjLMW8Ut6xQN5I7z4aa0omkpOuxJLJxRKDhpv9/ql AZIKzTRJANofDtO5QHODDSx0v9sykVod7S3XVDm4xkF9CUPNQS9ntmo0ZvG+BbMTzrf3 MMpIK5RwRfH7T/L+EMCUJnt1pSGl5bLu2Cq3KfbIDy2Ff02SvFNRXrv3V/49HQJE46MN JhJEQVb7AYkenvF7h0kixwDNnuZtav171q1zd629Ky2VcoSYVmmHliUhdEW0LYBqnQwa H6nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="VyiU0DD/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t11si7733953ils.106.2021.07.15.11.47.47; Thu, 15 Jul 2021 11:47:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="VyiU0DD/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240615AbhGOStb (ORCPT + 99 others); Thu, 15 Jul 2021 14:49:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:49360 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238617AbhGOSrV (ORCPT ); Thu, 15 Jul 2021 14:47:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C0476613D1; Thu, 15 Jul 2021 18:44:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626374668; bh=GLC8YvqTeDMAKwXFZl0ygxyY4LPNVjrarme30m8Id4M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VyiU0DD/sIv2993WA/e/mEcyrwXL+b3tgaVvHTB146WtYR04gnB0oVRPh8lLpHpue S707WhrHNnnfK7c2uHS9DF8l5E3uvo/nZkWKxVFmg0ZL9mC0gT/AIeSDkBtbXPlfv/ kG44DVC0fRAZdxYqnssoguX+kV1NYmQhzU6VYBJ0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Varad Gautam , linux-rt-users , netdev@vger.kernel.org, Steffen Klassert , Herbert Xu , "David S. Miller" , Jakub Kicinski , Florian Westphal , "Ahmed S. Darwish" Subject: [PATCH 5.4 097/122] xfrm: policy: Read seqcount outside of rcu-read side in xfrm_policy_lookup_bytype Date: Thu, 15 Jul 2021 20:39:04 +0200 Message-Id: <20210715182517.994942248@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182448.393443551@linuxfoundation.org> References: <20210715182448.393443551@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Varad Gautam commit d7b0408934c749f546b01f2b33d07421a49b6f3e upstream. xfrm_policy_lookup_bytype loops on seqcount mutex xfrm_policy_hash_generation within an RCU read side critical section. Although ill advised, this is fine if the loop is bounded. xfrm_policy_hash_generation wraps mutex hash_resize_mutex, which is used to serialize writers (xfrm_hash_resize, xfrm_hash_rebuild). This is fine too. On PREEMPT_RT=y, the read_seqcount_begin call within xfrm_policy_lookup_bytype emits a mutex lock/unlock for hash_resize_mutex. Mutex locking is fine, since RCU read side critical sections are allowed to sleep with PREEMPT_RT. xfrm_hash_resize can, however, block on synchronize_rcu while holding hash_resize_mutex. This leads to the following situation on PREEMPT_RT, where the writer is blocked on RCU grace period expiry, while the reader is blocked on a lock held by the writer: Thead 1 (xfrm_hash_resize) Thread 2 (xfrm_policy_lookup_bytype) rcu_read_lock(); mutex_lock(&hash_resize_mutex); read_seqcount_begin(&xfrm_policy_hash_generation); mutex_lock(&hash_resize_mutex); // block xfrm_bydst_resize(); synchronize_rcu(); // block Move the read_seqcount_begin call outside of the RCU read side critical section, and do an rcu_read_unlock/retry if we got stale data within the critical section. On non-PREEMPT_RT, this shortens the time spent within RCU read side critical section in case the seqcount needs a retry, and avoids unbounded looping. Fixes: 77cc278f7b20 ("xfrm: policy: Use sequence counters with associated lock") Signed-off-by: Varad Gautam Cc: linux-rt-users Cc: netdev@vger.kernel.org Cc: stable@vger.kernel.org # v4.9 Cc: Steffen Klassert Cc: Herbert Xu Cc: "David S. Miller" Cc: Jakub Kicinski Cc: Florian Westphal Cc: "Ahmed S. Darwish" Signed-off-by: Steffen Klassert Acked-by: Ahmed S. Darwish Signed-off-by: Greg Kroah-Hartman --- net/xfrm/xfrm_policy.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -2089,12 +2089,15 @@ static struct xfrm_policy *xfrm_policy_l if (unlikely(!daddr || !saddr)) return NULL; - rcu_read_lock(); retry: - do { - sequence = read_seqcount_begin(&xfrm_policy_hash_generation); - chain = policy_hash_direct(net, daddr, saddr, family, dir); - } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)); + sequence = read_seqcount_begin(&xfrm_policy_hash_generation); + rcu_read_lock(); + + chain = policy_hash_direct(net, daddr, saddr, family, dir); + if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)) { + rcu_read_unlock(); + goto retry; + } ret = NULL; hlist_for_each_entry_rcu(pol, chain, bydst) { @@ -2125,11 +2128,15 @@ static struct xfrm_policy *xfrm_policy_l } skip_inexact: - if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)) + if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)) { + rcu_read_unlock(); goto retry; + } - if (ret && !xfrm_pol_hold_rcu(ret)) + if (ret && !xfrm_pol_hold_rcu(ret)) { + rcu_read_unlock(); goto retry; + } fail: rcu_read_unlock();