History

workqueue: make alloc_workqueue() take printf fmt and args for name

alloc_workqueue() currently expects the passed in @name pointer to remain accessible. This is inconvenient and a bit silly given that the whole wq is being dynamically allocated. This patch updates alloc_workqueue() and friends to take printf format string instead of opaque string and matching varargs at the end. The name is allocated together with the wq and formatted. alloc_ordered_workqueue() is converted to a macro to unify varargs handling with alloc_workqueue(), and, while at it, add comment to alloc_workqueue(). None of the current in-kernel users pass in string with '%' as constant name and this change shouldn't cause any problem. [akpm@linux-foundation.org: use __printf] Signed-off-by: Tejun Heo <tj@kernel.org> Suggested-by: Christoph Hellwig <hch@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Tejun Heo committed 14 years ago

Commit b196be81 parent 04996802 files · +53 −26Browse files

Showing the changes to kernel/workqueue.c (1 of 2 files). Show all

Unified · Split

kernel/workqueue.c+22−10
@@ -242,10 +242,10 @@ struct workqueue_struct { · 241 unmodified lines
242242
243243 int nr_drainers; /* W: drain in progress */
244244 int saved_max_active; /* W: saved cwq max_active */
245− const char *name; /* I: workqueue name */
246245 #ifdef CONFIG_LOCKDEP
247246 struct lockdep_map lockdep_map;
248247 #endif
248+ char name[]; /* I: workqueue name */
249249 };
250250
251251 struct workqueue_struct *system_wq __read_mostly;
@@ -2954,14 +2954,29 @@ static int wq_clamp_max_active(int max_active, unsigned int flags, · 2702 unmodified lines
29542954 return clamp_val(max_active, 1, lim);
29552955 }
29562956
2957−struct workqueue_struct *__alloc_workqueue_key(const char *name,
2957+struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
29582958 unsigned int flags,
29592959 int max_active,
29602960 struct lock_class_key *key,
2961− const char *lock_name)
2961+ const char *lock_name, ...)
29622962 {
2963+ va_list args, args1;
29632964 struct workqueue_struct *wq;
29642965 unsigned int cpu;
2966+ size_t namelen;
2967+
2968+ /* determine namelen, allocate wq and format name */
2969+ va_start(args, lock_name);
2970+ va_copy(args1, args);
2971+ namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2972+
2973+ wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2974+ if (!wq)
2975+ goto err;
2976+
2977+ vsnprintf(wq->name, namelen, fmt, args1);
2978+ va_end(args);
2979+ va_end(args1);
29652980
29662981 /*
29672982 * Workqueues which may be used during memory reclaim should
@@ -2978,12 +2993,9 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, · 10 unmodified lines
29782993 flags |= WQ_HIGHPRI;
29792994
29802995 max_active = max_active ?: WQ_DFL_ACTIVE;
2981− max_active = wq_clamp_max_active(max_active, flags, name);
2982−
2983− wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2984− if (!wq)
2985− goto err;
2996+ max_active = wq_clamp_max_active(max_active, flags, wq->name);
29862997
2998+ /* init wq */
29872999 wq->flags = flags;
29883000 wq->saved_max_active = max_active;
29893001 mutex_init(&wq->flush_mutex);
@@ -2991,7 +3003,6 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, · 1 unmodified line
29913003 INIT_LIST_HEAD(&wq->flusher_queue);
29923004 INIT_LIST_HEAD(&wq->flusher_overflow);
29933005
2994− wq->name = name;
29953006 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
29963007 INIT_LIST_HEAD(&wq->list);
29973008
@@ -3020,7 +3031,8 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name, · 22 unmodified lines
30203031 if (!rescuer)
30213032 goto err;
30223033
3023− rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
3034+ rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3035+ wq->name);
30243036 if (IS_ERR(rescuer->task))
30253037 goto err;
30263038