summaryrefslogtreecommitdiff
path: root/Kernel/VM/PurgeablePageRanges.h
blob: 4b7fade270217ab5b92eaaa392e571615c7f4e7a (plain)
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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Bitmap.h>
#include <AK/RefCounted.h>
#include <Kernel/SpinLock.h>

namespace Kernel {

struct VolatilePageRange {
    size_t base { 0 };
    size_t count { 0 };
    bool was_purged { false };

    bool is_empty() const { return count == 0; }

    bool intersects(const VolatilePageRange& other) const
    {
        return other.base < base + count || other.base + other.count > base;
    }

    bool intersects_or_adjacent(const VolatilePageRange& other) const
    {
        return other.base <= base + count || other.base + other.count >= base;
    }

    bool contains(const VolatilePageRange& other) const
    {
        return base <= other.base && base + count >= other.base + other.count;
    }

    VolatilePageRange intersected(const VolatilePageRange& other) const
    {
        auto b = max(base, other.base);
        auto e = min(base + count, other.base + other.count);
        if (b >= e)
            return {};
        return { b, e - b, was_purged };
    }

    void combine_intersecting_or_adjacent(const VolatilePageRange& other)
    {
        VERIFY(intersects_or_adjacent(other));
        if (base <= other.base) {
            count = (other.base - base) + other.count;
        } else {
            count = (base - other.base) + count;
            base = other.base;
        }
        was_purged |= other.was_purged;
    }

    void subtract_intersecting(const VolatilePageRange& other)
    {
        if (!intersects(other))
            return;
        if (other.contains(*this)) {
            count = 0;
            return;
        }
        if (base <= other.base) {
            count = (other.base - base);
        } else {
            auto new_base = other.base + other.count;
            count = (base + count) - new_base;
            base = new_base;
        }
    }

    bool range_equals(const VolatilePageRange& other) const
    {
        return base == other.base && count == other.count;
    }
    bool operator==(const VolatilePageRange& other) const
    {
        return base == other.base && count == other.count && was_purged == other.was_purged;
    }
    bool operator!=(const VolatilePageRange& other) const
    {
        return base != other.base || count != other.count || was_purged != other.was_purged;
    }
};

class VolatilePageRanges {
public:
    VolatilePageRanges(const VolatilePageRange& total_range)
        : m_total_range(total_range)
    {
    }
    VolatilePageRanges(const VolatilePageRanges& other)
        : m_ranges(other.m_ranges)
        , m_total_range(other.m_total_range)
    {
    }

    bool is_empty() const { return m_ranges.is_empty(); }
    void clear() { m_ranges.clear_with_capacity(); }

    bool is_all() const
    {
        if (m_ranges.size() != 1)
            return false;
        return m_ranges[0] == m_total_range;
    }

    void set_all()
    {
        if (m_ranges.size() != 1)
            m_ranges = { m_total_range };
        else
            m_ranges[0] = m_total_range;
    }

    bool intersects(const VolatilePageRange&) const;
    bool contains(size_t index) const
    {
        return intersects({ index, 1 });
    }

    bool add(const VolatilePageRange&);
    void add_unchecked(const VolatilePageRange&);
    bool remove(const VolatilePageRange&, bool&);

    template<typename F>
    IterationDecision for_each_intersecting_range(const VolatilePageRange& range, F f)
    {
        auto r = m_total_range.intersected(range);
        if (r.is_empty())
            return IterationDecision::Continue;

        size_t nearby_index = 0;
        auto* existing_range = binary_search(
            m_ranges.span(), r, &nearby_index, [](auto& a, auto& b) {
                if (a.intersects(b))
                    return 0;
                return (signed)(a.base - (b.base + b.count - 1));
            });
        if (!existing_range)
            return IterationDecision::Continue;

        if (existing_range->range_equals(r))
            return f(r);
        VERIFY(existing_range == &m_ranges[nearby_index]); // sanity check
        while (nearby_index < m_ranges.size()) {
            existing_range = &m_ranges[nearby_index];
            if (!existing_range->intersects(range))
                break;

            IterationDecision decision = f(existing_range->intersected(r));
            if (decision != IterationDecision::Continue)
                return decision;

            nearby_index++;
        }
        return IterationDecision::Continue;
    }

    template<typename F>
    IterationDecision for_each_nonvolatile_range(F f) const
    {
        size_t base = m_total_range.base;
        for (const auto& volatile_range : m_ranges) {
            if (volatile_range.base == base)
                continue;
            IterationDecision decision = f({ base, volatile_range.base - base });
            if (decision != IterationDecision::Continue)
                return decision;
            base = volatile_range.base + volatile_range.count;
        }
        if (base < m_total_range.base + m_total_range.count)
            return f({ base, (m_total_range.base + m_total_range.count) - base });
        return IterationDecision::Continue;
    }

    Vector<VolatilePageRange>& ranges() { return m_ranges; }
    const Vector<VolatilePageRange>& ranges() const { return m_ranges; }

private:
    Vector<VolatilePageRange> m_ranges;
    VolatilePageRange m_total_range;
};

class AnonymousVMObject;

class PurgeablePageRanges {
    friend class AnonymousVMObject;

public:
    PurgeablePageRanges(const VMObject&);

    void copy_purgeable_page_ranges(const PurgeablePageRanges& other)
    {
        if (this == &other)
            return;
        ScopedSpinLock lock(m_volatile_ranges_lock);
        ScopedSpinLock other_lock(other.m_volatile_ranges_lock);
        m_volatile_ranges = other.m_volatile_ranges;
    }

    bool add_volatile_range(const VolatilePageRange& range);
    enum class RemoveVolatileError {
        Success = 0,
        SuccessNoChange,
        OutOfMemory
    };
    RemoveVolatileError remove_volatile_range(const VolatilePageRange& range, bool& was_purged);
    bool is_volatile_range(const VolatilePageRange& range) const;
    bool is_volatile(size_t) const;

    bool is_empty() const { return m_volatile_ranges.is_empty(); }

    void set_was_purged(const VolatilePageRange&);

    const VolatilePageRanges& volatile_ranges() const { return m_volatile_ranges; }

protected:
    void set_vmobject(AnonymousVMObject*);

    VolatilePageRanges m_volatile_ranges;
    mutable RecursiveSpinLock m_volatile_ranges_lock;
    AnonymousVMObject* m_vmobject { nullptr };
};

class CommittedCowPages : public RefCounted<CommittedCowPages> {
    AK_MAKE_NONCOPYABLE(CommittedCowPages);

public:
    CommittedCowPages() = delete;

    CommittedCowPages(size_t);
    ~CommittedCowPages();

    NonnullRefPtr<PhysicalPage> allocate_one();
    bool return_one();

private:
    size_t m_committed_pages;
};

}