Skip to contentSkip to conversation

workqueue: fix potential reentrancy issue

PATCH In review Libin 2 participants 3 messages Sep 9, 2013 – Sep 10, 2013 Workqueue

lore
Share & tools
lore

Libin

author
#0 lore raw

PATCH 1/1 workqueue: fix potential reentrancy issue

From: Li Bin <[email protected]>

When one work starts execution, the high bits of work's data contain pool ID. It can represent a maximum of WORK_OFFQ_POOL_NONE. Pool ID is assigned WORK_OFFQ_POOL_NONE when the work being initialized indicating that no pool is associated and get_work_pool() uses it to check the associated pool. So if worker_pool_assign_id() assigns a ID greater than or equal WORK_OFFQ_POOL_NONE to a pool, it may break the non-reentrance guarantee.

This patch fix this issue by modifying the worker_pool_assign_id() function to add the WORK_OFFQ_POOL_NONE check condition.

---
 kernel/workqueue.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/workqueue.c +10 -1
index 41019b1..97d9ff7 100644
@@ -518,7 +518,14 @@ static inline void debug_work_activate(struct work_struct *work) { }
 static inline void debug_work_deactivate(struct work_struct *work) { }
 #endif
 
-/* allocate ID and assign it to @pool */
+/**
+ * worker_pool_assign_id - allocate ID and assing it to @pool
+ * @pool: the pool pointer of interest
+ *
+ * Return 0 if ID assigned successful.
+ * Return non-zero if the allocation fails or the ID number out of
+ * %WORK_OFFQ_POOL_NONE range.
+ */
 static int worker_pool_assign_id(struct worker_pool *pool)
 {
 	int ret;
@@ -526,6 +533,8 @@ static int worker_pool_assign_id(struct worker_pool *pool)
 	lockdep_assert_held(&wq_pool_mutex);
 
 	ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
+	if (ret >= WORK_OFFQ_POOL_NONE)
+		return ret;
 	if (ret >= 0) {
 		pool->id = ret;
 		return 0;
Signature · 1 line
1.8.2.1
maintainer
#1 lore raw
Quoted 39 lines · Libin

From: Li Bin <[email protected]>

When one work starts execution, the high bits of work's data contain pool ID. It can represent a maximum of WORK_OFFQ_POOL_NONE. Pool ID is assigned WORK_OFFQ_POOL_NONE when the work being initialized indicating that no pool is associated and get_work_pool() uses it to check the associated pool. So if worker_pool_assign_id() assigns a ID greater than or equal WORK_OFFQ_POOL_NONE to a pool, it may break the non-reentrance guarantee.

This patch fix this issue by modifying the worker_pool_assign_id() function to add the WORK_OFFQ_POOL_NONE check condition.

---
 kernel/workqueue.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/workqueue.c +8 -1
index 41019b1..97d9ff7 100644
@@ -518,7 +518,14 @@ static inline void debug_work_activate(struct work_struct *work) { }
 static inline void debug_work_deactivate(struct work_struct *work) { }
 #endif
 
-/* allocate ID and assign it to @pool */
+/**
+ * worker_pool_assign_id - allocate ID and assing it to @pool
+ * @pool: the pool pointer of interest
+ *
+ * Return 0 if ID assigned successful.
+ * Return non-zero if the allocation fails or the ID number out of
+ * %WORK_OFFQ_POOL_NONE range.
+ */
 static int worker_pool_assign_id(struct worker_pool *pool)
 {
 	int ret;

kernel/workqueue.c static int worker_pool_assign_id(struct worker_pool *pool) · line 537

Quoted 5 lines
kernel/workqueue.c +2 -0
@@ -526,6 +533,8 @@ static int worker_pool_assign_id(struct worker_pool *pool)
 	lockdep_assert_held(&wq_pool_mutex);
 
 	ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
+	if (ret >= WORK_OFFQ_POOL_NONE)
+		return ret;

Hmmm.... this would leak the allocated ID. Just setting @end param to idr_alloc to WORK_OFFQ_POOL_NONE would work, right?

Thanks.

Signature · 1 line
tejun
author
#2 lore raw

Hello Tejun,

Quoted 42 lines · Tejun Heo
Quoted 41 lines · Libin

From: Li Bin <[email protected]>

When one work starts execution, the high bits of work's data contain pool ID. It can represent a maximum of WORK_OFFQ_POOL_NONE. Pool ID is assigned WORK_OFFQ_POOL_NONE when the work being initialized indicating that no pool is associated and get_work_pool() uses it to check the associated pool. So if worker_pool_assign_id() assigns a ID greater than or equal WORK_OFFQ_POOL_NONE to a pool, it may break the non-reentrance guarantee.

This patch fix this issue by modifying the worker_pool_assign_id() function to add the WORK_OFFQ_POOL_NONE check condition.

---
 kernel/workqueue.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/workqueue.c +8 -1
index 41019b1..97d9ff7 100644
@@ -518,7 +518,14 @@ static inline void debug_work_activate(struct work_struct *work) { }
 static inline void debug_work_deactivate(struct work_struct *work) { }
 #endif
 
-/* allocate ID and assign it to @pool */
+/**
+ * worker_pool_assign_id - allocate ID and assing it to @pool
+ * @pool: the pool pointer of interest
+ *
+ * Return 0 if ID assigned successful.
+ * Return non-zero if the allocation fails or the ID number out of
+ * %WORK_OFFQ_POOL_NONE range.
+ */
 static int worker_pool_assign_id(struct worker_pool *pool)
 {
 	int ret;
@@ -526,6 +533,8 @@ static int worker_pool_assign_id(struct worker_pool *pool)
 	lockdep_assert_held(&wq_pool_mutex);
 
Quoted 7 lines

kernel/workqueue.c static int worker_pool_assign_id(struct worker_pool *pool) · line 537

Quoted 3 lines
kernel/workqueue.c +2 -0
 	ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
+	if (ret >= WORK_OFFQ_POOL_NONE)
+		return ret;

Hmmm.... this would leak the allocated ID. Just setting @end param to idr_alloc to WORK_OFFQ_POOL_NONE would work, right?

Yes, I will update it with the last patch.
Thanks!
Libin

Quoted 2 lines

Thanks.