From patchwork Mon Aug 7 22:54:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 109599 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp3316985obb; Mon, 7 Aug 2017 15:55:15 -0700 (PDT) X-Received: by 10.98.12.78 with SMTP id u75mr2251985pfi.18.1502146515627; Mon, 07 Aug 2017 15:55:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502146515; cv=none; d=google.com; s=arc-20160816; b=wiOD5ZKHko4G4HvQ8ysXuJPCG8muEKLcfK+8RErB/8zfMyl8Katy95fZKZCCagOn1G 019rU94nWU7wuU0jaPXMX3uaS7YRv5Z798cPdbfphFP9AibZrGBbkZtN8fXTAahp4ajs cwxMYnKYxWjenRSs2oHEcusE1bJ4Y7aWRVYnqSaVv/J/7J548zFifUUrnh8K5AFwadrm v+yVhEKjXQ9A0hIBTonn7qPG3zdOEJZCEl8j9TTdv13xevwu9zEb2vA7Z691vRg6LalD RzIxKj04MnYvzEROHyEPnZM+kMvh0CqnZEOXfL/NXifX4dxarH/Z5SvB0Ly3sA0fVvZe n6Mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:message-id:date:subject:cc:to :from:dkim-signature:arc-authentication-results; bh=KZwuHEpkhTRaeByjoq8+nXQW9O/tpiAPgRKWmKfS2dM=; b=ZhgGlENhDqlYP4/yRhl1xxlQjVOH0/Cp7bqPeEtFVwrQYHrkzbDpub9rDbeQchs1yj lZjizVTu9RZxqFu1ZpPcXXY0rd92Bl0abqXdf+UOdlqnz+SW9RP3M/V2J6JOdeGYqxSx 1yRcHM/udjKpxYUptV/oDEK7nEB7K5LAyx8TJU8O9C7MN9OAX5R9FiRGqAQCvWx07FRw MeGNg1ri4LZcjEoDO+W5vZEXn8zmQeQ0i2uUng8kz0kqM/dtmJNI40T4mHU+/kol1rMu 6wuxlJbVyQkfAJ3hAM3M+XRVwBK3uci9myci3+A3NP9IUhrbzw/K+5ezVRddQBNP1YYF nyWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@CAVIUMNETWORKS.onmicrosoft.com header.b=McntjHSK; 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 12si5293043pfb.405.2017.08.07.15.55.15; Mon, 07 Aug 2017 15:55:15 -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=@CAVIUMNETWORKS.onmicrosoft.com header.b=McntjHSK; 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 S1751963AbdHGWzM (ORCPT + 25 others); Mon, 7 Aug 2017 18:55:12 -0400 Received: from mail-cys01nam02on0059.outbound.protection.outlook.com ([104.47.37.59]:6656 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751761AbdHGWzL (ORCPT ); Mon, 7 Aug 2017 18:55:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=CAVIUMNETWORKS.onmicrosoft.com; s=selector1-cavium-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=KZwuHEpkhTRaeByjoq8+nXQW9O/tpiAPgRKWmKfS2dM=; b=McntjHSKC8MQ3KphYRsp/92aNfx06o1PXeYgE/2MyRvJCKn6WmiN721NLina2hqPvsbZXCxKBcMAfpgarrJ//hTRFP8WfIhuPEl8rmpT9QdhLKZWwu3XwPmgA448qX4+tAwI1qazV21/VR6Zefbw+wAm6esyUR5yDNn7nn9wk50= Received: from localhost (41.141.196.40) by SN1PR07MB2127.namprd07.prod.outlook.com (10.164.46.157) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.1320.16; Mon, 7 Aug 2017 22:55:07 +0000 From: Yury Norov To: Andrew Morton , Noam Camus , Rasmus Villemoes Cc: Matthew Wilcox , Mauro Carvalho Chehab , linux-kernel@vger.kernel.org, Yury Norov Subject: [PATCH 1/2] lib: make bitmap_parselist thread-safe and much faster Date: Tue, 8 Aug 2017 01:54:37 +0300 Message-Id: <20170807225438.16161-1-ynorov@caviumnetworks.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 X-Originating-IP: [41.141.196.40] X-ClientProxiedBy: HE1PR0102CA0031.eurprd01.prod.exchangelabs.com (10.170.250.44) To SN1PR07MB2127.namprd07.prod.outlook.com (10.164.46.157) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9a093972-4ed8-4a33-cbae-08d4dde759f2 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:SN1PR07MB2127; X-Microsoft-Exchange-Diagnostics: 1; SN1PR07MB2127; 3:aJC3FXEoX22XX8GQ+mDT56bgUfQDkEz+Kj8iJUClwnIs5+394Uy+eXtiJLjmVndu0svwsVAENsQsbeRVJ1K6EO0trBe1RB+gURPrpgBjRPxSkEnq0E/vOT6an2YEqWjmzbiGFGS9xNOnNP7VTKMU77FR015GtBE8VecH5lPRjjkPCFxG1r9Do1EVFeXfFSNF9RlIQzUba05Vt2sOXS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Cacj/Q7UfOr6UPGZjfqrOFoZEd3KB7uu0S4pan2yJOaCZrbbV5FgBiyoqszWXDR9RLdrQ80OHleNlswzz3n7m+nuyWHzQ5pboIZaY6lsbZTAmXZ9FdAJEj93CIsV8Q+W+G1zqdOzeHbhTT08gzYze8AkBJdDj5TyPOlkdZg5mte/MzBEWqy1GaGxLxcB+PJcp94uxanA8EVtp8DXAFi7XLNnnAr06Gn+hk5D/6qCw46TXshkO1xN0=; 4:Mlt3yS1tN8qsSb5NvHicS/imTz9vvUPlLSjJO2xjg+kQwJyk5wZ/8+vQsXc+nVoxmNo5InqRSd0ld8epaKZnDwI4FeYCodV1yIrh5Bw6wWAXl0oaRKF62jrsmQ5Y5KxvfBJaOW6HU3k6ccPcA17WDokJYxTW+WJLbf6PrJTQEjuhtdiSKraBf1i2QSIapWUl00/Qn8VEAKqzOmshP/zP8KBBmjN0PWDYnfVkJa3zMI2Yu8U2DZsM6h98ZLPsypGj X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(3002001)(100000703101)(100105400095)(10201501046)(6041248)(20161123555025)(20161123560025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123564025)(6072148)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:SN1PR07MB2127; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:SN1PR07MB2127; X-Forefront-PRVS: 0392679D18 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(7370300001)(6009001)(6069001)(39450400003)(39410400002)(39840400002)(39400400002)(39850400002)(189002)(199003)(51444003)(8676002)(6116002)(106356001)(3846002)(33646002)(101416001)(42186005)(10646004)(50986999)(68736007)(76506005)(48376002)(50466002)(1076002)(5003940100001)(72206003)(305945005)(6486002)(4326008)(105586002)(7736002)(7350300001)(6496005)(53936002)(38730400002)(107886003)(478600001)(8666007)(97736004)(5001810100001)(25786009)(47776003)(2906002)(66066001)(54906002)(6666003)(36756003)(50226002)(10916006)(81166006)(81156014)(5660300001)(42882006)(189998001)(3486003); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1PR07MB2127; H:localhost; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: cavium.com does not designate permitted sender hosts) Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Yuri.Norov@cavium.com; X-Microsoft-Exchange-Diagnostics: 1; SN1PR07MB2127; 23:mwFNtpsa6EoQqeKnmc806w/9BbB6BJtDg1UpGSxIXeCQxSsCKFnZCGLxN/1J6HdkNlAbGyaIhJTkyMbX5aUcKt6UcvzDFxDZYBLExydycZhCxKQYtWE4JwCdsOIu3pTnFg1qJFonx+j/zBfCbmNb1pKJSG6F350jMTzOuGRlbjAl1jcVYkCRlcBqCawRwwEiaPjOOd5nFV4UpO05esKsgR50p4C9VcwLy/+JgEQQmGFeZWNAFdbgssFhsvfjOLsU4FdZSTmdHiFLBAyfcnMXk7Q5WBg3w/aN3+n3+egnhhWsSx6D3tusKA0wQhU4Dkc4me/HZUjhuUJFnGPkyZYhHaOh3KXyKmRkFfjTZhtiFR3w1C9BM/c6Ab88lZ3x0Cvkw8Yv32WatMBCIYl10oONTGFHZ1aq2TTSmsJRIpTl+HzzKbH9ezESv65GVnkiOQHYem0BRtCjwrywS/X8ABeG61x9dEDuSGhcKF/xoe8Gy+1xXd5/MZJKswkXEFH5DxE3i+shSbRF/wMlVlIUaqkZCXE1z0ZZ30iKt0NJJ2VSkPokqJ89merITVyXC54WsCSMnm1a9PnhPyywFCYZOXHSFqxShS1V3VTsRB0wDfAmjn0ypqFstfaJpaS5xZkfOyQN/f5yNnc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AW4LEkW7m/VlbgUN8AwBSZn5RsRFl+Mr1XgC2s40s/KWTRKfNg==; 5:JSsetwVFQNIUurP6ncn9EivMDShHFWZ9V9zzriPwaMspT6x3QerHx8RPE/4gFoiUN4N7XUJEGpM88+nVqavmhWKqFMKRl35nQQZKZzEw68tt3qYJ4SA8u9XHbjJE48v+/LkiWd2s54BA4Fmna5S/Sw==; 24:o6ocx6RZC67ozccAU/BjynuR8QZTKwdLphAdTJ+Fj7+K3NT8UU+lUO63eAd+BubVNAehxZBcm6rIZ26LOuVYZQESXMnU8sM0nFAaeTLpQJQ=; 7:gUF4sozugW9u6Eab+T0jd+E1H5m1mYkaufl0q0+W6vTBM0RP5+kf5clw/0ZeqhqXFfGZ/xiO2wPSTumFgOgVGxloWgIKGX06THS5LI0EqdVmD93bsLMK1W8AP/NdHdjZCizZ71hj5pV1+Yc4mYuQwKWT6YPqmBCcuuDdC3RZuWSw6Arol0cBk8ai97jytlDqm0J5MTMfjOer8YyURiZRtYMSP+2Qjke6IZrOHtHi9fc= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Aug 2017 22:55:07.2662 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR07MB2127 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Current implementation of bitmap_parselist() uses the static variable to save local state while setting bits in the bitmap. It is obviously wrong if we assume execution in multiprocessor environment. Fortunately, it's possible to rewrite this portion of code to avoid using the static variable. It is also possible to set bits in the mask per-range with bitmap_set(), not per-bit, as it is implemented now, with set_bit(); which is way faster. The important side effect of this changes is that setting bits in this function from now is not per-bit atomic and less memory-ordered. This is because set_bit() guaranties the order of memory accesses, while bitmap_set() does not. I think that it is the advantage of the new approach, because the bitmap_parselist() is intended to initialise bit arrays, and user should protect the whole bitmap during initialisation if needed. So protecting individual bits looks expensive and useless. Also, other range-oriented functions in lib/bitmap.c don't worry much about atomicity. With all that, setting 2k bits in map with the pattern like 0-2047:128/256 becomes ~50 times faster after applying the patch in my testing environment (arm64 hosted on qemu). The second patch of the series adds the test for bitmap_parselist(). It's not intended to cover all tricky cases, just to make sure that I didn't screw up during rework. Signed-off-by: Yury Norov --- lib/bitmap.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) -- 2.11.0 diff --git a/lib/bitmap.c b/lib/bitmap.c index 9a532805364b..c82c61b66e16 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -513,7 +513,7 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen, int nmaskbits) { unsigned int a, b, old_a, old_b; - unsigned int group_size, used_size; + unsigned int group_size, used_size, off; int c, old_c, totaldigits, ndigits; const char __user __force *ubuf = (const char __user __force *)buf; int at_start, in_range, in_partial_range; @@ -599,6 +599,8 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen, a = old_a; b = old_b; old_a = old_b = 0; + } else { + used_size = group_size = b - a + 1; } /* if no digit is after '-', it's wrong*/ if (at_start && in_range) @@ -608,17 +610,9 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen, if (b >= nmaskbits) return -ERANGE; while (a <= b) { - if (in_partial_range) { - static int pos_in_group = 1; - - if (pos_in_group <= used_size) - set_bit(a, maskp); - - if (a == b || ++pos_in_group > group_size) - pos_in_group = 1; - } else - set_bit(a, maskp); - a++; + off = min(b - a + 1, used_size); + bitmap_set(maskp, a, off); + a += group_size; } } while (buflen && c == ','); return 0;