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 C3B5BC369AB for ; Thu, 24 Apr 2025 19:57:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4239C6B00C3; Thu, 24 Apr 2025 15:57:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 383676B00C9; Thu, 24 Apr 2025 15:57:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 188906B00CA; Thu, 24 Apr 2025 15:57:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E89CF6B00C3 for ; Thu, 24 Apr 2025 15:57:18 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9B953C1CD4 for ; Thu, 24 Apr 2025 19:57:20 +0000 (UTC) X-FDA: 83369996640.26.CF22581 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 61BBE180007 for ; Thu, 24 Apr 2025 19:57:18 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gyi4dY7b; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745524638; a=rsa-sha256; cv=none; b=0rzVHILc4uBbqPnM0VbgbJVdhe1f0NYyJzRyPSBNks/yQYdG48Y+glstUXf0COwUKr3A4y qSC7V4KaHJQAqawtuEHS+lHcFVHuACne8q3sQrEz0w4FUfF4TrOL+1rMbL1Ig1Enskntgj BijSRRVrWEKbTmFtUK+batPWPezgIMo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gyi4dY7b; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1745524638; 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=i8hpAx0eweqVSXXbQhjnLIlU+eThfWl13dDIADUavMI=; b=4KSg0LQxXTZFRDl/MOwTYZWUXRr1rQMAEWx73nuwq+nWxCqJ4xqGX6XAc85zp9IzHETe0j ER6g+HLFWnj4u774tAybbliClElmjtWFWap+2n/fLAub1TdK+ieh2xycvl0K0kK6DhpimS zedvAjEeg7x0WGPoWLSbKRd/kcS/ngk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745524637; 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=i8hpAx0eweqVSXXbQhjnLIlU+eThfWl13dDIADUavMI=; b=gyi4dY7bqIESonTnhThuU49FLvG0xfwEtr///ilCODBK/4QYxN9MwtjXGKbZg1X/eBWAhh +5Mk1s1SQxM7XxfuBKQ938OeHAhPQBuysI8QDUX6vuYKu1PKa5CuwUguCuZbe8MRIj30bx JgXRbdyHD4kVgxfjGn2tr+1JgPyY7vg= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-274-K8fHUoGfMF6YL8h-g_C7Mg-1; Thu, 24 Apr 2025 15:57:13 -0400 X-MC-Unique: K8fHUoGfMF6YL8h-g_C7Mg-1 X-Mimecast-MFC-AGG-ID: K8fHUoGfMF6YL8h-g_C7Mg_1745524633 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7c572339444so227546585a.3 for ; Thu, 24 Apr 2025 12:57:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745524633; x=1746129433; 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=i8hpAx0eweqVSXXbQhjnLIlU+eThfWl13dDIADUavMI=; b=CXYoFUAgC8tEotVdB6TvnCo1R4q7/9yHTt8zg0AuMjussUz+7vjUiGAp2yB8GMDIP3 kTP0O9GSu0nRC0Pjq1iSvNdKRiikT2Xy3HaO6MYVAmUKG2fLT5ncemQcStxJQ8bLQodC HxKvwd+Xl8UaPRhP2XdtQViqUwyFkoJPcydZKOc07PS2AHRe9RXBx4uDAvtag9vSkzP2 ORgqGNa/I57CkHI72a0kByHcvN92m1sukEcGT2ITSVddGl1WxAjT5wpgxYyrJX69rj8Y AQ9ry39MjAT+Aj96Z8faViZ7BKg12PQqc3buEyX/z3BRJ0LTgV2qV8dxW4gdoopLIt7L uYwQ== X-Forwarded-Encrypted: i=1; AJvYcCXBqDlXVExUWfkf2cM3h17btuJmK0UrSkvGSpNe2tPKu+lTVzdRviWNUIAtleOaicvcUZQma7gimg==@kvack.org X-Gm-Message-State: AOJu0YzlwT7Y2U2Ip0kH9ueYiA1ijgsrKXw/0GcHM2f/lrbeRjIjzfZA 9HgMTKiKEWkFG3PoDQ8brVFu6QUZK785AFWSJUEcYOP7CJbAIXDbKP+l+OnrzqXCEx3WTCMpGjN mJTHXQfLOccjhwC+lD2fMzCLoCiF6LG++9cv+IS8Tp7ccz5sV X-Gm-Gg: ASbGncvqkCgPUaMhKjcFdICE82IPIfQSyms5tKby0KemMtbnQMCIL+VoO7V66MKNkAU klukFxtQocANTMd3U7wOu/1EkcBywTysjLq5o53fkgsvBmazlkgzMIcewTdhSHcDfzIhx4MsEst sY9BF7uOmzoYzoZNH+0KYDoT5Hs9JroAl3NxRJmJr+O+10qKQcqzQeAr5OpFhC9wxvAIDLO4qEq glGOa8ek9ih3GX9mNQdL0QBI1TnJLBco4V9D9x1FpmvEPRJfIRqggNuUe5Yui0MYpoysITOeb84 P8Y= X-Received: by 2002:a05:620a:1d02:b0:7c7:a356:105d with SMTP id af79cd13be357-7c956ea97f0mr731904885a.4.1745524633375; Thu, 24 Apr 2025 12:57:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGV96oy+u6GUAI7e6Y1VzaKBV3VTfTOAsdjbVU9gCpAcjyZBNWfDAeMQWeTUVag4urC4RbwLg== X-Received: by 2002:a05:620a:1d02:b0:7c7:a356:105d with SMTP id af79cd13be357-7c956ea97f0mr731901085a.4.1745524632928; Thu, 24 Apr 2025 12:57:12 -0700 (PDT) Received: from x1.local ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c958ce3d54sm127025185a.51.2025.04.24.12.57.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 12:57:11 -0700 (PDT) Date: Thu, 24 Apr 2025 15:57:09 -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: <26a0a28c-197f-4d0b-ad58-c003d72b1700@kernel.dk> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: JaUXdqM9dYaclAdXdjIG4buidpQRxrxQsAM616dbWFM_1745524633 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 61BBE180007 X-Stat-Signature: ghuconxqtyporrmspqmk3g4an5fr15s6 X-HE-Tag: 1745524638-449128 X-HE-Meta: U2FsdGVkX186wEbE6KN9qRJ3L470HuQc/HAtR5jyyZl36siEr93uIzMP88gWgXazwXwgy/6oFiz5Qt6fjUR6x6NbbxbR0W4ypOnSH7REd2E/lU4Zd/bpwZ3i/D+ewBNMWYm/5WJdyhbKAFtXZlT+PQ5g/J5P7ADVk4Jfm358TO1aglAo8lpZRu9xXQL18nzfLSRfZuWaItKul3OP26h7qw2rSbbvq9k+R0EynWrUF/trJRW/QbqxHeq0+tSdN+eiF5b9eQrEVNY0A8Quh4Xuna0vkwpXjb9PEWZsiluZ2U97M4T98ZAKMjQAcfm4VinxoCtVJfJVXj7W/vBDvX0zrnSrIOEDwFR6d3cAgcjyIaoR14kRo4NV7jflIT1HA1ooTVtnVi1eLnkqwBA9TcxE/4q+e06yayIWHZd1JlDFpSdc60V/Br4j56pDfA8yHxaujHY+eRkHBpPLiMud4vVG4+ovmWgplvL+XpBhe3a0jSCZBXs8/cvf7QZQlsREmObOOjgOxbWrI/8a52H6ugwI6IcIwH3eraGcVFiJKC4wykwhDYeZqLwdZlkLvxX9Un7E2SIRg6GNqbp48g3nsoEmZUS5kH6sI0CaLNBwor35IF6aj9bte4jcsVK846XGod4SrT1a3SJPnc/5ss0ujsGGDvBotkV/QXX+vEaoysQCc18BSytlnJwjzfZs6NXhJE7gN2627z+Cvf7/33lhHhoFl69uqDDR9VimGoIn1rYakL4xCy7mVGzhFbhovF30f1KqPIp6pf3HkCOL8pPZBzP1ouzdyBqghjnEwX+1R4bE5KqAzd1HRk72Cv00oRDcR6446EvosAjKtx7sUZN2OqD8sKZxnHIXKjPA7e3eY3BoADp6KQy1WY58kjFilHI6aRb5XS+Ils38i8lWyYZ2WyH/JAoG6xVWW68zN4kTYWfJFaZ+ahAZVHPqqWOmYz2TJ27OurRvKJkKYojISlo9PA3 jZpYYtvM fGB9+a2LW9r2FdfbUVB3evUfYunc6c9hemizf2Lf5mP/fwtq2NcskRwIilVr9NTZ7q8RbBoVBzXDpeH/04Nlain80xlTTZbEpIufX/8r5963tNsgzzHxnsNZLXzPGFvziT5rjIrjrh/GvI1RN+LAAzVFEjTXjJzKBvJyth1pdOhR/pgnXVJJkNHb/8wGVDzD6G/dapSb6gOqhoJ47Z+374rAaghjyxo/L6kmdFa/DPNcSixglgdrshKOn5/U2SQy+MEA6AjFQGgl8VWLYrRPH3JeiQevsKQpCuGNs9EuvplwzjUpOZpI4ru/dtlwuksCOF5d/+2+4uBRSaG28YPp/mwHy4LdRGKWsS8/l0OcklqJ99C5ojb1x1k+t7fxYS2k8Yl9zXo93ZSV9rpcvpDNlZ32pECmbdqCL3+occTDPeGBK8Y5NLxqAhD1fbUHDgRLw/W5M2cBX3nxa3wFh/N/byXOExQ== 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 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. Thanks, -- Peter Xu