Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp406513pxj; Thu, 17 Jun 2021 05:36:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6urjL3ih6V8+4f7jXqo5LQme5g0CH+Oy8LGdas5R6wopCbkbkZvXH5HLYDl/rin+g4akr X-Received: by 2002:a17:906:1806:: with SMTP id v6mr4976272eje.454.1623933373575; Thu, 17 Jun 2021 05:36:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623933373; cv=none; d=google.com; s=arc-20160816; b=aoIFKh3krEezjG0jXlc7Nc6dzsa9H8GICStQSBUb7BCrEU8HTqXrPzgfJXckmrkb+h mv+u6xRj+gvMRAhcSxCH8BrwaLd5WXe2JEV0jjI8fyX/IaS8fWLJb7IOZpFiR1uCxehg dWRJRWMBv+xcAn9OPHVzVmi26WQd+slFUAM0vK8vaFybt1g3AQSmfbM6VcTTPE98ZiBu fM3QX7BdnrpK7e72GTdMBGQCK9+znYBjPsDPx1S2KpFUV4GCSRMgqGNEl3FFQ/d7/Tak nveO7MKfa0M8jMVmxtY9u7q4SnoP8GnYCKlbygjOfDxnMHiUaba3w0+QwEZ6O96YxdqP g3KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=sxpiKhAlY8FVu3ARbixljNWvmWcylAxe9gwo5IQJ8Cs=; b=o/a6AElTR8ZsHmNI7TL1sQyHmJiA7n+51UO+rtLvRIZVRancFIRTHREZnjnfJrgQAa tGDLVanPyD9MR38WA6r2gtDYCCYttLcS1PxpEu49HZOtBT68V5jAGmEXzDintdsXI8nG Yr3QerGoZJ/Cd8uRXJUbnTobmSHjJ1eBFdtKVTfKcORF7cDVQFLEUgBcFwx05rC8MQ9Q 2dG6WrT8UKFupOR7WH86JzrErPloaIYgrvuRMDlKng8S7In1ZKyEWIhaqbkQUyxA2NVk Y3MSgQvJ7E616PkBEfmeMy4QJe3G/K5XpLAOovK0zZJ9UbYyM1NFyRqIe8C3ptAjarUh JK3w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hisilicon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i4si5118447eds.147.2021.06.17.05.35.50; Thu, 17 Jun 2021 05:36:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hisilicon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232068AbhFQKWa (ORCPT + 99 others); Thu, 17 Jun 2021 06:22:30 -0400 Received: from szxga08-in.huawei.com ([45.249.212.255]:8254 "EHLO szxga08-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229716AbhFQKW3 (ORCPT ); Thu, 17 Jun 2021 06:22:29 -0400 Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4G5HxB2FHXz1BNVB; Thu, 17 Jun 2021 18:15:10 +0800 (CST) Received: from dggemi761-chm.china.huawei.com (10.1.198.147) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2176.2; Thu, 17 Jun 2021 18:20:12 +0800 Received: from SWX921481.china.huawei.com (10.126.201.234) by dggemi761-chm.china.huawei.com (10.1.198.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2176.2; Thu, 17 Jun 2021 18:20:06 +0800 From: Barry Song To: , , CC: , , , , , , , , , , , , , , , , , , , Jonathan Cameron Subject: [PATCH v4 1/4] lib: bitmap: introduce bitmap_print_to_buf Date: Thu, 17 Jun 2021 22:19:07 +1200 Message-ID: <20210617101910.13228-2-song.bao.hua@hisilicon.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <20210617101910.13228-1-song.bao.hua@hisilicon.com> References: <20210617101910.13228-1-song.bao.hua@hisilicon.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.126.201.234] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggemi761-chm.china.huawei.com (10.1.198.147) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tian Tao New API bitmap_print_to_buf() with bin_attribute to avoid maskp exceeding PAGE_SIZE. bitmap_print_to_pagebuf() is a special case of bitmap_print_to_buf(), so in bitmap_print_to_pagebuf() call bitmap_print_to_buf(). Signed-off-by: Tian Tao Reviewed-by: Jonathan Cameron Cc: Andrew Morton Cc: Andy Shevchenko Cc: Randy Dunlap Cc: Stefano Brivio Cc: Alexander Gordeev Cc: "Ma, Jianpeng" Cc: Yury Norov Cc: Valentin Schneider Cc: Peter Zijlstra Cc: Daniel Bristot de Oliveira --- include/linux/bitmap.h | 2 ++ include/linux/cpumask.h | 21 +++++++++++++++++++++ lib/bitmap.c | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index a36cfcec4e77..0de6effa2797 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -226,6 +226,8 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits); int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, int nmaskbits); +int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, + int nmaskbits, loff_t off, size_t count); #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index bfc4690de4f4..1bef69e4b950 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -983,6 +983,27 @@ cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask) nr_cpu_ids); } +/** + * cpumap_print_to_buf - copies the cpumask into the buffer either + * as comma-separated list of cpus or hex values of cpumask + * @list: indicates whether the cpumap must be list + * @mask: the cpumask to copy + * @buf: the buffer to copy into + * @off: in the string from which we are copying, We copy to @buf + off + * @count: the maximum number of bytes to print + * + * The role of cpumap_print_to_buf() and cpumap_print_to_pagebuf() is + * the same, the difference is that buf of bitmap_print_to_buf() + * can be more than one pagesize. + */ +static inline ssize_t +cpumap_print_to_buf(bool list, char *buf, const struct cpumask *mask, + loff_t off, size_t count) +{ + return bitmap_print_to_buf(list, buf, cpumask_bits(mask), + nr_cpu_ids, off, count); +} + #if NR_CPUS <= BITS_PER_LONG #define CPU_MASK_ALL \ (cpumask_t) { { \ diff --git a/lib/bitmap.c b/lib/bitmap.c index 74ceb02f45e3..ed7ef6e2edca 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -461,6 +461,40 @@ int bitmap_parse_user(const char __user *ubuf, } EXPORT_SYMBOL(bitmap_parse_user); +/** + * bitmap_print_to_buf - convert bitmap to list or hex format ASCII string + * @list: indicates whether the bitmap must be list + * @buf: the kernel space buffer to read to + * @maskp: pointer to bitmap to convert + * @nmaskbits: size of bitmap, in bits + * @off: offset in data buffer below + * @count: the maximum number of bytes to print + * + * The role of bitmap_print_to_buf() and bitmap_print_to_pagebuf() is + * the same, the difference is that buf of bitmap_print_to_buf() + * can be more than one pagesize. + */ +int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, + int nmaskbits, loff_t off, size_t count) +{ + const char *fmt = list ? "%*pbl\n" : "%*pb\n"; + ssize_t size; + void *data; + + if (off == LLONG_MAX && count == PAGE_SIZE - offset_in_page(buf)) + return scnprintf(buf, count, fmt, nmaskbits, maskp); + + data = kasprintf(GFP_KERNEL, fmt, nmaskbits, maskp); + if (!data) + return -ENOMEM; + + size = memory_read_from_buffer(buf, count, &off, data, strlen(data) + 1); + kfree(data); + + return size; +} +EXPORT_SYMBOL(bitmap_print_to_buf); + /** * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string * @list: indicates whether the bitmap must be list @@ -482,8 +516,7 @@ int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, { ptrdiff_t len = PAGE_SIZE - offset_in_page(buf); - return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) : - scnprintf(buf, len, "%*pb\n", nmaskbits, maskp); + return bitmap_print_to_buf(list, buf, maskp, nmaskbits, LLONG_MAX, len); } EXPORT_SYMBOL(bitmap_print_to_pagebuf); -- 2.25.1