Skip to content

Commit

Permalink
pmm: add locking to get/put_free_frames()
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Aug 17, 2021
1 parent 90eda4b commit 269fb21
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions mm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <list.h>
#include <spinlock.h>

#include <mm/pmm.h>
#include <mm/regions.h>
Expand All @@ -37,6 +38,8 @@ static unsigned int free_frame_idx;

static size_t frames_count[MAX_PAGE_ORDER + 1];

static spinlock_t lock = SPINLOCK_INIT;

void display_frames_count(void) {
printk("Avail memory frames: (total size: %lu MB)\n", total_phys_memory / MB(1));

Expand Down Expand Up @@ -190,23 +193,26 @@ static inline frame_t *return_frame(frame_t *frame) {
return frame;
}

/* Reserves and returns the first free frame
* fulfilling the condition specified by
* the callback
/* Reserves and returns the first free frame fulfilling
* the condition specified by the callback
*/
frame_t *get_free_frames_cond(free_frames_cond_t cb) {
frame_t *frame;

spin_lock(&lock);
for_each_order (order) {
if (list_is_empty(&free_frames[order]))
continue;

list_for_each_entry (frame, &free_frames[order], list) {
if (cb(frame)) {
spin_unlock(&lock);
return reserve_frame(frame);
}
}
}
spin_unlock(&lock);

return NULL;
}

Expand All @@ -216,12 +222,15 @@ frame_t *get_free_frames(unsigned int order) {
if (order > MAX_PAGE_ORDER)
return NULL;

spin_lock(&lock);
if (list_is_empty(&free_frames[order])) {
/* FIXME: Add page split */
spin_unlock(&lock);
return NULL;
}

frame = reserve_frame(list_first_entry(&free_frames[order], frame_t, list));
spin_unlock(&lock);

return frame;
}
Expand All @@ -234,14 +243,17 @@ void put_free_frames(mfn_t mfn, unsigned int order) {
if (order > MAX_PAGE_ORDER)
return;

spin_lock(&lock);
list_for_each_entry (frame, &busy_frames[order], list) {
if (frame->mfn == mfn) {
/* FIXME: Maintain order wrt mfn value */
/* FIXME: Add frame merge */
return_frame(frame);
spin_unlock(&lock);
return;
}
}
spin_unlock(&lock);

panic("PMM: unable to find frame: %x in busy frames list\n");
}
Expand Down

0 comments on commit 269fb21

Please sign in to comment.