<div dir="ltr">Hi,<div>I was shocked when I found that `test_bit()` will return -1 or 0 to denote whether a bit is set or not in a bitmap.</div><div>Looking at its implementation here:</div><div>```</div><div><div>static __always_inline int constant_test_bit(long nr, const volatile unsigned long *addr)</div><div>{</div><div>        return ((1UL &lt;&lt; (nr &amp; (BITS_PER_LONG-1))) &amp;</div><div>                (addr[nr &gt;&gt; _BITOPS_LONG_SHIFT])) != 0;</div><div>}</div><div><br></div><div>static inline int variable_test_bit(long nr, volatile const unsigned long *addr)</div><div>{</div><div>        int oldbit;</div><div><br></div><div>        asm volatile(&quot;bt %2,%1\n\t&quot;</div><div>                     &quot;sbb %0,%0&quot;</div><div>                     : &quot;=r&quot; (oldbit)</div><div>                     : &quot;m&quot; (*(unsigned long *)addr), &quot;Ir&quot; (nr));</div><div><br></div><div>        return oldbit;</div><div>}</div></div><div>```</div><div>The sbb instruction will let `det = dst - (src + CF)`. So it seems that test_bit() will return -1 if a bit is set? And it seems to work that way in my project.</div><div><br></div><div>However, when I wrote another toy function to test it, I saw that it will return 1 for those bits set.</div><div>```</div><div><div>#define TEST_MAX_BITS<span class="" style="white-space:pre">        </span>512</div><div><br></div><div>static void rr_unit_test(struct kvm_vcpu *vcpu)</div><div>{</div><div><span class="" style="white-space:pre">        </span>static u64 counter = 0;</div><div><span class="" style="white-space:pre">        </span>int ret;</div><div><span class="" style="white-space:pre">        </span>DECLARE_BITMAP(test_map, TEST_MAX_BITS);</div><div><span class="" style="white-space:pre">        </span>u64 nr;</div><div><br></div><div><span class="" style="white-space:pre">        </span>if (vcpu-&gt;vcpu_id != 0)</div><div><span class="" style="white-space:pre">                </span>return;</div><div><span class="" style="white-space:pre">        </span>if (counter == 0) {</div><div><span class="" style="white-space:pre">                </span>++counter;</div><div><span class="" style="white-space:pre">                </span>bitmap_zero(test_map, TEST_MAX_BITS);</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(5, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">                </span>set_bit(5, test_map);</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(5, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(6, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(512, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">                </span>set_bit(512, test_map);</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(512, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">                </span>nr = 448;</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(nr, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">                </span>set_bit(nr, test_map);</div><div><span class="" style="white-space:pre">                </span>ret = test_bit(nr, test_map);</div><div><span class="" style="white-space:pre">                </span>printk(KERN_INFO &quot;vcpu=%d ret=%d\n&quot;, vcpu-&gt;vcpu_id, ret);</div><div><span class="" style="white-space:pre">        </span>}</div><div>}</div></div><div>```</div><div>So why will test_bit() behave differently?</div><div>Thanks very much!</div><div><br></div><div>Regards,</div><div>Le Tan</div></div>