{"id":817491,"url":"http://patchwork.ozlabs.org/api/patches/817491/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1506083624-20621-3-git-send-email-amarnath.valluri@intel.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<1506083624-20621-3-git-send-email-amarnath.valluri@intel.com>","list_archive_url":null,"date":"2017-09-22T12:33:38","name":"[v7,2/8] tpm-backend: Move thread handling inside TPMBackend","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"6b5fb4dbbd40bd8819216354077adf7a02d07361","submitter":{"id":71320,"url":"http://patchwork.ozlabs.org/api/people/71320/?format=json","name":"Valluri, Amarnath","email":"amarnath.valluri@intel.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1506083624-20621-3-git-send-email-amarnath.valluri@intel.com/mbox/","series":[{"id":4614,"url":"http://patchwork.ozlabs.org/api/series/4614/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=4614","date":"2017-09-22T12:33:37","name":"Provide support for the software TPM emulator","version":7,"mbox":"http://patchwork.ozlabs.org/series/4614/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/817491/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/817491/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xzCsw1yqvz9s9Y\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 22:47:00 +1000 (AEST)","from localhost ([::1]:58692 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dvNMA-0003ye-AE\n\tfor incoming@patchwork.ozlabs.org; Fri, 22 Sep 2017 08:46:58 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:56391)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <amarnath.valluri@intel.com>) id 1dvN8X-0000bR-Pf\n\tfor qemu-devel@nongnu.org; Fri, 22 Sep 2017 08:33:00 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <amarnath.valluri@intel.com>) id 1dvN8V-0005qX-BJ\n\tfor qemu-devel@nongnu.org; Fri, 22 Sep 2017 08:32:53 -0400","from mga07.intel.com ([134.134.136.100]:27337)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <amarnath.valluri@intel.com>)\n\tid 1dvN8U-0005pe-W2\n\tfor qemu-devel@nongnu.org; Fri, 22 Sep 2017 08:32:51 -0400","from fmsmga003.fm.intel.com ([10.253.24.29])\n\tby orsmga105.jf.intel.com with ESMTP; 22 Sep 2017 05:32:50 -0700","from avallurigigabyte.fi.intel.com ([10.237.68.147])\n\tby FMSMGA003.fm.intel.com with ESMTP; 22 Sep 2017 05:32:49 -0700"],"X-ExtLoop1":"1","X-IronPort-AV":"E=Sophos;i=\"5.42,427,1500966000\"; d=\"scan'208\";a=\"902897834\"","From":"Amarnath Valluri <amarnath.valluri@intel.com>","To":"qemu-devel@nongnu.org","Date":"Fri, 22 Sep 2017 15:33:38 +0300","Message-Id":"<1506083624-20621-3-git-send-email-amarnath.valluri@intel.com>","X-Mailer":"git-send-email 2.7.4","In-Reply-To":"<1506083624-20621-1-git-send-email-amarnath.valluri@intel.com>","References":"<1506083624-20621-1-git-send-email-amarnath.valluri@intel.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-detected-operating-system":"by eggs.gnu.org: Genre and OS details not\n\trecognized.","X-Received-From":"134.134.136.100","Subject":"[Qemu-devel] [PATCH v7 2/8] tpm-backend: Move thread handling\n\tinside TPMBackend","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Amarnath Valluri <amarnath.valluri@intel.com>, =?utf-8?q?Marc-Andr?=\n\t=?utf-8?b?w6kgTHVyZWF1?= <marcandre.lureau@gmail.com>,\n\tStefan Berger <stefanb@linux.vnet.ibm.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"Move thread handling inside TPMBackend, this way backend implementations need\nnot to maintain their own thread life cycle, instead they needs to implement\n'handle_request()' class method that always been called from a thread.\n\nThis change made tpm_backend_int.h kind of useless, hence removed it.\n\nSigned-off-by: Amarnath Valluri <amarnath.valluri@intel.com>\nReviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>\nReviewed-by: Stefan Berger <stefanb@linux.vnet.ibm.com>\n---\n backends/tpm.c                   | 62 +++++++++++++++++++++++++---------------\n hw/tpm/tpm_passthrough.c         | 58 ++++++-------------------------------\n include/sysemu/tpm_backend.h     | 32 +++++++++++++--------\n include/sysemu/tpm_backend_int.h | 41 --------------------------\n 4 files changed, 67 insertions(+), 126 deletions(-)\n delete mode 100644 include/sysemu/tpm_backend_int.h","diff":"diff --git a/backends/tpm.c b/backends/tpm.c\nindex 536f262..ce56c3b 100644\n--- a/backends/tpm.c\n+++ b/backends/tpm.c\n@@ -18,7 +18,24 @@\n #include \"qapi/qmp/qerror.h\"\n #include \"sysemu/tpm.h\"\n #include \"qemu/thread.h\"\n-#include \"sysemu/tpm_backend_int.h\"\n+\n+static void tpm_backend_worker_thread(gpointer data, gpointer user_data)\n+{\n+    TPMBackend *s = TPM_BACKEND(user_data);\n+    TPMBackendClass *k  = TPM_BACKEND_GET_CLASS(s);\n+\n+    assert(k->handle_request != NULL);\n+    k->handle_request(s, (TPMBackendCmd)data);\n+}\n+\n+static void tpm_backend_thread_end(TPMBackend *s)\n+{\n+    if (s->thread_pool) {\n+        g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL);\n+        g_thread_pool_free(s->thread_pool, FALSE, TRUE);\n+        s->thread_pool = NULL;\n+    }\n+}\n \n enum TpmType tpm_backend_get_type(TPMBackend *s)\n {\n@@ -39,6 +56,8 @@ void tpm_backend_destroy(TPMBackend *s)\n     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);\n \n     k->ops->destroy(s);\n+\n+    tpm_backend_thread_end(s);\n }\n \n int tpm_backend_init(TPMBackend *s, TPMState *state,\n@@ -46,13 +65,23 @@ int tpm_backend_init(TPMBackend *s, TPMState *state,\n {\n     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);\n \n-    return k->ops->init(s, state, datacb);\n+    s->tpm_state = state;\n+    s->recv_data_callback = datacb;\n+\n+    return k->ops->init(s);\n }\n \n int tpm_backend_startup_tpm(TPMBackend *s)\n {\n     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);\n \n+    /* terminate a running TPM */\n+    tpm_backend_thread_end(s);\n+\n+    s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,\n+                                       NULL);\n+    g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);\n+\n     return k->ops->startup_tpm(s);\n }\n \n@@ -72,9 +101,8 @@ size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)\n \n void tpm_backend_deliver_request(TPMBackend *s)\n {\n-    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);\n-\n-    k->ops->deliver_request(s);\n+    g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,\n+                       NULL);\n }\n \n void tpm_backend_reset(TPMBackend *s)\n@@ -82,6 +110,8 @@ void tpm_backend_reset(TPMBackend *s)\n     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);\n \n     k->ops->reset(s);\n+\n+    tpm_backend_thread_end(s);\n }\n \n void tpm_backend_cancel_cmd(TPMBackend *s)\n@@ -156,29 +186,14 @@ static void tpm_backend_instance_init(Object *obj)\n                              tpm_backend_prop_get_opened,\n                              tpm_backend_prop_set_opened,\n                              NULL);\n-}\n \n-void tpm_backend_thread_deliver_request(TPMBackendThread *tbt)\n-{\n-   g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL);\n }\n \n-void tpm_backend_thread_create(TPMBackendThread *tbt,\n-                               GFunc func, gpointer user_data)\n+static void tpm_backend_instance_finalize(Object *obj)\n {\n-    if (!tbt->pool) {\n-        tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL);\n-        g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);\n-    }\n-}\n+    TPMBackend *s = TPM_BACKEND(obj);\n \n-void tpm_backend_thread_end(TPMBackendThread *tbt)\n-{\n-    if (tbt->pool) {\n-        g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL);\n-        g_thread_pool_free(tbt->pool, FALSE, TRUE);\n-        tbt->pool = NULL;\n-    }\n+    tpm_backend_thread_end(s);\n }\n \n static const TypeInfo tpm_backend_info = {\n@@ -186,6 +201,7 @@ static const TypeInfo tpm_backend_info = {\n     .parent = TYPE_OBJECT,\n     .instance_size = sizeof(TPMBackend),\n     .instance_init = tpm_backend_instance_init,\n+    .instance_finalize = tpm_backend_instance_finalize,\n     .class_size = sizeof(TPMBackendClass),\n     .abstract = true,\n };\ndiff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c\nindex a0baf5f..f50d9cf 100644\n--- a/hw/tpm/tpm_passthrough.c\n+++ b/hw/tpm/tpm_passthrough.c\n@@ -30,7 +30,6 @@\n #include \"tpm_int.h\"\n #include \"hw/hw.h\"\n #include \"hw/i386/pc.h\"\n-#include \"sysemu/tpm_backend_int.h\"\n #include \"tpm_tis.h\"\n #include \"tpm_util.h\"\n \n@@ -47,20 +46,9 @@\n     OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)\n \n /* data structures */\n-typedef struct TPMPassthruThreadParams {\n-    TPMState *tpm_state;\n-\n-    TPMRecvDataCB *recv_data_callback;\n-    TPMBackend *tb;\n-} TPMPassthruThreadParams;\n-\n struct TPMPassthruState {\n     TPMBackend parent;\n \n-    TPMBackendThread tbt;\n-\n-    TPMPassthruThreadParams tpm_thread_params;\n-\n     char *tpm_dev;\n     int tpm_fd;\n     bool tpm_executing;\n@@ -214,12 +202,9 @@ static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,\n                                         selftest_done);\n }\n \n-static void tpm_passthrough_worker_thread(gpointer data,\n-                                          gpointer user_data)\n+static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd cmd)\n {\n-    TPMPassthruThreadParams *thr_parms = user_data;\n-    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);\n-    TPMBackendCmd cmd = (TPMBackendCmd)data;\n+    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);\n     bool selftest_done = false;\n \n     DPRINTF(\"tpm_passthrough: processing command type %d\\n\", cmd);\n@@ -227,12 +212,12 @@ static void tpm_passthrough_worker_thread(gpointer data,\n     switch (cmd) {\n     case TPM_BACKEND_CMD_PROCESS_CMD:\n         tpm_passthrough_unix_transfer(tpm_pt,\n-                                      thr_parms->tpm_state->locty_data,\n+                                      tb->tpm_state->locty_data,\n                                       &selftest_done);\n \n-        thr_parms->recv_data_callback(thr_parms->tpm_state,\n-                                      thr_parms->tpm_state->locty_number,\n-                                      selftest_done);\n+        tb->recv_data_callback(tb->tpm_state,\n+                               tb->tpm_state->locty_number,\n+                               selftest_done);\n         break;\n     case TPM_BACKEND_CMD_INIT:\n     case TPM_BACKEND_CMD_END:\n@@ -248,15 +233,6 @@ static void tpm_passthrough_worker_thread(gpointer data,\n  */\n static int tpm_passthrough_startup_tpm(TPMBackend *tb)\n {\n-    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);\n-\n-    /* terminate a running TPM */\n-    tpm_backend_thread_end(&tpm_pt->tbt);\n-\n-    tpm_backend_thread_create(&tpm_pt->tbt,\n-                              tpm_passthrough_worker_thread,\n-                              &tpm_pt->tpm_thread_params);\n-\n     return 0;\n }\n \n@@ -268,20 +244,11 @@ static void tpm_passthrough_reset(TPMBackend *tb)\n \n     tpm_passthrough_cancel_cmd(tb);\n \n-    tpm_backend_thread_end(&tpm_pt->tbt);\n-\n     tpm_pt->had_startup_error = false;\n }\n \n-static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,\n-                                TPMRecvDataCB *recv_data_cb)\n+static int tpm_passthrough_init(TPMBackend *tb)\n {\n-    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);\n-\n-    tpm_pt->tpm_thread_params.tpm_state = s;\n-    tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;\n-    tpm_pt->tpm_thread_params.tb = tb;\n-\n     return 0;\n }\n \n@@ -315,13 +282,6 @@ static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)\n     return sb->size;\n }\n \n-static void tpm_passthrough_deliver_request(TPMBackend *tb)\n-{\n-    TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);\n-\n-    tpm_backend_thread_deliver_request(&tpm_pt->tbt);\n-}\n-\n static void tpm_passthrough_cancel_cmd(TPMBackend *tb)\n {\n     TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);\n@@ -483,8 +443,6 @@ static void tpm_passthrough_destroy(TPMBackend *tb)\n \n     tpm_passthrough_cancel_cmd(tb);\n \n-    tpm_backend_thread_end(&tpm_pt->tbt);\n-\n     qemu_close(tpm_pt->tpm_fd);\n     qemu_close(tpm_pt->cancel_fd);\n \n@@ -520,7 +478,6 @@ static const TPMDriverOps tpm_passthrough_driver = {\n     .realloc_buffer           = tpm_passthrough_realloc_buffer,\n     .reset                    = tpm_passthrough_reset,\n     .had_startup_error        = tpm_passthrough_get_startup_error,\n-    .deliver_request          = tpm_passthrough_deliver_request,\n     .cancel_cmd               = tpm_passthrough_cancel_cmd,\n     .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,\n     .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,\n@@ -540,6 +497,7 @@ static void tpm_passthrough_class_init(ObjectClass *klass, void *data)\n     TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);\n \n     tbc->ops = &tpm_passthrough_driver;\n+    tbc->handle_request = tpm_passthrough_handle_request;\n }\n \n static const TypeInfo tpm_passthrough_info = {\ndiff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h\nindex 3708413..58308b3 100644\n--- a/include/sysemu/tpm_backend.h\n+++ b/include/sysemu/tpm_backend.h\n@@ -29,22 +29,24 @@\n \n typedef struct TPMBackendClass TPMBackendClass;\n typedef struct TPMBackend TPMBackend;\n-\n typedef struct TPMDriverOps TPMDriverOps;\n+typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done);\n \n-struct TPMBackendClass {\n-    ObjectClass parent_class;\n-\n-    const TPMDriverOps *ops;\n-\n-    void (*opened)(TPMBackend *s, Error **errp);\n-};\n+typedef enum TPMBackendCmd {\n+    TPM_BACKEND_CMD_INIT = 1,\n+    TPM_BACKEND_CMD_PROCESS_CMD,\n+    TPM_BACKEND_CMD_END,\n+    TPM_BACKEND_CMD_TPM_RESET,\n+} TPMBackendCmd;\n \n struct TPMBackend {\n     Object parent;\n \n     /*< protected >*/\n     bool opened;\n+    TPMState *tpm_state;\n+    GThreadPool *thread_pool;\n+    TPMRecvDataCB *recv_data_callback;\n \n     char *id;\n     enum TpmModel fe_model;\n@@ -54,7 +56,15 @@ struct TPMBackend {\n     QLIST_ENTRY(TPMBackend) list;\n };\n \n-typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done);\n+struct TPMBackendClass {\n+    ObjectClass parent_class;\n+\n+    const TPMDriverOps *ops;\n+\n+    void (*opened)(TPMBackend *s, Error **errp);\n+\n+    void (*handle_request)(TPMBackend *s, TPMBackendCmd cmd);\n+};\n \n typedef struct TPMSizedBuffer {\n     uint32_t size;\n@@ -71,7 +81,7 @@ struct TPMDriverOps {\n     void (*destroy)(TPMBackend *t);\n \n     /* initialize the backend */\n-    int (*init)(TPMBackend *t, TPMState *s, TPMRecvDataCB *datacb);\n+    int (*init)(TPMBackend *t);\n     /* start up the TPM on the backend */\n     int (*startup_tpm)(TPMBackend *t);\n     /* returns true if nothing will ever answer TPM requests */\n@@ -79,8 +89,6 @@ struct TPMDriverOps {\n \n     size_t (*realloc_buffer)(TPMSizedBuffer *sb);\n \n-    void (*deliver_request)(TPMBackend *t);\n-\n     void (*reset)(TPMBackend *t);\n \n     void (*cancel_cmd)(TPMBackend *t);\ndiff --git a/include/sysemu/tpm_backend_int.h b/include/sysemu/tpm_backend_int.h\ndeleted file mode 100644\nindex 00639dd..0000000\n--- a/include/sysemu/tpm_backend_int.h\n+++ /dev/null\n@@ -1,41 +0,0 @@\n-/*\n- *  common TPM backend driver functions\n- *\n- *  Copyright (c) 2012-2013 IBM Corporation\n- *  Authors:\n- *    Stefan Berger <stefanb@us.ibm.com>\n- *\n- * This library is free software; you can redistribute it and/or\n- * modify it under the terms of the GNU Lesser General Public\n- * License as published by the Free Software Foundation; either\n- * version 2 of the License, or (at your option) any later version.\n- *\n- * This library is distributed in the hope that it will be useful,\n- * but WITHOUT ANY WARRANTY; without even the implied warranty of\n- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n- * Lesser General Public License for more details.\n- *\n- * You should have received a copy of the GNU Lesser General Public\n- * License along with this library; if not, see <http://www.gnu.org/licenses/>\n- */\n-\n-#ifndef TPM_BACKEND_INT_H\n-#define TPM_BACKEND_INT_H\n-\n-typedef struct TPMBackendThread {\n-    GThreadPool *pool;\n-} TPMBackendThread;\n-\n-void tpm_backend_thread_deliver_request(TPMBackendThread *tbt);\n-void tpm_backend_thread_create(TPMBackendThread *tbt,\n-                               GFunc func, gpointer user_data);\n-void tpm_backend_thread_end(TPMBackendThread *tbt);\n-\n-typedef enum TPMBackendCmd {\n-    TPM_BACKEND_CMD_INIT = 1,\n-    TPM_BACKEND_CMD_PROCESS_CMD,\n-    TPM_BACKEND_CMD_END,\n-    TPM_BACKEND_CMD_TPM_RESET,\n-} TPMBackendCmd;\n-\n-#endif /* TPM_BACKEND_INT_H */\n","prefixes":["v7","2/8"]}