From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EEECC4338F for ; Tue, 27 Jul 2021 13:12:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5531361A07 for ; Tue, 27 Jul 2021 13:12:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236563AbhG0NMe (ORCPT ); Tue, 27 Jul 2021 09:12:34 -0400 Received: from out30-131.freemail.mail.aliyun.com ([115.124.30.131]:34864 "EHLO out30-131.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232123AbhG0NMd (ORCPT ); Tue, 27 Jul 2021 09:12:33 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R531e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04394;MF=xianting.tian@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0Uh9kV2H_1627391539; Received: from localhost(mailfrom:xianting.tian@linux.alibaba.com fp:SMTPD_---0Uh9kV2H_1627391539) by smtp.aliyun-inc.com(127.0.0.1); Tue, 27 Jul 2021 21:12:31 +0800 From: Xianting Tian To: amit@kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org, osandov@fb.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Xianting Tian Subject: [PATCH] virtio-console: avoid DMA from vmalloc area Date: Tue, 27 Jul 2021 21:12:17 +0800 Message-Id: <20210727131217.15092-1-xianting.tian@linux.alibaba.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch is to optimize the commit c4baad5029: virtio-console: avoid DMA from stack Commit c4baad5029(virtio-console: avoid DMA from stack) directly uses kmemdup() to alloc new buffer from kmalloc area and do memcpy no matter the buf is in kmalloc area or not. This patch is to optimize the commit c4baad5029, use is_vmalloc_addr() to judge the buf to avoid unnecessary memory alloc and copy. Signed-off-by: Xianting Tian --- drivers/char/virtio_console.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index 7eaf303a7..106247903 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -1119,6 +1119,7 @@ static int put_chars(u32 vtermno, const char *buf, int count) struct scatterlist sg[1]; void *data; int ret; + bool vmalloc_addr = false; if (unlikely(early_put_chars)) return early_put_chars(vtermno, buf, count); @@ -1127,13 +1128,18 @@ static int put_chars(u32 vtermno, const char *buf, int count) if (!port) return -EPIPE; - data = kmemdup(buf, count, GFP_ATOMIC); - if (!data) - return -ENOMEM; + if (is_vmalloc_addr(buf)) { + data = kmemdup(buf, count, GFP_ATOMIC); + if (!data) + return -ENOMEM; + vmalloc_addr = true; + } else + data = (void *)buf; sg_init_one(sg, data, count); ret = __send_to_port(port, sg, 1, count, data, false); - kfree(data); + if (vmalloc_addr) + kfree(data); return ret; } -- 2.17.1