Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2402638lqp; Sun, 24 Mar 2024 18:02:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXiQwUvpm4Mjr0xYwRfQ1NzbplxMN5dq5rbSSWodTM5aj8Sn+QXOJK62MQLYaHoeqzegfjZyx8AHuxaDaEds+gZ44wCOFw2V6Kqfr02pA== X-Google-Smtp-Source: AGHT+IEXfoRCVPI1lQwK3BUOh37Gjt3kxBtI/j38Nydkw4MCxt+T6oSLDbur+DUxvPD737TFtKZT X-Received: by 2002:a05:6a00:2da5:b0:6e6:b4e5:d878 with SMTP id fb37-20020a056a002da500b006e6b4e5d878mr7772589pfb.1.1711328535885; Sun, 24 Mar 2024 18:02:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711328535; cv=pass; d=google.com; s=arc-20160816; b=Avh+PHh1Go5ItFlQameU0xrhCxs5t6g66xdYS7pDFZrTXxIMk9r1WtbQMlIjWLjgFW hfsDgh+Z2WlfOPOn/0zEZ30OQuxCTYFVXEiZwYBy+j6ct6Z0CCFGG2ULNfVrCD9xEpWV Dzw6fLKeaMdiHsWR19qCFexj+Z1/h0+pshWq+Hlq2Ucwp3MnMSWPG2hzq1O7pCZlWAie bzeO8gKFIE6VOB2M5LDldvCf3q3kYmeqnEiZMXrSE6yKY4L/tptddFuywUSMdrGuSr4R xajncASA5Mv5/t8MY+4RqZS3zI9pu+uyehm1L2Pd/86/KDdQ0iqRCVYpsLeo/9qSXtXE QZ/w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=LEV9QNs9tplJjEHj4o5l2zOsm+jV0j/n33hjgDK9Ya8=; fh=oMsxzbuU7Hj8POEemN4mmtcVv4yvNP854dmRsdKEFNs=; b=gx4cK9f1MOsF6+WHk7r3HNvJEenxgLzF9OHBAg/mqN5A6vzFzJrXZOe5Lsc0Q4W5q8 DmOwyq7QRGLHZh0HqO7QFmlvsr4/w/ueObTX1EYdHdQnu4qoYvy0e1IH5qZgA8yDZ7kD 88RA4uQDYSnx97BfjmAGyAl/2wWqAZZ3vwQTn0SJ9awEMXmCE0/4aqd1v2QSZXosMUYv e0ce6NZ2CWQ5Kvl5NgCT1DPhyLg3RD6WoL6P2rVQXVah9ehOJT435OIzEqylNs20m52S 5/cFglGkfZdvrn1vCzHnWEh7ZaR1Jc24qJ8fZ0fPU5tGVcDUeTjQfzXMWpFY4phsczZY XV4A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=b65asg9J; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113544-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113544-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q12-20020aa7982c000000b006e68194dcb7si4183317pfl.401.2024.03.24.18.02.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 18:02:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-113544-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=b65asg9J; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113544-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113544-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 425F3284742 for ; Mon, 25 Mar 2024 01:00:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D89B13E3E5; Sun, 24 Mar 2024 22:45:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="b65asg9J" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72A801C2333; Sun, 24 Mar 2024 22:45:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711320346; cv=none; b=WE7pzej9tATNnr2xqOWDwceRsPEP2bPJiAkSURhhgnDNRoim/wPO6UvcwB1i5pDulaPHed/12qqiRdqXYVt35cr8BhCDHzJVcZBlcQjB67T7pXNAVoeHw6gLYMdRG+wqOWh4dikOTQuBQfbGUeQhduo0Bq/dhj8BEnoRKn/dtas= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711320346; c=relaxed/simple; bh=Q6mDEFDDJM5MnBK2grfADOVLskK9JJehYLpuYlRwFyQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qsppIqO3l+kc85RtG1FtPBbRt2ejVXcPwZTqSkMBIMjA/zvt0G0nQsTYB0Rf8QO5sUI+Agg8KA7n7nMQRkOsNBy/XoNfvdw+UKN9SJLj+Wp8IqZyJcMqP+egp+nqqfgOUCmEG1bi+/dBdBYHRePgQxECJsmvGonp5qpCvyz1lmI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=b65asg9J; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54538C43394; Sun, 24 Mar 2024 22:45:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711320346; bh=Q6mDEFDDJM5MnBK2grfADOVLskK9JJehYLpuYlRwFyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b65asg9JkOmHBjt8YcAlQJRhDKSOiEF4aUu84vX4o7k3nLaTi7yOqdVyRydLvVAuF j9LplR8CHP3OJgTE6+aoYzgdhGazA/7hzjlnsZHwEsyLBWfTmIfCMbHEeG+/vSmU3/ vwpJKzbs/wN6w9S9QQLdvcWqPGe2pKP406WD7ZklmwqGk37nXJWmZHabndGLHTyRaa SQP6F06hdAdzN3kgMIj1aALE/yzXwNwW/sGi84ENDoFzxlXSC3h2cgiD/6g3HyCaJH 702evJtgJKckz8IfFbz/LhP6CJsX23kMSf/EcnJxNfARUEcyuCKIN4sZ8vD6iYxjQ3 vV48Ch3c9uznA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Gabriel Krisman Bertazi , Jens Axboe , Sasha Levin Subject: [PATCH 6.8 653/715] io_uring: Fix release of pinned pages when __io_uaddr_map fails Date: Sun, 24 Mar 2024 18:33:52 -0400 Message-ID: <20240324223455.1342824-654-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324223455.1342824-1-sashal@kernel.org> References: <20240324223455.1342824-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Gabriel Krisman Bertazi [ Upstream commit 67d1189d1095d471ed7fa426c7e384a7140a5dd7 ] Looking at the error path of __io_uaddr_map, if we fail after pinning the pages for any reasons, ret will be set to -EINVAL and the error handler won't properly release the pinned pages. I didn't manage to trigger it without forcing a failure, but it can happen in real life when memory is heavily fragmented. Signed-off-by: Gabriel Krisman Bertazi Fixes: 223ef4743164 ("io_uring: don't allow IORING_SETUP_NO_MMAP rings on highmem pages") Link: https://lore.kernel.org/r/20240313213912.1920-1-krisman@suse.de Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- io_uring/io_uring.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 030cc930d1c6a..9f938874c5e13 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2702,7 +2702,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, struct page **page_array; unsigned int nr_pages; void *page_addr; - int ret, i; + int ret, i, pinned; *npages = 0; @@ -2716,12 +2716,12 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, if (!page_array) return ERR_PTR(-ENOMEM); - ret = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM, - page_array); - if (ret != nr_pages) { -err: - io_pages_free(&page_array, ret > 0 ? ret : 0); - return ret < 0 ? ERR_PTR(ret) : ERR_PTR(-EFAULT); + + pinned = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM, + page_array); + if (pinned != nr_pages) { + ret = (pinned < 0) ? pinned : -EFAULT; + goto free_pages; } page_addr = page_address(page_array[0]); @@ -2735,7 +2735,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, * didn't support this feature. */ if (PageHighMem(page_array[i])) - goto err; + goto free_pages; /* * No support for discontig pages for now, should either be a @@ -2744,13 +2744,17 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages, * just fail them with EINVAL. */ if (page_address(page_array[i]) != page_addr) - goto err; + goto free_pages; page_addr += PAGE_SIZE; } *pages = page_array; *npages = nr_pages; return page_to_virt(page_array[0]); + +free_pages: + io_pages_free(&page_array, pinned > 0 ? pinned : 0); + return ERR_PTR(ret); } static void *io_rings_map(struct io_ring_ctx *ctx, unsigned long uaddr, -- 2.43.0