Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4387749pxv; Tue, 27 Jul 2021 06:16:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwp2Ms6bzOecU3Fsr4A+RBrO/vYp76I+AKDrbo3bk8LmhWLxaLK8Ngeu5Tjd9EsucLREeJX X-Received: by 2002:a05:6402:48f:: with SMTP id k15mr27258685edv.262.1627391797118; Tue, 27 Jul 2021 06:16:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627391797; cv=none; d=google.com; s=arc-20160816; b=oYE+sSU8kw4HZI+vHtY09OzuBJ6O1NyoZoXKBJUh/EghkJjTA5Zmf/1T+abWbz/vUQ UQt5xRto084vAJq7zW7w7aREKGTAH9OYMNTHy2S3KIdFysUAx4r5eJCh29iXXg9RGKZt unyBWRmtsj6TMD/0d6YAnYJ/1wVd4XUF7NsRBOoU6XPb0+vJlRaVoB+tDmCxOLibFg37 v1+dctwFsw8LGXuKrjiXjXAHALJVRhh4rCnBBEY9dstAUpp3d1RMUWbyIkwGSXIhisGS UPYXpBf/HWTuMtcsme2zf3eph7xsTxXHZMiBQXzag6P0PiYMkOsnVtF+PnXMzrpJkjEZ ttaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=/YsxsN63XhPk58E6UIyXvYSSzfu0rKem6kZ6RFe83Ns=; b=baCor2irrjWR/81q8XDi4qqoW6UT0d/XCYZuLfiVlQOZTi66LG7FwcEtFWPgcP4Lhs q/QI2vR+7xUbhpb82zDeGlnZ97rZDHyEjSBZ8672bHpwvCO8AYRYFX7R890pYIaiAm5D fJ8QhsgncR9ziOwYYCMnn0JmiX6j7lT1cu+j01r0HuQHPPxleaLupmaVSd+7LmLTxnHj p9A859vAeXXXCICdEGWxDzEtUyvx+GWPg94mlb3pW4q51fZPY0NJeccZpXKbUiNbVETt zDKWIazwKt6a4hfBsOuXvMV4/c3Is0SQdXwLqy7zrHqsu8qXIoltEa+RJVXy2/CcgAv0 Y/fQ== 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 y13si2703871edr.600.2021.07.27.06.16.14; Tue, 27 Jul 2021 06:16:37 -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 S236509AbhG0NMe (ORCPT + 99 others); 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