~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to block/blk-cgroup.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
32
32
                                                  struct cgroup *);
33
 
static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
34
 
                              struct task_struct *, bool);
35
 
static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
36
 
                           struct cgroup *, struct task_struct *, bool);
 
33
static int blkiocg_can_attach_task(struct cgroup *, struct task_struct *);
 
34
static void blkiocg_attach_task(struct cgroup *, struct task_struct *);
37
35
static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
38
36
static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
39
37
 
46
44
struct cgroup_subsys blkio_subsys = {
47
45
        .name = "blkio",
48
46
        .create = blkiocg_create,
49
 
        .can_attach = blkiocg_can_attach,
50
 
        .attach = blkiocg_attach,
 
47
        .can_attach_task = blkiocg_can_attach_task,
 
48
        .attach_task = blkiocg_attach_task,
51
49
        .destroy = blkiocg_destroy,
52
50
        .populate = blkiocg_populate,
53
51
#ifdef CONFIG_BLK_CGROUP
114
112
}
115
113
EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
116
114
 
 
115
struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
 
116
{
 
117
        return container_of(task_subsys_state(tsk, blkio_subsys_id),
 
118
                            struct blkio_cgroup, css);
 
119
}
 
120
EXPORT_SYMBOL_GPL(task_blkio_cgroup);
 
121
 
117
122
static inline void
118
123
blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
119
124
{
371
376
}
372
377
EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
373
378
 
374
 
void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
 
379
void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
 
380
                                unsigned long unaccounted_time)
375
381
{
376
382
        unsigned long flags;
377
383
 
378
384
        spin_lock_irqsave(&blkg->stats_lock, flags);
379
385
        blkg->stats.time += time;
 
386
#ifdef CONFIG_DEBUG_BLK_CGROUP
 
387
        blkg->stats.unaccounted_time += unaccounted_time;
 
388
#endif
380
389
        spin_unlock_irqrestore(&blkg->stats_lock, flags);
381
390
}
382
391
EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
383
392
 
 
393
/*
 
394
 * should be called under rcu read lock or queue lock to make sure blkg pointer
 
395
 * is valid.
 
396
 */
384
397
void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
385
398
                                uint64_t bytes, bool direction, bool sync)
386
399
{
387
 
        struct blkio_group_stats *stats;
 
400
        struct blkio_group_stats_cpu *stats_cpu;
388
401
        unsigned long flags;
389
402
 
390
 
        spin_lock_irqsave(&blkg->stats_lock, flags);
391
 
        stats = &blkg->stats;
392
 
        stats->sectors += bytes >> 9;
393
 
        blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
394
 
                        sync);
395
 
        blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
396
 
                        direction, sync);
397
 
        spin_unlock_irqrestore(&blkg->stats_lock, flags);
 
403
        /*
 
404
         * Disabling interrupts to provide mutual exclusion between two
 
405
         * writes on same cpu. It probably is not needed for 64bit. Not
 
406
         * optimizing that case yet.
 
407
         */
 
408
        local_irq_save(flags);
 
409
 
 
410
        stats_cpu = this_cpu_ptr(blkg->stats_cpu);
 
411
 
 
412
        u64_stats_update_begin(&stats_cpu->syncp);
 
413
        stats_cpu->sectors += bytes >> 9;
 
414
        blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
 
415
                        1, direction, sync);
 
416
        blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
 
417
                        bytes, direction, sync);
 
418
        u64_stats_update_end(&stats_cpu->syncp);
 
419
        local_irq_restore(flags);
398
420
}
399
421
EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
400
422
 
417
439
}
418
440
EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
419
441
 
 
442
/*  Merged stats are per cpu.  */
420
443
void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
421
444
                                        bool sync)
422
445
{
 
446
        struct blkio_group_stats_cpu *stats_cpu;
423
447
        unsigned long flags;
424
448
 
425
 
        spin_lock_irqsave(&blkg->stats_lock, flags);
426
 
        blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
427
 
                        sync);
428
 
        spin_unlock_irqrestore(&blkg->stats_lock, flags);
 
449
        /*
 
450
         * Disabling interrupts to provide mutual exclusion between two
 
451
         * writes on same cpu. It probably is not needed for 64bit. Not
 
452
         * optimizing that case yet.
 
453
         */
 
454
        local_irq_save(flags);
 
455
 
 
456
        stats_cpu = this_cpu_ptr(blkg->stats_cpu);
 
457
 
 
458
        u64_stats_update_begin(&stats_cpu->syncp);
 
459
        blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
 
460
                                direction, sync);
 
461
        u64_stats_update_end(&stats_cpu->syncp);
 
462
        local_irq_restore(flags);
429
463
}
430
464
EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
431
465
 
 
466
/*
 
467
 * This function allocates the per cpu stats for blkio_group. Should be called
 
468
 * from sleepable context as alloc_per_cpu() requires that.
 
469
 */
 
470
int blkio_alloc_blkg_stats(struct blkio_group *blkg)
 
471
{
 
472
        /* Allocate memory for per cpu stats */
 
473
        blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
 
474
        if (!blkg->stats_cpu)
 
475
                return -ENOMEM;
 
476
        return 0;
 
477
}
 
478
EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
 
479
 
432
480
void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
433
481
                struct blkio_group *blkg, void *key, dev_t dev,
434
482
                enum blkio_policy_id plid)
499
547
}
500
548
EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
501
549
 
 
550
static void blkio_reset_stats_cpu(struct blkio_group *blkg)
 
551
{
 
552
        struct blkio_group_stats_cpu *stats_cpu;
 
553
        int i, j, k;
 
554
        /*
 
555
         * Note: On 64 bit arch this should not be an issue. This has the
 
556
         * possibility of returning some inconsistent value on 32bit arch
 
557
         * as 64bit update on 32bit is non atomic. Taking care of this
 
558
         * corner case makes code very complicated, like sending IPIs to
 
559
         * cpus, taking care of stats of offline cpus etc.
 
560
         *
 
561
         * reset stats is anyway more of a debug feature and this sounds a
 
562
         * corner case. So I am not complicating the code yet until and
 
563
         * unless this becomes a real issue.
 
564
         */
 
565
        for_each_possible_cpu(i) {
 
566
                stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
 
567
                stats_cpu->sectors = 0;
 
568
                for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
 
569
                        for (k = 0; k < BLKIO_STAT_TOTAL; k++)
 
570
                                stats_cpu->stat_arr_cpu[j][k] = 0;
 
571
        }
 
572
}
 
573
 
502
574
static int
503
575
blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
504
576
{
543
615
                }
544
616
#endif
545
617
                spin_unlock(&blkg->stats_lock);
 
618
 
 
619
                /* Reset Per cpu stats which don't take blkg->stats_lock */
 
620
                blkio_reset_stats_cpu(blkg);
546
621
        }
 
622
 
547
623
        spin_unlock_irq(&blkcg->lock);
548
624
        return 0;
549
625
}
589
665
        return val;
590
666
}
591
667
 
 
668
 
 
669
static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
 
670
                        enum stat_type_cpu type, enum stat_sub_type sub_type)
 
671
{
 
672
        int cpu;
 
673
        struct blkio_group_stats_cpu *stats_cpu;
 
674
        u64 val = 0, tval;
 
675
 
 
676
        for_each_possible_cpu(cpu) {
 
677
                unsigned int start;
 
678
                stats_cpu  = per_cpu_ptr(blkg->stats_cpu, cpu);
 
679
 
 
680
                do {
 
681
                        start = u64_stats_fetch_begin(&stats_cpu->syncp);
 
682
                        if (type == BLKIO_STAT_CPU_SECTORS)
 
683
                                tval = stats_cpu->sectors;
 
684
                        else
 
685
                                tval = stats_cpu->stat_arr_cpu[type][sub_type];
 
686
                } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
 
687
 
 
688
                val += tval;
 
689
        }
 
690
 
 
691
        return val;
 
692
}
 
693
 
 
694
static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
 
695
                struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
 
696
{
 
697
        uint64_t disk_total, val;
 
698
        char key_str[MAX_KEY_LEN];
 
699
        enum stat_sub_type sub_type;
 
700
 
 
701
        if (type == BLKIO_STAT_CPU_SECTORS) {
 
702
                val = blkio_read_stat_cpu(blkg, type, 0);
 
703
                return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
 
704
        }
 
705
 
 
706
        for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
 
707
                        sub_type++) {
 
708
                blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
 
709
                val = blkio_read_stat_cpu(blkg, type, sub_type);
 
710
                cb->fill(cb, key_str, val);
 
711
        }
 
712
 
 
713
        disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
 
714
                        blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
 
715
 
 
716
        blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
 
717
        cb->fill(cb, key_str, disk_total);
 
718
        return disk_total;
 
719
}
 
720
 
592
721
/* This should be called with blkg->stats_lock held */
593
722
static uint64_t blkio_get_stat(struct blkio_group *blkg,
594
723
                struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
600
729
        if (type == BLKIO_STAT_TIME)
601
730
                return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
602
731
                                        blkg->stats.time, cb, dev);
603
 
        if (type == BLKIO_STAT_SECTORS)
 
732
#ifdef CONFIG_DEBUG_BLK_CGROUP
 
733
        if (type == BLKIO_STAT_UNACCOUNTED_TIME)
604
734
                return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
605
 
                                        blkg->stats.sectors, cb, dev);
606
 
#ifdef CONFIG_DEBUG_BLK_CGROUP
 
735
                                        blkg->stats.unaccounted_time, cb, dev);
607
736
        if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
608
737
                uint64_t sum = blkg->stats.avg_queue_size_sum;
609
738
                uint64_t samples = blkg->stats.avg_queue_size_samples;
863
992
}
864
993
 
865
994
/*
866
 
 * Some rules/values in blkg have changed. Propogate those to respective
 
995
 * Some rules/values in blkg have changed. Propagate those to respective
867
996
 * policies.
868
997
 */
869
998
static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
898
1027
}
899
1028
 
900
1029
/*
901
 
 * A policy node rule has been updated. Propogate this update to all the
 
1030
 * A policy node rule has been updated. Propagate this update to all the
902
1031
 * block groups which might be affected by this update.
903
1032
 */
904
1033
static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
1063
1192
}
1064
1193
 
1065
1194
static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1066
 
                struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
1067
 
                bool show_total)
 
1195
                struct cftype *cft, struct cgroup_map_cb *cb,
 
1196
                enum stat_type type, bool show_total, bool pcpu)
1068
1197
{
1069
1198
        struct blkio_group *blkg;
1070
1199
        struct hlist_node *n;
1075
1204
                if (blkg->dev) {
1076
1205
                        if (!cftype_blkg_same_policy(cft, blkg))
1077
1206
                                continue;
1078
 
                        spin_lock_irq(&blkg->stats_lock);
1079
 
                        cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
1080
 
                                                type);
1081
 
                        spin_unlock_irq(&blkg->stats_lock);
 
1207
                        if (pcpu)
 
1208
                                cgroup_total += blkio_get_stat_cpu(blkg, cb,
 
1209
                                                blkg->dev, type);
 
1210
                        else {
 
1211
                                spin_lock_irq(&blkg->stats_lock);
 
1212
                                cgroup_total += blkio_get_stat(blkg, cb,
 
1213
                                                blkg->dev, type);
 
1214
                                spin_unlock_irq(&blkg->stats_lock);
 
1215
                        }
1082
1216
                }
1083
1217
        }
1084
1218
        if (show_total)
1102
1236
                switch(name) {
1103
1237
                case BLKIO_PROP_time:
1104
1238
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1105
 
                                                BLKIO_STAT_TIME, 0);
 
1239
                                                BLKIO_STAT_TIME, 0, 0);
1106
1240
                case BLKIO_PROP_sectors:
1107
1241
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1108
 
                                                BLKIO_STAT_SECTORS, 0);
 
1242
                                                BLKIO_STAT_CPU_SECTORS, 0, 1);
1109
1243
                case BLKIO_PROP_io_service_bytes:
1110
1244
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1111
 
                                                BLKIO_STAT_SERVICE_BYTES, 1);
 
1245
                                        BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1112
1246
                case BLKIO_PROP_io_serviced:
1113
1247
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1114
 
                                                BLKIO_STAT_SERVICED, 1);
 
1248
                                                BLKIO_STAT_CPU_SERVICED, 1, 1);
1115
1249
                case BLKIO_PROP_io_service_time:
1116
1250
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1117
 
                                                BLKIO_STAT_SERVICE_TIME, 1);
 
1251
                                                BLKIO_STAT_SERVICE_TIME, 1, 0);
1118
1252
                case BLKIO_PROP_io_wait_time:
1119
1253
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1120
 
                                                BLKIO_STAT_WAIT_TIME, 1);
 
1254
                                                BLKIO_STAT_WAIT_TIME, 1, 0);
1121
1255
                case BLKIO_PROP_io_merged:
1122
1256
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1123
 
                                                BLKIO_STAT_MERGED, 1);
 
1257
                                                BLKIO_STAT_CPU_MERGED, 1, 1);
1124
1258
                case BLKIO_PROP_io_queued:
1125
1259
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1126
 
                                                BLKIO_STAT_QUEUED, 1);
 
1260
                                                BLKIO_STAT_QUEUED, 1, 0);
1127
1261
#ifdef CONFIG_DEBUG_BLK_CGROUP
 
1262
                case BLKIO_PROP_unaccounted_time:
 
1263
                        return blkio_read_blkg_stats(blkcg, cft, cb,
 
1264
                                        BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
1128
1265
                case BLKIO_PROP_dequeue:
1129
1266
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1130
 
                                                BLKIO_STAT_DEQUEUE, 0);
 
1267
                                                BLKIO_STAT_DEQUEUE, 0, 0);
1131
1268
                case BLKIO_PROP_avg_queue_size:
1132
1269
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1133
 
                                                BLKIO_STAT_AVG_QUEUE_SIZE, 0);
 
1270
                                        BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
1134
1271
                case BLKIO_PROP_group_wait_time:
1135
1272
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1136
 
                                                BLKIO_STAT_GROUP_WAIT_TIME, 0);
 
1273
                                        BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
1137
1274
                case BLKIO_PROP_idle_time:
1138
1275
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1139
 
                                                BLKIO_STAT_IDLE_TIME, 0);
 
1276
                                                BLKIO_STAT_IDLE_TIME, 0, 0);
1140
1277
                case BLKIO_PROP_empty_time:
1141
1278
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1142
 
                                                BLKIO_STAT_EMPTY_TIME, 0);
 
1279
                                                BLKIO_STAT_EMPTY_TIME, 0, 0);
1143
1280
#endif
1144
1281
                default:
1145
1282
                        BUG();
1149
1286
                switch(name){
1150
1287
                case BLKIO_THROTL_io_service_bytes:
1151
1288
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1152
 
                                                BLKIO_STAT_SERVICE_BYTES, 1);
 
1289
                                                BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1153
1290
                case BLKIO_THROTL_io_serviced:
1154
1291
                        return blkio_read_blkg_stats(blkcg, cft, cb,
1155
 
                                                BLKIO_STAT_SERVICED, 1);
 
1292
                                                BLKIO_STAT_CPU_SERVICED, 1, 1);
1156
1293
                default:
1157
1294
                        BUG();
1158
1295
                }
1382
1519
                                BLKIO_PROP_dequeue),
1383
1520
                .read_map = blkiocg_file_read_map,
1384
1521
        },
 
1522
        {
 
1523
                .name = "unaccounted_time",
 
1524
                .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
 
1525
                                BLKIO_PROP_unaccounted_time),
 
1526
                .read_map = blkiocg_file_read_map,
 
1527
        },
1385
1528
#endif
1386
1529
};
1387
1530
 
1471
1614
 * of the main cic data structures.  For now we allow a task to change
1472
1615
 * its cgroup only if it's the only owner of its ioc.
1473
1616
 */
1474
 
static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1475
 
                                struct cgroup *cgroup, struct task_struct *tsk,
1476
 
                                bool threadgroup)
 
1617
static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1477
1618
{
1478
1619
        struct io_context *ioc;
1479
1620
        int ret = 0;
1488
1629
        return ret;
1489
1630
}
1490
1631
 
1491
 
static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1492
 
                                struct cgroup *prev, struct task_struct *tsk,
1493
 
                                bool threadgroup)
 
1632
static void blkiocg_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1494
1633
{
1495
1634
        struct io_context *ioc;
1496
1635