From mboxrd@z Thu Jan 1 00:00:00 1970 From: Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: Re: [PATCH 2/6] KVM: use kvm_{test,clear}_request instead of {test,clear}_bit Date: Fri, 7 Apr 2017 14:24:14 +0200 Message-ID: <20170407122413.GC23559@potion> References: <20170406202056.18379-1-rkrcmar@redhat.com> <20170406202056.18379-3-rkrcmar@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Christoffer Dall , Andrew Jones , Marc Zyngier , Paolo Bonzini , Cornelia Huck , James Hogan , Paul Mackerras To: Christian Borntraeger Return-path: Received: from mx1.redhat.com ([209.132.183.28]:58448 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933605AbdDGMYS (ORCPT ); Fri, 7 Apr 2017 08:24:18 -0400 Content-Disposition: inline In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: 2017-04-07 12:55+0200, Christian Borntraeger: > On 04/06/2017 10:20 PM, Radim Krčmář wrote: >> static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) >> { >> - if (test_bit(req, &vcpu->requests)) { >> - clear_bit(req, &vcpu->requests); >> + if (kvm_test_request(req, vcpu)) { >> + kvm_clear_request(req, vcpu); > > This looks fine. I am just asking myself why we do not use > test_and_clear_bit? Do we expect gcc to merge all test bits as > a fast path? This does not seem to work as far as I can tell and > almost everybody does a fast path like in test_and_clear_bit() is a slower operation even if the test is false (at least on x86), because it needs to be fully atomic. > arch/s390/kvm/kvm-s390.c: > if (!vcpu->requests) > return 0; > > arch/x86/kvm/x86.c: > if (vcpu->requests) { We'll mostly have only one request set, so splitting the test_and_clear improves the performance of many subsequent tests_and_clear()s even if the compiler doesn't optimize. GCC couldn't even optimize if we used test_and_clear_bit(), because that instruction adds barriers, but the forward check for vcpu->requests is there because we do not trust the optimizer to do it for us and it would make a big difference.