Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932366AbcLZESO (ORCPT ); Sun, 25 Dec 2016 23:18:14 -0500 Received: from mailout2.samsung.com ([203.254.224.25]:35133 "EHLO mailout2.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752237AbcLZESM (ORCPT ); Sun, 25 Dec 2016 23:18:12 -0500 X-AuditID: b6c32a2e-f79b36d000006b5e-06-58609a015153 From: Jaewon Kim To: gregkh@linuxfoundation.org, akpm@linux-foundation.org Cc: labbott@redhat.com, mina86@mina86.com, m.szyprowski@samsung.com, gregory.0xf0@gmail.com, laurent.pinchart@ideasonboard.com, akinobu.mita@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, jaewon31.kim@gmail.com, Jaewon Kim Subject: [PATCH] lib: bitmap: introduce bitmap_find_next_zero_area_and_size Date: Mon, 26 Dec 2016 13:18:11 +0900 Message-id: <1482725891-10866-1-git-send-email-jaewon31.kim@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAAzWSfUgTYRzHfe52txlNrpn1oBRyUKHh8uamZ81KeuHA/hDCsmLo4Z6ctN3k bkZGkGHlS76kpZlpTcresBYrysRUTLQCSdOCIq1l/tFqKhZir7bt6r/P9/t8v8/z4+GnwjUe IlKVJziQKPBWmlykuP84JiYONGZnxrvLYllvTylgm1xtJFt82UWy9efuEOypu+cBWzntxdgb nfMYW1ZzRcmOdDSR7HjbAsHe6htTss6B48TmxdzDxjEld6H0PMG5Z2uV3JOGnwquu7lNyd3+ 5cW46a6XJFd17ybgvrpXpofuRUYL4s1IjEZCjt2cJ+Sm0Gk7s7ZkGRLjmTgmmU2iowXehlLo rTvS47bnWf0z09EHeWuB30rnJYlet9Eo2gscKNpilxwp9D6G0WmZ+CStTqfT6hnTep3BH8lG lpL3hvy3kYfOljUri8DdiHIQqoKUHi7UzwOZl8GhcRdZDhapNFQrgC9bX+CyKMFg9dSsX6iC jV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Y5NZJzByL2BkWMUokVqQXFCclJ5rlJdarlecmFtcmpeul5yf u4kRHPrPpHcwHt7lfohRgINRiYfXQDwhQog1say4MvcQowQHs5II7+OJQCHelMTKqtSi/Pii 0pzU4kOMpkAHTGSWEk3OB8ZlXkm8oYm5ibmxgYW5paWJkZI4b+PsZ+FCAumJJanZqakFqUUw fUwcnFINjLttHLYt3XTEruOz4bRXTLYPdJ/atvqc0fH7IOKocc7u6qJLermnpkWopWt7xLsF tckt+OZlHPvr7JkNt9lvbtRcfdnz644bFfO8dgb48VWubu0Kyjk2P/Xk+u+b8islg1e9kb/e 80A4c9utOy5beUN036UHHxPmDmJq/cUXKnNlzyy5s/5/5yqxFGckGmoxFxUnAgBqqvLSkwIA AA== X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20161226041809epcas5p1981244de55764c10f1a80d80346f3664 X-Msg-Generator: CA X-Sender-IP: 203.254.230.27 X-Local-Sender: =?UTF-8?B?6rmA7J6s7JuQG1N5c3RlbSBTL1fqsJzrsJwy6re466O5KA==?= =?UTF-8?B?66y07ISgKRvsgrzshLHsoITsnpAbUzUo7LGF7J6EKS/ssYXsnoQ=?= X-Global-Sender: =?UTF-8?B?SmFld29uIEtpbRtTeXN0ZW0gUy9XIFImRCBHcm91cCAyG1Nh?= =?UTF-8?B?bXN1bmcgRWxlY3Ryb25pY3MbUzUvU2VuaW9yIEVuZ2luZWVy?= X-Sender-Code: =?UTF-8?B?QzEwG1RFTEUbQzEwRDkxMjI=?= CMS-TYPE: 105P DLP-Filter: Pass X-CFilter-Loop: Reflected X-HopCount: 7 X-CMS-RootMailID: 20161226041809epcas5p1981244de55764c10f1a80d80346f3664 X-RootMTR: 20161226041809epcas5p1981244de55764c10f1a80d80346f3664 References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3316 Lines: 102 There was no bitmap API which returns both next zero index and size of zeros from that index. This is helpful to look fragmentation. This is an test code to look size of zeros. Test result is '10+9+994=>1013 found of total: 1024' unsigned long search_idx, found_idx, nr_found_tot; unsigned long bitmap_max; unsigned int nr_found; unsigned long *bitmap; search_idx = nr_found_tot = 0; bitmap_max = 1024; bitmap = kzalloc(BITS_TO_LONGS(bitmap_max) * sizeof(long), GFP_KERNEL); /* test bitmap_set offset, count */ bitmap_set(bitmap, 10, 1); bitmap_set(bitmap, 20, 10); for (;;) { found_idx = bitmap_find_next_zero_area_and_size(bitmap, bitmap_max, search_idx, &nr_found); if (found_idx >= bitmap_max) break; if (nr_found_tot == 0) printk("%u", nr_found); else printk("+%u", nr_found); nr_found_tot += nr_found; search_idx = found_idx + nr_found; } printk("=>%lu found of total: %lu\n", nr_found_tot, bitmap_max); Signed-off-by: Jaewon Kim --- include/linux/bitmap.h | 6 ++++++ lib/bitmap.c | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 3b77588..b724a6c 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -46,6 +46,7 @@ * bitmap_clear(dst, pos, nbits) Clear specified bit area * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area * bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above + * bitmap_find_next_zero_area_and_size(buf, len, pos, n, mask) Find bit free area and its size * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src) @@ -123,6 +124,11 @@ extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map, unsigned long align_mask, unsigned long align_offset); +extern unsigned long bitmap_find_next_zero_area_and_size(unsigned long *map, + unsigned long size, + unsigned long start, + unsigned int *nr); + /** * bitmap_find_next_zero_area - find a contiguous aligned zero area * @map: The address to base the search on diff --git a/lib/bitmap.c b/lib/bitmap.c index 0b66f0e..d02817c 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -332,6 +332,31 @@ unsigned long bitmap_find_next_zero_area_off(unsigned long *map, } EXPORT_SYMBOL(bitmap_find_next_zero_area_off); +/** + * bitmap_find_next_zero_area_and_size - find a contiguous aligned zero area + * @map: The address to base the search on + * @size: The bitmap size in bits + * @start: The bitnumber to start searching at + * @nr: The number of zeroed bits we've found + */ +unsigned long bitmap_find_next_zero_area_and_size(unsigned long *map, + unsigned long size, + unsigned long start, + unsigned int *nr) +{ + unsigned long index, i; + + *nr = 0; + index = find_next_zero_bit(map, size, start); + + if (index >= size) + return index; + i = find_next_bit(map, size, index); + *nr = i - index; + return index; +} +EXPORT_SYMBOL(bitmap_find_next_zero_area_and_size); + /* * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers, * second version by Paul Jackson, third by Joe Korty. -- 1.9.1