---
title: "evdev的一个BUG"
author: "地球小野花"
date: "2011-03-31T10:53:57Z"
tags: []
categories: ["移动开发"]
slug: "evdev-bug"
draft: false
lastmod: "2011-03-31T10:53:57Z"
id: 981515
url: "https://android-sz.iteye.com/blog/981515"
---

自己调试的系统偶有死机，跟同事们一起琢磨的结果如下：在evdev.c文件中存在一个隐患的BUG。

static int evdev\_open(struct inode \*inode, struct file \*file)  
{  
                struct evdev \*evdev;  
  struct evdev\_client \*client;  
  int i = iminor(inode) - EVDEV\_MINOR\_BASE;  
  int error;

                if (i &gt;= EVDEV\_MINORS)  
  return -ENODEV;

  error = mutex\_lock\_interruptible(&evdev\_table\_mutex);  
  if (error)  
  return error;  
                evdev = evdev\_table\[i];  
  if (evdev)  
                                get\_device(&evdev-&gt;dev);  
                mutex\_unlock(&evdev\_table\_mutex);  
                if (!evdev)  
  return -ENODEV;  
  error = evdev\_open\_device(evdev);  
  if (error)  
                                goto err\_put\_evdev;

  client = kzalloc(sizeof(struct evdev\_client), GFP\_KERNEL);  
  if (!client) {  
                                error = -ENOMEM;  
                                goto err\_put\_evdev;  
                }

  spin\_lock\_init(&client-&gt;buffer\_lock);  
  wake\_lock\_init(&client-&gt;wake\_lock, WAKE\_LOCK\_SUSPEND, evdev-&gt;name);  
  client-&gt;evdev = evdev;  
  evdev\_attach\_client(evdev, client);

                error = evdev\_open\_device(evdev);  
                 if (error)  
                                goto err\_put\_evdev;

  file-&gt;private\_data = client;  
  return 0;

err\_free\_client:  
                evdev\_detach\_client(evdev, client);  
  kfree(client);  
err\_put\_evdev:  
                put\_device(&evdev-&gt;dev);  
                return error;  
}  
可能的问题在于：在wake\_lock\_init(&client-&gt;wake\_lock, WAKE\_LOCK\_SUSPEND, evdev-&gt;name);中会把wake\_lock插入到唤醒锁的链表中，如果在这个语句后面蓝色处还有导致err\_free\_client的发生，其里面有kfree(client)会把刚插入的那个wake\_lock空间也释放，造成链表断裂，引起系统不稳定。所以要将可能导致goto err\_put\_evdev的evdev\_open\_device放在插入wake\_lock之前，即红色处。

from:[http://blog.csdn.net/zhandoushi1982/archive/2010/08/09/5799937.aspx](http://blog.csdn.net/zhandoushi1982/archive/2010/08/09/5799937.aspx)

mark:以后测试机器有用