Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4921816ybv; Tue, 11 Feb 2020 05:59:43 -0800 (PST) X-Google-Smtp-Source: APXvYqxuSJFHu1nnZyX4KcNS5GJeRWO/64oFQkZ9vrjk2arfQK1wF7rnu8KvLFaHJz8se1JcamQX X-Received: by 2002:aca:5083:: with SMTP id e125mr2954731oib.96.1581429583558; Tue, 11 Feb 2020 05:59:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581429583; cv=none; d=google.com; s=arc-20160816; b=eIauDRdIOj42S+AAJkKJQRY8O5ARXx+9IQpAxizriuJMnLjJX57hUchsimaXW0DXjL 8f54ZaA9wC+jaxFE/MMp3We9uf7dUn7+AN88YQOVECgDtywnq6XcZdIETNze80JOORQn NArwYnP9LC+vd0rSfsSjx8TmMUzVY8x6XTZG8ioiKVpr3Sf8cOfLL034Bwp0oP+6PR4B nazA1n/77CqIDrflgcrgSDy3mkqfwMA7656YfnnYpXrvKmXxx7gapR/vbrQzOGmIsDJX Zx0JoXl/sZGWwtKNOrwKTcoih70nL8qGUyz4FdJm9tHqqF+KiHNy9Gd7QQzRPBh24Tlq L0DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=lYlTJ1jY8JnxpacazgipCT1s12isYW6IzQVTCcofmsU=; b=0NFgtFG8c9VcEVJA4BLuRzR0gAqMJbj4VQf0kPuoch3QUtnDYG2PQ7K8SVf75nRYRn 9JEvnNDM0atmqPKJBdRxRXZWcHvwLBC7hwf1ny2xVVbIjYlLGQTJg2f70B3zbkju0HBI DYqChg+1hu9TL9mpeSl5mElvrmNkR++neXOfKQWO04GPLcKb22OZgDENahxGo+x2TTK5 4+YlRDyKYgoMQx7Ej+eAVEvpayKX63gGzcUrAHmEa1FeEglQBVk9zPA9XJwyzgk+1M4W DNZyVHXqAX2SJ+S4n4RQCWN1DnmGTNPelATC3tmp8v1NHPc3mhZBLBJzBavEPT6f0C4h I05Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=ImQAEYyc; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l8si1739110oii.249.2020.02.11.05.59.31; Tue, 11 Feb 2020 05:59:43 -0800 (PST) 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=@lca.pw header.s=google header.b=ImQAEYyc; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728180AbgBKNy2 (ORCPT + 99 others); Tue, 11 Feb 2020 08:54:28 -0500 Received: from mail-qk1-f193.google.com ([209.85.222.193]:44386 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729768AbgBKNy1 (ORCPT ); Tue, 11 Feb 2020 08:54:27 -0500 Received: by mail-qk1-f193.google.com with SMTP id v195so10064708qkb.11 for ; Tue, 11 Feb 2020 05:54:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=from:to:cc:subject:date:message-id; bh=lYlTJ1jY8JnxpacazgipCT1s12isYW6IzQVTCcofmsU=; b=ImQAEYycKiLH6JQH4FTn5G26YRERLBu5hGWzQ+xekv40zttKFB/EdVLMpnF8LWt3ms 3QIstbyO2U5Z+QsTOntYhs89pUgnni8DOSvEbl6QBm/t13cxLl38kGSwhwfm+Ef3yYL/ dPkNPep87NC3f2BQ2Fq8S14ODKLXWpx5jN5hU2SDuScS/DGL0QMPzQCWnfKRFflz5vdc nZan2gpH/dLxyxsM0oD8m0mbmuzOQ93/U2TKGRt4P5WG024V9vSSfHS7dCFgPu7D7aXd sdR6ylo3YxM3Yrl0snU/HXsKpcdJJTQEPESs3RlwbLsSo3rsSXzz4DdPIwrWao0D5z59 EHyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=lYlTJ1jY8JnxpacazgipCT1s12isYW6IzQVTCcofmsU=; b=hHypf9O877QDq8xoewvQvNUvx/5y6VrJuAQgzpFIelHZAgBKNya7EieU6xw1BD5SeN /rMTRM0UIRsDdDZi5KUSVyOYZejTJh2rozG+XMhmET4IZSGgz9hTOxnQarG56rfJzyxF iN/W2igz1/NzrDIcb9BCqq9wz/WqGOkomFBGLepyRlmKKM02VN0fO3gowxKWAYgtqEo9 BkYgTQnSNKDT2T5F8EhyxlVCiGyeVUp8a+RoCorXjNjwCSn3/GQonP2SiuKOr8SyUuZn VsQx+s7ytOd6i/8ldFF3IePYeUSMnbrXiBZMxANPEYjcaM8aNnyUIXRZWRtqrOsQMhwn N7qA== X-Gm-Message-State: APjAAAW83rUNCd7alVJtVkLNk4pxRLPtWtVfcYR1BcZa+p7+mlTYcbiG Wa0PbYGtfX3ZuZ9PDrMxea1POMPCE1/Wpg== X-Received: by 2002:a37:7b84:: with SMTP id w126mr6180914qkc.280.1581429266697; Tue, 11 Feb 2020 05:54:26 -0800 (PST) Received: from qcai.nay.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id z6sm2077181qto.86.2020.02.11.05.54.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Feb 2020 05:54:26 -0800 (PST) From: Qian Cai To: peterz@infradead.org, mingo@redhat.com Cc: will@kernel.org, elver@google.com, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH -next v2] locking/osq_lock: annotate a data race in osq_lock Date: Tue, 11 Feb 2020 08:54:15 -0500 Message-Id: <1581429255-12542-1-git-send-email-cai@lca.pw> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org prev->next could be accessed concurrently as noticed by KCSAN, write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107: osq_lock+0x25f/0x350 osq_wait_next at kernel/locking/osq_lock.c:79 (inlined by) osq_lock at kernel/locking/osq_lock.c:185 rwsem_optimistic_spin read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100: osq_lock+0x196/0x350 osq_lock at kernel/locking/osq_lock.c:157 rwsem_optimistic_spin Since the write only stores NULL to prev->next and the read tests if prev->next equals to this_cpu_ptr(&osq_node). Even if the value is shattered, the code is still working correctly. Thus, mark it as an intentional data race using the data_race() macro. Signed-off-by: Qian Cai --- v2: insert some code comments. kernel/locking/osq_lock.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c index 1f7734949ac8..f733bcd99e8a 100644 --- a/kernel/locking/osq_lock.c +++ b/kernel/locking/osq_lock.c @@ -154,7 +154,11 @@ bool osq_lock(struct optimistic_spin_queue *lock) */ for (;;) { - if (prev->next == node && + /* + * cpu_relax() below implies a compiler barrier which would + * prevent this comparison being optimized away. + */ + if (data_race(prev->next == node) && cmpxchg(&prev->next, node, NULL) == node) break; -- 1.8.3.1