Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1920958ybv; Thu, 6 Feb 2020 12:18:00 -0800 (PST) X-Google-Smtp-Source: APXvYqzfV7TFF6LSsYnAnz2Yoz62rHENh+Mw3XoVabNvYp/BCK9xImjLLzJPGII5jxzhXuSGEDZU X-Received: by 2002:a05:6808:b18:: with SMTP id s24mr7928557oij.31.1581020279934; Thu, 06 Feb 2020 12:17:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581020279; cv=none; d=google.com; s=arc-20160816; b=zQO5Zi6BxtC7B6TPv7EEu4Nqj8doWMtNxOVYcvngs4BmSdCVOIefqCoOecXaPYDBhE 2VgVQ5tWnx90uPYC6qgypFUmyEy8DcBTM7PGwCvX16IFgrsBNCAsQP6FZLEYSMM9brt5 RjnRGUC+RBf7KxYwTqarx9CdBhlSY4EuTZy7Fxo5Vf5iW8FO7b8TD1lCJED7oAUKwyOD nG43+9L7Lzm328w3i7Wf1uvw3SFHCPM7TFUCS9PQO6Uv5EqGmd3vy2SK0AbGU+5tRQ87 YlMrh6v9OEzuT0Ag3hz8ekOtfFooe106Nb1lUK9LfvC+7P/Baf1oqziSWFel79TUhHal X6+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:dkim-signature; bh=Rl1cZ2hCYZ3zf8LtvrBF29RarrZSPRnALrhsal0XxzM=; b=ca7B4YIejcf/jEbI4lCJemPDHzhkvzJK1BBLYLaQj7dSfMzKFE7KQD+DmbQCGBrL/4 dBTYXRJ5bERIJcZtFIF7rI64oQnKLvukE3gBvuNASEPF19h871okdV93jT7/uuR2VFrL M1K1+flSv/mPx4YaVxGhGi2wWDqZ8ZAgG7UN7UOVh9RT7nLr8KAbSrujJ/pnt6wxaOok YdBC7vjVoFJ2cmeZu7fW2z/tTBUuyFhJdJm2FR+YLRKuV6b1qiXWWcR7nH/DdqD5JB/Q aqprqX4vBODpX1q7+5+cgP0J1q2Ja1Dt4KDCbGMQOnuVWskwnSSsWzsC450+QhmErmkd fWSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=hVCX27GH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t22si371663oth.211.2020.02.06.12.17.46; Thu, 06 Feb 2020 12:17:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=hVCX27GH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727984AbgBFUQb (ORCPT + 99 others); Thu, 6 Feb 2020 15:16:31 -0500 Received: from mail-io1-f67.google.com ([209.85.166.67]:38848 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727831AbgBFUQb (ORCPT ); Thu, 6 Feb 2020 15:16:31 -0500 Received: by mail-io1-f67.google.com with SMTP id s24so7671307iog.5 for ; Thu, 06 Feb 2020 12:16:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=Rl1cZ2hCYZ3zf8LtvrBF29RarrZSPRnALrhsal0XxzM=; b=hVCX27GHhG55W2iQC/Y4fgfE3z5pEMKyhxsAIUwz/wKAj1DPQnGrakm4LTV6MrlED0 nYO8+lCcV+IETyQdfaH1M4gUHbeHOcnNJX+M9/vFbyk3ADcYVMVjNNchm1yBoZ8CB4kL 8BLo9foCkLIscO7L7atj0hMAwz6CvZZGl00KpUDHCg12VWrHoBfeCoQzBiDeqKU3nhD8 5Wr2j7rj8nXyNFv5WUx2DkHA26yHDn+9qqMAFEv0PS+mv1asZ8GGDCu8jYvAuA8Hi9mT nGY2NFolzWTedWQlBGCEErnQyhlxP2PGcv5hw2zoCf3MujmctnSM5wIF20oSWmvE28ki OetQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Rl1cZ2hCYZ3zf8LtvrBF29RarrZSPRnALrhsal0XxzM=; b=mEkpARtWxfsFwNQ252hpLw72JC3C+rAhK3diyacMuCIZeozgpExzNzg0Rr8WnHfxvk 6vd2wXErGDle7h3zEmBi6h9uG2CJXAAqWtKqlclVyKhaZhhIc0hf6C3lVL/0+Fm3ThI+ sW5BLM1cULIQkPhGaxnlhGN95312ypvd9baMLbHzPfR6N2LUogq7NQ1C3paD2sX4AwOY aXRNacLKpT3J5ahslE4jyiPHOZBdzbjf8OJg6l/vVi8SOlijct6KRjF5eTfUDhUR+Fgx rIjmNS4vs1rzonMsi0ZGjwj9ROn83/7M8x8E/aUcpGYWsacXq/FxG+RjQMIVcbwCp/4R XZwg== X-Gm-Message-State: APjAAAV9dX+2ZOQSi9b7uJ7hJDlk8hWuDh2SG4kxC9bslN0wZBYk1Npd uq/Egq7/Uo9Ht2B4uwZ/FpGx5svSWLg= X-Received: by 2002:a6b:3b10:: with SMTP id i16mr36600512ioa.46.1581020189887; Thu, 06 Feb 2020 12:16:29 -0800 (PST) Received: from [192.168.1.159] ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id s88sm286832ilk.79.2020.02.06.12.16.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 06 Feb 2020 12:16:29 -0800 (PST) Subject: Re: [PATCH] io_uring: fix deferred req iovec leak To: Pavel Begunkov , io-uring@vger.kernel.org, linux-kernel@vger.kernel.org References: <6e7207b6-95c4-4287-5872-fb05abf60e88@kernel.dk> <4f7f61d3-b3f9-43db-ad32-ee502dc06c8b@gmail.com> From: Jens Axboe Message-ID: <28cacc0c-68e4-a9d1-bb5e-03dbeff8a586@kernel.dk> Date: Thu, 6 Feb 2020 13:16:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <4f7f61d3-b3f9-43db-ad32-ee502dc06c8b@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/6/20 1:00 PM, Pavel Begunkov wrote: > On 06/02/2020 22:56, Jens Axboe wrote: >> On 2/6/20 10:16 AM, Pavel Begunkov wrote: >>> On 06/02/2020 20:04, Pavel Begunkov wrote: >>>> On 06/02/2020 19:51, Pavel Begunkov wrote: >>>>> After defer, a request will be prepared, that includes allocating iovec >>>>> if needed, and then submitted through io_wq_submit_work() but not custom >>>>> handler (e.g. io_rw_async()/io_sendrecv_async()). However, it'll leak >>>>> iovec, as it's in io-wq and the code goes as follows: >>>>> >>>>> io_read() { >>>>> if (!io_wq_current_is_worker()) >>>>> kfree(iovec); >>>>> } >>>>> >>>>> Put all deallocation logic in io_{read,write,send,recv}(), which will >>>>> leave the memory, if going async with -EAGAIN. >>>>> >>>> Interestingly, this will fail badly if it returns -EAGAIN from io-wq context. >>>> Apparently, I need to do v2. >>>> >>> Or not... >>> Jens, can you please explain what's with the -EAGAIN handling in >>> io_wq_submit_work()? Checking the code, it seems neither of >>> read/write/recv/send can return -EAGAIN from async context (i.e. >>> force_nonblock=false). Are there other ops that can do it? >> >> Nobody should return -EAGAIN with force_nonblock=false, they should >> end the io_kiocb inline for that. >> > > If so for those 4, then the patch should work well. Maybe I'm dense, but I'm not seeing the leak? We have two cases here: - The number of vecs is less than UIO_FASTIOV, in which case we use the on-stack inline_vecs. If we need to go async, we copy that inline vec to our async_ctx area. - The number of vecs is more than UIO_FASTIOV, this is where iovec is allocated by the vec import. If we make it to completion here, we free it at the end of eg io_read(). If we need to go async, we stash that pointer in our async_ctx area and free it when the work item has run and completed. -- Jens Axboe