DF-2865 / fix.diff
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 | --- a/sys/vm/vm_pager.c +++ b/sys/vm/vm_pager.c @@ -156,7 +156,8 @@ &devicepagerops, /* OBJT_DEVICE */ &devicepagerops, /* OBJT_MGTDEVICE */ &physpagerops, /* OBJT_PHYS */ - &deadpagerops /* OBJT_DEAD */ + &deadpagerops, /* OBJT_DEAD */ + &deadpagerops /* OBJT_MARKER (list-scan marker; never paged) */ }; /* @@ -441,8 +442,34 @@ } TAILQ_REMOVE(&bswlist_raw[iter], bp, b_freelist); atomic_add_int(&pbuf_raw_count, -1); - if (pfreecnt) - atomic_add_int(pfreecnt, -1); + if (pfreecnt) { + int c; + + /* + * Atomically reserve against pfreecnt. + * The gate at the top of the loop is + * unlocked and the bucket spinlock is + * per-bucket, so a plain decrement here + * can race multiple CPUs past a count of 1 + * and drive the counter negative. + */ + for (;;) { + c = *(volatile int *)pfreecnt; + if (c <= 0) + break; + if (atomic_cmpset_int(pfreecnt, + c, c - 1)) + break; + } + if (c <= 0) { + /* lost the race, put it back */ + TAILQ_INSERT_HEAD(&bswlist_raw[iter], + bp, b_freelist); + atomic_add_int(&pbuf_raw_count, 1); + spin_unlock(&bswspin_raw[iter]); + continue; + } + } spin_unlock(&bswspin_raw[iter]); initpbuf(bp); @@ -486,8 +513,25 @@ } TAILQ_REMOVE(&bswlist_kva[iter], bp, b_freelist); atomic_add_int(&pbuf_kva_count, -1); - if (pfreecnt) - atomic_add_int(pfreecnt, -1); + if (pfreecnt) { + int c; + + for (;;) { + c = *(volatile int *)pfreecnt; + if (c <= 0) + break; + if (atomic_cmpset_int(pfreecnt, + c, c - 1)) + break; + } + if (c <= 0) { + TAILQ_INSERT_HEAD(&bswlist_kva[iter], + bp, b_freelist); + atomic_add_int(&pbuf_kva_count, 1); + spin_unlock(&bswspin_kva[iter]); + continue; + } + } spin_unlock(&bswspin_kva[iter]); initpbuf(bp); @@ -535,8 +579,25 @@ } TAILQ_REMOVE(&bswlist_mem[iter], bp, b_freelist); atomic_add_int(&pbuf_mem_count, -1); - if (pfreecnt) - atomic_add_int(pfreecnt, -1); + if (pfreecnt) { + int c; + + for (;;) { + c = *(volatile int *)pfreecnt; + if (c <= 0) + break; + if (atomic_cmpset_int(pfreecnt, + c, c - 1)) + break; + } + if (c <= 0) { + TAILQ_INSERT_HEAD(&bswlist_mem[iter], + bp, b_freelist); + atomic_add_int(&pbuf_mem_count, 1); + spin_unlock(&bswspin_mem[iter]); + continue; + } + } spin_unlock(&bswspin_mem[iter]); initpbuf(bp); @@ -575,7 +636,24 @@ } TAILQ_REMOVE(&bswlist_raw[iter], bp, b_freelist); atomic_add_int(&pbuf_raw_count, -1); - atomic_add_int(pfreecnt, -1); + { + int c; + + for (;;) { + c = *(volatile int *)pfreecnt; + if (c <= 0) + break; + if (atomic_cmpset_int(pfreecnt, c, c - 1)) + break; + } + if (c <= 0) { + TAILQ_INSERT_HEAD(&bswlist_raw[iter], bp, + b_freelist); + atomic_add_int(&pbuf_raw_count, 1); + spin_unlock(&bswspin_raw[iter]); + continue; + } + } spin_unlock(&bswspin_raw[iter]); @@ -607,7 +685,24 @@ } TAILQ_REMOVE(&bswlist_kva[iter], bp, b_freelist); atomic_add_int(&pbuf_kva_count, -1); - atomic_add_int(pfreecnt, -1); + { + int c; + + for (;;) { + c = *(volatile int *)pfreecnt; + if (c <= 0) + break; + if (atomic_cmpset_int(pfreecnt, c, c - 1)) + break; + } + if (c <= 0) { + TAILQ_INSERT_HEAD(&bswlist_kva[iter], bp, + b_freelist); + atomic_add_int(&pbuf_kva_count, 1); + spin_unlock(&bswspin_kva[iter]); + continue; + } + } spin_unlock(&bswspin_kva[iter]); |