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 41742C369DC for ; Thu, 1 May 2025 16:18:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F1A86B0089; Thu, 1 May 2025 12:18:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A0006B008C; Thu, 1 May 2025 12:18:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 068016B0092; Thu, 1 May 2025 12:18:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D72746B0089 for ; Thu, 1 May 2025 12:18:43 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 059D71A10BE for ; Thu, 1 May 2025 16:18:44 +0000 (UTC) X-FDA: 83394847368.19.1328275 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 9D19516000C for ; Thu, 1 May 2025 16:18:41 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NA3M1qNh; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746116321; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NGjIYWqwjaqx0wjkbFDj85fINsD60E6U2Seb+FFfQNg=; b=Fimc2+ISM+1K4lL7Nn3FvdZsRqrHlQ/KGlJlUb7pCrhLESq8aWMY/AmN2ROWxtpX1qjfpy P25wHR6JJDJtPhFT3vRAHOrUV+iDndKK/mWeTGb42spbU2gmezF731JDXhEy2HQDWxOQ8h mDpdgr7RTjE2LgC2YhQvZ93RSub/KNw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746116321; a=rsa-sha256; cv=none; b=rnc1auSg1m0G1EJw4qg2zjtKUpsT9TO6o8UVSszrrLZZH1IovqZWCV7kuBLGldHnguw6hN EMXB/jE4i8nIWaKm+EXqdHHu3cJaHYyvFJd0+gv59ZRpDAnw8TQM6lgTLcpzSIF/za2imz qrllLc/GNfzpv05v1+hIb7fA2MPcr2A= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NA3M1qNh; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746116321; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NGjIYWqwjaqx0wjkbFDj85fINsD60E6U2Seb+FFfQNg=; b=NA3M1qNh6olTJnnk/FC8PlotIpMazVfenG36QV5OkFAyJQAWvhE1pdH49ddbLI4fFM9hEV 68uZsbNxomk9wgb7UfHyQGtzFr9kgPBbIqUgf2m14SesUeQDuuTy8HHLjbdi9kpvAYoF3Q OBUkNIOiyp8tpcRKggc621+RqdL8S2o= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-31-1jmvohD3N-eM3oEc68Vy0Q-1; Thu, 01 May 2025 12:18:39 -0400 X-MC-Unique: 1jmvohD3N-eM3oEc68Vy0Q-1 X-Mimecast-MFC-AGG-ID: 1jmvohD3N-eM3oEc68Vy0Q_1746116319 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-4768f9fea35so30304171cf.2 for ; Thu, 01 May 2025 09:18:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746116319; x=1746721119; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NGjIYWqwjaqx0wjkbFDj85fINsD60E6U2Seb+FFfQNg=; b=RQkgVY/9ACl+4DhnNXZnq7gpEqPFsk38+kmmXLcZxtw0M8dXiT9Vgn0hcwYGrOZ5Oo YQOGRXJ2f9AJaKBOvTLEanYmjQLSqNBHpMz45JWgMGt+X3NY8BXqyiyVrrOYpLHMjwN0 qfywyD6uQ8s0ShM0IzKOcydUquJV4VY9r40hBhfOD0YcHorQsWutlZTz7tHfx/JoEi3I g5chUtaK/nLItVd8PsYvTRilGY/wVrQJE6qU9HkE+Q5Yg/Fb2x6K0KpdS5XiE7GEW8Z3 2PuzhIaw7Uax26oib7RTgS2nyyrMRIlSMO5fEbgfI+xP0Xbuy6OpyKwkIV/qb8gzpVrI kxhw== X-Forwarded-Encrypted: i=1; AJvYcCUk8rH4D+uw4BO5ThH9+odjKMvOOPqT6Y7OctMJ4LpCkBtw6EBXqQeQB0sZZvzCTPwJ6xqRwSJi3w==@kvack.org X-Gm-Message-State: AOJu0YzNyD0nqc6/eMzD1oQdDuSKLNe3qbb3gkTY761fBGcdI+6UaQja dWBMEmM3/BEr8qez6R9IKtuhabgI3P/9CpGAVxoH22dYE6yixcUZQiMKVzwoOT3NTUHITB1aY+s eR5f0T4SFw4C5lIxw3OpgHh01MbmU6SGiBI5g+QEx1MackU2u X-Gm-Gg: ASbGncu5Y2O4JVJLqMq2lAjVVUQhJUpf8yWWjysfVJtY69XV9yjSmo8hp90feZVa58Y 2qwBo7NTtlNIX6l1bumTCrZc+IavzCSm+ROzFUKI4RML3doJTp/sDoaIxYOlpN5viqNd6etZUl7 514X0YOX9czkfxq8Jhbdu2hSyMwbFB3hPxmcgiBKrzV67a/Ca1KvUQeoQKKV2cm/AtRdHe9dZY8 gCZNBVfmOneTK+AW7ikK5vZjbC6isVGhzBWj8ZBCfQTXgtwPuG54ehVat7ydSWQ5m56V5XyocnW G7k= X-Received: by 2002:a05:622a:58ce:b0:476:8e3e:2da3 with SMTP id d75a77b69052e-489e4f6fc2dmr127590461cf.30.1746116319176; Thu, 01 May 2025 09:18:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcqIuCWmckq0jndOW/QTvuIZgj2Fq8NtAAmLCfxHQ8TD/Z8m2S6F5ri3+/pogtyaymp4yTeA== X-Received: by 2002:a05:622a:58ce:b0:476:8e3e:2da3 with SMTP id d75a77b69052e-489e4f6fc2dmr127589841cf.30.1746116318571; Thu, 01 May 2025 09:18:38 -0700 (PDT) Received: from x1.local ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-48b98d0fb2dsm5669031cf.77.2025.05.01.09.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 May 2025 09:18:38 -0700 (PDT) Date: Thu, 1 May 2025 12:18:35 -0400 From: Peter Xu To: Jens Axboe Cc: Johannes Weiner , Andrew Morton , "linux-fsdevel@vger.kernel.org" , Linux-MM Subject: Re: [PATCH] mm/userfaultfd: prevent busy looping for tasks with signals pending Message-ID: References: <27c3a7f5-aad8-4f2a-a66e-ff5ae98f31eb@kernel.dk> <20250424140344.GA840@cmpxchg.org> <86e2e26e-e939-4c45-879c-5021473cfb5a@kernel.dk> <26a0a28c-197f-4d0b-ad58-c003d72b1700@kernel.dk> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: o7vgLqml4RYAgwdT3peCvIGJvDrW5h_yKpxtQ-FhWRo_1746116319 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9D19516000C X-Rspam-User: X-Stat-Signature: spq58ixtdu44k7ha1bh551itgni743eg X-HE-Tag: 1746116321-748252 X-HE-Meta: U2FsdGVkX1/QoeFi7xKB0Mbu/eEEm7m08imF+xpShKKdWgNc8gXSStsVS+jZNgG5AsAmjlCy9A029qHpDlKKZtIyUFqcI8zcnDHdD/BomgXG7KAQqYs5BRKuAnd3DhycW8ShDTWYZwzA4FHUob9RjY8WBlfM/Ml4QNhgLy/+hOmVAogm9hDgk2rLv8cq7ZdByajDgwBqcwPdxjl+KAVll0YPD1xYPMbV0UibYoxF5XsVZg2KncIK1HFATmGfxWxJ5odYKHWSb0yw2vI6FYSJnttHIGf6plZ0NtEjTZyGExEVwkIZ+uzR3y78pLaasqK/5lw8YiSMGmgTSlZuMiloEQT8P6HPNR3r9m2Je7i28U8nU4lrjVFcPj0puHM87EOZwnnDSF1BON940ejhgnyf5xh4wvQa8lR8goEWQ9RU+I9Ry/PEDvo5f1ujoGvDt5m2uhiBWs63DIS8jADyUzxFc30NIiRKWmc4M7c7PppS4B3yx9zpv26rARyISu6aJGfu+QFIQE1GoLQkbNlhO0HjLBqRH26UAYWi+OJx9i13ctAx3BS0+Ky/N+irvf5UtPwZ0Kj4V1lhDDr7OIEksO5X7TdB1uQJvsAOqeb3i7uf2m6QcFJOoML88Q/2hfpe5nAwOd+yccVsYwFmnUA5LSA3+kkKy4nVMIl2sX9r8ybWiYV2o1Sf0VePo8lyHB9tEZzOULa9/BOCJu5/tTi9rKJlXbbj/Tl2HJ+Wdo+L7ju6tqH+bY0Cx62fIMmWAPT98OE/rM99AaDc79pfijD0aIVhLPijJWFeHy91ByWmovEtOXb9WvQhIKSnykuyVJqVuRHTdbmYKLPo5YzG5gW3q/TnddcpL8XfQIKfZYoEEzXSNrNmctyy+ryTVce8sB9EqpX3cE0Mirzau6WTiH7P19hjbX0rMeFM/W/oTVMXj2/6bn7WCnTZtSwnmjlklAaszP7S31W1tJnNhen07hFoybQ Ev35F4Z+ FCeUjirFxsTKIhGq+lmgjCgJP7FXxJLLQ9EABzzxvgg9b6AYMmN91bp3geKf/2f1baWWqYm/mNHKpqJyoPuQdzcUGFFyMN9i2Zyu5MqrL/7qf5VahrqjjzNI1qDbldPm0i4g1UsjAh2O6rOdf93hek7IsP0HwB8mgvvj4ZiuhwzWYYC7RmGoDxFY8FtLdnEu5iXVkuqT5MAfR8g052Vu1qMXJ2XvXAlT9ZyVYsLv0gf+WVxh1Ii2nGsILGTCKO6ZbZAf4V80TNtvF2Qumr+CsLMteAfVQKgZsMXLzwQOvPl/sm8aYHMW+cvusgvYuzuEWzm36ymQdXUh/OYCrEO91eYmHr59g/II5R6yLPb+kzAVC0Z+WgABId6h4muBrps1qnDltneeUih80IL34Ia9jHZnkvQ== 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: On Thu, Apr 24, 2025 at 03:57:09PM -0400, Peter Xu wrote: > On Thu, Apr 24, 2025 at 01:20:46PM -0600, Jens Axboe wrote: > > On 4/24/25 1:13 PM, Peter Xu wrote: > > > > (skipping to this bit as I think we're mostly in agreement on the above) > > > > >>> diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c > > >>> index 296d294142c8..fa721525d93a 100644 > > >>> --- a/arch/x86/mm/fault.c > > >>> +++ b/arch/x86/mm/fault.c > > >>> @@ -1300,9 +1300,14 @@ void do_user_addr_fault(struct pt_regs *regs, > > >>> * We set FAULT_FLAG_USER based on the register state, not > > >>> * based on X86_PF_USER. User space accesses that cause > > >>> * system page faults are still user accesses. > > >>> + * > > >>> + * When we're in user mode, allow fast response on non-fatal > > >>> + * signals. Do not set this in kernel mode faults because normally > > >>> + * a kernel fault means the fault must be resolved anyway before > > >>> + * going back to userspace. > > >>> */ > > >>> if (user_mode(regs)) > > >>> - flags |= FAULT_FLAG_USER; > > >>> + flags |= FAULT_FLAG_USER | FAULT_FLAG_INTERRUPTIBLE; > > >>> > > >>> #ifdef CONFIG_X86_64 > > >>> /* > > >>> diff --git a/include/linux/mm.h b/include/linux/mm.h > > >>> index 9b701cfbef22..a80f3f609b37 100644 > > >>> --- a/include/linux/mm.h > > >>> +++ b/include/linux/mm.h > > >>> @@ -487,8 +487,7 @@ extern unsigned int kobjsize(const void *objp); > > >>> * arch-specific page fault handlers. > > >>> */ > > >>> #define FAULT_FLAG_DEFAULT (FAULT_FLAG_ALLOW_RETRY | \ > > >>> - FAULT_FLAG_KILLABLE | \ > > >>> - FAULT_FLAG_INTERRUPTIBLE) > > >>> + FAULT_FLAG_KILLABLE) > > >>> ===8<=== > > >>> > > >>> That also kind of matches with what we do with fault_signal_pending(). > > >>> Would it make sense? > > >> > > >> I don't think doing a non-bounded non-interruptible sleep for a > > >> condition that may never resolve (eg userfaultfd never fills the fault) > > >> is a good idea. What happens if the condition never becomes true? You > > > > > > If page fault is never going to be resolved, normally we sigkill the > > > program as it can't move any further with no way to resolve the page fault. > > > > > > But yeah that's based on the fact sigkill will work first.. > > > > Yep > > > > >> can't even kill the task at that point... Generally UNINTERRUPTIBLE > > >> sleep should only be used if it's a bounded wait. > > >> > > >> For example, if I ran my previous write(2) reproducer here and the task > > >> got killed or exited before the userfaultfd fills the fault, then you'd > > >> have the task stuck in 'D' forever. Can't be killed, can't get > > >> reclaimed. > > >> > > >> In other words, this won't work. > > > > > > .. Would you help explain why it didn't work even for SIGKILL? Above will > > > still set FAULT_FLAG_KILLABLE, hence I thought SIGKILL would always work > > > regardless. > > > > > > For such kernel user page access, IIUC it should respond to SIGKILL in > > > handle_userfault(), then fault_signal_pending() would trap the SIGKILL this > > > time -> going kernel fixups. Then the upper stack should get -EFAULT in the > > > exception fixup path. > > > > > > I could have missed something.. > > > > It won't work because sending the signal will not wake the process in > > question as it's sleeping uninterruptibly, forever. My looping approach > > still works for fatal signals as we abort the loop every now and then, > > hence we know it won't be stuck forever. But if you don't have a timeout > > on that uninterruptible sleep, it's not waking from being sent a signal > > alone. > > > > Example: > > > > axboe@m2max-kvm ~> sudo ./tufd > > got buf 0xffff89800000 > > child will write > > Page fault > > flags = 0; address = ffff89800000 > > wait on child > > fish: Job 1, 'sudo ./tufd' terminated by signal SIGKILL (Forced quit) > > > > meanwhile in ps: > > > > root 837 837 0.0 2 0.0 14628 1220 ? Dl 12:37 0:00 ./tufd > > root 837 838 0.0 2 0.0 14628 1220 ? Sl 12:37 0:00 ./tufd > > I don't know TASK_WAKEKILL well, but I was hoping it would work in this > case.. E.g., even if with the patch, we still have chance to not use any > timeout at [1] below? > > 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(); <----------------------------- [1] > + else > + schedule_timeout(HZ / 10); > } > > So my understanding is sigkill also need to work always for [1] if > FAULT_FLAG_KILLABLE is set (which should always be, iiuc). > > Did I miss something else? It would be helpful too if you could share the > reproducer; I can give it a shot. Since the signal issue alone can definitely be reproduced with any reproducer that triggers the fault in the kernel.. I wrote one today with write() syscall, I'll attach that at the end. I did try this patch, meanwhile I also verified that actually what I provided previously (at the end of the reply) can also avoid the cpu spinning, and it is also killable at least here.. https://lore.kernel.org/all/aAqCXfPirHqWMlb4@x1.local/ Jens, could you help me to find why that simpler (and IMHO must cleaner) change wouldn't work for your case? The important thing is, as I mentioned above sigkill need to also work for [1], and I really want to know when it won't.. meanwhile it's logically incorrect to set INTERRUPTIBLE for kernel faults, which this patch didn't really address. Thanks, -- Peter Xu