[PATCH] mm: fix and clarify section_active_mask()
From: Dan Williams
Date: Mon Jan 23 2017 - 20:05:19 EST
section_active_mask() converts a range of pfns into a bitmask where each
bit represents a 2M sub-range of a 128MB section (on x86_64). Clarify
that we expect that the arguments do not cross a section boundary, fix
the case where the start pfn is section aligned, but nr_pages is less
than a section, and use ULONG_MAX instead of -1 to represent to the mask
full case.
Cc: Michal Hocko <mhocko@xxxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxx>
Cc: Johannes Weiner <hannes@xxxxxxxxxxx>
Cc: Logan Gunthorpe <logang@xxxxxxxxxxxx>
Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx>
Cc: Stephen Bates <stephen.bates@xxxxxxxxxxxxx>
Reported-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Dan Williams <dan.j.williams@xxxxxxxxx>
---
mm/sparse.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/mm/sparse.c b/mm/sparse.c
index 4267d09b656b..7bb792b719b5 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -195,11 +195,18 @@ static unsigned long
section_active_mask(unsigned long pfn,
int idx_start, idx_size;
phys_addr_t start, size;
+ WARN_ON((pfn & ~PAGE_SECTION_MASK) + nr_pages > PAGES_PER_SECTION);
if (!nr_pages)
return 0;
+ /*
+ * The size is the number of pages left in the section or
+ * nr_pages, whichever is smaller. The size will be rounded up
+ * to the next SECTION_ACTIVE_SIZE boundary, the start will be
+ * rounded down.
+ */
start = PFN_PHYS(pfn);
- size = PFN_PHYS(min(nr_pages, PAGES_PER_SECTION
+ size = PFN_PHYS(min_not_zero(nr_pages, PAGES_PER_SECTION
- (pfn & ~PAGE_SECTION_MASK)));
size = ALIGN(size, SECTION_ACTIVE_SIZE);
@@ -207,7 +214,7 @@ static unsigned long section_active_mask(unsigned long pfn,
idx_size = section_active_index(size);
if (idx_size == 0)
- return -1;
+ return ULONG_MAX; /* full section */
return ((1UL << idx_size) - 1) << idx_start;
}
--
2.7.4
--001a114098d455bcf80546ccfb5c
Content-Type: text/x-patch; charset=US-ASCII;
name="0001-mm-fix-and-clarify-section_active_mask.patch"
Content-Disposition: attachment;
filename="0001-mm-fix-and-clarify-section_active_mask.patch"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_iyau86kq0
RnJvbSA4ZGM1YmExNWM1NDk0MmFhYmYwZTJmMzBiNzNjMjAyYTI1MjYzM2MxIE1vbiBTZXAgMTcg
MDA6MDA6MDAgMjAwMQpGcm9tOiBEYW4gV2lsbGlhbXMgPGRhbi5qLndpbGxpYW1zQGludGVsLmNv
bT4KRGF0ZTogTW9uLCAyMyBKYW4gMjAxNyAxNzowNToxOSAtMDgwMApTdWJqZWN0OiBbUEFUQ0hd
IG1tOiBmaXggYW5kIGNsYXJpZnkgc2VjdGlvbl9hY3RpdmVfbWFzaygpCgpzZWN0aW9uX2FjdGl2
ZV9tYXNrKCkgY29udmVydHMgYSByYW5nZSBvZiBwZm5zIGludG8gYSBiaXRtYXNrIHdoZXJlIGVh
Y2gKYml0IHJlcHJlc2VudHMgYSAyTSBzdWItcmFuZ2Ugb2YgYSAxMjhNQiBzZWN0aW9uIChvbiB4
ODZfNjQpLiBDbGFyaWZ5CnRoYXQgd2UgZXhwZWN0IHRoYXQgdGhlIGFyZ3VtZW50cyBkbyBub3Qg
Y3Jvc3MgYSBzZWN0aW9uIGJvdW5kYXJ5LCBmaXgKdGhlIGNhc2Ugd2hlcmUgdGhlIHN0YXJ0IHBm
biBpcyBzZWN0aW9uIGFsaWduZWQsIGJ1dCBucl9wYWdlcyBpcyBsZXNzCnRoYW4gYSBzZWN0aW9u
LCBhbmQgdXNlIFVMT05HX01BWCBpbnN0ZWFkIG9mIC0xIHRvIHJlcHJlc2VudCB0byB0aGUgbWFz
awpmdWxsIGNhc2UuCgpDYzogTWljaGFsIEhvY2tvIDxtaG9ja29Ac3VzZS5jb20+CkNjOiBWbGFz
dGltaWwgQmFia2E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