Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3212263pxb; Sat, 9 Oct 2021 04:57:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAaEtaJjTatlE+UxYI3XB1Cud1d7wGMq56C/capQDHppQxgMuto+RdnKseQ/DH7A+UK30Y X-Received: by 2002:a17:906:6011:: with SMTP id o17mr10628738ejj.157.1633780663081; Sat, 09 Oct 2021 04:57:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633780663; cv=none; d=google.com; s=arc-20160816; b=ikb4ksi0djBkSKcu96bDIpXSlQ39+im37Nw70YSs1bjkFRc4YxouSLgbfpjVfH4T0x vuziYfRYCrOHbbBw4dk7BvWcmHH0VMUkUvXWAHrkDow16T8i4ECeny7TLyTK8qGWRCwv jPTeHB+17iZCqXl8nij9OPq7++rluUIckS+InMApp5H1FMINWp/fd3IBq+M/SnK8Twbi 0qCGU+AJI2S91XSGfLRtRRkTkQlt18FuMY2phdKPIxIZngkRmhrH2hO5sIoZLAgECTXy fT/JolYZyaXB87s0Pc1pja0yfPhI9rmxfK/I690OnG8hg5LjsAvqV3RCyoT0G4tPFxSi Ng4w== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=l7p+HRQEI+THoAKI2qMFu/PdUnnx8/W4cpoaHxcXUB8=; b=iSP6mJqs2sWAgCE+ukgc0+ZibK9aY/cIAsVlAW8morV/7Nk9OwQEgNNyxmpzPiNiWk hrORPrWXV6m/GpvxJ/W4x1mqv4GWLVCBq/iL+27IwIvDE9CIq6iVbZFrxDF/gMrcwaSK Bh71oz5zHUDq3EmUWHCV32mVkFBQONS2EIBXyQrkPkMAPcJbi8U17ATAhZ99CPjcA7kx YkAqm4q6whH6lKgGKx8oy+ertq5FgxgGXa4E9B2ELkveumJ1WZVrEKuVd8klNIwHDZFp i6Yi12LjdMvm+R/kWkRbbdBbOCcNGqyTVPve4PnNt+GA7A7/ytiEQE4oUlU4eq+/CALI nozg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lhjEKytD; 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 mp24si4281416ejc.731.2021.10.09.04.57.19; Sat, 09 Oct 2021 04:57:43 -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=lhjEKytD; 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 S232865AbhJIL5w (ORCPT + 99 others); Sat, 9 Oct 2021 07:57:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:43234 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232664AbhJIL5v (ORCPT ); Sat, 9 Oct 2021 07:57:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2DF1560EE9; Sat, 9 Oct 2021 11:55:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633780554; bh=pJABJ1JzJYukeyq4nIhuXfqM6wKmGcJGn3PyObatZxc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lhjEKytDQn7a443MT8nwdCQOHxRHPAAyXRJwge8eC5aRs2y+ZofQPjiMdWpAsFxsF H46jA6kTZJ6IUkNRs6p5Hbaqmsecjvjcrs1EJuVXtY6bDMi3gftFFMZ9srQaEed366 Gkfsxsb6Y/hiWQD6LKl9b34DXLogfFP9xQjvlWyo= Date: Sat, 9 Oct 2021 13:55:52 +0200 From: Greg KH To: Xianting Tian Cc: jirislaby@kernel.org, amit@kernel.org, arnd@arndb.de, osandov@fb.com, shile.zhang@linux.alibaba.com, linuxppc-dev@lists.ozlabs.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v10 2/3] tty: hvc: pass DMA capable memory to put_chars() Message-ID: References: <20211009114829.1071021-1-xianting.tian@linux.alibaba.com> <20211009114829.1071021-3-xianting.tian@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211009114829.1071021-3-xianting.tian@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Oct 09, 2021 at 07:48:28PM +0800, Xianting Tian wrote: > As well known, hvc backend can register its opertions to hvc backend. > the operations contain put_chars(), get_chars() and so on. > > Some hvc backend may do dma in its operations. eg, put_chars() of > virtio-console. But in the code of hvc framework, it may pass DMA > incapable memory to put_chars() under a specific configuration, which > is explained in commit c4baad5029(virtio-console: avoid DMA from stack): > 1, c[] is on stack, > hvc_console_print(): > char c[N_OUTBUF] __ALIGNED__; > cons_ops[index]->put_chars(vtermnos[index], c, i); > 2, ch is on stack, > static void hvc_poll_put_char(,,char ch) > { > struct tty_struct *tty = driver->ttys[0]; > struct hvc_struct *hp = tty->driver_data; > int n; > > do { > n = hp->ops->put_chars(hp->vtermno, &ch, 1); > } while (n <= 0); > } > > Commit c4baad5029 is just the fix to avoid DMA from stack memory, which > is passed to virtio-console by hvc framework in above code. But I think > the fix is aggressive, it directly uses kmemdup() to alloc new buffer > from kmalloc area and do memcpy no matter the memory is in kmalloc area > or not. But most importantly, it should better be fixed in the hvc > framework, by changing it to never pass stack memory to the put_chars() > function in the first place. Otherwise, we still face the same issue if > a new hvc backend using dma added in the furture. > > In this patch, add 'char cons_outbuf[]' as part of 'struct hvc_struct', > so hp->cons_outbuf is no longer the stack memory, we can use it in above > case 1. Add 'char outchar' as part of 'struct hvc_struct', we can use it > in above case 2. We also add lock for each above buf to protect them > separately instead of using the global lock of hvc. > > Introduce another array(cons_hvcs[]) for hvc pointers next to the > cons_ops[] and vtermnos[] arrays. With the array, we can easily find > hvc's cons_outbuf and its lock. > > With the patch, we can revert the fix c4baad5029. > > Signed-off-by: Xianting Tian > Signed-off-by: Shile Zhang > --- > drivers/tty/hvc/hvc_console.c | 37 +++++++++++++++++++++-------------- > drivers/tty/hvc/hvc_console.h | 24 +++++++++++++++++++++-- > 2 files changed, 44 insertions(+), 17 deletions(-) > > diff --git a/drivers/tty/hvc/hvc_console.c b/drivers/tty/hvc/hvc_console.c > index 5bb8c4e44..4d8f112f2 100644 > --- a/drivers/tty/hvc/hvc_console.c > +++ b/drivers/tty/hvc/hvc_console.c > @@ -41,16 +41,6 @@ > */ > #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */ > > -/* > - * These sizes are most efficient for vio, because they are the > - * native transfer size. We could make them selectable in the > - * future to better deal with backends that want other buffer sizes. > - */ > -#define N_OUTBUF 16 > -#define N_INBUF 16 > - > -#define __ALIGNED__ __attribute__((__aligned__(sizeof(long)))) > - Are you sure this applies on top of patch 1/3 here? > +/* > + * These sizes are most efficient for vio, because they are the > + * native transfer size. We could make them selectable in the > + * future to better deal with backends that want other buffer sizes. > + */ > +#define N_OUTBUF 16 > +#define N_INBUF 16 > + > +#define __ALIGNED__ __attribute__((__aligned__(sizeof(long)))) Again, are you sure this is correct? thanks, greg k-h