From patchwork Mon Jun 5 05:45:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerald Yang X-Patchwork-Id: 1790224 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=AKjpUjmP; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QZN09349sz20Tb for ; Mon, 5 Jun 2023 15:46:49 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1q633A-0007OO-KQ; Mon, 05 Jun 2023 05:46:40 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1q632z-00078h-Ot for kernel-team@lists.ubuntu.com; Mon, 05 Jun 2023 05:46:29 +0000 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id A76993F0E9 for ; Mon, 5 Jun 2023 05:46:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1685943987; bh=8qoHshf5QUZZtiv7N3nL9XZU8DQgdbNdIQQp/cA8rI0=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AKjpUjmPbqHKQRCxawDbf3VZUSsgxWTAaDP2BCL9syncn59FlUr9AR0wsDf3V1bTl p5ug4JWJFCFiINXfUD6wbdOtL/LbQMU6X1GInDtdpj1LRXdNjEaDjTkqXzPha/Usk6 Woisd0MOURKLiXHDDZP0+zrX+4Rk6B9r4uIwvzmwC+ughmwRfC6gduiU0fNuzKtrDC HDDoT+FzMs2t2sP22zyp5KVWvtTZi1kKX169dHzioDJtOY9Rg/HJS05Kir172YnODC rQGTFIg2iHUvgLc0j0BzxEPmbSmQ90Yv3wY2uHcGVnu1TghOcd6CuhT6pw0uysSJzn NHVImed4GB0pw== Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2563afb0150so3865625a91.0 for ; Sun, 04 Jun 2023 22:46:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685943984; x=1688535984; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8qoHshf5QUZZtiv7N3nL9XZU8DQgdbNdIQQp/cA8rI0=; b=klDvl3d+sob6WfDqOCtULBCePrK4kZ/ldyHU0RguayBNWEw6ZeRYm++3S/jiPd/8cc cHyjtyNIRSzEYo0CbHKeThsx3dGEyghbjDDKM87cmggUkJIYMGGFJpKV1oSpG1vc14eb yacVlWi3kd/5CM4ELZd8w8dTA92lUdCX6gqF80/fwvz2C1h8BAQ8+7jwH0ZfS9swKPxz /PaMAFe3Di5yrvxLGhAl0Pwmad9QYX44e9VJl3hKCEwHvGmbXFbalhzDENO34NL63KiI 2psIt6B5vBV2qR84KsyUnf7ya3FvUsDSxPRVu7w91mzXDGgfSuGsYj80lPVdd/x86Bxu slPg== X-Gm-Message-State: AC+VfDwPV0GwytrczMsMF1yp5EBmBt0vYPpFUEkvCUBi+AtNXMJaDmBo hx3cJBMoEY1KIuGKeVzmDC2cxHMCTYK4TCcN933P9v2rfPv6bQ8JGqiTt8VNCex7F8E4Ip5qXOb B2Wx9t0RUHOKySmVJ15QQ/zKpWLtRXgQ3+ssg+lhhZtcfEp/mUw== X-Received: by 2002:a17:902:ce89:b0:1b2:a63:95aa with SMTP id f9-20020a170902ce8900b001b20a6395aamr2286131plg.54.1685943984770; Sun, 04 Jun 2023 22:46:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ50ErUE3YEINUU2PwaG3szLE93HIyReYuT2LblnoR9ckFBFk5/2Wjo/3Fvbrtqajg0jH9LZSQ== X-Received: by 2002:a17:902:ce89:b0:1b2:a63:95aa with SMTP id f9-20020a170902ce8900b001b20a6395aamr2286117plg.54.1685943984445; Sun, 04 Jun 2023 22:46:24 -0700 (PDT) Received: from localhost.localdomain (220-135-31-21.hinet-ip.hinet.net. [220.135.31.21]) by smtp.gmail.com with ESMTPSA id p5-20020a170902eac500b001b03a1a3151sm5637798pld.70.2023.06.04.22.46.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jun 2023 22:46:23 -0700 (PDT) From: Gerald Yang To: kernel-team@lists.ubuntu.com Subject: [SRU][K][PATCH 6/8] sbitmap: Use atomic_long_try_cmpxchg in __sbitmap_queue_get_batch Date: Mon, 5 Jun 2023 13:45:59 +0800 Message-Id: <20230605054601.1410517-7-gerald.yang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230605054601.1410517-1-gerald.yang@canonical.com> References: <20230605054601.1410517-1-gerald.yang@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Uros Bizjak Use atomic_long_try_cmpxchg instead of atomic_long_cmpxchg (*ptr, old, new) == old in __sbitmap_queue_get_batch. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). Also, atomic_long_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails, enabling further code simplifications, e.g. an extra memory read can be avoided in the loop. No functional change intended. Cc: Jens Axboe Signed-off-by: Uros Bizjak Link: https://lore.kernel.org/r/20220908151200.9993-1-ubizjak@gmail.com Signed-off-by: Jens Axboe Signed-off-by: Gerald Yang --- lib/sbitmap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 47cd8fb894ba..cbfd2e677d87 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -533,16 +533,16 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, nr = find_first_zero_bit(&map->word, map_depth); if (nr + nr_tags <= map_depth) { atomic_long_t *ptr = (atomic_long_t *) &map->word; - unsigned long val, ret; + unsigned long val; get_mask = ((1UL << nr_tags) - 1) << nr; + val = READ_ONCE(map->word); do { - val = READ_ONCE(map->word); if ((val & ~get_mask) != val) goto next; - ret = atomic_long_cmpxchg(ptr, val, get_mask | val); - } while (ret != val); - get_mask = (get_mask & ~ret) >> nr; + } while (!atomic_long_try_cmpxchg(ptr, &val, + get_mask | val)); + get_mask = (get_mask & ~val) >> nr; if (get_mask) { *offset = nr + (index << sb->shift); update_alloc_hint_after_get(sb, depth, hint,