Skip to contentSkip to conversation

lib: sbi: Improve sbi_hsm_hart_interruptible_mask

PATCH New Xiang W 2 participants 2 messages Nov 27, 2023 – Dec 10, 2023

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH 1/1 lib: sbi: Improve sbi_hsm_hart_interruptible_mask

Improve bit scanning with for_each_set_bit.

---
 lib/sbi/sbi_hsm.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
lib/sbi/sbi_hsm.c +3 -7
index 147f954..bffac27 100644
@@ -114,22 +114,18 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,
 				    ulong hbase, ulong *out_hmask)
 {
 	int hstate;
-	ulong i, hmask, dmask;
+	ulong i, dmask;
 
 	*out_hmask = 0;
 	if (!sbi_hartid_valid(hbase))
 		return SBI_EINVAL;
 
 	dmask = sbi_domain_get_assigned_hartmask(dom, hbase);
-	for (i = 0; i < BITS_PER_LONG; i++) {
-		hmask = 1UL << i;
-		if (!(dmask & hmask))
-			continue;
-
+	for_each_set_bit(i, &dmask, BITS_PER_LONG) {
 		hstate = __sbi_hsm_hart_get_state(hbase + i);
 		if (hstate == SBI_HSM_STATE_STARTED ||
 		    hstate == SBI_HSM_STATE_SUSPENDED)
-			*out_hmask |= hmask;
+			*out_hmask |= 1UL << i;
 	}
 
 	return 0;
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/1 lib: sbi: Improve sbi_hsm_hart_interruptible_mask

Quoted 20 lines · On Mon, Nov 27, 2023 at 6:10?PM Xiang W <[email protected]> wrote:

Improve bit scanning with for_each_set_bit.

---
 lib/sbi/sbi_hsm.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
lib/sbi/sbi_hsm.c +1 -1
index 147f954..bffac27 100644
@@ -114,22 +114,18 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,
                                    ulong hbase, ulong *out_hmask)
 {
        int hstate;
-       ulong i, hmask, dmask;
+       ulong i, dmask;
 
        *out_hmask = 0;

lib/sbi/sbi_hsm.c int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom, · line 124

Quoted 10 lines
lib/sbi/sbi_hsm.c +1 -5
        if (!sbi_hartid_valid(hbase))
                return SBI_EINVAL;
 
        dmask = sbi_domain_get_assigned_hartmask(dom, hbase);
-       for (i = 0; i < BITS_PER_LONG; i++) {
-               hmask = 1UL << i;
-               if (!(dmask & hmask))
-                       continue;
-
+       for_each_set_bit(i, &dmask, BITS_PER_LONG) {

We have intentionally avoided using for_each_set_bit() here because for_each_set_bit() is quite heavy handed and involves function calls to find_first_bit() and find_next_bit. The for_each_set_bitbetter for dealing with large bitmaps but for small bitmaps it becomes an overkill.

Also, please keep in mind that sbi_hsm_hart_interruptible_mask() is in hot-path because it is called by sbi_ipi_send_many().

Quoted 12 lines
lib/sbi/sbi_hsm.c +1 -2
                hstate = __sbi_hsm_hart_get_state(hbase + i);
                if (hstate == SBI_HSM_STATE_STARTED ||
                    hstate == SBI_HSM_STATE_SUSPENDED)
-                       *out_hmask |= hmask;
+                       *out_hmask |= 1UL << i;
        }
 
        return 0;
--

2.42.0

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup