summaryrefslogblamecommitdiffstats
path: root/private/ntos/ps/psquota.c
blob: ce2be656463cd3693920c30be8484c7a8dc2ff7d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517




































































































































































































































































































































































































































































































































                                                                                                                                               
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    psquota.c

Abstract:

    This module implements the quota mechanism for NT

Author:

    Mark Lucovsky (markl) 18-Sep-1989

Revision History:

--*/

#include "psp.h"

PEPROCESS_QUOTA_BLOCK
PsChargeSharedPoolQuota(
    IN PEPROCESS Process,
    IN ULONG PagedAmount,
    IN ULONG NonPagedAmount
    )

/*++

Routine Description:

    This function charges shared pool quota of the specified pool type
    to the specified process's pooled quota block.  If the quota charge
    would exceed the limits allowed to the process, then an exception is
    raised and quota is not charged.

Arguments:

    Process - Supplies the process to charge quota to.

    PagedAmount - Supplies the amount of paged pool quota to charge.

    PagedAmount - Supplies the amount of non paged pool quota to charge.

Return Value:

    NULL - Quota was exceeded

    NON-NULL - A referenced pointer to the quota block that was charged

--*/

{

    KIRQL OldIrql;
    ULONG NewPoolUsage;
    PEPROCESS_QUOTA_BLOCK QuotaBlock;
    ULONG NewLimit;

    ASSERT((Process->Pcb.Header.Type == ProcessObject) || (Process->Pcb.Header.Type == 0));

    QuotaBlock = Process->QuotaBlock;

retry_charge:
    if ( QuotaBlock != &PspDefaultQuotaBlock ) {
        ExAcquireSpinLock(&QuotaBlock->QuotaLock,&OldIrql);
do_charge:

        if ( PagedAmount ) {

            NewPoolUsage = QuotaBlock->QuotaPoolUsage[PagedPool] + PagedAmount;

            //
            // See if enough quota exists in the block to satisfy the
            // request
            //

            if ( NewPoolUsage > QuotaBlock->QuotaPoolLimit[PagedPool] ) {
                while ( (PspDefaultPagedLimit == 0) && MmRaisePoolQuota(PagedPool,QuotaBlock->QuotaPoolLimit[PagedPool],&NewLimit) ) {
                    QuotaBlock->QuotaPoolLimit[PagedPool] = NewLimit;
                    if ( NewPoolUsage <= NewLimit ) {
                        goto LimitRaised0;
                        }
                    }
//DbgPrint("PS: ChargeShared(0) Failed P %8x QB %8x PA %8x NPA %8x\n",Process,QuotaBlock,PagedAmount,NonPagedAmount);DbgBreakPoint();
                ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
                return NULL;
                }
LimitRaised0:
            if ( NewPoolUsage < QuotaBlock->QuotaPoolUsage[PagedPool] ||
                 NewPoolUsage < PagedAmount ) {

                ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
//DbgPrint("PS: ChargeShared(1) Failed P %8x QB %8x PA %8x NPA %8x\n",Process,QuotaBlock,PagedAmount,NonPagedAmount);DbgBreakPoint();
                return NULL;
                }

            QuotaBlock->QuotaPoolUsage[PagedPool] = NewPoolUsage;
            if ( NewPoolUsage > QuotaBlock->QuotaPeakPoolUsage[PagedPool] ) {
                QuotaBlock->QuotaPeakPoolUsage[PagedPool] = NewPoolUsage;
                }
            }

        if ( NonPagedAmount ) {

            NewPoolUsage = QuotaBlock->QuotaPoolUsage[NonPagedPool] + NonPagedAmount;

            //
            // See if enough quota exists in the block to satisfy the
            // request
            //

            if ( NewPoolUsage > QuotaBlock->QuotaPoolLimit[NonPagedPool] ) {
                while ( (PspDefaultNonPagedLimit == 0) && MmRaisePoolQuota(NonPagedPool,QuotaBlock->QuotaPoolLimit[NonPagedPool],&NewLimit) ) {
                    QuotaBlock->QuotaPoolLimit[NonPagedPool] = NewLimit;
                    if ( NewPoolUsage <= NewLimit ) {
                        goto LimitRaised1;
                        }
                    }
                if ( PagedAmount ) {
                    QuotaBlock->QuotaPoolUsage[PagedPool] -= PagedAmount;
                    }
                ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
//DbgPrint("PS: ChargeShared(2) Failed P %8x QB %8x PA %8x NPA %8x\n",Process,QuotaBlock,PagedAmount,NonPagedAmount);DbgBreakPoint();
                return NULL;
                }

LimitRaised1:
            if ( NewPoolUsage < QuotaBlock->QuotaPoolUsage[NonPagedPool] ||
                 NewPoolUsage < NonPagedAmount ) {
                if ( PagedAmount ) {
                    QuotaBlock->QuotaPoolUsage[PagedPool] -= PagedAmount;
                    }

                ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
//DbgPrint("PS: ChargeShared(3) Failed P %8x QB %8x PA %8x NPA %8x\n",Process,QuotaBlock,PagedAmount,NonPagedAmount);DbgBreakPoint();
                return NULL;
                }

            QuotaBlock->QuotaPoolUsage[NonPagedPool] = NewPoolUsage;
            if ( NewPoolUsage > QuotaBlock->QuotaPeakPoolUsage[NonPagedPool] ) {
                QuotaBlock->QuotaPeakPoolUsage[NonPagedPool] = NewPoolUsage;
                }
            }

        QuotaBlock->ReferenceCount++;
        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        }
    else {

        //
        // Don't do ANY quota operations on the initial system process
        //

        if ( Process == PsInitialSystemProcess ) {
            return (PEPROCESS_QUOTA_BLOCK)1;
            }

        ExAcquireSpinLock(&PspDefaultQuotaBlock.QuotaLock,&OldIrql);
        if ( (QuotaBlock = Process->QuotaBlock) != &PspDefaultQuotaBlock ) {
            ExReleaseSpinLock(&PspDefaultQuotaBlock.QuotaLock,OldIrql);
            goto retry_charge;
            }
        goto do_charge;

        }
    return QuotaBlock;

}

VOID
PsReturnSharedPoolQuota(
    IN PEPROCESS_QUOTA_BLOCK QuotaBlock,
    IN ULONG PagedAmount,
    IN ULONG NonPagedAmount
    )

/*++

Routine Description:

    This function returns pool quota of the specified pool type to the
    specified process.

Arguments:

    QuotaBlock - Supplies the quota block to return quota to.

    PagedAmount - Supplies the amount of paged pool quota to return.

    PagedAmount - Supplies the amount of non paged pool quota to return.

Return Value:

    None.

--*/

{

    KIRQL OldIrql;

    //
    // if we bypassed the quota charge, don't do anything here either
    //

    if ( QuotaBlock == (PEPROCESS_QUOTA_BLOCK)1 ) {
        return;
        }

    ExAcquireSpinLock(&QuotaBlock->QuotaLock,&OldIrql);

    if ( PagedAmount ) {

        if ( PagedAmount <= QuotaBlock->QuotaPoolUsage[PagedPool] ) {
            QuotaBlock->QuotaPoolUsage[PagedPool] -= PagedAmount;
            }
        else {
            ASSERT(FALSE);
            QuotaBlock->QuotaPoolUsage[PagedPool] = 0;
            }
        }
    if ( NonPagedAmount ) {

        if ( NonPagedAmount <= QuotaBlock->QuotaPoolUsage[NonPagedPool] ) {
            QuotaBlock->QuotaPoolUsage[NonPagedPool] -= NonPagedAmount;
            }
        else {
            ASSERT(FALSE);
            QuotaBlock->QuotaPoolUsage[NonPagedPool] = 0;
            }
        }
    QuotaBlock->ReferenceCount--;
    if ( QuotaBlock->ReferenceCount == 0 ) {
        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        ExFreePool(QuotaBlock);
        }
    else {
        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        }
}

VOID
PsChargePoolQuota(
    IN PEPROCESS Process,
    IN POOL_TYPE PoolType,
    IN ULONG Amount
    )

/*++

Routine Description:

    This function charges pool quota of the specified pool type to
    the specified process. If the quota charge would exceed the limits
    allowed to the process, then an exception is raised and quota is
    not charged.

Arguments:

    Process - Supplies the process to charge quota to.

    PoolType - Supplies the type of pool quota to charge.

    Amount - Supplies the amount of pool quota to charge.

Return Value:

    Raises STATUS_QUOTA_EXCEEDED if the quota charge would exceed the
        limits allowed to the process.

--*/

{

    KIRQL OldIrql;
    ULONG NewPoolUsage;
    PEPROCESS_QUOTA_BLOCK QuotaBlock;
    ULONG NewLimit;
    ULONG HardLimit;

    ASSERT((Process->Pcb.Header.Type == ProcessObject) || (Process->Pcb.Header.Type == 0));

    QuotaBlock = Process->QuotaBlock;

retry_charge:
    if ( QuotaBlock != &PspDefaultQuotaBlock ) {
        ExAcquireSpinLock(&QuotaBlock->QuotaLock,&OldIrql);
do_charge:
        NewPoolUsage = QuotaBlock->QuotaPoolUsage[PoolType] + Amount;

        //
        // See if enough quota exists in the block to satisfy the
        // request
        //

        if ( NewPoolUsage > QuotaBlock->QuotaPoolLimit[PoolType] ) {
            if ( PoolType == PagedPool ) {
                HardLimit = PspDefaultPagedLimit;
                }
            else {
                HardLimit = PspDefaultNonPagedLimit;
                }

            while ( (HardLimit == 0) && MmRaisePoolQuota(PoolType,QuotaBlock->QuotaPoolLimit[PoolType],&NewLimit) ) {
                QuotaBlock->QuotaPoolLimit[PoolType] = NewLimit;
                if ( NewPoolUsage <= NewLimit ) {
                    goto LimitRaised2;
                    }
                }

//DbgPrint("PS: ChargePool Failed P %8x QB %8x PT %8x Amount %8x\n",Process,QuotaBlock,PoolType,Amount);DbgBreakPoint();
            ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
            ExRaiseStatus(STATUS_QUOTA_EXCEEDED);
            }
LimitRaised2:
        if ( NewPoolUsage < QuotaBlock->QuotaPoolUsage[PoolType] ||
             NewPoolUsage < Amount ) {

            ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
            ExRaiseStatus(STATUS_QUOTA_EXCEEDED);
            }

        QuotaBlock->QuotaPoolUsage[PoolType] = NewPoolUsage;
        if ( NewPoolUsage > QuotaBlock->QuotaPeakPoolUsage[PoolType] ) {
            QuotaBlock->QuotaPeakPoolUsage[PoolType] = NewPoolUsage;
            }

        NewPoolUsage = Process->QuotaPoolUsage[PoolType] + Amount;
        Process->QuotaPoolUsage[PoolType] = NewPoolUsage;
        if ( NewPoolUsage > Process->QuotaPeakPoolUsage[PoolType] ) {
            Process->QuotaPeakPoolUsage[PoolType] = NewPoolUsage;
            }

        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        }
    else {
        if ( Process == PsInitialSystemProcess ) {
            return;
            }

        ExAcquireSpinLock(&PspDefaultQuotaBlock.QuotaLock,&OldIrql);
        if ( (QuotaBlock = Process->QuotaBlock) != &PspDefaultQuotaBlock ) {
            ExReleaseSpinLock(&PspDefaultQuotaBlock.QuotaLock,OldIrql);
            goto retry_charge;
            }
        goto do_charge;

        }

}

VOID
PsReturnPoolQuota(
    IN PEPROCESS Process,
    IN POOL_TYPE PoolType,
    IN ULONG Amount
    )

/*++

Routine Description:

    This function returns pool quota of the specified pool type to the
    specified process.

Arguments:

    Process - Supplies the process to return quota to.

    PoolType - Supplies the type of pool quota to return.

    Amount - Supplies the amount of pool quota to return

Return Value:

    Raises STATUS_QUOTA_EXCEEDED if the quota charge would exceed the
        limits allowed to the process.

--*/

{

    KIRQL OldIrql;
    PEPROCESS_QUOTA_BLOCK QuotaBlock;
    ULONG GiveBackLimit;
    ULONG RoundedUsage;

    ASSERT((Process->Pcb.Header.Type == ProcessObject) || (Process->Pcb.Header.Type == 0));

    QuotaBlock = Process->QuotaBlock;
retry_return:
    if ( QuotaBlock != &PspDefaultQuotaBlock) {
        ExAcquireSpinLock(&QuotaBlock->QuotaLock,&OldIrql);

        if ( PspDoingGiveBacks ) {
            if ( PoolType == PagedPool ) {
                GiveBackLimit = MMPAGED_QUOTA_INCREASE;
                }
            else {
                GiveBackLimit = MMNONPAGED_QUOTA_INCREASE;
                }
            }
        else {
            GiveBackLimit = 0;
            }
do_return:
        if ( Amount <= Process->QuotaPoolUsage[PoolType] ) {
            Process->QuotaPoolUsage[PoolType] -= Amount;
            }
        else {
            ASSERT(FALSE);
            GiveBackLimit = 0;
            Process->QuotaPoolUsage[PoolType] = 0;
            }

        if ( Amount <= QuotaBlock->QuotaPoolUsage[PoolType] ) {
            QuotaBlock->QuotaPoolUsage[PoolType] -= Amount;
            }
        else {
            ASSERT(FALSE);
            GiveBackLimit = 0;
            QuotaBlock->QuotaPoolUsage[PoolType] = 0;
            }

        if ( GiveBackLimit ) {
            if (QuotaBlock->QuotaPoolLimit[PoolType] - QuotaBlock->QuotaPoolUsage[PoolType] > GiveBackLimit ) {

                //
                // round up the current usage to a page multiple
                //

                RoundedUsage = ROUND_TO_PAGES(QuotaBlock->QuotaPoolUsage[PoolType]);

                //
                // Give back the limit minus the rounded usage
                //

                GiveBackLimit = QuotaBlock->QuotaPoolLimit[PoolType] - RoundedUsage;
                QuotaBlock->QuotaPoolLimit[PoolType] -= GiveBackLimit;
                MmReturnPoolQuota(PoolType,GiveBackLimit);

                }
            }
        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        }
    else {

        if ( Process == PsInitialSystemProcess ) {
            return;
            }

        ExAcquireSpinLock(&PspDefaultQuotaBlock.QuotaLock,&OldIrql);
        if ( (QuotaBlock = Process->QuotaBlock) != &PspDefaultQuotaBlock) {
            ExReleaseSpinLock(&PspDefaultQuotaBlock.QuotaLock,OldIrql);
            goto retry_return;
            }
        GiveBackLimit = 0;
        goto do_return;
        }
}

VOID
PspInheritQuota(
    IN PEPROCESS NewProcess,
    IN PEPROCESS ParentProcess
    )
{
    PEPROCESS_QUOTA_BLOCK QuotaBlock;
    KIRQL OldIrql;

    if ( ParentProcess ) {
        QuotaBlock = ParentProcess->QuotaBlock;
        }
    else {
        QuotaBlock = &PspDefaultQuotaBlock;
        }

    ExAcquireSpinLock(&QuotaBlock->QuotaLock,&OldIrql);
    QuotaBlock->ReferenceCount++;
    NewProcess->QuotaBlock = QuotaBlock;
    ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
}

VOID
MiReturnPageFileQuota (
    IN ULONG QuotaCharge,
    IN PEPROCESS CurrentProcess
    );

VOID
PspDereferenceQuota(
    IN PEPROCESS Process
    )
{
    PEPROCESS_QUOTA_BLOCK QuotaBlock;
    KIRQL OldIrql;

    QuotaBlock = Process->QuotaBlock;

    PsReturnPoolQuota(Process,NonPagedPool,Process->QuotaPoolUsage[NonPagedPool]);
    PsReturnPoolQuota(Process,PagedPool,Process->QuotaPoolUsage[PagedPool]);
    MiReturnPageFileQuota(Process->PagefileUsage,Process);

    ExAcquireSpinLock(&QuotaBlock->QuotaLock,&OldIrql);

    QuotaBlock->ReferenceCount--;
    if ( QuotaBlock->ReferenceCount == 0 ) {
        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        ExFreePool(QuotaBlock);
        }
    else {
        ExReleaseSpinLock(&QuotaBlock->QuotaLock,OldIrql);
        }
}