From mboxrd@z Thu Jan 1 00:00:00 1970 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 09FD5C369C2 for ; Thu, 24 Apr 2025 15:13:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF8CC6B00BD; Thu, 24 Apr 2025 11:13:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA7C26B00BE; Thu, 24 Apr 2025 11:13:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C97996B00BF; Thu, 24 Apr 2025 11:13:06 -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 B0FBE6B00BD for ; Thu, 24 Apr 2025 11:13:06 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1B9191CD36E for ; Thu, 24 Apr 2025 15:13:07 +0000 (UTC) X-FDA: 83369280414.04.C59ED14 Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.48]) by imf09.hostedemail.com (Postfix) with ESMTP id 6273E140006 for ; Thu, 24 Apr 2025 15:13:05 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="mYy1nH/Q"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of qq282012236@gmail.com designates 209.85.160.48 as permitted sender) smtp.mailfrom=qq282012236@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745507585; a=rsa-sha256; cv=none; b=w++2HxaDjQWkRCwoZbASKbjisWLdAqXPWEWJ47OUHOh/Zcbnuke3VsO9rD5z2jvygRNtXr 5f31t3vAuUfWMeLOEtCxL5dEbCsiAofTxxqxrYyj4HZfrmywBO3r6ZQNMQmNfAwYEKW0xd DdBXmRCQPPgolPzTfLo9tjtPO+eJiUs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745507585; 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=RJQwgudwUlt+h9dEjDjiK4usQBmjK0EIPccbWAz3KNk=; b=E4GeSEnEIApfmfYlLfNq11aSz/ui2w1ehpUgRoiOUkDkPL3Hkg54gv8r0K3oiMj6ZuwNbS OvqANjXNfc+tzoKBgZKf2Ky32Eq82CdL0hi3UIRpao8BMIkoOtQcyM++7qOqsDx+l1J4ci YQgSWwgpkxecDNzYyF3umA54MAG8shA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="mYy1nH/Q"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of qq282012236@gmail.com designates 209.85.160.48 as permitted sender) smtp.mailfrom=qq282012236@gmail.com Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-2c12b7af278so864747fac.0 for ; Thu, 24 Apr 2025 08:13:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745507584; x=1746112384; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RJQwgudwUlt+h9dEjDjiK4usQBmjK0EIPccbWAz3KNk=; b=mYy1nH/QZ6jTJozyIsYd5+5Ib+R11Ksfv1Dn3YJkDRh1ihVlAB8BQyrxRSWNT6vra8 5oPFodrirGGzyYDoePzMYz4ke9eQBNdUVfzkZ8JoTUcEJFQeB+404mKWZfRFwGUuvaXq IdfVwZDFC8LE0wCi6rrQaXs+zuM6CoaN7Vb6FOSigVgFFZ090GQ23gFN7hrj7MCksYpr MSsSobp6/cSt94+UyZLUU9uWEOr6CPgyxz14+gle7upeG5pOSSQYDwKBIHXU2m2HeQ5w U4YV3CDl/AH362VoO3Y6pT+RBLLyhPVwlsTW2jFjvenj4eZvh8kuzyIMtV3zTKT3HXXP 7OGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745507584; x=1746112384; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RJQwgudwUlt+h9dEjDjiK4usQBmjK0EIPccbWAz3KNk=; b=ZfPBIMBMCk4xW8nQh7H9jgDYYClCD/nsUKvSX4H4ldpfjVcokckFPlZLmG8kG2cV6m UF5Vlq9TkBfLkHE+qJvHomH1LdHf1yDHKHTprjmo+qYtEnVoFUY4Uz+46yeHE5AIkL7v 5+JGtt7zPpKb9oPDg/jmaFeCsJYLVd2W47pGFYr2ZsvZeyQ/oiJJAMEvJ8wquJ139Hse bZ+MkMRT7vOrybQOIfsLfTFtccZSBG4GS1HhLYlAH/CjLoXTq0X2IQbuC3b9Yu0eIrrn WRDmrp4VGf+XBzz/8pYfG66+3VbNN+3KpNDlkM1cuByuCpthtJIc3cPyRYPtEPhOiCIW ktTg== X-Forwarded-Encrypted: i=1; AJvYcCXUV4qmt8RCr+WSfCO+XFNaO5GskppmPzjQMdes10868nD5WJEEye9ZdUg3feHGj8NfCgeyTxmBKw==@kvack.org X-Gm-Message-State: AOJu0YyTpIZLLVLQpGVNFnk+vUHFQOUNR7pqzGJk+BadWcFXdA89UE0n efojndx5a0L/ypMWjJW1eden9tULkbAvnD5oEEeCb7EAQrnQ1WLMYYO+xppMqHarRBiv7XSMVC9 mTYt/DJ2Weyx7V2k+FjxzZEk+7+A= X-Gm-Gg: ASbGncvWBVWdfTf0E1OxYeE+w9Ryw36GVoRAfkkLUZ8dIMBb7LNGVQ/ZQ6UcJgf7Yh2 UdmGsv/FBy2VLNHQp+qYh85xGt3ulPkDYspy8vDGdFbQZHhmM6BhMX4UkR7xg0OTFmplqCT8/2v mtMV8grGbSHKQFQA5vDNT93do= X-Google-Smtp-Source: AGHT+IEPbj1NyCGg1IYiplR7XlzA2UlL86knW/idvppbgEbTo86Bg3TpcVscvkDRGjFGBed26A2/pMD3d16tSdCnn8I= X-Received: by 2002:a05:6870:e99a:b0:2d4:f2da:9bb8 with SMTP id 586e51a60fabf-2d97311dbb3mr1584506fac.1.1745507583922; Thu, 24 Apr 2025 08:13:03 -0700 (PDT) MIME-Version: 1.0 References: <20250422162913.1242057-1-qq282012236@gmail.com> <20250422162913.1242057-2-qq282012236@gmail.com> <14195206-47b1-4483-996d-3315aa7c33aa@kernel.dk> <7bea9c74-7551-4312-bece-86c4ad5c982f@kernel.dk> <52d55891-36e3-43e7-9726-a2cd113f5327@kernel.dk> <5c20b5ca-ce41-43c4-870a-c50206ab058d@kernel.dk> <1ed67bb5-5d3d-4af8-b5a7-4f644186708b@kernel.dk> In-Reply-To: <1ed67bb5-5d3d-4af8-b5a7-4f644186708b@kernel.dk> From: =?UTF-8?B?5aec5pm65Lyf?= Date: Thu, 24 Apr 2025 23:12:49 +0800 X-Gm-Features: ATxdqUHe5gwFojH5W1zzXhzHzgiZG4lIpiDsVFZLaTqhcnSH7zbPMgWC8Xwren8 Message-ID: Subject: Re: [PATCH v2 1/2] io_uring: Add new functions to handle user fault scenarios To: Jens Axboe Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, akpm@linux-foundation.org, peterx@redhat.com, asml.silence@gmail.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, io-uring@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam06 X-Rspamd-Queue-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sysYZJlN9qJGD9WnV9b5rVlLLIPwV6jzqeC1qWK39iK2gnDWejhef0s+6KEE4BdmoFlP6P3KvYGDZq7thCb0Z7Kc+XOOVQIR4 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: Jens Axboe =E4=BA=8E2025=E5=B9=B44=E6=9C=8824=E6=97=A5=E5= =91=A8=E5=9B=9B 22:53=E5=86=99=E9=81=93=EF=BC=9A > > On 4/24/25 8:45 AM, ??? wrote: > > Jens Axboe ?2025?4?24??? 22:13??? > >> > >> On 4/24/25 8:08 AM, ??? wrote: > >>> Jens Axboe ?2025?4?24??? 06:58??? > >>>> > >>>> On 4/23/25 9:55 AM, Jens Axboe wrote: > >>>>> Something like this, perhaps - it'll ensure that io-wq workers get = a > >>>>> chance to flush out pending work, which should prevent the looping.= I've > >>>>> attached a basic test case. It'll issue a write that will fault, an= d > >>>>> then try and cancel that as a way to trigger the TIF_NOTIFY_SIGNAL = based > >>>>> looping. > >>>> > >>>> Something that may actually work - use TASK_UNINTERRUPTIBLE IFF > >>>> signal_pending() is true AND the fault has already been tried once > >>>> before. If that's the case, rather than just call schedule() with > >>>> TASK_INTERRUPTIBLE, use TASK_UNINTERRUPTIBLE and schedule_timeout() = with > >>>> a suitable timeout length that prevents the annoying parts busy loop= ing. > >>>> I used HZ / 10. > >>>> > >>>> I don't see how to fix userfaultfd for this case, either using io_ur= ing > >>>> or normal write(2). Normal syscalls can pass back -ERESTARTSYS and g= et > >>>> it retried, but there's no way to do that from inside fault handling= . So > >>>> I think we just have to be nicer about it. > >>>> > >>>> Andrew, as the userfaultfd maintainer, what do you think? > >>>> > >>>> diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c > >>>> index d80f94346199..1016268c7b51 100644 > >>>> --- a/fs/userfaultfd.c > >>>> +++ b/fs/userfaultfd.c > >>>> @@ -334,15 +334,29 @@ static inline bool userfaultfd_must_wait(struc= t userfaultfd_ctx *ctx, > >>>> return ret; > >>>> } > >>>> > >>>> -static inline unsigned int userfaultfd_get_blocking_state(unsigned = int flags) > >>>> +struct userfault_wait { > >>>> + unsigned int task_state; > >>>> + bool timeout; > >>>> +}; > >>>> + > >>>> +static struct userfault_wait userfaultfd_get_blocking_state(unsigne= d int flags) > >>>> { > >>>> + /* > >>>> + * If the fault has already been tried AND there's a signal = pending > >>>> + * for this task, use TASK_UNINTERRUPTIBLE with a small time= out. > >>>> + * This prevents busy looping where schedule() otherwise doe= s nothing > >>>> + * for TASK_INTERRUPTIBLE when the task has a signal pending= . > >>>> + */ > >>>> + if ((flags & FAULT_FLAG_TRIED) && signal_pending(current)) > >>>> + return (struct userfault_wait) { TASK_UNINTERRUPTIBL= E, true }; > >>>> + > >>>> if (flags & FAULT_FLAG_INTERRUPTIBLE) > >>>> - return TASK_INTERRUPTIBLE; > >>>> + return (struct userfault_wait) { TASK_INTERRUPTIBLE,= false }; > >>>> > >>>> if (flags & FAULT_FLAG_KILLABLE) > >>>> - return TASK_KILLABLE; > >>>> + return (struct userfault_wait) { TASK_KILLABLE, fals= e }; > >>>> > >>>> - return TASK_UNINTERRUPTIBLE; > >>>> + return (struct userfault_wait) { TASK_UNINTERRUPTIBLE, false= }; > >>>> } > >>>> > >>>> /* > >>>> @@ -368,7 +382,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf= , unsigned long reason) > >>>> struct userfaultfd_wait_queue uwq; > >>>> vm_fault_t ret =3D VM_FAULT_SIGBUS; > >>>> bool must_wait; > >>>> - unsigned int blocking_state; > >>>> + struct userfault_wait wait_mode; > >>>> > >>>> /* > >>>> * We don't do userfault handling for the final child pid up= date > >>>> @@ -466,7 +480,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf= , unsigned long reason) > >>>> uwq.ctx =3D ctx; > >>>> uwq.waken =3D false; > >>>> > >>>> - blocking_state =3D userfaultfd_get_blocking_state(vmf->flags= ); > >>>> + wait_mode =3D userfaultfd_get_blocking_state(vmf->flags); > >>>> > >>>> /* > >>>> * Take the vma lock now, in order to safely call > >>>> @@ -488,7 +502,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf= , unsigned long reason) > >>>> * following the spin_unlock to happen before the list_add i= n > >>>> * __add_wait_queue. > >>>> */ > >>>> - set_current_state(blocking_state); > >>>> + set_current_state(wait_mode.task_state); > >>>> spin_unlock_irq(&ctx->fault_pending_wqh.lock); > >>>> > >>>> if (!is_vm_hugetlb_page(vma)) > >>>> @@ -501,7 +515,11 @@ vm_fault_t handle_userfault(struct vm_fault *vm= f, unsigned long reason) > >>>> > >>>> if (likely(must_wait && !READ_ONCE(ctx->released))) { > >>>> wake_up_poll(&ctx->fd_wqh, EPOLLIN); > >>>> - schedule(); > >>>> + /* See comment in userfaultfd_get_blocking_state() *= / > >>>> + if (!wait_mode.timeout) > >>>> + schedule(); > >>>> + else > >>>> + schedule_timeout(HZ / 10); > >>>> } > >>>> > >>>> __set_current_state(TASK_RUNNING); > >>>> > >>>> -- > >>>> Jens Axboe > >>> I guess the previous io_work_fault patch might have already addressed > >>> the issue sufficiently. The later patch that adds a timeout for > >>> userfaultfd might > >> > >> That one isn't guaranteed to be safe, as it's not necessarily a safe > >> context to prune the conditions that lead to a busy loop rather than t= he > >> normal "schedule until the condition is resolved". Running task_work > >> should only be done at the outermost point in the kernel, where the ta= sk > >> state is known sane in terms of what locks etc are being held. For som= e > >> conditions the patch will work just fine, but it's not guaranteed to b= e > >> the case. > >> > >>> not be necessary wouldn?t returning after a timeout just cause the > >>> same fault to repeat indefinitely again? Regardless of whether the > >>> thread is in UN or IN state, the expected behavior should be to wait > >>> until the page is filled or the uffd resource is released to be woken > >>> up, which seems like the correct logic. > >> > >> Right, it'll just sleep timeout for a bit as not to be a 100% busy loo= p. > >> That's unfortunately the best we can do for this case... The expected > >> behavior is indeed to schedule until we get woken, however that just > >> doesn't work if there are signals pending, or other conditions that le= ad > >> to TASK_INTERRUPTIBLE + schedule() being a no-op. > >> > >> -- > >> Jens Axboe > > In my testing, clearing the NOTIFY flag in the original io_work_fault > > ensures that the next schedule correctly waits. However, adding a > > That's symptom fixing again - the NOTIFY flag is the thing that triggers > for io_uring, but any legitimate signal (or task_work added with > signaling) will cause the same issue. > > > timeout causes the issue to return to multiple faults again. > > Also, after clearing the NOTIFY flag in handle_userfault, > > it?s possible that some task work hasn?t been executed. > > But if task_work_run isn?t added back, tasks might get lost? > > It seems like there isn?t an appropriate place to add it back. > > So, do you suggest adjusting the fault frequency in userfaultfd > > to make it more rhythmic to alleviate the issue? > > The task_work is still there, you just removed the notification > mechanism that tells the kernel that there's task_work there. For this > particular case, you could re-set TIF_NOTIFY_SIGNAL at the end after > schedule(), but again it'd only fix that specific one case, not the > generic issue. > > What's the objection to the sleep approach? If the task is woken by the > fault being filled, it'll still wake on time, no delay. If not, then it > prevents a busy loop, which is counterproductive. > > -- > Jens Axboe OK Thanks .and i=E2=80=99m curious about what exactly is meant by a 'specific one case 'and what qualifies as a 'generic issue' with re-set TIF_NOTIFY_SIGNAL. So, in your final opinion, do you think the code in io_uring is not suitabl= e for modification, should focus on making adjustments in userfaultfd to mitigate the issue?