Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4734450iob; Sun, 8 May 2022 23:40:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrVcN8lUuPK3LYets5BjpeYuQ53UoZSwU5iIYnHLbo5cMIrsWek76vJi2vrBXIbQKYiGZH X-Received: by 2002:a05:6638:12cb:b0:32b:7ed6:6ff4 with SMTP id v11-20020a05663812cb00b0032b7ed66ff4mr6686326jas.171.1652078446927; Sun, 08 May 2022 23:40:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652078446; cv=none; d=google.com; s=arc-20160816; b=bfeWuAVgjhV49dkSUDoLvL1CvQlmM5f9LATiTiMIDbngFXjemBbAv4PSQVwCvKE0s7 Yjj7I8roGTfYPxWkLmOgG9PMXKIaBiboJSv/1H16rJOdy74qMlv5ixL3Ks5D5a/snPp4 bS+6LeYOLytX26rl3AnfVhfBHYR+j8I0OyEeAkQvC6B8YsEBlxBKKmmJNze8h8TOuurZ t5XsGTSVD2/eIJcjI7Wun6lNxtiSDc9pbkHRINecOENIgNevllQ5LKVo9vYUnSCuGIuj T7j1XTb/fVsQpKgpVuiY1JZCvJ8ztueYEqgDr9cUTr8/qZitKgKySPzkepNkJwBIYwJN j08w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=OzdNfZxxmkcZgQmcrr/fMou7WbepUbdCKxZFmXlJ3Vg=; b=MiVEJTjV0glsZejOjiBbzk9KBN+OQMnvedrGMLfqpc1BhAEr8audhVXZMbVzHqUgtx ar0vqS//nkNBeaBM/jQcxAvW6JeUn+PDuKiCQxQou5MusRnLNIKsy4qWgnyjshucGhds PoDO4ninl1kIPwOeC4BMx3DB1V78eyzIJ20FTs9lRIZK5Pe20Qt6ZpzM7wkKz/7SQx3d HbvHumDlrPdO1qzYpYpNkKDPcDVJgXKRDM49vZ+hBN89AAnPQBAjkWmMWInTyE+vgmPG DahUDYjqHtgZ/8I9aVWKOm9laajOuOXYD1VPiJubkXnQmFxPeIAxJKfjxb7s+AD+4Tzc 4gtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=dWYzdn9T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n16-20020a027150000000b0032b60686b55si6401712jaf.114.2022.05.08.23.40.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 23:40:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=dWYzdn9T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2E2CF1882BE; Sun, 8 May 2022 23:38:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356595AbiEGGfl (ORCPT + 99 others); Sat, 7 May 2022 02:35:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237563AbiEGGfj (ORCPT ); Sat, 7 May 2022 02:35:39 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D6C645AD3; Fri, 6 May 2022 23:31:53 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id c11so9360204plg.13; Fri, 06 May 2022 23:31:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:to:cc:references :from:in-reply-to:content-transfer-encoding; bh=OzdNfZxxmkcZgQmcrr/fMou7WbepUbdCKxZFmXlJ3Vg=; b=dWYzdn9T7cKZ5BiAcTVmluX1pOnqbUbNreFbH0//vZnGggWaVlZW+O57dO7TuM12Ls SNV3kyQJ8IbthKLO2CN+gO4B9DReUhXGRssVl3K72uNQZq34R2+Jf0VbDZPFzFwKB0mh yYBpuVl+In0oM2FYXDnRVnk3RfCjqYbBEJvOBqWBbCGBUmuzYn2HlYADzgVJyTsKb1/k 3fEeXK9jXsYZhSKxQ6HCLjOcQ5slnnUDe8JFCZIlY2oGcl7EY4sPLtFrxcTMmew1WDTr sHuE6shWmpWr3yqiGOYMue1MKl64YHsZL6A4hIWkcY4HV3lRVIYpTeRE6mn5yubgv6aA D3Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :to:cc:references:from:in-reply-to:content-transfer-encoding; bh=OzdNfZxxmkcZgQmcrr/fMou7WbepUbdCKxZFmXlJ3Vg=; b=1Y0ndhpXSrk31S6D7WBxN5S0P6CIx8n9zI/3jH7svr0PpIHGfkTswyMnb2L+cM0hwB fa/KgkYKgSFKGp+tcPivezdvVWBmeHO3L8R8jUt0QLGKv6B5dSH4VRVo0iGiQvfLXT2F kVnQfehbs3cR42wzxaWJIjRtGb1yTm7RgefDDcVtenbdFzZHhXQDHxAWKnIWg+cMQRNf YzElbNfzEtCKo93pOMaoyBRwCqj7dj9iIS6B4zuNH/TU4Ke8mEt/x2ybiiVEiXWyRRwn 0/icQM2skVcTbulFqHgQ+WDejp4T7qLUn5BXZ13OUZGCHxbDETeGNlyWInyJ9hlXkmr7 DArw== X-Gm-Message-State: AOAM5330o1WgXsl3VtUEdIIqYgU7bpwKIO4sKUuYmL8+ZqxRX4cuMa6f ZXtRR2gPQExEO4rYmtxbqUE= X-Received: by 2002:a17:90b:1e49:b0:1dc:81d9:2d97 with SMTP id pi9-20020a17090b1e4900b001dc81d92d97mr16627343pjb.221.1651905113104; Fri, 06 May 2022 23:31:53 -0700 (PDT) Received: from [192.168.255.10] ([106.53.4.151]) by smtp.gmail.com with ESMTPSA id b2-20020a056a0002c200b0050dc7628143sm4394648pft.29.2022.05.06.23.31.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 06 May 2022 23:31:52 -0700 (PDT) Message-ID: <8b934240-4388-d0ff-0d36-69acb253a2a6@gmail.com> Date: Sat, 7 May 2022 14:32:01 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH 3/5] io_uring: let fast poll support multishot To: Jens Axboe , Pavel Begunkov , io-uring@vger.kernel.org Cc: linux-kernel@vger.kernel.org References: <20220506070102.26032-1-haoxu.linux@gmail.com> <20220506070102.26032-4-haoxu.linux@gmail.com> <8e81111d-398c-3810-50b4-e1475e956b6f@kernel.dk> From: Hao Xu In-Reply-To: <8e81111d-398c-3810-50b4-e1475e956b6f@kernel.dk> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 在 2022/5/7 上午6:02, Jens Axboe 写道: > On 5/6/22 11:19 AM, Pavel Begunkov wrote: >> On 5/6/22 08:01, Hao Xu wrote: >>> From: Hao Xu >>> >>> For operations like accept, multishot is a useful feature, since we can >>> reduce a number of accept sqe. Let's integrate it to fast poll, it may >>> be good for other operations in the future. >>> >>> Signed-off-by: Hao Xu >>> --- >>> fs/io_uring.c | 41 ++++++++++++++++++++++++++--------------- >>> 1 file changed, 26 insertions(+), 15 deletions(-) >>> >>> diff --git a/fs/io_uring.c b/fs/io_uring.c >>> index 8ebb1a794e36..d33777575faf 100644 >>> --- a/fs/io_uring.c >>> +++ b/fs/io_uring.c >>> @@ -5952,7 +5952,7 @@ static void io_poll_remove_entries(struct io_kiocb *req) >>> * either spurious wakeup or multishot CQE is served. 0 when it's done with >>> * the request, then the mask is stored in req->cqe.res. >>> */ >>> -static int io_poll_check_events(struct io_kiocb *req, bool locked) >>> +static int io_poll_check_events(struct io_kiocb *req, bool *locked) >>> { >>> struct io_ring_ctx *ctx = req->ctx; >>> int v; >>> @@ -5981,17 +5981,26 @@ static int io_poll_check_events(struct io_kiocb *req, bool locked) >>> /* multishot, just fill an CQE and proceed */ >>> if (req->cqe.res && !(req->apoll_events & EPOLLONESHOT)) { >>> - __poll_t mask = mangle_poll(req->cqe.res & req->apoll_events); >>> - bool filled; >>> - >>> - spin_lock(&ctx->completion_lock); >>> - filled = io_fill_cqe_aux(ctx, req->cqe.user_data, mask, >>> - IORING_CQE_F_MORE); >>> - io_commit_cqring(ctx); >>> - spin_unlock(&ctx->completion_lock); >>> - if (unlikely(!filled)) >>> - return -ECANCELED; >>> - io_cqring_ev_posted(ctx); >>> + if (req->flags & REQ_F_APOLL_MULTISHOT) { >>> + io_tw_lock(req->ctx, locked); >>> + if (likely(!(req->task->flags & PF_EXITING))) >>> + io_queue_sqe(req); >> >> That looks dangerous, io_queue_sqe() usually takes the request >> ownership and doesn't expect that someone, i.e. >> io_poll_check_events(), may still be actively using it. > > I took a look at this, too. We do own the request at this point, but > it's still on the poll list. If io_accept() fails, then we do run the > poll_clean. > >> E.g. io_accept() fails on fd < 0, return an error, io_queue_sqe() -> >> io_queue_async() -> io_req_complete_failed() kills it. Then >> io_poll_check_events() and polling in general carry on using the freed >> request => UAF. Didn't look at it too carefully, but there might other >> similar cases. > > But we better have done poll_clean() before returning the error. What am > I missing here? Sorry, I don't get it. I've done the poll_clean() before returnning error in io_accept() >