From patchwork Fri Aug 30 20:28:21 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: 13785528 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 056C3CA1013 for ; Fri, 30 Aug 2024 20:29:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90D606B024E; Fri, 30 Aug 2024 16:29:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BB816B024F; Fri, 30 Aug 2024 16:29:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75C426B025C; Fri, 30 Aug 2024 16:29:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4E4D16B024E for ; Fri, 30 Aug 2024 16:29:23 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CCB4740576 for ; Fri, 30 Aug 2024 20:29:22 +0000 (UTC) X-FDA: 82510051764.18.F2129B1 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf07.hostedemail.com (Postfix) with ESMTP id F1EB040004 for ; Fri, 30 Aug 2024 20:29:20 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Cu8C3XTt; spf=pass (imf07.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725049671; 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=GjM4TCQPcyD2c0/3KvrBI/E6RtEEAPLZc40kpXbn95A=; b=KRFrl1v7yjbd03+a0CuGhFjiwB8N+nWoR0DrQjyRuaLIxLxqKidmU/ouErXNB+hAIRMUOb rxWxsm7DqTWChz/euaQSR1RJU/7VB7eANI7jNhL3+nEvW0EjlCE5UIBf/gwNBUIEBcXGmR EGtISTydkHVgNbjSeZXHL1AAapW2WQo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725049671; a=rsa-sha256; cv=none; b=cwtMToIWXhGrub8vAQFDsT8ddDugYXEkKlpD8Vpji4a4B6EeTrzT7mcPLNz7aqGamverpu BNk/bltStNHAR6DwmYF+COEbmAJObt6QdhdH3sbaxLG5DqVUo/hs3Ra8+5+qzFmHM8flrY iZ/80gAMva8gBt8wsdpcoo0hDV+G5hQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Cu8C3XTt; spf=pass (imf07.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7141b04e7a3so1900733b3a.3 for ; Fri, 30 Aug 2024 13:29:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725049759; x=1725654559; 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=GjM4TCQPcyD2c0/3KvrBI/E6RtEEAPLZc40kpXbn95A=; b=Cu8C3XTtYtxQF6avWkLW9of1O2wKBh1WtRZl75onBDp9nItjG9Ma4+zVTVQ+3JJdTh BXPsHFFRHCcExUSp17QWNYezAbDcrpXtqe1U+Aans8gljuaRo5+muf3djMkdUfR1bMDh XCA8Dq38lC42cEZ/brEcU/vtDHn5VTd8LdSiYBAXO3YyR2H6DjFdcrKqn9fJQR7T16QD zY7G+PfNndHBOuBycmrWRPmLMm0W89gkJ8hT3h5iAQK2+Xq45ZIFB4SGEXxmvqlnGNiC y9V3rLBUYJNAEvh9ve+fsQpo0aDNtq9otDk9hmicXtq/dp5glibqX9yfaX1pWsnnTn/W EGqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725049759; x=1725654559; 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=GjM4TCQPcyD2c0/3KvrBI/E6RtEEAPLZc40kpXbn95A=; b=DclA0suXQZo0m2EdFP3UtDd73bK/fM/S7TgA6f+ek1IrL55wRsf6Z8gNGu8DVok4Su +1+rwCSfD/xt71QKRSHbGdCIkFLStg+wENWQgDFnLYHmosq/mgPmhcP1ArEG+4voDlEV jj4Kb7iq+NHP7T40mpJLd5MQc/a51bBj/rSbbCFzHQ0f1ML+W0T96M1/Gq3S6BjUI92c 8KCSPWXZ/ZDVhmkuS1gJosipMOZzLDzzY9bsDqO0yaQnF5t0ebEq5oaLvdnupYW53Zq+ 6FWF+zBAdf+HAfK/Rpw4kt6NsObXgoOhrJ308nH3xDMFY6AKOcBOGCg2Wx5x3zlGojTq k1jA== X-Forwarded-Encrypted: i=1; AJvYcCW+cxtfU/DDuvkheR2JJ+VbfF3XaHL+OcA/Ty6SvDmk8RGnMn3t56sq5T+0x2V/gM4SP/KcI234Lg==@kvack.org X-Gm-Message-State: AOJu0YxYoMt72YMMmRAC5e/2CzjwI3Hk4CyWfvzovYobszo+gvsjOfBe XPgXoBsWOXS1N6o7pwn+ElCNlqkwaBTTuZVMR5+tANHg+vl/3tHn X-Google-Smtp-Source: AGHT+IHPNF3P8o/k5P1eGJ2+IaqrDvOqEe4xWUj5fF+7bw0WYag1yVFsYkqRh8YXWo7n9ciahhhJHA== X-Received: by 2002:a05:6a00:a06:b0:70b:cf1:8dc9 with SMTP id d2e1a72fcca58-715dfced5efmr9881420b3a.25.1725049759477; Fri, 30 Aug 2024 13:29:19 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-715e56d74eesm3257035b3a.147.2024.08.30.13.29.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 13:29:19 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org, virtualization@lists.linux.dev Cc: david@redhat.com, 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, laoar.shao@gmail.com, Jason Wang , Xie Yongji Subject: [PATCH v4 1/3] vduse: avoid using __GFP_NOFAIL Date: Sat, 31 Aug 2024 08:28:21 +1200 Message-Id: <20240830202823.21478-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240830202823.21478-1-21cnbao@gmail.com> References: <20240830202823.21478-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: F1EB040004 X-Stat-Signature: a8bo3fnjhhujsoh1ux9k6kkkge9szwts X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1725049760-144650 X-HE-Meta: U2FsdGVkX1+OmXnM5/juO3P0Qa4SM2bPc+Z4YI90+5yLhZ9ig5uT7EHsRlm/THu6CSf0t8+0Wf3u6+AQHH2ecWhPOPFD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wC52H8vBDFgWgWqtIAZFSJ7eYBbQjJCHaMy+sIXOq44UmUucD1R4MlpQortqCsOCJLycvawwtufZnqi+94E+BXXBVE32kidd8LGUZd/V5JlcZ3bFIxaKiraKBkZKoCnyyNNQoE6SkSU8ZJ5v7VYNEY6SY2gfdDczQaKwfY00j+5t6e9VQpa4eK4ktN4TSR5LMhVxmbJfGTko//3xeVqI7ucYJGexSPi3+itWpTSn5m68LwM5DcpBv1COo1iMrd1RIDg8mqe0MlulbhGN0xvomFENNv3eetvnJNifYUbEGA6NcJeFFhjBrbBf0XNOot2l3T9s4A33LXeGHEl3e4lUUG/UIubMdjyox5VrB+wAqoknUamXqEPE+0BoQbkBNrXV+c72+D9Uly4oKYktOkSIsbn0mQ= 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) { ... 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; };