Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp434101pxj; Wed, 2 Jun 2021 02:54:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykkemDxCdvxz8Hplw1qsbEepzAwEmEGQJZPL1KTQ5k1C3jpkuLljDcAN+7b73M8sF/ONK4 X-Received: by 2002:a17:906:6849:: with SMTP id a9mr25615882ejs.415.1622627653752; Wed, 02 Jun 2021 02:54:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622627653; cv=none; d=google.com; s=arc-20160816; b=dzqjbuZCDtGOm0ZNmq2ttQVmdPdL8G3xIdr3DchUDyfez1Nbf3HhkNAEvRQguyD+7Y 5wusiRPBqpp4kuwz5cItaDuzItEN4bNEEwtm08g942N60SpIrX1XtZpqpJ9dNQBdWTL1 KhzyP0/ygr6WSazpLNOK1gWNgDKSfIyTx/PmA3BW1oYJqEWYsrIaw7IvjKmdOe8iEPgL q4Aze9/iBhQJalZR+xshPL5YrnQrFu/YS/W0LOXA6P0K7xUkjQWJOP9UjBn5ZL9KLpxX ReQ1q09vcIRJ2+sUEo9dXuA39Enaxx9ywKXTR/BhrovCPByd8oQXsw0dPg1Dd/Y/2ehq aP0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=pe1K5oLu3r8MUqhrVEf789IGciUxPIiOroYhlVQhckc=; b=jDG/wAsS+ozbyo5B5VNaijwp38bSBH/y6ZqDGjZxuwIPHN8PK/te/+mSYVUiJKEfPx vnrZNxn4eThIm5TOuyFdVq2Xtp5E3ZH72fDWrw65YKhuGe9ZJkknoLDykXsgkdsx1u5d C5Nw3liT4PlyJL9uhGDL1n33gWFXngvfH++Nj/AqQZANaieKWNqHw8voUcboR1DRQv7D 2ebF3UWY/jYnIQ25+KNVK2EI46iFNn8qk084clOCvLxjISjukJyJAjYQ4/Wb+5zSTDFG Ftz333yjwIYr1TcEORINHJa4b/Z7HdO9EvmNkX+aq4FV1je4/IHk1vV4wFrNxmtyPfdN nZlw== 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=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id he12si6072022ejc.636.2021.06.02.02.53.49; Wed, 02 Jun 2021 02:54: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=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229828AbhFBJCD (ORCPT + 99 others); Wed, 2 Jun 2021 05:02:03 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:2845 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbhFBJCB (ORCPT ); Wed, 2 Jun 2021 05:02:01 -0400 Received: from dggeme759-chm.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Fw2tF6fY4zWqMK; Wed, 2 Jun 2021 16:55:33 +0800 (CST) Received: from [127.0.0.1] (10.40.188.144) 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; Wed, 2 Jun 2021 17:00:16 +0800 Subject: Re: [PATCH 1/2] topology: use bin_attribute to avoid buff overflow To: Andy Shevchenko CC: Greg KH , Tian Tao , Linux Kernel Mailing List , Andrew Morton , Barry Song , Andy Shevchenko , "Rafael J. Wysocki" , Jonathan Cameron References: <1622516210-10886-1-git-send-email-tiantao6@hisilicon.com> <1622516210-10886-2-git-send-email-tiantao6@hisilicon.com> <4c9c7c17-e8d1-d601-6262-8064293a06a9@huawei.com> From: "tiantao (H)" Message-ID: Date: Wed, 2 Jun 2021 17:00:16 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.40.188.144] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) 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 在 2021/6/2 16:48, Andy Shevchenko 写道: > On Wed, Jun 2, 2021 at 9:45 AM tiantao (H) wrote: >> 在 2021/6/2 14:18, Greg KH 写道: >>> On Wed, Jun 02, 2021 at 02:14:49PM +0800, tiantao (H) wrote: >>>> 在 2021/6/1 12:58, Greg KH 写道: >>>>> On Tue, Jun 01, 2021 at 10:56:49AM +0800, Tian Tao wrote: > ... > >>>>>> /** >>>>>> + * bitmap_print_to_buf - convert bitmap to list or hex format ASCII string >>>>>> + * @list: indicates whether the bitmap must be list >>>>>> + * @buf: page aligned buffer into which string is placed >>>>>> + * @maskp: pointer to bitmap to convert >>>>>> + * @nmaskbits: size of bitmap, in bits >>>>>> + * @off: offset in buf >>>>>> + * @count: count that already output >>>>>> + * >>>>>> + * the role of bitmap_print_to_buf and bitmap_print_to_pagebuf is >>>>>> + * the same, the difference is that the second parameter 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) >>>>>> +{ >>>>>> + int len, size; >>>>>> + void *data; >>>>>> + char *fmt = list ? "%*pbl\n" : "%*pb\n"; >>>>>> + >>>>>> + len = snprintf(NULL, 0, fmt, nmaskbits, maskp); >>>>>> + >>>>>> + data = kvmalloc(len+1, GFP_KERNEL); >>>>>> + if (!data) >>>>>> + return -ENOMEM; >>>>>> + >>>>>> + size = scnprintf(data, len+1, fmt, nmaskbits, maskp); >>>>>> + size = memory_read_from_buffer(buf, count, &off, data, size); >>>>>> + kvfree(data); >>>>>> + >>>>>> + return size; >>>>> Why is this so different from bitmap_print_to_pagebuf()? Can't you just >>>>> use this function as the "real" function and then change >>>>> bitmap_print_to_pagebuf() to call it with a size of PAGE_SIZE? >>>> Do you mean do following change, is that correct? :-) >>> Maybe, it is whitespace corrupted, and it still feels like this function >>> is much bigger than it needs to be given the function it is replacing is >>> only a simple sprintf() call. >>> >>>> +int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, >>>> + int nmaskbits, loff_t off, size_t count) >>>> +{ >>>> + int len, size; >>>> + void *data; >>>> + const char *fmt = list ? "%*pbl\n" : "%*pb\n"; >>>> + >>>> + if (off == LLONG_MAX && count == PAGE_SIZE - offset_in_page(buf)) >>>> + return scnprintf(buf, count, fmt, nmaskbits, maskp); >>>> + >>>> + len = snprintf(NULL, 0, fmt, nmaskbits, maskp); >>>> + >>>> + data = kvmalloc(len+1, GFP_KERNEL); >>> Why do you need to allocate more memory? And why kvmalloc()? >> Because the memory here will exceed a pagesize and we don't know the >> exact size, we have to call >> >> snprintf first to get the actual size. kvmalloc() is used because when >> physical memory is tight, kmalloc >> >> may fail, but vmalloc will succeed. It is not so bad that the memory is >> not requested here. > To me it sounds like the function is overengineered / lacks thought > through / optimization. > Can you provide a few examples that require the above algorithm? so you think we should use kmalloc instead of kvmalloc ? > >>>> + if (!data) >>>> + return -ENOMEM; >>>> + >>>> + size = scnprintf(data, len+1, fmt, nmaskbits, maskp); >>>> + >>>> + size = memory_read_from_buffer(buf, count, &off, data, size); >>>> + kvfree(data); >>>> + >>>> + return size; >>>> +} > > -- > With Best Regards, > Andy Shevchenko > . >