diff mbox

[v6,3/4] kvm: add kvm_support_device() helper function

Message ID 1458711153-15988-4-git-send-email-peterx@redhat.com
State New
Headers show

Commit Message

Peter Xu March 23, 2016, 5:32 a.m. UTC
This can be used when probing whether KVM support specific device. Here,
a raw vmfd is used.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/sysemu/kvm.h |  9 +++++++++
 kvm-all.c            | 15 +++++++++++++++
 2 files changed, 24 insertions(+)

Comments

Sergey Fedorov March 23, 2016, 12:28 p.m. UTC | #1
On 23/03/16 08:32, Peter Xu wrote:
> diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
> index 6695fa7..8738fa1 100644
> --- a/include/sysemu/kvm.h
> +++ b/include/sysemu/kvm.h
> @@ -306,6 +306,15 @@ void kvm_device_access(int fd, int group, uint64_t attr,
>   */
>  int kvm_create_device(KVMState *s, uint64_t type, bool test);
>  
> +/**
> + * kvm_support_device - probe whether KVM support specific device
> + *
> + * @vmfd: The fd handler for VM
> + * @type: type of device
> + *
> + * @return: true if supported, otherwise false.
> + */
> +bool kvm_support_device(int vmfd, uint64_t type);

Why don't name the function like 'kvm_device_supported' to better express its predicative nature?

Kind regards,
Sergey
Peter Xu March 23, 2016, 2:56 p.m. UTC | #2
On Wed, Mar 23, 2016 at 03:28:28PM +0300, Sergey Fedorov wrote:
> On 23/03/16 08:32, Peter Xu wrote:
> > diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
> > index 6695fa7..8738fa1 100644
> > --- a/include/sysemu/kvm.h
> > +++ b/include/sysemu/kvm.h
> > @@ -306,6 +306,15 @@ void kvm_device_access(int fd, int group, uint64_t attr,
> >   */
> >  int kvm_create_device(KVMState *s, uint64_t type, bool test);
> >  
> > +/**
> > + * kvm_support_device - probe whether KVM support specific device
> > + *
> > + * @vmfd: The fd handler for VM
> > + * @type: type of device
> > + *
> > + * @return: true if supported, otherwise false.
> > + */
> > +bool kvm_support_device(int vmfd, uint64_t type);
> 
> Why don't name the function like 'kvm_device_supported' to better express its predicative nature?

Because I am trying to follow existing naming style, like:
"kvm_create_device" (please see above).

Thanks.

-- peterx
Sergey Fedorov March 23, 2016, 3:03 p.m. UTC | #3
On 23/03/16 17:56, Peter Xu wrote:
> On Wed, Mar 23, 2016 at 03:28:28PM +0300, Sergey Fedorov wrote:
>> On 23/03/16 08:32, Peter Xu wrote:
>>> diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
>>> index 6695fa7..8738fa1 100644
>>> --- a/include/sysemu/kvm.h
>>> +++ b/include/sysemu/kvm.h
>>> @@ -306,6 +306,15 @@ void kvm_device_access(int fd, int group, uint64_t attr,
>>>   */
>>>  int kvm_create_device(KVMState *s, uint64_t type, bool test);
>>>  
>>> +/**
>>> + * kvm_support_device - probe whether KVM support specific device
>>> + *
>>> + * @vmfd: The fd handler for VM
>>> + * @type: type of device
>>> + *
>>> + * @return: true if supported, otherwise false.
>>> + */
>>> +bool kvm_support_device(int vmfd, uint64_t type);
>> Why don't name the function like 'kvm_device_supported' to better express its predicative nature?
> Because I am trying to follow existing naming style, like:
> "kvm_create_device" (please see above).

Yes, but kvm_create_device() returns a file descriptor whereas this
function is predicative. Personally, I like the convention described in
chapter 16 of Linux kernel coding style [1]:

	If the name of a function is an action or an imperative command,
	the function should return an error-code integer.  If the name
	is a predicate, the function should return a "succeeded" boolean.


[1] https://www.kernel.org/doc/Documentation/CodingStyle

Kind regards,
Sergey
Peter Xu March 24, 2016, 1:59 a.m. UTC | #4
On Wed, Mar 23, 2016 at 06:03:32PM +0300, Sergey Fedorov wrote:
> Yes, but kvm_create_device() returns a file descriptor whereas this
> function is predicative. Personally, I like the convention described in
> chapter 16 of Linux kernel coding style [1]:
> 
> 	If the name of a function is an action or an imperative command,
> 	the function should return an error-code integer.  If the name
> 	is a predicate, the function should return a "succeeded" boolean.

The above is talking about return values. Maybe you were trying to
reference this one as example about add_work() and
pci_dev_present():

    For example, "add work" is a command, and the add_work()
    function returns 0 for success or -EBUSY for failure.  In the
    same way, "PCI device present" is a predicate, and the
    pci_dev_present() function returns 1 if it succeeds in finding a
    matching device or 0 if it doesn't.

Seems make sense. Will take your advice.  Thanks.

-- peterx
diff mbox

Patch

diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
index 6695fa7..8738fa1 100644
--- a/include/sysemu/kvm.h
+++ b/include/sysemu/kvm.h
@@ -306,6 +306,15 @@  void kvm_device_access(int fd, int group, uint64_t attr,
  */
 int kvm_create_device(KVMState *s, uint64_t type, bool test);
 
+/**
+ * kvm_support_device - probe whether KVM support specific device
+ *
+ * @vmfd: The fd handler for VM
+ * @type: type of device
+ *
+ * @return: true if supported, otherwise false.
+ */
+bool kvm_support_device(int vmfd, uint64_t type);
 
 /* Arch specific hooks */
 
diff --git a/kvm-all.c b/kvm-all.c
index 44c0464..77deccc 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -2339,6 +2339,21 @@  int kvm_create_device(KVMState *s, uint64_t type, bool test)
     return test ? 0 : create_dev.fd;
 }
 
+bool kvm_support_device(int vmfd, uint64_t type)
+{
+    struct kvm_create_device create_dev = {
+        .type = type,
+        .fd = -1,
+        .flags = KVM_CREATE_DEVICE_TEST,
+    };
+
+    if (ioctl(vmfd, KVM_CHECK_EXTENSION, KVM_CAP_DEVICE_CTRL) <= 0) {
+        return false;
+    }
+
+    return (ioctl(vmfd, KVM_CREATE_DEVICE, &create_dev) >= 0);
+}
+
 int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source)
 {
     struct kvm_one_reg reg;