From patchwork Sat Aug 17 06:24:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13767002 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 7A7F4C52D7F for ; Sat, 17 Aug 2024 06:25:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0427C6B040B; Sat, 17 Aug 2024 02:25:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F33326B040C; Sat, 17 Aug 2024 02:25:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD38B6B040D; Sat, 17 Aug 2024 02:25:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BD94D6B040B for ; Sat, 17 Aug 2024 02:25:15 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2F5ECC0504 for ; Sat, 17 Aug 2024 06:25:15 +0000 (UTC) X-FDA: 82460750190.24.38581B3 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf03.hostedemail.com (Postfix) with ESMTP id 4EBC120016 for ; Sat, 17 Aug 2024 06:25:13 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="LDRag/iK"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723875829; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xisu3WuwKYF+5Cz14TC0TNfL+QngYLYmdZWec26IjWw=; b=JgQyWcvkngs+LFyXNgpF9vT6vakaQFla9F+hBldomfipzRav4pgWfGIJAR5qwPtIjHntw6 JgPcg+7Zyo+LlbJYwF67ZKHk+p3nNJecKX15gwHTkJ+TFlSRfdFvFOxAGQnOmHSlGlD8lz 6n/tQ30LBFkdnd2e0IV7LSgK2z7owbc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723875829; a=rsa-sha256; cv=none; b=bg/QtE/MiZHZ7KNsUOMMXtcU48fSzUIVnBAEkBZGjB+oAoz16C+ddoIsaPwz7w4mLs6P7n iJHqvIJJfto3yaoyxDGzFix/QILLr0wTQS5DCVBpWqIZ5GwB9rkL29uE+q0zn1dLJo9Kpi qsQ2JJj2WwT4Y1auZe+nW6DUOUkWug0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="LDRag/iK"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=21cnbao@gmail.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2021537a8e6so8916145ad.2 for ; Fri, 16 Aug 2024 23:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723875912; x=1724480712; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xisu3WuwKYF+5Cz14TC0TNfL+QngYLYmdZWec26IjWw=; b=LDRag/iK/pU9OYlFJsPQ79BQGA+TirDCbr+2rZoRqNKZ7aw9LNxYZkz82Ac0cwlEpx jH9FqZEAUBAcikuiESgMHMcz08TbyfAyMKCnXOOm8fh2Tmqot2/ViwMq4BeQ0wBzqBjd p//0rmmszSIPp7m+0gUn8XAnO8Yza8GozZMHPkpmqRHOd0aMRpabEbak02F0Yap3tFi/ d3/04XS1oazDLJNjo2eK3tJxyIQ5L3yYwl/causcEc3EEcebOHy0GQE0fqJ4ZcxmsA5x VQ8FXe+pCzogP4kBZ/vO7dJQGNlGD2Yz+4T/7c+FAHHf4VRVXmIbuFkNjrPYXySTxqKE KjIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723875912; x=1724480712; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xisu3WuwKYF+5Cz14TC0TNfL+QngYLYmdZWec26IjWw=; b=jg2OtjEfVDrwR3PjKFKZ0gF1rdsrteIBiFXWAKgMfSkGG1fsQVFw64hf2onCODQBqG ePMwnbthHr2kE9rZcn7GSpt38dVy4FVh7mhcMY06o8UUCzCWXQMZ+U+ZkD9rtygmPHjH kc11CrFq9xmGvT1h8jU/d15D0fP9LLeBDKZTOGmW0tim03P+c/ZsitcrTQ6G1xRQnUfg a4wQ3MY+2bWS8T+O4hxAjLqIXetFF+6s5EZlV8fDBQlJfxFvu+mJF0IqtQzyq/Hfejt+ tbqHxhDazqGNPIiHSj+n1u8SsV4t0VsF5sE78OCkOKX1p+kzOcxnaCGW7yUS2Nz7ke22 8UHA== X-Forwarded-Encrypted: i=1; AJvYcCUwA6vJZDlDtIed16xsLM4YbxHBhxsEgV275n82mAJFPIxRK3g/t6TAb9kbkSvzRybEXv7nnDwsPMDYwGMrdowBVdw= X-Gm-Message-State: AOJu0Yy8jR4DT3i/sJLjPIuFJs4jKCd7x4NZWXX0kQKRpi5tgbfEEjKo 2IvFxNaQOdnu8Pvc1PBybADwlQ1ZRN+RXlPu+2ccxnyDP++OvDhczC9yL7P+ X-Google-Smtp-Source: AGHT+IENdPsIQj8xmveRlL6yyjeMypK32/OjrVrt2yi6p6JSCZBnX1qKEm72i6xM5tYhi5eXrnZ5vA== X-Received: by 2002:a17:90b:315:b0:2c7:c788:d34d with SMTP id 98e67ed59e1d1-2d3e0870ccfmr5702366a91.38.1723875911896; Fri, 16 Aug 2024 23:25:11 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fd84:292a:c6d0:8b67]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac854f3bsm6768404a91.51.2024.08.16.23.25.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 16 Aug 2024 23:25:11 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: 42.hyeyoo@gmail.com, cl@linux.com, hailong.liu@oppo.com, hch@infradead.org, iamjoonsoo.kim@lge.com, mhocko@suse.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, torvalds@linux-foundation.org, urezki@gmail.com, v-songbaohua@oppo.com, vbabka@suse.cz, virtualization@lists.linux.dev, Jason Wang , Xie Yongji Subject: [PATCH v3 1/4] vduse: avoid using __GFP_NOFAIL Date: Sat, 17 Aug 2024 18:24:46 +1200 Message-Id: <20240817062449.21164-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240817062449.21164-1-21cnbao@gmail.com> References: <20240817062449.21164-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4EBC120016 X-Stat-Signature: d5f4ps6kjw1jckpfafpxh6aawt4dps5a X-Rspam-User: X-HE-Tag: 1723875913-99729 X-HE-Meta: U2FsdGVkX1+NuERxlTTO46b+uHnsvWfKCFNosBv+QWsqEzVaAk8EwLf1pM9zWtPMmVWJF5dow265t3X3qM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3O5PPhWEf70eVXmHVaYloIbxL+4whqb5uDvlSCLrMRApzEfTPzyPmHeBY4cQ6LH7lBrYzj6Iz6Kahh1I/+lrEznPmiunZfYTfa8KUQn2Oj7A/Mpg49k+/N1+7BTklwUt42dMpfjqPArvZEhJvyth40PozYBSkqM09u7CIg+5oSCTbhbolOdCUpfqY/NRTEj3wKY2vKUKxNIsNAupO9gipRAoxun50IsO7EFtifpg0RhRcwcBCLnlYdKkG986/aLWgYldolLUVGIuQO5nk3rfAZ3FM/kYh7rePV1L0tOp8BB34ejOJ+cG2zV4wJ1QZntEvh6vyPSvT9HYMdDJq1E3xV+7M0w5sdkLlwqOOkR5EufbfaqbLX6oAXMhF2PmCjDgEIqD3j4A5afCRMhKj8hORDBX6IiXFUABEUu4BSHgSdA94MG/VWyTZrkyhPpDmtxwdM+bZjAfih7+Lw7zsIRPilnf 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: From: Jason Wang mm doesn't support non-blockable __GFP_NOFAIL allocation. Because persisting in providing __GFP_NOFAIL services for non-block users who cannot perform direct memory reclaim may only result in an endless busy loop. Therefore, in such cases, the current mm-core may directly return a NULL pointer: static inline struct page * __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, struct alloc_context *ac) { ... /* * Make sure that __GFP_NOFAIL request doesn't leak out and make sure * we always retry */ if (gfp_mask & __GFP_NOFAIL) { /* * All existing users of the __GFP_NOFAIL are blockable, so warn * of any new users that actually require GFP_NOWAIT */ if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) goto fail; ... } ... fail: warn_alloc(gfp_mask, ac->nodemask, "page allocation failure: order:%u", order); got_pg: return page; } Unfortuantely, vpda does that nofail allocation under non-sleepable 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: " 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 userspace bounce pages. Then we can avoid allocating in paths where failure is not expected.(e.g in the release). We pay this for more memory usage as we don't release kernel bounce pages but further optimizations could be done on top. Fixes: 6c77ed22880d ("vduse: Support using userspace pages as bounce buffer") Reviewed-by: Xie Yongji Tested-by: Xie Yongji Signed-off-by: Jason Wang [v-songbaohua@oppo.com: Refine the changelog] Signed-off-by: Barry Song --- drivers/vdpa/vdpa_user/iova_domain.c | 19 +++++++++++-------- drivers/vdpa/vdpa_user/iova_domain.h | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/iova_domain.c index 791d38d6284c..58116f89d8da 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,17 @@ 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); + map->user_bounce_page = NULL; } 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; };