From: Alexander Graf Subject: [PATCH] Inform users about busy device assignment attempt Date: Wed, 9 Dec 2009 19:04:13 +0100 Message-ID: <1260381853-8954-1-git-send-email-agraf@suse.de> To: kvm@vger.kernel.org Return-path: Received: from cantor.suse.de ([195.135.220.2]:35485 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756200AbZLISEH (ORCPT ); Wed, 9 Dec 2009 13:04:07 -0500 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.221.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.suse.de (Postfix) with ESMTP id 1475190847 for ; Wed, 9 Dec 2009 19:04:14 +0100 (CET) Sender: kvm-owner@vger.kernel.org List-ID: When using -pcidevice on a device that is already in use by a kernel driver all the user gets is the following (very useful) information: Failed to assign device "04:00.0" : Device or resource busy Failed to deassign device "04:00.0" : Invalid argument Error initializing device pci-assign Since I usually prefer to have my computer do the thinking for me, I figured it might be a good idea to check and see if a device is actually used by a driver. If so, tell the user. So with this patch applied you get the following output: Failed to assign device "04:00.0" : Device or resource busy *** The driver 'igb' is occupying your device 04:00.0. *** Try running "rmmod igb" on the commandline Failed to deassign device "04:00.0" : Invalid argument Error initializing device pci-assign That should keep people like me from doing the most obvious misuses :-). Signed-off-by: Alexander Graf --- hw/device-assignment.c | 21 +++++++++++++++++++-- 1 files changed, 19 insertions(+), 2 deletions(-) diff --git a/hw/device-assignment.c b/hw/device-assignment.c index beb488c..2612c25 100644 --- a/hw/device-assignment.c +++ b/hw/device-assignment.c @@ -747,7 +747,8 @@ static uint32_t calc_assigned_dev_id(uint8_t bus, uint8_t devfn) static int assign_device(AssignedDevice *dev) { struct kvm_assigned_pci_dev assigned_dev_data; - int r; + char dir[128], driver[128], *ns; + int r, v; memset(&assigned_dev_data, 0, sizeof(assigned_dev_data)); assigned_dev_data.assigned_dev_id = @@ -769,9 +770,25 @@ static int assign_device(AssignedDevice *dev) #endif r = kvm_assign_pci_device(kvm_context, &assigned_dev_data); - if (r < 0) + if (r < 0) { fprintf(stderr, "Failed to assign device \"%s\" : %s\n", dev->dev.qdev.id, strerror(-r)); + + snprintf(dir, sizeof(dir), + "/sys/bus/pci/devices/0000:%02x:%02x.%x/driver", + dev->host.bus, dev->host.dev, dev->host.func); + + v = readlink(dir, driver, sizeof(driver)); + if ((v > 0) && (ns = strrchr(driver, '/'))) { + ns++; + fprintf(stderr, "*** The driver '%s' is occupying your " + "device %02x:%02x.%x.\n" + "*** Try running \"rmmod %s\" on the command" + "line\n", + ns, dev->host.bus, dev->host.dev, dev->host.func, + ns); + } + } return r; } -- 1.6.0.2