~ubuntu-branches/ubuntu/raring/linux-nexus7/raring-proposed

« back to all changes in this revision

Viewing changes to block/bfq-cgroup.c

  • Committer: Package Import Robot
  • Author(s): Tim Gardner, Andy Whitcroft, Erez Zadok, Miklos Szeredi, Neil Brown, Paolo Pisati, Tim Gardner
  • Date: 2013-02-27 11:55:02 UTC
  • Revision ID: package-import@ubuntu.com-20130227115502-979tupqdiwnd6bob
Tags: 3.1.10-10.28
[ Andy Whitcroft ]

* ubuntu: overlayfs -- overlayfs: add statfs support
  - LP: #1076317
* ubuntu: overlayfs -- overlayfs: apply device cgroup and security
  permissions to overlay files
  - LP: #1076317, #915941, #918212
  - CVE-2012-0055
* [packaging] Rename from linaro to nexus7 -- part 2

[ Erez Zadok ]

* ubuntu: overlayfs -- overlayfs: implement show_options
  - LP: #1076317

[ Miklos Szeredi ]

* ubuntu: overlayfs -- vfs: pass struct path to __dentry_open()
  - LP: #1076317
* ubuntu: overlayfs -- vfs: add i_op->open()
  - LP: #1076317
* ubuntu: overlayfs -- vfs: export do_splice_direct() to modules
  - LP: #1076317
* ubuntu: overlayfs -- vfs: introduce clone_private_mount()
  - LP: #1076317
* ubuntu: overlayfs -- overlay filesystem
  - LP: #1076317
* ubuntu: overlayfs -- fs: limit filesystem stacking depth
  - LP: #1076317

[ Neil Brown ]

* ubuntu: overlayfs -- overlay: overlay filesystem documentation
  - LP: #1076317

[ Paolo Pisati ]

* [Config] OVERLAYFS_FS=m
  - LP: #1076317
* SAUCE: compilation fix for missing symbols
  - LP: #1076317

[ Tim Gardner ]

* Rebased against git://phablet.ubuntu.com/CyanogenMod/android_kernel_asus_grouper.git phablet-10.1
* Updated configs to be more device consistent with
  arch/arm/configs/cyanogenmod_grouper_defconfig
  https://wiki.ubuntu.com/Touch/Porting#Kernel
  http://phablet.ubuntu.com/gitweb?p=CyanogenMod/android_kernel_asus_grouper.git;a=shortlog;h=refs/heads/phablet-10.1
* Pull config changes from git://phablet.ubuntu.com/CyanogenMod/android_kernel_asus_grouper.git phablet-10.1
  CONFIG_NAMESPACES=y
  CONFIG_UTS_NS=y
  CONFIG_IPC_NS=y
  CONFIG_USER_NS=y
  CONFIG_PID_NS=y
  CONFIG_NET_NS=y
  CONFIG_DEVTMPFS=y
  CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
  CONFIG_DNOTIFY=y
  CONFIG_FANOTIFY=y
  CONFIG_FANOTIFY_ACCESS_PERMISSIONS=y
  ONFIG_ANDROID_PARANOID_NETWORK=n
  ONFIG_DEVPTS_MULTIPLE_INSTANCES=y
  CONFIG_SYSVIPC=y
* [packaging] Rename from linaro to nexus7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * BFQ: CGROUPS support.
 
3
 *
 
4
 * Based on ideas and code from CFQ:
 
5
 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
 
6
 *
 
7
 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
 
8
 *                    Paolo Valente <paolo.valente@unimore.it>
 
9
 *
 
10
 * Licensed under the GPL-2 as detailed in the accompanying COPYING.BFQ file.
 
11
 */
 
12
 
 
13
#ifdef CONFIG_CGROUP_BFQIO
 
14
static struct bfqio_cgroup bfqio_root_cgroup = {
 
15
        .weight = BFQ_DEFAULT_GRP_WEIGHT,
 
16
        .ioprio = BFQ_DEFAULT_GRP_IOPRIO,
 
17
        .ioprio_class = BFQ_DEFAULT_GRP_CLASS,
 
18
};
 
19
 
 
20
static inline void bfq_init_entity(struct bfq_entity *entity,
 
21
                                   struct bfq_group *bfqg)
 
22
{
 
23
        entity->weight = entity->new_weight;
 
24
        entity->orig_weight = entity->new_weight;
 
25
        entity->ioprio = entity->new_ioprio;
 
26
        entity->ioprio_class = entity->new_ioprio_class;
 
27
        entity->parent = bfqg->my_entity;
 
28
        entity->sched_data = &bfqg->sched_data;
 
29
}
 
30
 
 
31
static struct bfqio_cgroup *cgroup_to_bfqio(struct cgroup *cgroup)
 
32
{
 
33
        return container_of(cgroup_subsys_state(cgroup, bfqio_subsys_id),
 
34
                            struct bfqio_cgroup, css);
 
35
}
 
36
 
 
37
/*
 
38
 * Search the bfq_group for bfqd into the hash table (by now only a list)
 
39
 * of bgrp.  Must be called under rcu_read_lock().
 
40
 */
 
41
static struct bfq_group *bfqio_lookup_group(struct bfqio_cgroup *bgrp,
 
42
                                            struct bfq_data *bfqd)
 
43
{
 
44
        struct bfq_group *bfqg;
 
45
        struct hlist_node *n;
 
46
        void *key;
 
47
 
 
48
        hlist_for_each_entry_rcu(bfqg, n, &bgrp->group_data, group_node) {
 
49
                key = rcu_dereference(bfqg->bfqd);
 
50
                if (key == bfqd)
 
51
                        return bfqg;
 
52
        }
 
53
 
 
54
        return NULL;
 
55
}
 
56
 
 
57
static inline void bfq_group_init_entity(struct bfqio_cgroup *bgrp,
 
58
                                         struct bfq_group *bfqg)
 
59
{
 
60
        struct bfq_entity *entity = &bfqg->entity;
 
61
 
 
62
        entity->weight = entity->new_weight = bgrp->weight;
 
63
        entity->orig_weight = entity->new_weight;
 
64
        entity->ioprio = entity->new_ioprio = bgrp->ioprio;
 
65
        entity->ioprio_class = entity->new_ioprio_class = bgrp->ioprio_class;
 
66
        entity->ioprio_changed = 1;
 
67
        entity->my_sched_data = &bfqg->sched_data;
 
68
}
 
69
 
 
70
static inline void bfq_group_set_parent(struct bfq_group *bfqg,
 
71
                                        struct bfq_group *parent)
 
72
{
 
73
        struct bfq_entity *entity;
 
74
 
 
75
        BUG_ON(parent == NULL);
 
76
        BUG_ON(bfqg == NULL);
 
77
 
 
78
        entity = &bfqg->entity;
 
79
        entity->parent = parent->my_entity;
 
80
        entity->sched_data = &parent->sched_data;
 
81
}
 
82
 
 
83
/**
 
84
 * bfq_group_chain_alloc - allocate a chain of groups.
 
85
 * @bfqd: queue descriptor.
 
86
 * @cgroup: the leaf cgroup this chain starts from.
 
87
 *
 
88
 * Allocate a chain of groups starting from the one belonging to
 
89
 * @cgroup up to the root cgroup.  Stop if a cgroup on the chain
 
90
 * to the root has already an allocated group on @bfqd.
 
91
 */
 
92
static struct bfq_group *bfq_group_chain_alloc(struct bfq_data *bfqd,
 
93
                                               struct cgroup *cgroup)
 
94
{
 
95
        struct bfqio_cgroup *bgrp;
 
96
        struct bfq_group *bfqg, *prev = NULL, *leaf = NULL;
 
97
 
 
98
        for (; cgroup != NULL; cgroup = cgroup->parent) {
 
99
                bgrp = cgroup_to_bfqio(cgroup);
 
100
 
 
101
                bfqg = bfqio_lookup_group(bgrp, bfqd);
 
102
                if (bfqg != NULL) {
 
103
                        /*
 
104
                         * All the cgroups in the path from there to the
 
105
                         * root must have a bfq_group for bfqd, so we don't
 
106
                         * need any more allocations.
 
107
                         */
 
108
                        break;
 
109
                }
 
110
 
 
111
                bfqg = kzalloc(sizeof(*bfqg), GFP_ATOMIC);
 
112
                if (bfqg == NULL)
 
113
                        goto cleanup;
 
114
 
 
115
                bfq_group_init_entity(bgrp, bfqg);
 
116
                bfqg->my_entity = &bfqg->entity;
 
117
 
 
118
                if (leaf == NULL) {
 
119
                        leaf = bfqg;
 
120
                        prev = leaf;
 
121
                } else {
 
122
                        bfq_group_set_parent(prev, bfqg);
 
123
                        /*
 
124
                         * Build a list of allocated nodes using the bfqd
 
125
                         * filed, that is still unused and will be initialized
 
126
                         * only after the node will be connected.
 
127
                         */
 
128
                        prev->bfqd = bfqg;
 
129
                        prev = bfqg;
 
130
                }
 
131
        }
 
132
 
 
133
        return leaf;
 
134
 
 
135
cleanup:
 
136
        while (leaf != NULL) {
 
137
                prev = leaf;
 
138
                leaf = leaf->bfqd;
 
139
                kfree(prev);
 
140
        }
 
141
 
 
142
        return NULL;
 
143
}
 
144
 
 
145
/**
 
146
 * bfq_group_chain_link - link an allocatd group chain to a cgroup hierarchy.
 
147
 * @bfqd: the queue descriptor.
 
148
 * @cgroup: the leaf cgroup to start from.
 
149
 * @leaf: the leaf group (to be associated to @cgroup).
 
150
 *
 
151
 * Try to link a chain of groups to a cgroup hierarchy, connecting the
 
152
 * nodes bottom-up, so we can be sure that when we find a cgroup in the
 
153
 * hierarchy that already as a group associated to @bfqd all the nodes
 
154
 * in the path to the root cgroup have one too.
 
155
 *
 
156
 * On locking: the queue lock protects the hierarchy (there is a hierarchy
 
157
 * per device) while the bfqio_cgroup lock protects the list of groups
 
158
 * belonging to the same cgroup.
 
159
 */
 
160
static void bfq_group_chain_link(struct bfq_data *bfqd, struct cgroup *cgroup,
 
161
                                 struct bfq_group *leaf)
 
162
{
 
163
        struct bfqio_cgroup *bgrp;
 
164
        struct bfq_group *bfqg, *next, *prev = NULL;
 
165
        unsigned long flags;
 
166
 
 
167
        assert_spin_locked(bfqd->queue->queue_lock);
 
168
 
 
169
        for (; cgroup != NULL && leaf != NULL; cgroup = cgroup->parent) {
 
170
                bgrp = cgroup_to_bfqio(cgroup);
 
171
                next = leaf->bfqd;
 
172
 
 
173
                bfqg = bfqio_lookup_group(bgrp, bfqd);
 
174
                BUG_ON(bfqg != NULL);
 
175
 
 
176
                spin_lock_irqsave(&bgrp->lock, flags);
 
177
 
 
178
                rcu_assign_pointer(leaf->bfqd, bfqd);
 
179
                hlist_add_head_rcu(&leaf->group_node, &bgrp->group_data);
 
180
                hlist_add_head(&leaf->bfqd_node, &bfqd->group_list);
 
181
 
 
182
                spin_unlock_irqrestore(&bgrp->lock, flags);
 
183
 
 
184
                prev = leaf;
 
185
                leaf = next;
 
186
        }
 
187
 
 
188
        BUG_ON(cgroup == NULL && leaf != NULL);
 
189
        if (cgroup != NULL && prev != NULL) {
 
190
                bgrp = cgroup_to_bfqio(cgroup);
 
191
                bfqg = bfqio_lookup_group(bgrp, bfqd);
 
192
                bfq_group_set_parent(prev, bfqg);
 
193
        }
 
194
}
 
195
 
 
196
/**
 
197
 * bfq_find_alloc_group - return the group associated to @bfqd in @cgroup.
 
198
 * @bfqd: queue descriptor.
 
199
 * @cgroup: cgroup being searched for.
 
200
 *
 
201
 * Return a group associated to @bfqd in @cgroup, allocating one if
 
202
 * necessary.  When a group is returned all the cgroups in the path
 
203
 * to the root have a group associated to @bfqd.
 
204
 *
 
205
 * If the allocation fails, return the root group: this breaks guarantees
 
206
 * but is a safe fallbak.  If this loss becames a problem it can be
 
207
 * mitigated using the equivalent weight (given by the product of the
 
208
 * weights of the groups in the path from @group to the root) in the
 
209
 * root scheduler.
 
210
 *
 
211
 * We allocate all the missing nodes in the path from the leaf cgroup
 
212
 * to the root and we connect the nodes only after all the allocations
 
213
 * have been successful.
 
214
 */
 
215
static struct bfq_group *bfq_find_alloc_group(struct bfq_data *bfqd,
 
216
                                              struct cgroup *cgroup)
 
217
{
 
218
        struct bfqio_cgroup *bgrp = cgroup_to_bfqio(cgroup);
 
219
        struct bfq_group *bfqg;
 
220
 
 
221
        bfqg = bfqio_lookup_group(bgrp, bfqd);
 
222
        if (bfqg != NULL)
 
223
                return bfqg;
 
224
 
 
225
        bfqg = bfq_group_chain_alloc(bfqd, cgroup);
 
226
        if (bfqg != NULL)
 
227
                bfq_group_chain_link(bfqd, cgroup, bfqg);
 
228
        else
 
229
                bfqg = bfqd->root_group;
 
230
 
 
231
        return bfqg;
 
232
}
 
233
 
 
234
/**
 
235
 * bfq_bfqq_move - migrate @bfqq to @bfqg.
 
236
 * @bfqd: queue descriptor.
 
237
 * @bfqq: the queue to move.
 
238
 * @entity: @bfqq's entity.
 
239
 * @bfqg: the group to move to.
 
240
 *
 
241
 * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
 
242
 * it on the new one.  Avoid putting the entity on the old group idle tree.
 
243
 *
 
244
 * Must be called under the queue lock; the cgroup owning @bfqg must
 
245
 * not disappear (by now this just means that we are called under
 
246
 * rcu_read_lock()).
 
247
 */
 
248
static void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
 
249
                          struct bfq_entity *entity, struct bfq_group *bfqg)
 
250
{
 
251
        int busy, resume;
 
252
 
 
253
        busy = bfq_bfqq_busy(bfqq);
 
254
        resume = !RB_EMPTY_ROOT(&bfqq->sort_list);
 
255
 
 
256
        BUG_ON(resume && !entity->on_st);
 
257
        BUG_ON(busy && !resume && entity->on_st && bfqq != bfqd->active_queue);
 
258
 
 
259
        if (busy) {
 
260
                BUG_ON(atomic_read(&bfqq->ref) < 2);
 
261
 
 
262
                if (!resume)
 
263
                        bfq_del_bfqq_busy(bfqd, bfqq, 0);
 
264
                else
 
265
                        bfq_deactivate_bfqq(bfqd, bfqq, 0);
 
266
        } else if (entity->on_st)
 
267
                bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
 
268
 
 
269
        /*
 
270
         * Here we use a reference to bfqg.  We don't need a refcounter
 
271
         * as the cgroup reference will not be dropped, so that its
 
272
         * destroy() callback will not be invoked.
 
273
         */
 
274
        entity->parent = bfqg->my_entity;
 
275
        entity->sched_data = &bfqg->sched_data;
 
276
 
 
277
        if (busy && resume)
 
278
                bfq_activate_bfqq(bfqd, bfqq);
 
279
}
 
280
 
 
281
/**
 
282
 * __bfq_cic_change_cgroup - move @cic to @cgroup.
 
283
 * @bfqd: the queue descriptor.
 
284
 * @cic: the cic to move.
 
285
 * @cgroup: the cgroup to move to.
 
286
 *
 
287
 * Move cic to cgroup, assuming that bfqd->queue is locked; the caller
 
288
 * has to make sure that the reference to cgroup is valid across the call.
 
289
 *
 
290
 * NOTE: an alternative approach might have been to store the current
 
291
 * cgroup in bfqq and getting a reference to it, reducing the lookup
 
292
 * time here, at the price of slightly more complex code.
 
293
 */
 
294
static struct bfq_group *__bfq_cic_change_cgroup(struct bfq_data *bfqd,
 
295
                                                 struct cfq_io_context *cic,
 
296
                                                 struct cgroup *cgroup)
 
297
{
 
298
        struct bfq_queue *async_bfqq = cic_to_bfqq(cic, 0);
 
299
        struct bfq_queue *sync_bfqq = cic_to_bfqq(cic, 1);
 
300
        struct bfq_entity *entity;
 
301
        struct bfq_group *bfqg;
 
302
 
 
303
        bfqg = bfq_find_alloc_group(bfqd, cgroup);
 
304
        if (async_bfqq != NULL) {
 
305
                entity = &async_bfqq->entity;
 
306
 
 
307
                if (entity->sched_data != &bfqg->sched_data) {
 
308
                        cic_set_bfqq(cic, NULL, 0);
 
309
                        bfq_log_bfqq(bfqd, async_bfqq,
 
310
                                     "cic_change_group: %p %d",
 
311
                                     async_bfqq, atomic_read(&async_bfqq->ref));
 
312
                        bfq_put_queue(async_bfqq);
 
313
                }
 
314
        }
 
315
 
 
316
        if (sync_bfqq != NULL) {
 
317
                entity = &sync_bfqq->entity;
 
318
                if (entity->sched_data != &bfqg->sched_data)
 
319
                        bfq_bfqq_move(bfqd, sync_bfqq, entity, bfqg);
 
320
        }
 
321
 
 
322
        return bfqg;
 
323
}
 
324
 
 
325
/**
 
326
 * bfq_cic_change_cgroup - move @cic to @cgroup.
 
327
 * @cic: the cic being migrated.
 
328
 * @cgroup: the destination cgroup.
 
329
 *
 
330
 * When the task owning @cic is moved to @cgroup, @cic is immediately
 
331
 * moved into its new parent group.
 
332
 */
 
333
static void bfq_cic_change_cgroup(struct cfq_io_context *cic,
 
334
                                  struct cgroup *cgroup)
 
335
{
 
336
        struct bfq_data *bfqd;
 
337
        unsigned long uninitialized_var(flags);
 
338
 
 
339
        bfqd = bfq_get_bfqd_locked(&cic->key, &flags);
 
340
        if (bfqd != NULL &&
 
341
            !strncmp(bfqd->queue->elevator->elevator_type->elevator_name,
 
342
                     "bfq", ELV_NAME_MAX)) {
 
343
                __bfq_cic_change_cgroup(bfqd, cic, cgroup);
 
344
                bfq_put_bfqd_unlock(bfqd, &flags);
 
345
        }
 
346
}
 
347
 
 
348
/**
 
349
 * bfq_cic_update_cgroup - update the cgroup of @cic.
 
350
 * @cic: the @cic to update.
 
351
 *
 
352
 * Make sure that @cic is enqueued in the cgroup of the current task.
 
353
 * We need this in addition to moving cics during the cgroup attach
 
354
 * phase because the task owning @cic could be at its first disk
 
355
 * access or we may end up in the root cgroup as the result of a
 
356
 * memory allocation failure and here we try to move to the right
 
357
 * group.
 
358
 *
 
359
 * Must be called under the queue lock.  It is safe to use the returned
 
360
 * value even after the rcu_read_unlock() as the migration/destruction
 
361
 * paths act under the queue lock too.  IOW it is impossible to race with
 
362
 * group migration/destruction and end up with an invalid group as:
 
363
 *   a) here cgroup has not yet been destroyed, nor its destroy callback
 
364
 *      has started execution, as current holds a reference to it,
 
365
 *   b) if it is destroyed after rcu_read_unlock() [after current is
 
366
 *      migrated to a different cgroup] its attach() callback will have
 
367
 *      taken care of remove all the references to the old cgroup data.
 
368
 */
 
369
static struct bfq_group *bfq_cic_update_cgroup(struct cfq_io_context *cic)
 
370
{
 
371
        struct bfq_data *bfqd = cic->key;
 
372
        struct bfq_group *bfqg;
 
373
        struct cgroup *cgroup;
 
374
 
 
375
        BUG_ON(bfqd == NULL);
 
376
 
 
377
        rcu_read_lock();
 
378
        cgroup = task_cgroup(current, bfqio_subsys_id);
 
379
        bfqg = __bfq_cic_change_cgroup(bfqd, cic, cgroup);
 
380
        rcu_read_unlock();
 
381
 
 
382
        return bfqg;
 
383
}
 
384
 
 
385
/**
 
386
 * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
 
387
 * @st: the service tree being flushed.
 
388
 */
 
389
static inline void bfq_flush_idle_tree(struct bfq_service_tree *st)
 
390
{
 
391
        struct bfq_entity *entity = st->first_idle;
 
392
 
 
393
        for (; entity != NULL; entity = st->first_idle)
 
394
                __bfq_deactivate_entity(entity, 0);
 
395
}
 
396
 
 
397
/**
 
398
 * bfq_reparent_leaf_entity - move leaf entity to the root_group.
 
399
 * @bfqd: the device data structure with the root group.
 
400
 * @entity: the entity to move.
 
401
 */
 
402
static inline void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
 
403
                                            struct bfq_entity *entity)
 
404
{
 
405
        struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
 
406
 
 
407
        BUG_ON(bfqq == NULL);
 
408
        bfq_bfqq_move(bfqd, bfqq, entity, bfqd->root_group);
 
409
        return;
 
410
}
 
411
 
 
412
/**
 
413
 * bfq_reparent_active_entities - move to the root group all active entities.
 
414
 * @bfqd: the device data structure with the root group.
 
415
 * @bfqg: the group to move from.
 
416
 * @st: the service tree with the entities.
 
417
 *
 
418
 * Needs queue_lock to be taken and reference to be valid over the call.
 
419
 */
 
420
static inline void bfq_reparent_active_entities(struct bfq_data *bfqd,
 
421
                                                struct bfq_group *bfqg,
 
422
                                                struct bfq_service_tree *st)
 
423
{
 
424
        struct rb_root *active = &st->active;
 
425
        struct bfq_entity *entity = NULL;
 
426
 
 
427
        if (!RB_EMPTY_ROOT(&st->active))
 
428
                entity = bfq_entity_of(rb_first(active));
 
429
 
 
430
        for (; entity != NULL ; entity = bfq_entity_of(rb_first(active)))
 
431
                bfq_reparent_leaf_entity(bfqd, entity);
 
432
 
 
433
        if (bfqg->sched_data.active_entity != NULL)
 
434
                bfq_reparent_leaf_entity(bfqd, bfqg->sched_data.active_entity);
 
435
 
 
436
        return;
 
437
}
 
438
 
 
439
/**
 
440
 * bfq_destroy_group - destroy @bfqg.
 
441
 * @bgrp: the bfqio_cgroup containing @bfqg.
 
442
 * @bfqg: the group being destroyed.
 
443
 *
 
444
 * Destroy @bfqg, making sure that it is not referenced from its parent.
 
445
 */
 
446
static void bfq_destroy_group(struct bfqio_cgroup *bgrp, struct bfq_group *bfqg)
 
447
{
 
448
        struct bfq_data *bfqd;
 
449
        struct bfq_service_tree *st;
 
450
        struct bfq_entity *entity = bfqg->my_entity;
 
451
        unsigned long uninitialized_var(flags);
 
452
        int i;
 
453
 
 
454
        hlist_del(&bfqg->group_node);
 
455
 
 
456
        /*
 
457
         * Empty all service_trees belonging to this group before deactivating
 
458
         * the group itself.
 
459
         */
 
460
        for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
 
461
                st = bfqg->sched_data.service_tree + i;
 
462
 
 
463
                /*
 
464
                 * The idle tree may still contain bfq_queues belonging
 
465
                 * to exited task because they never migrated to a different
 
466
                 * cgroup from the one being destroyed now.  Noone else
 
467
                 * can access them so it's safe to act without any lock.
 
468
                 */
 
469
                bfq_flush_idle_tree(st);
 
470
 
 
471
                /*
 
472
                 * It may happen that some queues are still active
 
473
                 * (busy) upon group destruction (if the corresponding
 
474
                 * processes have been forced to terminate). We move
 
475
                 * all the leaf entities corresponding to these queues
 
476
                 * to the root_group.
 
477
                 * Also, it may happen that the group has an entity
 
478
                 * under service, which is disconnected from the active
 
479
                 * tree: it must be moved, too.
 
480
                 * There is no need to put the sync queues, as the
 
481
                 * scheduler has taken no reference.
 
482
                 */
 
483
                bfqd = bfq_get_bfqd_locked(&bfqg->bfqd, &flags);
 
484
                if (bfqd != NULL) {
 
485
                        bfq_reparent_active_entities(bfqd, bfqg, st);
 
486
                        bfq_put_bfqd_unlock(bfqd, &flags);
 
487
                }
 
488
                BUG_ON(!RB_EMPTY_ROOT(&st->active));
 
489
                BUG_ON(!RB_EMPTY_ROOT(&st->idle));
 
490
        }
 
491
        BUG_ON(bfqg->sched_data.next_active != NULL);
 
492
        BUG_ON(bfqg->sched_data.active_entity != NULL);
 
493
 
 
494
        /*
 
495
         * We may race with device destruction, take extra care when
 
496
         * dereferencing bfqg->bfqd.
 
497
         */
 
498
        bfqd = bfq_get_bfqd_locked(&bfqg->bfqd, &flags);
 
499
        if (bfqd != NULL) {
 
500
                hlist_del(&bfqg->bfqd_node);
 
501
                __bfq_deactivate_entity(entity, 0);
 
502
                bfq_put_async_queues(bfqd, bfqg);
 
503
                bfq_put_bfqd_unlock(bfqd, &flags);
 
504
        }
 
505
        BUG_ON(entity->tree != NULL);
 
506
 
 
507
        /*
 
508
         * No need to defer the kfree() to the end of the RCU grace
 
509
         * period: we are called from the destroy() callback of our
 
510
         * cgroup, so we can be sure that noone is a) still using
 
511
         * this cgroup or b) doing lookups in it.
 
512
         */
 
513
        kfree(bfqg);
 
514
}
 
515
 
 
516
/**
 
517
 * bfq_disconnect_groups - diconnect @bfqd from all its groups.
 
518
 * @bfqd: the device descriptor being exited.
 
519
 *
 
520
 * When the device exits we just make sure that no lookup can return
 
521
 * the now unused group structures.  They will be deallocated on cgroup
 
522
 * destruction.
 
523
 */
 
524
static void bfq_disconnect_groups(struct bfq_data *bfqd)
 
525
{
 
526
        struct hlist_node *pos, *n;
 
527
        struct bfq_group *bfqg;
 
528
 
 
529
        bfq_log(bfqd, "disconnect_groups beginning") ;
 
530
        hlist_for_each_entry_safe(bfqg, pos, n, &bfqd->group_list, bfqd_node) {
 
531
                hlist_del(&bfqg->bfqd_node);
 
532
 
 
533
                __bfq_deactivate_entity(bfqg->my_entity, 0);
 
534
 
 
535
                /*
 
536
                 * Don't remove from the group hash, just set an
 
537
                 * invalid key.  No lookups can race with the
 
538
                 * assignment as bfqd is being destroyed; this
 
539
                 * implies also that new elements cannot be added
 
540
                 * to the list.
 
541
                 */
 
542
                rcu_assign_pointer(bfqg->bfqd, NULL);
 
543
 
 
544
                bfq_log(bfqd, "disconnect_groups: put async for group %p",
 
545
                        bfqg) ;
 
546
                bfq_put_async_queues(bfqd, bfqg);
 
547
        }
 
548
}
 
549
 
 
550
static inline void bfq_free_root_group(struct bfq_data *bfqd)
 
551
{
 
552
        struct bfqio_cgroup *bgrp = &bfqio_root_cgroup;
 
553
        struct bfq_group *bfqg = bfqd->root_group;
 
554
 
 
555
        bfq_put_async_queues(bfqd, bfqg);
 
556
 
 
557
        spin_lock_irq(&bgrp->lock);
 
558
        hlist_del_rcu(&bfqg->group_node);
 
559
        spin_unlock_irq(&bgrp->lock);
 
560
 
 
561
        /*
 
562
         * No need to synchronize_rcu() here: since the device is gone
 
563
         * there cannot be any read-side access to its root_group.
 
564
         */
 
565
        kfree(bfqg);
 
566
}
 
567
 
 
568
static struct bfq_group *bfq_alloc_root_group(struct bfq_data *bfqd, int node)
 
569
{
 
570
        struct bfq_group *bfqg;
 
571
        struct bfqio_cgroup *bgrp;
 
572
        int i;
 
573
 
 
574
        bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
 
575
        if (bfqg == NULL)
 
576
                return NULL;
 
577
 
 
578
        bfqg->entity.parent = NULL;
 
579
        for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
 
580
                bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
 
581
 
 
582
        bgrp = &bfqio_root_cgroup;
 
583
        spin_lock_irq(&bgrp->lock);
 
584
        rcu_assign_pointer(bfqg->bfqd, bfqd);
 
585
        hlist_add_head_rcu(&bfqg->group_node, &bgrp->group_data);
 
586
        spin_unlock_irq(&bgrp->lock);
 
587
 
 
588
        return bfqg;
 
589
}
 
590
 
 
591
#define SHOW_FUNCTION(__VAR)                                            \
 
592
static u64 bfqio_cgroup_##__VAR##_read(struct cgroup *cgroup,           \
 
593
                                       struct cftype *cftype)           \
 
594
{                                                                       \
 
595
        struct bfqio_cgroup *bgrp;                                      \
 
596
        u64 ret;                                                        \
 
597
                                                                        \
 
598
        if (!cgroup_lock_live_group(cgroup))                            \
 
599
                return -ENODEV;                                         \
 
600
                                                                        \
 
601
        bgrp = cgroup_to_bfqio(cgroup);                                 \
 
602
        spin_lock_irq(&bgrp->lock);                                     \
 
603
        ret = bgrp->__VAR;                                              \
 
604
        spin_unlock_irq(&bgrp->lock);                                   \
 
605
                                                                        \
 
606
        cgroup_unlock();                                                \
 
607
                                                                        \
 
608
        return ret;                                                     \
 
609
}
 
610
 
 
611
SHOW_FUNCTION(weight);
 
612
SHOW_FUNCTION(ioprio);
 
613
SHOW_FUNCTION(ioprio_class);
 
614
#undef SHOW_FUNCTION
 
615
 
 
616
#define STORE_FUNCTION(__VAR, __MIN, __MAX)                             \
 
617
static int bfqio_cgroup_##__VAR##_write(struct cgroup *cgroup,          \
 
618
                                        struct cftype *cftype,          \
 
619
                                        u64 val)                        \
 
620
{                                                                       \
 
621
        struct bfqio_cgroup *bgrp;                                      \
 
622
        struct bfq_group *bfqg;                                         \
 
623
        struct hlist_node *n;                                           \
 
624
                                                                        \
 
625
        if (val < (__MIN) || val > (__MAX))                             \
 
626
                return -EINVAL;                                         \
 
627
                                                                        \
 
628
        if (!cgroup_lock_live_group(cgroup))                            \
 
629
                return -ENODEV;                                         \
 
630
                                                                        \
 
631
        bgrp = cgroup_to_bfqio(cgroup);                                 \
 
632
                                                                        \
 
633
        spin_lock_irq(&bgrp->lock);                                     \
 
634
        bgrp->__VAR = (unsigned short)val;                              \
 
635
        hlist_for_each_entry(bfqg, n, &bgrp->group_data, group_node) {  \
 
636
                bfqg->entity.new_##__VAR = (unsigned short)val;         \
 
637
                smp_wmb();                                              \
 
638
                bfqg->entity.ioprio_changed = 1;                        \
 
639
        }                                                               \
 
640
        spin_unlock_irq(&bgrp->lock);                                   \
 
641
                                                                        \
 
642
        cgroup_unlock();                                                \
 
643
                                                                        \
 
644
        return 0;                                                       \
 
645
}
 
646
 
 
647
STORE_FUNCTION(weight, BFQ_MIN_WEIGHT, BFQ_MAX_WEIGHT);
 
648
STORE_FUNCTION(ioprio, 0, IOPRIO_BE_NR - 1);
 
649
STORE_FUNCTION(ioprio_class, IOPRIO_CLASS_RT, IOPRIO_CLASS_IDLE);
 
650
#undef STORE_FUNCTION
 
651
 
 
652
static struct cftype bfqio_files[] = {
 
653
        {
 
654
                .name = "weight",
 
655
                .read_u64 = bfqio_cgroup_weight_read,
 
656
                .write_u64 = bfqio_cgroup_weight_write,
 
657
        },
 
658
        {
 
659
                .name = "ioprio",
 
660
                .read_u64 = bfqio_cgroup_ioprio_read,
 
661
                .write_u64 = bfqio_cgroup_ioprio_write,
 
662
        },
 
663
        {
 
664
                .name = "ioprio_class",
 
665
                .read_u64 = bfqio_cgroup_ioprio_class_read,
 
666
                .write_u64 = bfqio_cgroup_ioprio_class_write,
 
667
        },
 
668
};
 
669
 
 
670
static int bfqio_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
 
671
{
 
672
        return cgroup_add_files(cgroup, subsys, bfqio_files,
 
673
                                ARRAY_SIZE(bfqio_files));
 
674
}
 
675
 
 
676
static struct cgroup_subsys_state *bfqio_create(struct cgroup_subsys *subsys,
 
677
                                                struct cgroup *cgroup)
 
678
{
 
679
        struct bfqio_cgroup *bgrp;
 
680
 
 
681
        if (cgroup->parent != NULL) {
 
682
                bgrp = kzalloc(sizeof(*bgrp), GFP_KERNEL);
 
683
                if (bgrp == NULL)
 
684
                        return ERR_PTR(-ENOMEM);
 
685
        } else
 
686
                bgrp = &bfqio_root_cgroup;
 
687
 
 
688
        spin_lock_init(&bgrp->lock);
 
689
        INIT_HLIST_HEAD(&bgrp->group_data);
 
690
        bgrp->ioprio = BFQ_DEFAULT_GRP_IOPRIO;
 
691
        bgrp->ioprio_class = BFQ_DEFAULT_GRP_CLASS;
 
692
 
 
693
        return &bgrp->css;
 
694
}
 
695
 
 
696
/*
 
697
 * We cannot support shared io contexts, as we have no mean to support
 
698
 * two tasks with the same ioc in two different groups without major rework
 
699
 * of the main cic/bfqq data structures.  By now we allow a task to change
 
700
 * its cgroup only if it's the only owner of its ioc; the drawback of this
 
701
 * behavior is that a group containing a task that forked using CLONE_IO
 
702
 * will not be destroyed until the tasks sharing the ioc die.
 
703
 */
 
704
static int bfqio_can_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
 
705
                            struct task_struct *tsk)
 
706
{
 
707
        struct io_context *ioc;
 
708
        int ret = 0;
 
709
 
 
710
        /* task_lock() is needed to avoid races with exit_io_context() */
 
711
        task_lock(tsk);
 
712
        ioc = tsk->io_context;
 
713
        if (ioc != NULL && atomic_read(&ioc->nr_tasks) > 1)
 
714
                /*
 
715
                 * ioc == NULL means that the task is either too young or
 
716
                 * exiting: if it has still no ioc the ioc can't be shared,
 
717
                 * if the task is exiting the attach will fail anyway, no
 
718
                 * matter what we return here.
 
719
                 */
 
720
                ret = -EINVAL;
 
721
        task_unlock(tsk);
 
722
 
 
723
        return ret;
 
724
}
 
725
 
 
726
static void bfqio_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
 
727
                         struct cgroup *prev, struct task_struct *tsk)
 
728
{
 
729
        struct io_context *ioc;
 
730
        struct cfq_io_context *cic;
 
731
        struct hlist_node *n;
 
732
 
 
733
        task_lock(tsk);
 
734
        ioc = tsk->io_context;
 
735
        if (ioc != NULL) {
 
736
                BUG_ON(atomic_long_read(&ioc->refcount) == 0);
 
737
                atomic_long_inc(&ioc->refcount);
 
738
        }
 
739
        task_unlock(tsk);
 
740
 
 
741
        if (ioc == NULL)
 
742
                return;
 
743
 
 
744
        rcu_read_lock();
 
745
        hlist_for_each_entry_rcu(cic, n, &ioc->bfq_cic_list, cic_list)
 
746
                bfq_cic_change_cgroup(cic, cgroup);
 
747
        rcu_read_unlock();
 
748
 
 
749
        put_io_context(ioc);
 
750
}
 
751
 
 
752
static void bfqio_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
 
753
{
 
754
        struct bfqio_cgroup *bgrp = cgroup_to_bfqio(cgroup);
 
755
        struct hlist_node *n, *tmp;
 
756
        struct bfq_group *bfqg;
 
757
 
 
758
        /*
 
759
         * Since we are destroying the cgroup, there are no more tasks
 
760
         * referencing it, and all the RCU grace periods that may have
 
761
         * referenced it are ended (as the destruction of the parent
 
762
         * cgroup is RCU-safe); bgrp->group_data will not be accessed by
 
763
         * anything else and we don't need any synchronization.
 
764
         */
 
765
        hlist_for_each_entry_safe(bfqg, n, tmp, &bgrp->group_data, group_node)
 
766
                bfq_destroy_group(bgrp, bfqg);
 
767
 
 
768
        BUG_ON(!hlist_empty(&bgrp->group_data));
 
769
 
 
770
        kfree(bgrp);
 
771
}
 
772
 
 
773
struct cgroup_subsys bfqio_subsys = {
 
774
        .name = "bfqio",
 
775
        .create = bfqio_create,
 
776
        .can_attach = bfqio_can_attach,
 
777
        .attach = bfqio_attach,
 
778
        .destroy = bfqio_destroy,
 
779
        .populate = bfqio_populate,
 
780
        .subsys_id = bfqio_subsys_id,
 
781
};
 
782
#else
 
783
static inline void bfq_init_entity(struct bfq_entity *entity,
 
784
                                   struct bfq_group *bfqg)
 
785
{
 
786
        entity->weight = entity->new_weight;
 
787
        entity->orig_weight = entity->new_weight;
 
788
        entity->ioprio = entity->new_ioprio;
 
789
        entity->ioprio_class = entity->new_ioprio_class;
 
790
        entity->sched_data = &bfqg->sched_data;
 
791
}
 
792
 
 
793
static inline struct bfq_group *
 
794
bfq_cic_update_cgroup(struct cfq_io_context *cic)
 
795
{
 
796
        struct bfq_data *bfqd = cic->key;
 
797
        return bfqd->root_group;
 
798
}
 
799
 
 
800
static inline void bfq_bfqq_move(struct bfq_data *bfqd,
 
801
                                 struct bfq_queue *bfqq,
 
802
                                 struct bfq_entity *entity,
 
803
                                 struct bfq_group *bfqg)
 
804
{
 
805
}
 
806
 
 
807
static inline void bfq_disconnect_groups(struct bfq_data *bfqd)
 
808
{
 
809
        bfq_put_async_queues(bfqd, bfqd->root_group);
 
810
}
 
811
 
 
812
static inline void bfq_free_root_group(struct bfq_data *bfqd)
 
813
{
 
814
        kfree(bfqd->root_group);
 
815
}
 
816
 
 
817
static struct bfq_group *bfq_alloc_root_group(struct bfq_data *bfqd, int node)
 
818
{
 
819
        struct bfq_group *bfqg;
 
820
        int i;
 
821
 
 
822
        bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
 
823
        if (bfqg == NULL)
 
824
                return NULL;
 
825
 
 
826
        for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
 
827
                bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
 
828
 
 
829
        return bfqg;
 
830
}
 
831
#endif