Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp14533592ybl; Mon, 30 Dec 2019 11:56:04 -0800 (PST) X-Google-Smtp-Source: APXvYqzJvlsDzwWuntqGMURrnwNQTMBCd7WtbqErnaQkam/JuM3yT7hqMf/gesR7kkxRxM6mfMdB X-Received: by 2002:a9d:650f:: with SMTP id i15mr58749098otl.97.1577735764211; Mon, 30 Dec 2019 11:56:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577735764; cv=none; d=google.com; s=arc-20160816; b=PAwL3vcJ77lzv9/UIxcHBZ/P5X7EZXt4aHmWJKMaMS4xhYY6imkAoJHzAWuBv5iz0H ytU+ZYkX0KSRRTbucmPL0/Jl/1vsqBXWKMvOgrRW1VYXHGut+YZ0mjO46OFyC29bo3wY DwqI5+SROTbh4B+PVMITMe0j88jZIMFDIcuUkMPmLsnYWSg+0CATVW/vq5rypdSi3jLu MEhVaP+lrNl0bvvUntwyZaHqF6eIp77r3BGHdm1s2Y5u4qFewY192Bo9eV7zHOmoMjQb NRniq3kEeKb9hKVEfGz5y30FAzs4Rgj0qvIulAm8+iFkZE7Q1p6lLybaA6SDXaJhAt7O xtIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=q38WciEesnuRJRnd58/bcF+E8f7Yll8x5e78tHW7KKc=; b=cMh5jaNllY8ZkmS3JaaKn69pXF0ds4G/w5Pf93xLJV5OrSkOaT0Rd/1LhoZCs9yJ1t 0V+NgdondkrM79P6bDP3/Rp5wV9hQe4LRbB3+paoQwg8Fsi3dlqHmNDukoNDs5Ugvf4i 723lAGuDLjCiUx3QenQY5h4q+RzGtaeM2DVbebkhC/VEDq9BekrQyJpP/KMwEvlOw/HJ 7FJPfMfPpI0Sz7fp5Yxydzcb4N0EeiUBF3EyLlmRJKZDdKJVdO63XoBjdtoKCue6UKO1 UY6nCwxao44De8tFsumoVLyxBCnZ3iqbknCqDWf/+rFD7Hcf9yzJNgZJHjuYTmoCSPOf 5sUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2019-08-05 header.b=dQS4kODD; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k4si16780723oiw.55.2019.12.30.11.55.52; Mon, 30 Dec 2019 11:56:04 -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=@oracle.com header.s=corp-2019-08-05 header.b=dQS4kODD; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727686AbfL3Txw (ORCPT + 99 others); Mon, 30 Dec 2019 14:53:52 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:58780 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727278AbfL3Txv (ORCPT ); Mon, 30 Dec 2019 14:53:51 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xBUJn8jx140757; Mon, 30 Dec 2019 19:52:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2019-08-05; bh=q38WciEesnuRJRnd58/bcF+E8f7Yll8x5e78tHW7KKc=; b=dQS4kODD52nhn6IIkrqU9jLXPCHRyABE/SR/Y68eNu8jfTZHa8KQQKVhqMN+E2IIZnJf YmCy0iiTh2WJx512pTJ8nefrHbIQ5oS1FDhAUseV/FNtn3Zypb9f/2JqNsueNMl0TFyC FxCNjh3HH0Fwz81lDwFMIghuzusx+68ANNRms8HMmpzEBcohRI9Ek9SLowzqGt1plwM4 qPtQ6LzVCAU5LEZBhFbz429aaQ7seygHdYAUYJHNDeOpfkycSfN+YuudNMpWAHZRPRrR r/yHTlvysx31iY4R2SJ34BkhLsvmXAi/qLxVq0uFDLNMS+OMIAAjJG298/WkPPumvxdh 3g== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by aserp2120.oracle.com with ESMTP id 2x5y0pexke-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Dec 2019 19:52:48 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xBUJmRih115107; Mon, 30 Dec 2019 19:52:47 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userp3030.oracle.com with ESMTP id 2x6gh94hdu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Dec 2019 19:52:47 +0000 Received: from abhmp0003.oracle.com (abhmp0003.oracle.com [141.146.116.9]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id xBUJqZMi009978; Mon, 30 Dec 2019 19:52:35 GMT Received: from neelam.us.oracle.com (/10.152.128.16) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 30 Dec 2019 11:52:34 -0800 From: Alex Kogan To: linux@armlinux.org.uk, peterz@infradead.org, mingo@redhat.com, will.deacon@arm.com, arnd@arndb.de, longman@redhat.com, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@alien8.de, hpa@zytor.com, x86@kernel.org, guohanjun@huawei.com, jglauber@marvell.com Cc: steven.sistare@oracle.com, daniel.m.jordan@oracle.com, alex.kogan@oracle.com, dave.dice@oracle.com Subject: [PATCH v8 5/5] locking/qspinlock: Introduce the shuffle reduction optimization into CNA Date: Mon, 30 Dec 2019 14:40:42 -0500 Message-Id: <20191230194042.67789-6-alex.kogan@oracle.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20191230194042.67789-1-alex.kogan@oracle.com> References: <20191230194042.67789-1-alex.kogan@oracle.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9486 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1912300178 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9486 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1912300178 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This performance optimization reduces the probability threads will be shuffled between the main and secondary queues when the secondary queue is empty. It is helpful when the lock is only lightly contended. Signed-off-by: Alex Kogan Reviewed-by: Steve Sistare --- kernel/locking/qspinlock_cna.h | 46 ++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/kernel/locking/qspinlock_cna.h b/kernel/locking/qspinlock_cna.h index 30feff02865d..f21056560104 100644 --- a/kernel/locking/qspinlock_cna.h +++ b/kernel/locking/qspinlock_cna.h @@ -4,6 +4,7 @@ #endif #include +#include /* * Implement a NUMA-aware version of MCS (aka CNA, or compact NUMA-aware lock). @@ -57,6 +58,7 @@ struct cna_node { enum { LOCAL_WAITER_FOUND = 2, /* 0 and 1 are reserved for @locked */ FLUSH_SECONDARY_QUEUE = 3, + PASS_LOCK_IMMEDIATELY = 4, MIN_ENCODED_TAIL }; @@ -70,6 +72,34 @@ enum { */ int intra_node_handoff_threshold __ro_after_init = 1 << 16; +/* + * Controls the probability for enabling the scan of the main queue when + * the secondary queue is empty. The chosen value reduces the amount of + * unnecessary shuffling of threads between the two waiting queues when + * the contention is low, while responding fast enough and enabling + * the shuffling when the contention is high. + */ +#define SHUFFLE_REDUCTION_PROB_ARG (7) + +/* Per-CPU pseudo-random number seed */ +static DEFINE_PER_CPU(u32, seed); + +/* + * Return false with probability 1 / 2^@num_bits. + * Intuitively, the larger @num_bits the less likely false is to be returned. + * @num_bits must be a number between 0 and 31. + */ +static bool probably(unsigned int num_bits) +{ + u32 s; + + s = this_cpu_read(seed); + s = next_pseudo_random32(s); + this_cpu_write(seed, s); + + return s & ((1 << num_bits) - 1); +} + static void __init cna_init_nodes_per_cpu(unsigned int cpu) { struct mcs_spinlock *base = per_cpu_ptr(&qnodes[0].mcs, cpu); @@ -251,8 +281,11 @@ __always_inline u32 cna_pre_scan(struct qspinlock *lock, struct cna_node *cn = (struct cna_node *)node; cn->pre_scan_result = - cn->intra_count == intra_node_handoff_threshold ? - FLUSH_SECONDARY_QUEUE : cna_scan_main_queue(node, node); + (node->locked <= 1 && probably(SHUFFLE_REDUCTION_PROB_ARG)) ? + PASS_LOCK_IMMEDIATELY : + cn->intra_count == intra_node_handoff_threshold ? + FLUSH_SECONDARY_QUEUE : + cna_scan_main_queue(node, node); return 0; } @@ -266,6 +299,14 @@ static inline void cna_pass_lock(struct mcs_spinlock *node, u32 scan = cn->pre_scan_result; + /* + * perf. optimization - check if we can skip the logic of triaging + * through other possible values in @scan (helps under light lock + * contention) + */ + if (scan == PASS_LOCK_IMMEDIATELY) + goto pass_lock; + /* * check if a successor from the same numa node has not been found in * pre-scan, and if so, try to find it in post-scan starting from the @@ -294,6 +335,7 @@ static inline void cna_pass_lock(struct mcs_spinlock *node, tail_2nd->next = next; } +pass_lock: arch_mcs_pass_lock(&next_holder->locked, val); } -- 2.21.0 (Apple Git-122.2)