Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp505910pxj; Wed, 2 Jun 2021 04:52:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzG79y7SPZxE6qRgzAGmDhFTbzlRqMpI+2gg91qiGQoXaJBawuaGPIuaAHIgW87PEl6KW+N X-Received: by 2002:a92:7f07:: with SMTP id a7mr24875184ild.202.1622634746597; Wed, 02 Jun 2021 04:52:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622634746; cv=none; d=google.com; s=arc-20160816; b=gTa2dFOM4xr3CsXR32f/toEPBnfPGt3u3877mGmI4/+PDIezuT0tlx8JTL5afFoObT fhDcDTSz8oXuAQnZkydYPvyOflm5pLi3iAqjCKFBDJoxBdGjoSwWUqOL6J+C9B4B/pdv g+AG0y7V7/LDTohtqujIGzZRpXV9q8OAfRsO7xlOYnjwXEY3V5IXMD8nBmjhuxRYrKIG /MC9afF1hHJS2qPTDjl+k3PEsCoIws/K4W543BTyIU8Czs9oFIDFC/y6k+8q72vo/AC5 ZUdntR6NYKusKT9JMrbiB1ZPXRfbftti7iKu98WHOtpUF9AFVHjqTVUuyATakuloxI81 4laQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=HsLEyAq/QqeV49M76ld98fel6iToa+7mRBrTMrPUJdg=; b=zvJYYRUAztwld99qT3XahCt7BXe8Dw7qKX0tQj2ZzH3IWg7AWW0wMEBfW9pJbvZdkJ bKFIdh+59xXwQh4Rn3LogYPRZGjMWDd5e873nJ3aPgaA4pHLwGHUObcga+lOObRhmsuS yL6NDwRfVnQsDFTkA1jXSJvr42xHRxMoyl058L9N3EmqAH8RUxHbl9FsxLQSWD0uITdS fw+I2NVzBb7YAnrYsg0/mRi5k2JhFCuZ82xULJqg2rh4vtKDOPR5iD+p99N+dvihraVK QOADwZiDc49hjF2qgMt08K67Po/i2SJJf/PWB+C6mXhbT9C2sJSB3vNlxltWo6amBt1n uDCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Q44Hcwhx; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o15si23143609ilu.104.2021.06.02.04.52.13; Wed, 02 Jun 2021 04:52:26 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Q44Hcwhx; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230262AbhFBJII (ORCPT + 99 others); Wed, 2 Jun 2021 05:08:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:45682 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230099AbhFBJII (ORCPT ); Wed, 2 Jun 2021 05:08:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 78E93613AC; Wed, 2 Jun 2021 09:06:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622624784; bh=bYcNDOKdBmTW3M+pfiUN2wg1jTBSFQ4m469qCqR2dhI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Q44HcwhxipmzgGNQoqO2WvwJ+Ulc3XZW+zyFHGbjKONnWTSKLVpUNNkRNcBAt17Qc RYeNLsAKlCcstML2PZTRmCxOKv+eZDC7xylxhp+/xGJBAcOSYZrw+jtleKymwwxxaR Lfhz6ezO+5FWaMAEJ9jAFn+FdaYZQGqBqhaa4e9s= Date: Wed, 2 Jun 2021 11:06:21 +0200 From: Greg KH To: "tiantao (H)" Cc: Andy Shevchenko , Tian Tao , Linux Kernel Mailing List , Andrew Morton , Barry Song , Andy Shevchenko , "Rafael J. Wysocki" , Jonathan Cameron Subject: Re: [PATCH 1/2] topology: use bin_attribute to avoid buff overflow Message-ID: 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> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 02, 2021 at 05:00:16PM +0800, tiantao (H) wrote: > > 在 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 ? What size bitmap would trigger a vmalloc() call to be forced here? thanks, greg k-h