From patchwork Mon Sep 17 08:03:29 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 1465801 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 2BF8D3FCFC for ; Mon, 17 Sep 2012 08:03:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754760Ab2IQIDX (ORCPT ); Mon, 17 Sep 2012 04:03:23 -0400 Received: from mail-we0-f174.google.com ([74.125.82.174]:53609 "EHLO mail-we0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754645Ab2IQIDS (ORCPT ); Mon, 17 Sep 2012 04:03:18 -0400 Received: by mail-we0-f174.google.com with SMTP id x8so3701159wey.19 for ; Mon, 17 Sep 2012 01:03:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=ApzJiHaNTEYxe7HdM0ETnhZkOzS/NgGhKXGHVjRHwh4=; b=rcNphWBwUZZsbi1rnTCv6Zd2h6qliKPKjj1gkXGWjSO9Jl77kg5LI9m09BbcVGuILN v5aE1cP/exzng9WyFT5ySpvh3D/uO+tB5A5VGjg8sZZau+SygME2rDl3rpHo2Sp5epKu bUYo7LCSG+/W20m9J4QdgWEjR0fUTj3bmlhuzz0DLZ4e2RSlBuYEkMBATHzgC/SYgHsl CIyGs3+fdatWsXNGfsahKnKNYK4Z0k+KrcedgEHoqu0D/JcF/CoFepgTz5MzxKuKaCsz ljN/4IudyDPx+UJkxpsZLnfY7PUICSydyM/nwciCJ55OzX1Lzp3Q6jIyTdSqcXrMO2DA YDYg== Received: by 10.180.81.193 with SMTP id c1mr14302153wiy.12.1347868997059; Mon, 17 Sep 2012 01:03:17 -0700 (PDT) Received: from localhost.localdomain ([37.46.33.122]) by mx.google.com with ESMTPS id v3sm16632604wiw.7.2012.09.17.01.03.15 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 17 Sep 2012 01:03:16 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: asias.hejun@gmail.com, mingo@elte.hu, gorcunov@openvz.org, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 1/8] kvm tools: add init/exit automatic calls Date: Mon, 17 Sep 2012 10:03:29 +0200 Message-Id: <1347869016-17204-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This adds a method to call init/exit functions similar to the kernel's init functions. Signed-off-by: Sasha Levin --- tools/kvm/include/kvm/util-init.h | 51 +++++++++++++++++++++++++++++ tools/kvm/util/init.c | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tools/kvm/include/kvm/util-init.h create mode 100644 tools/kvm/util/init.c diff --git a/tools/kvm/include/kvm/util-init.h b/tools/kvm/include/kvm/util-init.h new file mode 100644 index 0000000..beccb4e --- /dev/null +++ b/tools/kvm/include/kvm/util-init.h @@ -0,0 +1,51 @@ +#ifndef KVM__UTIL_INIT_H +#define KVM__UTIL_INIT_H + +struct kvm; + +struct init_item { + struct hlist_node n; + const char *fn_name; + int (*init)(struct kvm *); +}; + +int init_list__init(struct kvm *kvm); +int init_list__exit(struct kvm *kvm); + +int init_list_add(struct init_item *t, int (*init)(struct kvm *), + int priority, const char *name); +int exit_list_add(struct init_item *t, int (*init)(struct kvm *), + int priority, const char *name); + +#define __init_list_add(cb, l) \ +static void __attribute__ ((constructor)) __init__##cb(void) \ +{ \ + static char name[] = #cb; \ + static struct init_item t; \ + init_list_add(&t, cb, l, name); \ +} + +#define __exit_list_add(cb, l) \ +static void __attribute__ ((constructor)) __init__##cb(void) \ +{ \ + static char name[] = #cb; \ + static struct init_item t; \ + exit_list_add(&t, cb, l, name); \ +} + +#define core_init(cb) __init_list_add(cb, 0) +#define base_init(cb) __init_list_add(cb, 2) +#define dev_base_init(cb) __init_list_add(cb, 4) +#define dev_init(cb) __init_list_add(cb, 5) +#define virtio_dev_init(cb) __init_list_add(cb, 6) +#define firmware_init(cb) __init_list_add(cb, 7) +#define late_init(cb) __init_list_add(cb, 9) + +#define core_exit(cb) __exit_list_add(cb, 0) +#define base_exit(cb) __exit_list_add(cb, 2) +#define dev_base_exit(cb) __exit_list_add(cb, 4) +#define dev_exit(cb) __exit_list_add(cb, 5) +#define virtio_dev_exit(cb) __exit_list_add(cb, 6) +#define firmware_exit(cb) __exit_list_add(cb, 7) +#define late_exit(cb) __exit_list_add(cb, 9) +#endif diff --git a/tools/kvm/util/init.c b/tools/kvm/util/init.c new file mode 100644 index 0000000..b3face1 --- /dev/null +++ b/tools/kvm/util/init.c @@ -0,0 +1,69 @@ +#include +#include + +#include "kvm/kvm.h" +#include "kvm/util-init.h" + +#define PRIORITY_LISTS 10 + +static struct hlist_head init_lists[PRIORITY_LISTS]; +static struct hlist_head exit_lists[PRIORITY_LISTS]; + +int init_list_add(struct init_item *t, int (*init)(struct kvm *), + int priority, const char *name) +{ + t->init = init; + t->fn_name = name; + hlist_add_head(&t->n, &init_lists[priority]); + + return 0; +} + +int exit_list_add(struct init_item *t, int (*init)(struct kvm *), + int priority, const char *name) +{ + t->init = init; + t->fn_name = name; + hlist_add_head(&t->n, &exit_lists[priority]); + + return 0; +} + +int init_list__init(struct kvm *kvm) +{ + unsigned int i; + int r = 0; + struct hlist_node *n; + struct init_item *t; + + for (i = 0; i < ARRAY_SIZE(init_lists); i++) + hlist_for_each_entry(t, n, &init_lists[i], n) { + r = t->init(kvm); + if (r < 0) { + pr_warning("Failed init: %s\n", t->fn_name); + goto fail; + } + } + +fail: + return r; +} + +int init_list__exit(struct kvm *kvm) +{ + int i; + int r = 0; + struct hlist_node *n; + struct init_item *t; + + for (i = ARRAY_SIZE(exit_lists) - 1; i >= 0; i--) + hlist_for_each_entry(t, n, &exit_lists[i], n) { + r = t->init(kvm); + if (r < 0) { + pr_warning("%s failed.\n", t->fn_name); + goto fail; + } + } +fail: + return r; +}