From patchwork Wed Jul 31 00:01:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13747974 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 6B7EAC3DA7F for ; Wed, 31 Jul 2024 00:02:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A1386B0089; Tue, 30 Jul 2024 20:02:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0038C6B008A; Tue, 30 Jul 2024 20:02:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D718B6B008C; Tue, 30 Jul 2024 20:02:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AE5C66B0089 for ; Tue, 30 Jul 2024 20:02:54 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 56C181C0654 for ; Wed, 31 Jul 2024 00:02:54 +0000 (UTC) X-FDA: 82398097068.05.D5C25FF Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf21.hostedemail.com (Postfix) with ESMTP id 77B591C0015 for ; Wed, 31 Jul 2024 00:02:51 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WQUW53nj; spf=pass (imf21.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.182 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=1722384116; 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:in-reply-to:references:references:dkim-signature; bh=R9U0Fc8KKm2y+wN/fyBwzUhu47z8lWa3dO4TTNX5JgU=; b=zARiDDRH8+qqtzRkgtY0Zzpq+pjpw8+lWYLhbnaelH1tG0eai12HunzV+9wJf9+NFDTbJ0 5rWjoiHtwC9eOcQqQW5Y7LCIHoUVl1A+/LrXLh0l0AbrmcqEh1No/QNvv6W3/8k79ccptY 1Ebwabmdtdh0wveOPB2V2jNm0q71uoM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722384116; a=rsa-sha256; cv=none; b=W4AfzRDkXc9iyMSuoZirWduwIDeEODZyYwoVmBxE7YciX96g7EZJ05sjLTpcJPlzrGoUQA JUGaz4TkrUu3zV2+OKOvBWc2MYAhKx6r0Q595apd+/FBm3TdPQe9npC/GEc5nLCx+5IsEN 1ulRry34S6yIHi1LbEL4Ks0dnj3N578= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WQUW53nj; spf=pass (imf21.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-70d2d7e692eso4135745b3a.0 for ; Tue, 30 Jul 2024 17:02:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722384170; x=1722988970; 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=R9U0Fc8KKm2y+wN/fyBwzUhu47z8lWa3dO4TTNX5JgU=; b=WQUW53nj2MQaUIr+tPU2lbBfFTQGcqnR+QRu9oQiqDY4vVpzJ0cAy2FzmANeRVscJj WlxDfQGGmZSgZOCfgcJB4f+MoBc9zPtTeJXyXvNCz52PRE37tzdz4HC4af6rugjGNLoo l2ilo65hYSw1IqZsex3vlnPWx9bFRyp/Zq6DtHR9nGrSZSQaSXciqqXuvoEfaLxqwWdU qFHnumuRCjwySUaMb55w+DEXIPBBotdkT7xxItwPax81rL3EFUUtd8rqG+CjliBGY8sv VQhAknboZ/e93wUgx6gQTTrdB7f9IWN0nZCqajOQX4gejGgOT95yPhHMHMEboXvjRVJq 7NJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722384170; x=1722988970; 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=R9U0Fc8KKm2y+wN/fyBwzUhu47z8lWa3dO4TTNX5JgU=; b=bxQm4PVXWdEmXc3una9jLMAC5YEw+0NntNQyxvMds6c1QEZd5Ahdmky2fYC2arWUJY VwI/D0qLnM/pO+kgyOzvHwTQcqHXgwp0+dCjW07+Z9/ImlgosYf9TwES0wRwCoK+30bd 6u5TuNwXxwN6OGysvdgoD0k2zL+/YYISqC89u3jJ6KajEJcbXh1Y7OuMP4JxoTCrxS7R tMvNt522Ld+FEEMQuSdJim506yV9RcvVj2U8FGY9u9urPsh/u/yFp8qzVr4OHDe3BG39 YaxU/CsVbYJvIwlXg2rcsX1pMq64SDOxnqtBbBcA7T6x9t12XvPndX/iPdQ0Pu8xjOMn M3mQ== X-Forwarded-Encrypted: i=1; AJvYcCU9pGJllkqc/wIX5eX3CzEkZb3rDtxln9h/HgVfbOWQr0RM9VqYgM5ElMgOjssGRd3KOOylrcRIXFjF1PMPbzF+TCo= X-Gm-Message-State: AOJu0YwIQPfbaQoCoqGNHj0ALrNqY/RZPba93VDjetKStpooz8jBrHqd OX59niv1YI1mv7by1kLA3KqL/2GSvFQ2P55MJ+sWueuSSQyaMV/7 X-Google-Smtp-Source: AGHT+IGI7znVrv6bYKQx7zu+8g6RSUkckxO77j0m9ryhAOr73jAUCoQuk4sG//4hffslQyeyVIjtfg== X-Received: by 2002:a05:6a21:32aa:b0:1c4:9f31:ac8f with SMTP id adf61e73a8af0-1c4a14d92e1mr11148238637.37.1722384169966; Tue, 30 Jul 2024 17:02:49 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e1a2asm8871689b3a.23.2024.07.30.17.02.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 17:02:49 -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, lstoakes@gmail.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, "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Maxime Coquelin Subject: [PATCH RFT v2 1/4] vpda: try to fix the potential crash due to misusing __GFP_NOFAIL Date: Wed, 31 Jul 2024 12:01:52 +1200 Message-Id: <20240731000155.109583-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731000155.109583-1-21cnbao@gmail.com> References: <20240731000155.109583-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 77B591C0015 X-Stat-Signature: pw471ftne7iwo34r9r4ghq6azrxw1gs6 X-HE-Tag: 1722384171-926928 X-HE-Meta: U2FsdGVkX189Ui9Asroq/GFTuNnBtr6C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8a+nLsLH79Ff9/mU9AtO4tEsFCABBMT7kcOwQfUWD+Xq0B5SEQxpc6evuLME+ERlpuHXQTdsM3yFNyotg5014nsn9jeZhI7VyM7DJSztl2am3K9BaidAbg9eQvW2eq/K4KRVO5H0k6F2juoAAfq+CPVRviYmwcLAWhDTlCB/xVGpINHwRZhJk0u+Od3+vSveON4IpuY0wRKkgVth0aw3rjUDhjucc64tpq4tDGnPEgO6j0sE4Nhs7yOPnxDXoBCKUvoUmRksD7M8Rg9jl5qXy2Le4pATyRJdMpfx7M81nn13p1s0Tza1hK5XUNQbol8Cxcz/XkO1IK/yqSTgJR9pzQXf3RHJmTBHKmhzQNrLrlwMxajTmmLf9/khehD0KGuNyRsRDdmeuGvnAuRnE+rOBf1rGhugUSOdhup1/xRP5e4ib58sHZ0H6lk8XYQO8feiL81Qqtzjl3J1BPYJJ379W2HXnT63GtGA+Q4WDwGVdtYeG6d42mPqWLPBPKPHNnursbMVbyvEIBFkB3FBhdN07fGOSuIqd 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: Barry Song 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. 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; } Let's move the memory allocation out of the atomic context and use the normal sleepable context to get pages. [RFT]: This has only been compile-tested; I'd prefer if the VDPA maintainers handles it. Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: Xuan Zhuo Cc: "Eugenio PĂ©rez" Cc: Maxime Coquelin Signed-off-by: Barry Song --- drivers/vdpa/vdpa_user/iova_domain.c | 31 +++++++++++++++++++++++----- drivers/vdpa/vdpa_user/iova_domain.h | 5 ++++- drivers/vdpa/vdpa_user/vduse_dev.c | 4 +++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/iova_domain.c index 791d38d6284c..9318f059a8b5 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -283,7 +283,23 @@ int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain, return ret; } -void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain) +struct page **vduse_domain_alloc_pages_to_remove_bounce(struct vduse_iova_domain *domain) +{ + struct page **pages; + unsigned long count, i; + + if (!domain->user_bounce_pages) + return NULL; + + count = domain->bounce_size >> PAGE_SHIFT; + pages = kmalloc_array(count, sizeof(*pages), GFP_KERNEL | __GFP_NOFAIL); + for (i = 0; i < count; i++) + pages[i] = alloc_page(GFP_KERNEL | __GFP_NOFAIL); + + return pages; +} + +void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain, struct page **pages) { struct vduse_bounce_map *map; unsigned long i, count; @@ -294,15 +310,16 @@ void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain) count = domain->bounce_size >> PAGE_SHIFT; for (i = 0; i < count; i++) { - struct page *page = NULL; + struct page *page = pages[i]; map = &domain->bounce_maps[i]; - if (WARN_ON(!map->bounce_page)) + if (WARN_ON(!map->bounce_page)) { + put_page(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); memcpy_from_page(page_address(page), map->bounce_page, 0, PAGE_SIZE); } @@ -310,6 +327,7 @@ void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain) map->bounce_page = page; } domain->user_bounce_pages = false; + kfree(pages); out: write_unlock(&domain->bounce_lock); } @@ -543,10 +561,13 @@ static int vduse_domain_mmap(struct file *file, struct vm_area_struct *vma) static int vduse_domain_release(struct inode *inode, struct file *file) { struct vduse_iova_domain *domain = file->private_data; + struct page **pages; + + pages = vduse_domain_alloc_pages_to_remove_bounce(domain); spin_lock(&domain->iotlb_lock); vduse_iotlb_del_range(domain, 0, ULLONG_MAX); - vduse_domain_remove_user_bounce_pages(domain); + vduse_domain_remove_user_bounce_pages(domain, pages); vduse_domain_free_kernel_bounce_pages(domain); spin_unlock(&domain->iotlb_lock); put_iova_domain(&domain->stream_iovad); diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/iova_domain.h index f92f22a7267d..17efa5555b3f 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -74,7 +74,10 @@ void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain); int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain, struct page **pages, int count); -void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain); +void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain, + struct page **pages); + +struct page **vduse_domain_alloc_pages_to_remove_bounce(struct vduse_iova_domain *domain); void vduse_domain_destroy(struct vduse_iova_domain *domain); diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c index 7ae99691efdf..5d8d5810df57 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -1030,6 +1030,7 @@ static int vduse_dev_queue_irq_work(struct vduse_dev *dev, static int vduse_dev_dereg_umem(struct vduse_dev *dev, u64 iova, u64 size) { + struct page **pages; int ret; mutex_lock(&dev->mem_lock); @@ -1044,7 +1045,8 @@ static int vduse_dev_dereg_umem(struct vduse_dev *dev, if (dev->umem->iova != iova || size != dev->domain->bounce_size) goto unlock; - vduse_domain_remove_user_bounce_pages(dev->domain); + pages = vduse_domain_alloc_pages_to_remove_bounce(dev->domain); + vduse_domain_remove_user_bounce_pages(dev->domain, pages); unpin_user_pages_dirty_lock(dev->umem->pages, dev->umem->npages, true); atomic64_sub(dev->umem->npages, &dev->umem->mm->pinned_vm);