From patchwork Mon Aug 5 08:21:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 13753265 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11384C3DA7F for ; Mon, 5 Aug 2024 08:21:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AE296B0089; Mon, 5 Aug 2024 04:21:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85DF16B008A; Mon, 5 Aug 2024 04:21:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7257B6B008C; Mon, 5 Aug 2024 04:21:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 548186B0089 for ; Mon, 5 Aug 2024 04:21:26 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0673340161 for ; Mon, 5 Aug 2024 08:21:26 +0000 (UTC) X-FDA: 82417497372.03.8CDD448 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 5BE8940003 for ; Mon, 5 Aug 2024 08:21:24 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eaBwdpb8; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of jasowang@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=jasowang@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722846016; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=E+p/W2q7vNtsCSk1hop06eQcM6R9eKnUBXTd9dXAayE=; b=QEPbw2sLR8lAbGgOmRMgptg4dDX6wHR5rV8Y7DABrPIFX1HkmX9Ht/K7lGjHt3GYBvNQ5y BjxnEURMcDUDHWfhk+VHGsEu7AUYVL43vzbdSeSiqt2TZOp1QDDUvHGLmjDbX4fyOo2bxC EW0gX44sHhWCoicZo03fl1M5w8qal80= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722846016; a=rsa-sha256; cv=none; b=LMgc4cDzdkAMQ0fANictVcknsWLxgYoRM+sChtPDbHSAnYUoe8oY8Bm+6tTNHuDlKx/g12 F/7gmyGZYZim+e+3eJHQEHfqMWdzvr0o7T8g2qIAvd2xpmnfHyRxOtqRbhccrR9kL5oG8/ vAX9SIjrTxryp0cUUDnfSEPPYCf+CrQ= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eaBwdpb8; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of jasowang@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=jasowang@redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722846083; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=E+p/W2q7vNtsCSk1hop06eQcM6R9eKnUBXTd9dXAayE=; b=eaBwdpb86gf19WNykzS+gt4GYmeswCITsGST3UzZa9UKUSYYaNHtFbg+FBQk0JyrrbpwV/ /pMO9Yq8SxDTVofBSIBLC+7MnHaLkFnZcrXMQW9bRVfpjNStXNCHfFVcXl2tD2Sx/lxyJB Zpfl2WC3c+KMoaVezTtPuWLytzPwGxQ= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-528-rSsPaaEGPBG5G-FpSH6Fow-1; Mon, 05 Aug 2024 04:21:19 -0400 X-MC-Unique: rSsPaaEGPBG5G-FpSH6Fow-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DD8F41955BED; Mon, 5 Aug 2024 08:21:17 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.148]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5D44519560AE; Mon, 5 Aug 2024 08:21:09 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: maxime.coquelin@redhat.com, xieyongji@bytedance.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, 21cnbao@gmail.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, akpm@linux-foundation.org Subject: [PATCH] vduse: avoid using __GFP_NOFAIL Date: Mon, 5 Aug 2024 16:21:06 +0800 Message-ID: <20240805082106.65847-1-jasowang@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5BE8940003 X-Stat-Signature: gzwhpn8zhktox9ew76a69qup51u6geu4 X-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KbfHviegosLpyTc3H6km5WVYZDeQmHi4cyAo02z3ArCzcLZX0dnNOmwnWn6MZDDSwfBgatA+ISDXPCzTYd1lHkgdmPPWTjrmwHCO1Zire0JIqoiJFy8mWgA0X3RPkomRXEwk/bB0lf9901Tz09PMVgT9SUedz+BViPyfQJOoW1rttDjrXF8AnkbVEYHxjJY6jgIov2UznKiVmKp4LF1txgXkFm2aoYzpUH4rWwMahxuo1lYxjRN0xt+TPhW1aGJR82mph7c2+xdvydwMlCcJnpLBuecXJzC/ONLpJuynqGbW7+TtGBG0L5blYSSr7fBdr669wW0+r2arXKCIsqD4IvsrLeF/lliDUgERI3NjZzfqcVgL4k390a9TeYcWp1JfV/Eh/WmwVUqlk8VLMxWtlRcGvGd8SMIgBOWmLKzaazw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Barry said [1]: """ mm doesn't support non-blockable __GFP_NOFAIL allocation. Because __GFP_NOFAIL without direct reclamation may just result in a busy loop within non-sleepable contexts. ""“ Unfortuantely, we do that under read lock. A possible way to fix that is to move the pages allocation out of the lock into the caller, but having to allocate a huge number of pages and auxiliary page array seems to be problematic as well per Tetsuon [2]: """ You should implement proper error handling instead of using __GFP_NOFAIL if count can become large. """ So I choose another way, which does not release kernel bounce pages when user tries to register usersapce bounce pages. Then we don't need to do allocation in the path which is not expected to be fail (e.g in the release). We pay this for more memory usage but further optimizations could be done on top. [1] https://lore.kernel.org/all/CACGkMEtcOJAA96SF9B8m-nZ1X04-XZr+nq8ZQ2saLnUdfOGOLg@mail.gmail.com/T/#m3caef86a66ea6318ef94f9976ddb3a0ccfe6fcf8 [2] https://lore.kernel.org/all/CACGkMEtcOJAA96SF9B8m-nZ1X04-XZr+nq8ZQ2saLnUdfOGOLg@mail.gmail.com/T/#m7ad10eaba48ade5abf2d572f24e185d9fb146480 Fixes: 6c77ed22880d ("vduse: Support using userspace pages as bounce buffer") Signed-off-by: Jason Wang --- drivers/vdpa/vdpa_user/iova_domain.c | 18 ++++++++++-------- drivers/vdpa/vdpa_user/iova_domain.h | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/iova_domain.c index 791d38d6284c..933d2f7cd49a 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -162,6 +162,7 @@ static void vduse_domain_bounce(struct vduse_iova_domain *domain, enum dma_data_direction dir) { struct vduse_bounce_map *map; + struct page *page; unsigned int offset; void *addr; size_t sz; @@ -178,7 +179,10 @@ static void vduse_domain_bounce(struct vduse_iova_domain *domain, map->orig_phys == INVALID_PHYS_ADDR)) return; - addr = kmap_local_page(map->bounce_page); + page = domain->user_bounce_pages ? + map->user_bounce_page : map->bounce_page; + + addr = kmap_local_page(page); do_bounce(map->orig_phys + offset, addr + offset, sz, dir); kunmap_local(addr); size -= sz; @@ -270,9 +274,8 @@ int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain, memcpy_to_page(pages[i], 0, page_address(map->bounce_page), PAGE_SIZE); - __free_page(map->bounce_page); } - map->bounce_page = pages[i]; + map->user_bounce_page = pages[i]; get_page(pages[i]); } domain->user_bounce_pages = true; @@ -297,17 +300,16 @@ void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain) struct page *page = NULL; map = &domain->bounce_maps[i]; - if (WARN_ON(!map->bounce_page)) + if (WARN_ON(!map->user_bounce_page)) continue; /* Copy user page to kernel page if it's in use */ if (map->orig_phys != INVALID_PHYS_ADDR) { - page = alloc_page(GFP_ATOMIC | __GFP_NOFAIL); + page = map->bounce_page; memcpy_from_page(page_address(page), - map->bounce_page, 0, PAGE_SIZE); + map->user_bounce_page, 0, PAGE_SIZE); } - put_page(map->bounce_page); - map->bounce_page = page; + put_page(map->user_bounce_page); } domain->user_bounce_pages = false; out: diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/iova_domain.h index f92f22a7267d..7f3f0928ec78 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -21,6 +21,7 @@ struct vduse_bounce_map { struct page *bounce_page; + struct page *user_bounce_page; u64 orig_phys; };