Skip to content

Commit

Permalink
Bluetooth: hidp: Fix module reference cleanup
Browse files Browse the repository at this point in the history
Calling module_put(THIS_MODULE) is *never* safe when we cannot go sure that we
own at least two references. This is because the call may unload our module
before it returns and then the "return" will jump into invalid memory.

Gladly, module.h provides a wrapper for kthread-users: module_put_and_exit().
This puts our module and then exits the kthread without returning to the module.

This patch fixes the hidp kthread to use this wrapper instead of manually
freeing its own reference. See nfsd or lockd for other kthreads using this.

Calling __module_get() inside the kthread is safe as the hidp module will always
wait until the kthread sets "waiting_for_startup" to 0.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
  • Loading branch information
David Herrmann authored and Gustavo F. Padovan committed Nov 7, 2011
1 parent 7f103a0 commit 25df084
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions net/bluetooth/hidp/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr)

static void __hidp_link_session(struct hidp_session *session)
{
__module_get(THIS_MODULE);
list_add(&session->list, &hidp_session_list);
}

Expand All @@ -103,7 +102,6 @@ static void __hidp_unlink_session(struct hidp_session *session)
hci_conn_put_device(session->conn);

list_del(&session->list);
module_put(THIS_MODULE);
}

static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
Expand Down Expand Up @@ -703,6 +701,7 @@ static int hidp_session(void *arg)

BT_DBG("session %p", session);

__module_get(THIS_MODULE);
set_user_nice(current, -15);

init_waitqueue_entry(&ctrl_wait, current);
Expand Down Expand Up @@ -781,6 +780,7 @@ static int hidp_session(void *arg)

kfree(session->rd_data);
kfree(session);
module_put_and_exit(0);
return 0;
}

Expand Down

0 comments on commit 25df084

Please sign in to comment.