Skip to content

Commit db46df9

Browse files
fyin1jren1
authored andcommitted
DM: add init/deinit function for mevent
Current, mevent cleanup path has issue. There are possible following calling sequence happen when reboot/poweroff UOS: 1. mevent_dispatch() calls mevent_destroy 2. do_close_post calls some virtual device deinit to delete mevent. This patch introduce mevent init/deinit to make sure: 1. mevent init 2. mevent_add is called when init virtual device 3. mevent_del is called when deinit virtual device 4. mevent deinit Signed-off-by: Yin Fengwei <fengwei.yin@intel.com> Reviewed-by: Zhao Yakui <yakui.zhao@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent c8116fc commit db46df9

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

devicemodel/core/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,13 @@ main(int argc, char *argv[])
770770
goto fail;
771771
}
772772

773+
err = mevent_init();
774+
if (err) {
775+
fprintf(stderr, "Unable to initialize mevent (%d)\n",
776+
errno);
777+
goto mevent_fail;
778+
}
779+
773780
init_mem();
774781
init_inout();
775782
pci_irq_init(ctx);
@@ -848,6 +855,7 @@ main(int argc, char *argv[])
848855
vrtc_deinit(ctx);
849856
atkbdc_deinit(ctx);
850857
vm_unsetup_memory(ctx);
858+
mevent_deinit();
851859
vm_destroy(ctx);
852860
vm_close(ctx);
853861
_ctx = 0;
@@ -861,6 +869,7 @@ main(int argc, char *argv[])
861869
monitor_close();
862870
vrtc_deinit(ctx);
863871
atkbdc_deinit(ctx);
872+
mevent_fail:
864873
vm_unsetup_memory(ctx);
865874
fail:
866875
vm_destroy(ctx);

devicemodel/core/mevent.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#define MEV_DISABLE 3
5959
#define MEV_DEL_PENDING 4
6060

61+
static int epoll_fd;
6162
static pthread_t mevent_tid;
6263
static int mevent_pipefd[2];
6364
static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
@@ -341,23 +342,38 @@ mevent_set_name(void)
341342
pthread_setname_np(mevent_tid, "mevent");
342343
}
343344

345+
int
346+
mevent_init(void)
347+
{
348+
epoll_fd = epoll_create1(0);
349+
assert(epoll_fd >= 0);
350+
351+
if (epoll_fd >= 0)
352+
return 0;
353+
else
354+
return -1;
355+
}
356+
357+
void
358+
mevent_deinit(void)
359+
{
360+
mevent_destroy();
361+
close(epoll_fd);
362+
}
363+
344364
void
345365
mevent_dispatch(void)
346366
{
347367
struct ctl_event clist[MEVENT_MAX];
348368
struct epoll_event eventlist[MEVENT_MAX];
349369

350370
struct mevent *pipev;
351-
int mfd;
352371
int numev;
353372
int ret;
354373

355374
mevent_tid = pthread_self();
356375
mevent_set_name();
357376

358-
mfd = epoll_create1(0);
359-
assert(mfd > 0);
360-
361377
/*
362378
* Open the pipe that will be used for other threads to force
363379
* the blocking kqueue call to exit by writing to it. Set the
@@ -385,19 +401,19 @@ mevent_dispatch(void)
385401
int i;
386402
struct epoll_event *e;
387403

388-
numev = mevent_build(mfd, clist);
404+
numev = mevent_build(epoll_fd, clist);
389405

390406
for (i = 0; i < numev; i++) {
391407
e = &clist[i].ee;
392-
ret = epoll_ctl(mfd, clist[i].op, clist[i].fd, e);
408+
ret = epoll_ctl(epoll_fd, clist[i].op, clist[i].fd, e);
393409
if (ret == -1)
394410
perror("Error return from epoll_ctl");
395411
}
396412

397413
/*
398414
* Block awaiting events
399415
*/
400-
ret = epoll_wait(mfd, eventlist, MEVENT_MAX, -1);
416+
ret = epoll_wait(epoll_fd, eventlist, MEVENT_MAX, -1);
401417
if (ret == -1 && errno != EINTR)
402418
perror("Error return from epoll_wait");
403419

@@ -409,7 +425,5 @@ mevent_dispatch(void)
409425
if (vm_get_suspend_mode() != VM_SUSPEND_NONE)
410426
break;
411427
}
412-
mevent_build(mfd, clist);
413-
mevent_destroy();
414-
close(mfd);
428+
mevent_build(epoll_fd, clist);
415429
}

devicemodel/include/mevent.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ int mevent_delete_close(struct mevent *evp);
4949
int mevent_notify(void);
5050

5151
void mevent_dispatch(void);
52+
int mevent_init(void);
53+
void mevent_deinit(void);
5254

5355
#define list_foreach_safe(var, head, field, tvar) \
5456
for ((var) = LIST_FIRST((head)); \

0 commit comments

Comments
 (0)