From patchwork Tue Jun 6 10:28:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerald Yang X-Patchwork-Id: 1791052 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=gn9lRCSa; 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 4Qb6CB2mBnz20Ty for ; Tue, 6 Jun 2023 20:28:54 +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 1q6Tvj-0005uR-HI; Tue, 06 Jun 2023 10:28:47 +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 1q6Tvh-0005sW-BV for kernel-team@lists.ubuntu.com; Tue, 06 Jun 2023 10:28:45 +0000 Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.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 29FEA3F0CB for ; Tue, 6 Jun 2023 10:28:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1686047325; bh=VJ8QsgM/p+NQinqeHGdtKTJtpsBq8gLMWxMvIAZzaXk=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gn9lRCSaNMtubw6G2hgV8b+jiUsVU3t98lmTHlgK8PQjO8wSUDxPcnDP2e9qlC6dk Vq4+91NZQ8DB3YgaqDjKt2QjbMPJvAwbmC4zX9kAUVp6ZLmbvFnnBX4dMy89MRROIc 58jUMW2c1UgnfJnCToWLmUOSJM3ZbJMIoc5zx9rBetkwMbsgbHfFvnC5WkY8WL2c5A Jsvhpb7DVnu1edRwIkGShV+/kn4nojlGNpJEkn/QpgxXd7IpfekF2GEMnveB/jUop6 x03FZ1Ct8kIIF8H+9LPHyYuyWBsCF6O02cc109wvJL5DtsOoBMlDuS8DhzzQfOagKV MqH0LijVXb9Cw== Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-5559caee9d3so3981671eaf.2 for ; Tue, 06 Jun 2023 03:28:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686047324; x=1688639324; 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=VJ8QsgM/p+NQinqeHGdtKTJtpsBq8gLMWxMvIAZzaXk=; b=BbJbL0OaaeFQsV2oIgACF10b9O8RDOMgR9CFVmbJAcoa1YAB9SThf+tCSA5u+E0T6N badrcHiKxmbeb6yK0fCL5WO/iWr2BJhr/LljxohPKPMv6B5YBbqU2RAO0wkvjauGDftm VgrCblIqjI8FXBhEhaoNPTJYTiubw8BVDbOGNxf5N2f9Vr/SzGI9Hz5PATG2bhXdwKBQ +IeLpEqiGXud5xpLM55DEwCfUQQg2Xlr0xw6IfzvMb1ytazmK76f+FG37KqpfWiP+3NR 51ShS9rx2TS4urs0ZqQbBkZQB5z4CpB4h19FZf5vewRNlfbYo3cq6Yzbiq1+IpshIPKn hVog== X-Gm-Message-State: AC+VfDy0EVhNC1qAYa8M4JST8Wdpx0fnYUZOEzJxv3Uh8ysW4FkvK440 E9pcnuhSGKrf2oWTOvXYN1mbyyoo82Na9HWEJfTfSzidPO09Om5pOJooQSQ1m2s3J/Eic3ACy/a ZOXiFuvzlpZS6jTn5oO4IEm4QbCO+hM3AdZJq9LnM72UT8oeLrw== X-Received: by 2002:aca:210d:0:b0:398:6008:f460 with SMTP id 13-20020aca210d000000b003986008f460mr1885656oiz.28.1686047323882; Tue, 06 Jun 2023 03:28:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5ePxbrk12LV64GbsKRWRd8W5vm8VWu2aCxsIFfNV03TRfQRDANlNGCs/k0GzJMkPbMQ4qkTA== X-Received: by 2002:aca:210d:0:b0:398:6008:f460 with SMTP id 13-20020aca210d000000b003986008f460mr1885637oiz.28.1686047323564; Tue, 06 Jun 2023 03:28:43 -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 8-20020a17090a0c0800b00256a6ec8507sm9777629pjs.19.2023.06.06.03.28.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 03:28:43 -0700 (PDT) From: Gerald Yang To: kernel-team@lists.ubuntu.com Subject: [SRU][K][PATCH 4/6] sbitmap: Use atomic_long_try_cmpxchg in __sbitmap_queue_get_batch Date: Tue, 6 Jun 2023 18:28:26 +0800 Message-Id: <20230606102828.218014-5-gerald.yang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230606102828.218014-1-gerald.yang@canonical.com> References: <20230606102828.218014-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 BugLink: https://bugs.launchpad.net/bugs/2022318 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 (cherry picked from commit c35227d4e8cbc70a6622cc7cc5f8c3bff513f1fa) 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,