Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp121063pxj; Thu, 3 Jun 2021 02:25:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyY0Gt9TKxor4TrMtqXmU8wUOKOLw5ki4m7SMf+1OrTLROr3azcQ8Kq0sK24vSCklUb/clf X-Received: by 2002:a17:907:779a:: with SMTP id ky26mr31015718ejc.293.1622712317920; Thu, 03 Jun 2021 02:25:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622712317; cv=none; d=google.com; s=arc-20160816; b=JeTzKgIkqTPQTk24gk/pKWR1MrPjnoY5kfFOIn8N6w9NcZp7x3GUA6n9wueV+sEbrq sh5G+FoTdGBc68QqSv6zTIZEN4gqLL+AOIdRmg+57wktNfU21kfSnA38zx1zpM3Amai4 GAllQPsdjRb3x4asA4Swcy/w/W89iG4rolMAkQPJDW8qUjqypQCWgsrrej//mzA6avkV YhN3cnThUkU4TSoCKI/22WCpZkrNI07XM9euwPlClca1foWK0CTTsCnAnVWk8Uh95PhA 6p2i0e1shzw2c94iz/Sx10QwRB7mlfuxZBERv+1Ezx8UjBlmMsw55YFiAXnByImAGWku 8+7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=v/gLmANLXClZLGWxFLwz7EZX4XpyBRykf/momGni9XM=; b=eZjhtxxi2Jbrn1PT3QCNH8HOfz01pq+O7et049RmxUVq59XwUaeEtz0wfRJagxliqH Yvx1mOYWZCsMIib4OsaX2P6bXFC+TfUnYhVkSxeDIt6O2mD1+y3e9mZoHh6ZvZ9FIJ2L CMCIKcJcIbdUfncbRjRlaQ7KJcEy7bS9d6bKDnLDQ1da/+AUKjSf/dFPTDD4J0CxOAAT zk08SnnH83lT+3+pyoHxdTyYbwqCW0xvn6gCbiUS7c0Xn4dv+l8LaYCisYn0uopUvnZX 30l3j99DJC+kDJ0chyhEjkIHtDNBcS1FG1XznRlexXbpKWfcRnhuDwGOJ6FzQW0aCTik wVfQ== 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 f22si1867437ejb.54.2021.06.03.02.24.55; Thu, 03 Jun 2021 02:25:17 -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 S229754AbhFCJYm (ORCPT + 99 others); Thu, 3 Jun 2021 05:24:42 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:7087 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229710AbhFCJYk (ORCPT ); Thu, 3 Jun 2021 05:24:40 -0400 Received: from dggeme759-chm.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4FwgN9714VzYqM8; Thu, 3 Jun 2021 17:20:09 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggeme759-chm.china.huawei.com (10.3.19.105) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2176.2; Thu, 3 Jun 2021 17:22:55 +0800 From: Tian Tao To: , , , CC: , , , Tian Tao Subject: [PATCH v3 3/3] drivers/base/node.c: use bin_attribute to avoid buff overflow Date: Thu, 3 Jun 2021 17:22:42 +0800 Message-ID: <1622712162-7028-4-git-send-email-tiantao6@hisilicon.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1622712162-7028-1-git-send-email-tiantao6@hisilicon.com> References: <1622712162-7028-1-git-send-email-tiantao6@hisilicon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggeme759-chm.china.huawei.com (10.3.19.105) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reading sys/devices/system/cpu/cpuX/nodeX/ returns cpumap and cpulist. However, the size of this file is limited to PAGE_SIZE because of the limitation for sysfs attribute. so we use bin_attribute instead of attribute to avoid NR_CPUS too big to cause buff overflow. Signed-off-by: Tian Tao Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" --- drivers/base/node.c | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/drivers/base/node.c b/drivers/base/node.c index 4cef82c..537d046 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -27,42 +27,45 @@ static struct bus_type node_subsys = { }; -static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf) +static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf, + loff_t off, size_t count) { ssize_t n; cpumask_var_t mask; struct node *node_dev = to_node(dev); - /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */ - BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1)); - if (!alloc_cpumask_var(&mask, GFP_KERNEL)) return 0; cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask); - n = cpumap_print_to_pagebuf(list, buf, mask); + n = cpumap_print_to_buf(list, buf, mask, off, count); free_cpumask_var(mask); return n; } -static inline ssize_t cpumap_show(struct device *dev, - struct device_attribute *attr, - char *buf) +static inline ssize_t cpumap_read(struct file *file, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t off, size_t count) { - return node_read_cpumap(dev, false, buf); + struct device *dev = kobj_to_dev(kobj); + + return node_read_cpumap(dev, false, buf, off, count); } -static DEVICE_ATTR_RO(cpumap); -static inline ssize_t cpulist_show(struct device *dev, - struct device_attribute *attr, - char *buf) +static BIN_ATTR_RO(cpumap, 0); + +static inline ssize_t cpulist_read(struct file *file, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t off, size_t count) { - return node_read_cpumap(dev, true, buf); + struct device *dev = kobj_to_dev(kobj); + + return node_read_cpumap(dev, true, buf, off, count); } -static DEVICE_ATTR_RO(cpulist); +static BIN_ATTR_RO(cpulist, 0); /** * struct node_access_nodes - Access class device to hold user visible @@ -557,15 +560,28 @@ static ssize_t node_read_distance(struct device *dev, static DEVICE_ATTR(distance, 0444, node_read_distance, NULL); static struct attribute *node_dev_attrs[] = { - &dev_attr_cpumap.attr, - &dev_attr_cpulist.attr, &dev_attr_meminfo.attr, &dev_attr_numastat.attr, &dev_attr_distance.attr, &dev_attr_vmstat.attr, NULL }; -ATTRIBUTE_GROUPS(node_dev); + +static struct bin_attribute *node_dev_bin_attrs[] = { + &bin_attr_cpumap, + &bin_attr_cpulist, + NULL +}; + +static const struct attribute_group node_dev_group = { + .attrs = node_dev_attrs, + .bin_attrs = node_dev_bin_attrs +}; + +static const struct attribute_group *node_dev_groups[] = { + &node_dev_group, + NULL +}; #ifdef CONFIG_HUGETLBFS /* -- 2.7.4