Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp96853pxb; Tue, 17 Aug 2021 20:18:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyw39uRsj7H8USdm+DiJ3OyP4q+M7lSCqR0tpqhXrM89oOVbdYo6I5vfyvPk1a5zZIxOFNH X-Received: by 2002:a05:6402:5249:: with SMTP id t9mr7509516edd.260.1629256732111; Tue, 17 Aug 2021 20:18:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629256732; cv=none; d=google.com; s=arc-20160816; b=GQJmKQCmo4aWt1ndl24gJln+zp71wRMCMeDel3ck3E6Ft3wzDXGmGM2tO+/MAddG3q JTgFBjHLA4paQMenMdWOX6jm/zP0/TltCVMbKSTmlNKYZsAe/q616kSDfS7Z3MzX6bwI EVaUitkGwz5ulni6TX2Do90cR6H5CVoxnfW3aSin/zaYOi/MIXRVI5PG86wbjGCGSUXw OJIm5OrrfAzhuMU7EnpVNWCLdlGpXn5IlwxJkcrzjTPBHyptAbD1/XLMaXd1KIMY/LqZ EclNIcdhiBK+eygsPktT04RrygEEBMZpBmletl7ev++mYZg4xT2oon0CbyYYVE6lOe3L aM6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=DPc7urjlfbXi4JUqkG5U5x2Zz7cdVK7h03OfY0SFENs=; b=pu996JUity4xVWX1COipJ5ItFGgk5wdY6JVmv/Asu/LDlXL8iCMKacg9qhHBx9QPZX vRLYIhNMi82erOtrGLC8iWOG2tQJIOLmGhyhsTQKfgHDKpJV5Uichw6bq9A5DkwXTAA2 PzcCMl8MA/2moyTCJgQHzDvMBFqQbCt6Pffrf8iMNrDuGzrpL9ckk0EscnwXqa6wkp8K ZenF/ZvDJLaNWT3t3hZm5KMkpO/vImX2sAeRTb3WSvo1ngHjc0hSYmFOoBSycZq6ESUw ewQ4CWClPQwIHrZOQ+TnTQ3Nw9t5dO8bc3qg+beespXcFHYcocmfCH3wv5F0iTEa0mHj iVrQ== 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v7si3577648edd.142.2021.08.17.20.18.28; Tue, 17 Aug 2021 20:18:52 -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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231207AbhHRDRs (ORCPT + 99 others); Tue, 17 Aug 2021 23:17:48 -0400 Received: from mail-ed1-f49.google.com ([209.85.208.49]:45755 "EHLO mail-ed1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229883AbhHRDRr (ORCPT ); Tue, 17 Aug 2021 23:17:47 -0400 Received: by mail-ed1-f49.google.com with SMTP id cq23so793768edb.12 for ; Tue, 17 Aug 2021 20:17:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=DPc7urjlfbXi4JUqkG5U5x2Zz7cdVK7h03OfY0SFENs=; b=oo0bZ9p77le6IOefWW9UBEFofRE3Zx22yMMMu+rQgpkP08fMcp7ciouvbfPEYODIpj tQiw1+emU1sCyH9f6heirZ/VoAUUy7SYAkwhyMw93nWblPG14g4pTLdYBlXn24awJL73 Y3FzU867oFVAH8jnuZsBAB9oXnWAAfUWmyRFOzE2o2EhRdfyDOT5P18e0ncSzcNmrDkO Pvyc6XG4nsQ1fb89sahXZydkoJw4Q8FBzlclaPiVMaJFHmFUnpEh3j73GjcWQ3ztz9tg rVjJAIm+TmpC/DJ0W8QPnO8K1rIqmCoV5V9sVlLZnMFxP6EAMDqLB4ikGJapzHqg6h14 P6zA== X-Gm-Message-State: AOAM533M4m1fHO/J+BcsCKkAKl0B5JaXs8hW06eFlDDKQotDLw8P5InR ArX8FDIQEilQQkqtKCwaggZPksICwfE= X-Received: by 2002:aa7:cb8a:: with SMTP id r10mr7773117edt.237.1629256632601; Tue, 17 Aug 2021 20:17:12 -0700 (PDT) Received: from ?IPv6:2a0b:e7c0:0:107::70f? ([2a0b:e7c0:0:107::70f]) by smtp.gmail.com with ESMTPSA id v13sm1424956ejx.24.2021.08.17.20.17.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Aug 2021 20:17:11 -0700 (PDT) Subject: Re: [PATCH v7 1/2] tty: hvc: pass DMA capable memory to put_chars() To: Xianting Tian , 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> From: Jiri Slaby Message-ID: <5b728c71-a754-b3ef-4ad3-6e33db1b7647@kernel.org> Date: Wed, 18 Aug 2021 05:17:10 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <20210817132300.165014-2-xianting.tian@linux.alibaba.com> Content-Type: text/plain; charset=iso-8859-2; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > 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. thanks, -- js suse labs