Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1386044pxv; Fri, 2 Jul 2021 02:30:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwICnEl0/N40SD8Czj6Sjysfi7LMeZb09D/yk6i2+dXtm+csBb+VSSBM0V3zU6vT0FnvRMi X-Received: by 2002:a17:906:9b8f:: with SMTP id dd15mr4102706ejc.305.1625218200749; Fri, 02 Jul 2021 02:30:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625218200; cv=none; d=google.com; s=arc-20160816; b=MULcItzdXT0ARYHNXNQfrE0TTXBNNjJcBz6Mml/ShK/1OUSbWMvfa6rLajYqv10ZtF oeGxQJlmHuD8vTU6SwB1/BJ/7HhYpPO7Jz4Lrp/etR+Z44GTQtgPuWXPGU2+ztJ1uLu0 JKFcqK8HOXSWAXhsxD91VOl2cp5RONnoPEB8y5o54oXiFNmBDxWFH8MPFCrTPDxabJmu wZIcKzDDMLvLIVFy/Ik2NQuoY0bOr8pr+GfUn78o/xqi4eI6az7Au9N822FuFdztwrAU OfuWJMuiaFDB95vjqtQQhGkq+/Xwc+ek/DZ7kTluV4ndIaJub/3MmdjSYHnk7O/wbp/M PCxQ== 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=WLk/2//Z04J8rb1s2yvHLw4Qjwqz0JH5egosG0xmndU=; b=TR6Aa5URFHsAbNMcByrXj9voH7BlybMKCBJv/rCv0230t+ZHYruWA0naffNC4+Pqzy Z1FcBn+XAFnjuxgNgxSMo8KZmxRoo/fwHbo1lrMtjxr3EVayD2kPZgqBMJ511+ePr3ST HPeqkajz4XJkhlZqLSL/ake7+dwn9LinTXeO4mZrBPPoTHYR8QkI8KqPGpkUORO3b2Am z5d+HqLos4ZEvpw8YOv5xxQj7+JBomuxJ2n2+4rCPAvoCPkP1M6xseXVeeNk1jRMC9N/ W4cTlSeif0EBrxksiZRsvfTStx2KpZkDtwSgv93fOVXFwg1CR0OLQhaemvZT3YfuI5kF oaJA== 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 z21si1277636edb.572.2021.07.02.02.29.25; Fri, 02 Jul 2021 02:30:00 -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 S231372AbhGBJ3H (ORCPT + 99 others); Fri, 2 Jul 2021 05:29:07 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:6036 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231320AbhGBJ3G (ORCPT ); Fri, 2 Jul 2021 05:29:06 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4GGV1y712TzXlql; Fri, 2 Jul 2021 17:21:10 +0800 (CST) Received: from dggemi761-chm.china.huawei.com (10.1.198.147) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2176.2; Fri, 2 Jul 2021 17:26:33 +0800 Received: from SWX921481.china.huawei.com (10.126.203.116) 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; Fri, 2 Jul 2021 17:26:28 +0800 From: Barry Song To: , , , CC: , , , , , , , , , , , , , , , , , Tian Tao , Barry Song Subject: [PATCH v5 1/3] cpumask: introduce cpumap_print_to_buf to support large bitmask and list Date: Fri, 2 Jul 2021 21:25:57 +1200 Message-ID: <20210702092559.8776-2-song.bao.hua@hisilicon.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <20210702092559.8776-1-song.bao.hua@hisilicon.com> References: <20210702092559.8776-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.203.116] 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 The existing cpumap_print_to_pagebuf() is used by cpu topology and other drivers to export hexadecimal bitmask and decimal list to userspace by sysfs ABI. Right now, those drivers are using a normal attribute for this kind of ABIs. A normal attribute typically has show entry as below: static ssize_t example_dev_show(struct device *dev, struct device_attribute *attr, char *buf) { ... return cpumap_print_to_pagebuf(true, buf, &pmu_mmdc->cpu); } show entry of attribute has no offset and count parameters and this means the file is limited to one page only. cpumap_print_to_pagebuf() API works terribly well for this kind of normal attribute with buf parameter and without offset, count: static inline ssize_t cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask) { return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask), nr_cpu_ids); } The problem is once we have many cpus, we have a chance to make bitmask or list more than one page. Especially for list, it could be as complex as 0,3,5,7,9,...... We have no simple way to know it exact size. It turns out bin_attribute is a way to break this limit. bin_attribute has show entry as below: static ssize_t example_bin_attribute_show(struct file *filp, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t offset, size_t count) { ... } With the new offset and count parameters, this makes sysfs ABI be able to support file size more than one page. For example, offset could be >= 4096. This patch introduces cpumap_print_to_buf() so that those drivers can move to bin_attribute to support large bitmask and list. In result, we have to pass the corresponding parameters from bin_attribute to this new API. Signed-off-by: Tian Tao 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 Signed-off-by: Barry Song --- include/linux/cpumask.h | 19 +++++++++++++++++++ lib/cpumask.c | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index bfc4690de4f4..24f410a2e793 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -983,6 +983,25 @@ 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; + * Typically used by bin_attribute to export cpumask bitmask and + * list ABI. + * @list: indicates whether the cpumap must be list + * true: print in decimal list format + * fasle: print in hexadecimal bitmask format + * @mask: the cpumask to copy + * @buf: the buffer to copy into + * @off: in the string from which we are copying, We copy to @buf + * @count: the maximum number of bytes to print + * + * Returns the length of how many bytes have been copied. + */ +extern ssize_t +cpumap_print_to_buf(bool list, char *buf, const struct cpumask *mask, + loff_t off, size_t count); + #if NR_CPUS <= BITS_PER_LONG #define CPU_MASK_ALL \ (cpumask_t) { { \ diff --git a/lib/cpumask.c b/lib/cpumask.c index c3c76b833384..40421a6d31bc 100644 --- a/lib/cpumask.c +++ b/lib/cpumask.c @@ -279,3 +279,21 @@ int cpumask_any_distribute(const struct cpumask *srcp) return next; } EXPORT_SYMBOL(cpumask_any_distribute); + +ssize_t cpumap_print_to_buf(bool list, char *buf, const struct cpumask *mask, + loff_t off, size_t count) +{ + const char *fmt = list ? "%*pbl\n" : "%*pb\n"; + ssize_t size; + void *data; + + data = kasprintf(GFP_KERNEL, fmt, nr_cpu_ids, cpumask_bits(mask)); + if (!data) + return -ENOMEM; + + size = memory_read_from_buffer(buf, count, &off, data, strlen(data) + 1); + kfree(data); + + return size; +} +EXPORT_SYMBOL(cpumap_print_to_buf); -- 2.25.1