Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp107371pxb; Tue, 17 Aug 2021 20:37:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9y6eCQOmdsYJc7iShSI51IKVN4EHU78yo5sEgwh1w3Vmj6R7Ip57QPoKq7umr5a+hHzZl X-Received: by 2002:a17:906:ced1:: with SMTP id si17mr7314831ejb.506.1629257826984; Tue, 17 Aug 2021 20:37:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629257826; cv=none; d=google.com; s=arc-20160816; b=aMzEjH3gCwy9ZA6Z9pWU/vNhNJ5hvfSzYCN7HX+4ikfdSmGAIO5nsfCILYhWBFPnea DPnOX+nGF3PxhR6vexzJdfUPFRQCoq2I/Th6keMBfKJKbE/tm3z1RgK9bf/nzsetCwID 1zTfTUOUGYVGvrBYXGKKovAnWcEu2DvuUKd/ft0mWBVn4LeIEFCIfP/QNzpRiTWhxko2 L62oBfXP+eE1jxpjGLMslBBK4Z8o4VTW9emrd4KkIwGYIXHBVvlfXjTWKm5u4BSqx6UW dLw9JnfFGBqFJz0qOulMl4wosbNlGdZZ8dElqnZ/y55C90kn/wxA2IJIvXI/U1drwieY 5dWw== 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=COrgUusGa2s/GWUx2kNNUAOyYTz22cfrGsutPFa0pXg=; b=Jlav1vhk7kQaU1e6ioyzuiWO5cp4ouadDWEztok1K632FTP1NBQU0GaAnkvs8se77s qt8b17KAPljTPKSn8R3D3HGihI990kxdsm5/UnHZimH3I9EUsGiPk6EIlUjUsYIuR1be jaxBhacYqhtMZ4eybm66IRNecHtSCdVm8RxOZDtuNtUQDR76w5Ou+M2a+7ZjaJULa7mR WPDvWfV9tq3Z0KxeMhVbujwp7L5ZKMwJH5KhPzz9o2H8FjG+IC6lFSY6IB8n0Gn6bB64 aCn9zM9tIXUUIpGZB8h5PHbYfDC9ORWTaPLIUReUacZMAh9BY1hm4i03910zP4+EnK7k yirA== 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=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b26si4522974ejq.659.2021.08.17.20.36.44; Tue, 17 Aug 2021 20:37:06 -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=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236111AbhHRDft (ORCPT + 99 others); Tue, 17 Aug 2021 23:35:49 -0400 Received: from out30-131.freemail.mail.aliyun.com ([115.124.30.131]:52297 "EHLO out30-131.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236235AbhHRDfs (ORCPT ); Tue, 17 Aug 2021 23:35:48 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R221e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04395;MF=xianting.tian@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0Ujd4SCS_1629257711; Received: from B-LB6YLVDL-0141.local(mailfrom:xianting.tian@linux.alibaba.com fp:SMTPD_---0Ujd4SCS_1629257711) by smtp.aliyun-inc.com(127.0.0.1); Wed, 18 Aug 2021 11:35:11 +0800 Subject: Re: [PATCH v7 1/2] tty: hvc: pass DMA capable memory to put_chars() To: Jiri Slaby , gregkh@linuxfoundation.org, amit@kernel.org, arnd@arndb.de, osandov@fb.com Cc: linuxppc-dev@lists.ozlabs.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org References: <20210817132300.165014-1-xianting.tian@linux.alibaba.com> <20210817132300.165014-2-xianting.tian@linux.alibaba.com> <5b728c71-a754-b3ef-4ad3-6e33db1b7647@kernel.org> From: Xianting TIan Message-ID: Date: Wed, 18 Aug 2021 11:35:11 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <5b728c71-a754-b3ef-4ad3-6e33db1b7647@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 在 2021/8/18 上午11:17, Jiri Slaby 写道: > Hi, > > On 17. 08. 21, 15:22, Xianting Tian wrote: >> As well known, hvc backend can register its opertions to hvc backend. >> the opertions contain put_chars(), get_chars() and so on. > > "operations". And there too: > >> Some hvc backend may do dma in its opertions. 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. >> >> We make 'char c[N_OUTBUF]' part of 'struct hvc_struct', so hp->c is no >> longer the stack memory. we can use it in above two cases. > > In fact, you need only a single char for the poll case > (hvc_poll_put_char), so hvc_struct needs to contain only c, not an array. > > OTOH, you need c[N_OUTBUF] in the console case (hvc_console_print), > but not whole hvc_struct. So cons_hvcs should be an array of structs > composed of only the lock and the buffer. > > Hum. > > Or maybe rethink and take care of the console case by kmemdup and be > done with that case? What problem do you have with allocating 16 > bytes? It should be quite easy and really fast (lockless) in most > cases. On the contrary, your solution has to take a spinlock to access > the global buffer. May be we can change hvc_struct as below, struct hvc_struct {         ...         char out_ch;         char c[N_OUTBUF] __ALIGNED__;         int outbuf_size;         char outbuf[0] __ALIGNED__; }; c[N_OUTBUF]  is only used for hvc_console_print(); out_ch is only used for hvc_poll_put_char(). Thus no competition exits, the spinlock can be removed. Then cons_hvcs array can only contains the buffer. Is it OK for you?  thanks > >> Other fix is use L1_CACHE_BYTES as the alignment, use 'sizeof(long)' as >> dma alignment is wrong. And use struct_size() to calculate size of >> hvc_struct. > > This ought to be in separate patches. OK, thanks > > thanks,