From patchwork Tue Oct 10 01:33:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 9994761 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B8DD260216 for ; Tue, 10 Oct 2017 01:35:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A62C92888E for ; Tue, 10 Oct 2017 01:35:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8418228880; Tue, 10 Oct 2017 01:35:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2BCDA28860 for ; Tue, 10 Oct 2017 01:35:49 +0000 (UTC) Received: from localhost ([::1]:60611 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1jSW-0003lC-5Q for patchwork-qemu-devel@patchwork.kernel.org; Mon, 09 Oct 2017 21:35:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41611) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1jR9-0003jv-6l for qemu-devel@nongnu.org; Mon, 09 Oct 2017 21:34:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e1jR6-0000D9-07 for qemu-devel@nongnu.org; Mon, 09 Oct 2017 21:34:23 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:40620 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e1jR5-0000CX-Pv for qemu-devel@nongnu.org; Mon, 09 Oct 2017 21:34:19 -0400 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v9A1Y9sm057636 for ; Mon, 9 Oct 2017 21:34:15 -0400 Received: from e37.co.us.ibm.com (e37.co.us.ibm.com [32.97.110.158]) by mx0b-001b2d01.pphosted.com with ESMTP id 2dgjf95d46-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 09 Oct 2017 21:34:15 -0400 Received: from localhost by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 9 Oct 2017 19:34:14 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (9.17.130.18) by e37.co.us.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 9 Oct 2017 19:34:12 -0600 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v9A1YBvE64028722; Mon, 9 Oct 2017 18:34:11 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C1E7A78037; Mon, 9 Oct 2017 19:34:11 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id 38D9578043; Mon, 9 Oct 2017 19:34:11 -0600 (MDT) From: Stefan Berger To: qemu-devel@nongnu.org Date: Mon, 9 Oct 2017 21:33:47 -0400 X-Mailer: git-send-email 2.5.5 In-Reply-To: <1507599235-25123-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1507599235-25123-1-git-send-email-stefanb@linux.vnet.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 17101001-0024-0000-0000-0000175062ED X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007871; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000235; SDB=6.00928882; UDB=6.00467501; IPR=6.00709153; BA=6.00005631; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017467; XFM=3.00000015; UTC=2017-10-10 01:34:13 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17101001-0025-0000-0000-00004D0A1428 Message-Id: <1507599235-25123-3-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-10-09_07:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=4 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1710100022 X-MIME-Autoconverted: from 8bit to quoted-printable by mx0b-001b2d01.pphosted.com id v9A1Y9sm057636 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PULL v2 02/10] tpm-backend: Move thread handling inside TPMBackend X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , amarnath.valluri@intel.com, marcandre.lureau@gmail.com, Stefan Berger Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Amarnath Valluri Move thread handling inside TPMBackend, this way backend implementations need not to maintain their own thread life cycle, instead they needs to implement 'handle_request()' class method that always been called from a thread. This change made tpm_backend_int.h kind of useless, hence removed it. Signed-off-by: Amarnath Valluri Reviewed-by: Marc-André Lureau Reviewed-by: Stefan Berger Signed-off-by: Stefan Berger --- backends/tpm.c | 62 +++++++++++++++++++++++++--------------- hw/tpm/tpm_passthrough.c | 58 ++++++------------------------------- include/sysemu/tpm_backend.h | 32 +++++++++++++-------- include/sysemu/tpm_backend_int.h | 41 -------------------------- 4 files changed, 67 insertions(+), 126 deletions(-) delete mode 100644 include/sysemu/tpm_backend_int.h diff --git a/backends/tpm.c b/backends/tpm.c index 536f262..ce56c3b 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -18,7 +18,24 @@ #include "qapi/qmp/qerror.h" #include "sysemu/tpm.h" #include "qemu/thread.h" -#include "sysemu/tpm_backend_int.h" + +static void tpm_backend_worker_thread(gpointer data, gpointer user_data) +{ + TPMBackend *s = TPM_BACKEND(user_data); + TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); + + assert(k->handle_request != NULL); + k->handle_request(s, (TPMBackendCmd)data); +} + +static void tpm_backend_thread_end(TPMBackend *s) +{ + if (s->thread_pool) { + g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL); + g_thread_pool_free(s->thread_pool, FALSE, TRUE); + s->thread_pool = NULL; + } +} enum TpmType tpm_backend_get_type(TPMBackend *s) { @@ -39,6 +56,8 @@ void tpm_backend_destroy(TPMBackend *s) TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); k->ops->destroy(s); + + tpm_backend_thread_end(s); } int tpm_backend_init(TPMBackend *s, TPMState *state, @@ -46,13 +65,23 @@ int tpm_backend_init(TPMBackend *s, TPMState *state, { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - return k->ops->init(s, state, datacb); + s->tpm_state = state; + s->recv_data_callback = datacb; + + return k->ops->init(s); } int tpm_backend_startup_tpm(TPMBackend *s) { TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); + /* terminate a running TPM */ + tpm_backend_thread_end(s); + + s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, + NULL); + g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL); + return k->ops->startup_tpm(s); } @@ -72,9 +101,8 @@ size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb) void tpm_backend_deliver_request(TPMBackend *s) { - TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); - - k->ops->deliver_request(s); + g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, + NULL); } void tpm_backend_reset(TPMBackend *s) @@ -82,6 +110,8 @@ void tpm_backend_reset(TPMBackend *s) TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); k->ops->reset(s); + + tpm_backend_thread_end(s); } void tpm_backend_cancel_cmd(TPMBackend *s) @@ -156,29 +186,14 @@ static void tpm_backend_instance_init(Object *obj) tpm_backend_prop_get_opened, tpm_backend_prop_set_opened, NULL); -} -void tpm_backend_thread_deliver_request(TPMBackendThread *tbt) -{ - g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL); } -void tpm_backend_thread_create(TPMBackendThread *tbt, - GFunc func, gpointer user_data) +static void tpm_backend_instance_finalize(Object *obj) { - if (!tbt->pool) { - tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL); - g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL); - } -} + TPMBackend *s = TPM_BACKEND(obj); -void tpm_backend_thread_end(TPMBackendThread *tbt) -{ - if (tbt->pool) { - g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL); - g_thread_pool_free(tbt->pool, FALSE, TRUE); - tbt->pool = NULL; - } + tpm_backend_thread_end(s); } static const TypeInfo tpm_backend_info = { @@ -186,6 +201,7 @@ static const TypeInfo tpm_backend_info = { .parent = TYPE_OBJECT, .instance_size = sizeof(TPMBackend), .instance_init = tpm_backend_instance_init, + .instance_finalize = tpm_backend_instance_finalize, .class_size = sizeof(TPMBackendClass), .abstract = true, }; diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index a0baf5f..f50d9cf 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -30,7 +30,6 @@ #include "tpm_int.h" #include "hw/hw.h" #include "hw/i386/pc.h" -#include "sysemu/tpm_backend_int.h" #include "tpm_tis.h" #include "tpm_util.h" @@ -47,20 +46,9 @@ OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH) /* data structures */ -typedef struct TPMPassthruThreadParams { - TPMState *tpm_state; - - TPMRecvDataCB *recv_data_callback; - TPMBackend *tb; -} TPMPassthruThreadParams; - struct TPMPassthruState { TPMBackend parent; - TPMBackendThread tbt; - - TPMPassthruThreadParams tpm_thread_params; - char *tpm_dev; int tpm_fd; bool tpm_executing; @@ -214,12 +202,9 @@ static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt, selftest_done); } -static void tpm_passthrough_worker_thread(gpointer data, - gpointer user_data) +static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd cmd) { - TPMPassthruThreadParams *thr_parms = user_data; - TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb); - TPMBackendCmd cmd = (TPMBackendCmd)data; + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); bool selftest_done = false; DPRINTF("tpm_passthrough: processing command type %d\n", cmd); @@ -227,12 +212,12 @@ static void tpm_passthrough_worker_thread(gpointer data, switch (cmd) { case TPM_BACKEND_CMD_PROCESS_CMD: tpm_passthrough_unix_transfer(tpm_pt, - thr_parms->tpm_state->locty_data, + tb->tpm_state->locty_data, &selftest_done); - thr_parms->recv_data_callback(thr_parms->tpm_state, - thr_parms->tpm_state->locty_number, - selftest_done); + tb->recv_data_callback(tb->tpm_state, + tb->tpm_state->locty_number, + selftest_done); break; case TPM_BACKEND_CMD_INIT: case TPM_BACKEND_CMD_END: @@ -248,15 +233,6 @@ static void tpm_passthrough_worker_thread(gpointer data, */ static int tpm_passthrough_startup_tpm(TPMBackend *tb) { - TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); - - /* terminate a running TPM */ - tpm_backend_thread_end(&tpm_pt->tbt); - - tpm_backend_thread_create(&tpm_pt->tbt, - tpm_passthrough_worker_thread, - &tpm_pt->tpm_thread_params); - return 0; } @@ -268,20 +244,11 @@ static void tpm_passthrough_reset(TPMBackend *tb) tpm_passthrough_cancel_cmd(tb); - tpm_backend_thread_end(&tpm_pt->tbt); - tpm_pt->had_startup_error = false; } -static int tpm_passthrough_init(TPMBackend *tb, TPMState *s, - TPMRecvDataCB *recv_data_cb) +static int tpm_passthrough_init(TPMBackend *tb) { - TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); - - tpm_pt->tpm_thread_params.tpm_state = s; - tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb; - tpm_pt->tpm_thread_params.tb = tb; - return 0; } @@ -315,13 +282,6 @@ static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb) return sb->size; } -static void tpm_passthrough_deliver_request(TPMBackend *tb) -{ - TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); - - tpm_backend_thread_deliver_request(&tpm_pt->tbt); -} - static void tpm_passthrough_cancel_cmd(TPMBackend *tb) { TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); @@ -483,8 +443,6 @@ static void tpm_passthrough_destroy(TPMBackend *tb) tpm_passthrough_cancel_cmd(tb); - tpm_backend_thread_end(&tpm_pt->tbt); - qemu_close(tpm_pt->tpm_fd); qemu_close(tpm_pt->cancel_fd); @@ -520,7 +478,6 @@ static const TPMDriverOps tpm_passthrough_driver = { .realloc_buffer = tpm_passthrough_realloc_buffer, .reset = tpm_passthrough_reset, .had_startup_error = tpm_passthrough_get_startup_error, - .deliver_request = tpm_passthrough_deliver_request, .cancel_cmd = tpm_passthrough_cancel_cmd, .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag, .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag, @@ -540,6 +497,7 @@ static void tpm_passthrough_class_init(ObjectClass *klass, void *data) TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass); tbc->ops = &tpm_passthrough_driver; + tbc->handle_request = tpm_passthrough_handle_request; } static const TypeInfo tpm_passthrough_info = { diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 3708413..58308b3 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -29,22 +29,24 @@ typedef struct TPMBackendClass TPMBackendClass; typedef struct TPMBackend TPMBackend; - typedef struct TPMDriverOps TPMDriverOps; +typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done); -struct TPMBackendClass { - ObjectClass parent_class; - - const TPMDriverOps *ops; - - void (*opened)(TPMBackend *s, Error **errp); -}; +typedef enum TPMBackendCmd { + TPM_BACKEND_CMD_INIT = 1, + TPM_BACKEND_CMD_PROCESS_CMD, + TPM_BACKEND_CMD_END, + TPM_BACKEND_CMD_TPM_RESET, +} TPMBackendCmd; struct TPMBackend { Object parent; /*< protected >*/ bool opened; + TPMState *tpm_state; + GThreadPool *thread_pool; + TPMRecvDataCB *recv_data_callback; char *id; enum TpmModel fe_model; @@ -54,7 +56,15 @@ struct TPMBackend { QLIST_ENTRY(TPMBackend) list; }; -typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty, bool selftest_done); +struct TPMBackendClass { + ObjectClass parent_class; + + const TPMDriverOps *ops; + + void (*opened)(TPMBackend *s, Error **errp); + + void (*handle_request)(TPMBackend *s, TPMBackendCmd cmd); +}; typedef struct TPMSizedBuffer { uint32_t size; @@ -71,7 +81,7 @@ struct TPMDriverOps { void (*destroy)(TPMBackend *t); /* initialize the backend */ - int (*init)(TPMBackend *t, TPMState *s, TPMRecvDataCB *datacb); + int (*init)(TPMBackend *t); /* start up the TPM on the backend */ int (*startup_tpm)(TPMBackend *t); /* returns true if nothing will ever answer TPM requests */ @@ -79,8 +89,6 @@ struct TPMDriverOps { size_t (*realloc_buffer)(TPMSizedBuffer *sb); - void (*deliver_request)(TPMBackend *t); - void (*reset)(TPMBackend *t); void (*cancel_cmd)(TPMBackend *t); diff --git a/include/sysemu/tpm_backend_int.h b/include/sysemu/tpm_backend_int.h deleted file mode 100644 index 00639dd..0000000 --- a/include/sysemu/tpm_backend_int.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * common TPM backend driver functions - * - * Copyright (c) 2012-2013 IBM Corporation - * Authors: - * Stefan Berger - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - */ - -#ifndef TPM_BACKEND_INT_H -#define TPM_BACKEND_INT_H - -typedef struct TPMBackendThread { - GThreadPool *pool; -} TPMBackendThread; - -void tpm_backend_thread_deliver_request(TPMBackendThread *tbt); -void tpm_backend_thread_create(TPMBackendThread *tbt, - GFunc func, gpointer user_data); -void tpm_backend_thread_end(TPMBackendThread *tbt); - -typedef enum TPMBackendCmd { - TPM_BACKEND_CMD_INIT = 1, - TPM_BACKEND_CMD_PROCESS_CMD, - TPM_BACKEND_CMD_END, - TPM_BACKEND_CMD_TPM_RESET, -} TPMBackendCmd; - -#endif /* TPM_BACKEND_INT_H */