From patchwork Wed Jul 24 08:55:40 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: 13740693 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 D5248C3DA61 for ; Wed, 24 Jul 2024 08:56:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 609B76B0088; Wed, 24 Jul 2024 04:56:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BA396B0089; Wed, 24 Jul 2024 04:56:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45B086B008A; Wed, 24 Jul 2024 04:56:14 -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 290B86B0088 for ; Wed, 24 Jul 2024 04:56:14 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CBE29A0795 for ; Wed, 24 Jul 2024 08:56:13 +0000 (UTC) X-FDA: 82374039426.30.E5989BA Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf06.hostedemail.com (Postfix) with ESMTP id E2A9F180013 for ; Wed, 24 Jul 2024 08:56:11 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HuNNqol+; spf=pass (imf06.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 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=1721811348; 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=t7uDCr0s9dVOVBXGdSQRDcVebs8SNqq3Sg0LbsWUCdM=; b=OG9Ez29cmuABpqEm0ahOI/hB8iXQ5lSN74yQ/55Uop3qmSsZHKJ/HGG8/Lhj1hZc5HSRx7 msba1IVE9CH38wcjQ6CGiZ0wcMhDuWJOaDpAgYomZy9zEZM64M/DaGUGM2XUE2S2NKhLS4 cMR0fa7FzRuWJXknM92/Yp9kUW++CXM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HuNNqol+; spf=pass (imf06.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721811348; a=rsa-sha256; cv=none; b=dhSaSay5+m9lXCkY1F3uKxXNclo/D49KoVbmwOqwBHLZe1+BTCRDs0KNrSlSBRDjcQdIai hjEXvweYB82o9se5xFuf2U1+1XACagsoek+/qz5TW/GcC4oY0e/UImcB98KcqT1sekuefI MbgzJ6C9QVFjo9L9IF/RfcmT516jcZw= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fd6ed7688cso15304155ad.3 for ; Wed, 24 Jul 2024 01:56:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721811371; x=1722416171; 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=t7uDCr0s9dVOVBXGdSQRDcVebs8SNqq3Sg0LbsWUCdM=; b=HuNNqol+R9X/XU0jskrOE+WQK8Hex7qkQae4O8s5KQfcvkMx+eGCWN+80ezY6Hoq1R 2uSURh09ZInmT795j5ulhwhD/7/wEwG5iFhLypIl2ZvVOmn3tviiwTy4unPQmCrB62r+ mb5Ysjc9WhbfJ7VqpoNQ2vLV4mZYLwbi6mq2lQ9Mo46/QfY1doj8RyjtNZbbjmBZVFPx OkRp0tPjQs1jb1grZ/8JWSbe3ypNu9AC7IGWdbCFc0H5jsg9LUgxG0NzesBhv5K98mTX +mUmO6bceUY9rxqRXAI+L7J/uuQ7ScXguYerZ/2wmxScFVPmihghRz9qySnjp4zRiWON 9hWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721811371; x=1722416171; 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=t7uDCr0s9dVOVBXGdSQRDcVebs8SNqq3Sg0LbsWUCdM=; b=BcNr6KoguwRppWJpueHWj9rXxxfCXr3GwikY231QQcF80ykw5lLkBqEOdLdC6iQITs /gt4zgV3vfN5DnaI7Vydddvy87exQ/h/uiJxDAIN6dQ1J/3LTyCWmxBGBzN738JdClUH vPB602S3UYJqg5QVA6pGnzT3CGe0cc+Q8ag708EfxipJPa5PNyXLM3Kfsc4eUeyhD3tZ xaiChlQgykhd0zGoxnAXVET4coFLgxjUBgOd7SGJB3unSRrLucEeAW6qQxIySvpxtiye g4E7dT5WAvJaHFcCPZAoI5K5uo4cfTIJvXN/zmdzazOZNYBV6rTSku8UCFmOSZDoTMKD vmEw== X-Forwarded-Encrypted: i=1; AJvYcCUEB7FmqWBCMyXNFBtH/j6iYLU75MRHAhlKWI7BRZtc7yI6uf3T3CuONmHOsWMAxfBhaLAAEFmjl05PbiqVnpixcjk= X-Gm-Message-State: AOJu0Yyu0Ro7XUmIOiF8DPrtMBrgFLSF4q3ASRYt0XIhjpQ2H2xb/0CK 7w+eMc5+0WfxMvW8OxWdP6ROSPIfauAmCW/scEQca5r25klkvTbR X-Google-Smtp-Source: AGHT+IFp7Y6XHWq5brJl9ajMXq+ZC2ugZjed/8XGp+DlegY+Ffu8Y1qBx5thXFo+DxGn4BRYSROzPw== X-Received: by 2002:a17:902:c404:b0:1fd:67a8:845f with SMTP id d9443c01a7336-1fdd550e28dmr19098215ad.14.1721811370524; Wed, 24 Jul 2024 01:56:10 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fd6f31855fsm89021895ad.156.2024.07.24.01.56.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jul 2024 01:56:10 -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, hch@infradead.org, iamjoonsoo.kim@lge.com, lstoakes@gmail.com, mhocko@suse.com, penberg@kernel.org, rientjes@google.com, roman.gushchin@linux.dev, urezki@gmail.com, v-songbaohua@oppo.com, vbabka@suse.cz, virtualization@lists.linux.dev, hailong.liu@oppo.com, torvalds@linux-foundation.org, "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Maxime Coquelin Subject: [PATCH RFC 1/5] vpda: try to fix the potential crash due to misusing __GFP_NOFAIL Date: Wed, 24 Jul 2024 20:55:40 +1200 Message-Id: <20240724085544.299090-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240724085544.299090-1-21cnbao@gmail.com> References: <20240724085544.299090-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: E2A9F180013 X-Stat-Signature: kj6gfmdnmeyju6nhzsfzkktznpo1jyiu X-HE-Tag: 1721811371-210393 X-HE-Meta: U2FsdGVkX188Vd6tP3nIXMcqd0LSagkgoyGKBAoQ/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D/6MIro6HyMwhCLO7pz7MKR+h49nkEQEf0uPYX7JRYdYV5Og7pvmRkdw5YBZqaDow7KG8xz93XlX1oE55pFCbZu2IVvJtn8z7KykwwAI/VPccRxULkwhwvQuNK2VTjczPUSKjQeR7tP46BIUmX5cnFGDH0rpNPz/yJ3xrfLGbmcbelkr0caW8fLzxp1l67H4Phs3qq99RY+HL5a5M8gHDvhmSBJwSmqxc73ILumIT5cQ2qt5u0ljinZwbahh8OOpOEYHkgETPu0FnNdJvsf8uunAkougVw/4K9Bn7mU8cV2hfvKHmuyIw3GEguPSqOqQG7pbc4W1qjghGJU0o5lVVZDIx9Dp6IBaBGmdE9rO6qcIYfpwPh9cdKiNfxA3NKgyMBI3qnT23vRaKfzEvabU2QvzDcnRTg8+Vjj2Mlvj/GfCJoykC9Br5Ousk7eYqP/4QNVf5XvQSJ/alExbCdrxLb4tEhYJzBAJBq9k7nW3PtwRULhx1VrbKAd6QG5mBz4CuN+lKRM+XgN8AOnHUrwyM7Gk3ESAHNvz8Q3P2WQR+DO22aIL 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. [RFC]: 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 Signed-off-by: Barry Song --- drivers/vdpa/vdpa_user/iova_domain.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/iova_domain.c index 791d38d6284c..eff700e5f7a2 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -287,28 +287,44 @@ void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain) { struct vduse_bounce_map *map; unsigned long i, count; + struct page **pages = NULL; write_lock(&domain->bounce_lock); if (!domain->user_bounce_pages) goto out; - count = domain->bounce_size >> PAGE_SHIFT; + write_unlock(&domain->bounce_lock); + + pages = kmalloc_array(count, sizeof(*pages), GFP_KERNEL | __GFP_NOFAIL); + for (i = 0; i < count; i++) + pages[i] = alloc_page(GFP_KERNEL | __GFP_NOFAIL); + + write_lock(&domain->bounce_lock); + if (!domain->user_bounce_pages) { + for (i = 0; i < count; i++) + put_page(pages[i]); + kfree(pages); + goto out; + } + 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); } put_page(map->bounce_page); map->bounce_page = page; } + kfree(pages); domain->user_bounce_pages = false; out: write_unlock(&domain->bounce_lock);