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 78CD1C369DC for ; Thu, 1 May 2025 16:29:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 67EEF6B0089; Thu, 1 May 2025 12:29:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 62D416B008C; Thu, 1 May 2025 12:29:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CEF46B0092; Thu, 1 May 2025 12:29:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2F5986B0089 for ; Thu, 1 May 2025 12:29:00 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 55DF01210D7 for ; Thu, 1 May 2025 16:29:01 +0000 (UTC) X-FDA: 83394873282.26.F84A1FD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 9E08E80003 for ; Thu, 1 May 2025 16:28:58 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gFtkk9N5; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf30.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=1746116939; a=rsa-sha256; cv=none; b=tB/2LaLYfBdKn7YWBVRCpypnG0/vaR89xOJCiwbvuThNFQoUWRD36CHj89ViIvD9ci/ZSd gG+WLs4TuCtHARhrP5eLg3GjBfFrAQcY8QYyDIaHP79IaxfiULef8hEW5XlHbk+Eo2JT01 zb/eJq8bh+/3SR8nAK6FmsSjvCFkFNQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gFtkk9N5; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf30.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=1746116939; 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=POFJZm9mCkwmAkqxkeFxdJcXQWJvnT1NIr0tGy5Ovr8=; b=bEaHSK6wAFlNmwExGpvmuiQ2wkRrmijYwGtrts/7tTR4Afp8F+2e9+p9zOEN7lZppUIASe 2rBS7Yp63JvDIqbVvUGA2n8eCTaV39R7j5B5kk1MydO6OcspqgiJRWS+/80Z73nwIeF3Tr NUCmep/7bTmo90LrQ+lpHB/Obl/mQks= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746116937; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=POFJZm9mCkwmAkqxkeFxdJcXQWJvnT1NIr0tGy5Ovr8=; b=gFtkk9N5jGXIbbz55GrsOZ4gFGYV3TamJHM+nMFHmOunBNsMLcjKiX5DD49bhlUi04lz+u vsh3RZ2iFUouJtWNNxRCGe4JCpOQaauDlXDbAgHiTXIn4O3FaxQjLvqOei4QK2mB/clx8d NeDgBLVHnjOy6DtZFMenLgcE03/uI98= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-438-6NHF31LAOD-LbZqB0Db-Tw-1; Thu, 01 May 2025 12:28:53 -0400 X-MC-Unique: 6NHF31LAOD-LbZqB0Db-Tw-1 X-Mimecast-MFC-AGG-ID: 6NHF31LAOD-LbZqB0Db-Tw_1746116933 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7c5c9abdbd3so107095585a.1 for ; Thu, 01 May 2025 09:28:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746116932; x=1746721732; h=in-reply-to:content-transfer-encoding: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=POFJZm9mCkwmAkqxkeFxdJcXQWJvnT1NIr0tGy5Ovr8=; b=MDstl3qXrKHXqyHH/SvxAQ2h7ybicRkfKZxpDgJ37J9AvadtNp3LQ9Z/k/Vonxxd0/ QSj24uHWS7r2+K5C7J0mwVdNw8dGO28DlzAHSVNhsKpdkFEXbZrPWOW54eEP1H9CEvWI p35pxAqmjje5O7QbMafs+FqpvFHkxAt9vj6h79ksO7mMbO4yCBLO1neHamIMlaHN9U1D Va85I68iCSfVcQIMVJeDP3PlajPorvpWinoNu5g+zIVm1PLGAB+eIsLd7pxoFC0ZDUy8 URLMuGUKq85ZiEBT8AHXpx8M0aw3rsmohnh8P5oimxK2FYJF3EVUeI7yzk5ZHjPkZ9ji dcoQ== X-Forwarded-Encrypted: i=1; AJvYcCWlq3OoQfWqtf7Q3E3tWWoEypDDF//UxQxfp75m4gVt6GIfK0b2aoT5pbAnXNcR+dY4IL6oaxjhiA==@kvack.org X-Gm-Message-State: AOJu0YzD6A2hJbn3OEVhDBVjirqrv9P53tjXJKa/kh/jwx+K0Qq6D8FZ pWwYEkDBNRuHNemnl234hp9fg2qJcYeqPndAP2CZEjspohOOB9nOoFZuvUzY7OnhWwA4kSq5jV7 f7xEUXZ1RmRHfrsduQWywQnSsAEwYDphHDB3WXy88Pil9zvqK X-Gm-Gg: ASbGncu2zuq4lvSkkNfQCMUTIBhiv0baPV+z+seaOWU6HL8IxK71fy2Ek394Gf1eQBo eOfihxJL1mYleDHYRfQ9xJTrqAp423bVAk3JQMUAns1hsdrT33s13NBvztI45TQ25Fst3Z5ICUK Gxf2w2O1EkP1E5bEi6E5iJ1iQ3lJ7FBsYWZioQFwUzgjpkAG/TgQCSlvdxXtbeV39KjmPkYNveQ 1G5/OMUrsJwSR8qjCPKmXZQfFpFItFg6w35RsZ7wQVNKnP55NnKAn15DbaYdFzh/Ob2/5Z9sNlN JK4= X-Received: by 2002:a05:620a:4150:b0:7c5:602f:51fc with SMTP id af79cd13be357-7caceffd66fmr404954585a.44.1746116932615; Thu, 01 May 2025 09:28:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGaLtDw2oaUnXy5i1K0ukWVnpC8lWGFP2VjjF8AJY1HY9brz4VmAyqEbWQHCliEQppWDHSJEA== X-Received: by 2002:a05:620a:4150:b0:7c5:602f:51fc with SMTP id af79cd13be357-7caceffd66fmr404951385a.44.1746116932148; Thu, 01 May 2025 09:28:52 -0700 (PDT) Received: from x1.local ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7cad23c3cf3sm65689985a.36.2025.05.01.09.28.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 May 2025 09:28:51 -0700 (PDT) Date: Thu, 1 May 2025 12:28:49 -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: jSDNf0qrEpQ6F1e0BX7FfJHrIbcxBZd9eJ0pnw-7yNw_1746116933 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 9E08E80003 X-Rspamd-Server: rspam04 X-Stat-Signature: ad36x3bzhtb3k39bkyazzwt8fgpfsd9c X-HE-Tag: 1746116938-190888 X-HE-Meta: U2FsdGVkX1/Az31fGihohq1jPaiOeIpndemDtE0dTwIZ+blExaY93MnHelH8K8jyTVzsnlW7b0iMJ/zgiHShezoOacqVmqN2opLileOMaUT/wzqXAs4rPdUF8oRWvh+1yOZC77KfiJBa1PrgE89yIuBklSJlQirF7Bffz5znYAMz0zj6WRUD2z5+UgwaPC10FZPAAD0/KgX9bS2ZE4GuRxOTJhCoWH+N0LKh4+wsnIzNd8WlF+PAGNTyIwctyOuceybOElu1x7ef2S89nXUVbkrcjJY8yE9Zy0Ip7GC7jwhoAIT5OpqE6AlCUJejG/pPrhbJn+XAqYtN3GkzBlt1VeqgBxMpUntLZxr2rzWv1IVoQgGc82G0/N4KbQwa78UxSZ+UIIqvVnqMKSwUMoqI5A9emhvg67po7jf0gX3+7TaB4oICx55H7lxNmmc0un5LIPqVs4DQ5HBuN6ILvlXu1KzhPQi89idCjVu48ZgI0cWs/se4Wfng3y6D5FOA9TOrwWkrjmbu38P5QBhNJ1Zwxmlk7Sht+EoIMbJXFicgaJldzHdrQlGEA9I+NvEaFAQZ4gyMjQxN54+linYrJ/wO4rEstTQineH4n8kMTaRVIOToEPPXenGhW0VazurejQUJ54AfflwHsXGIj6+ksxBWXiAHZOAv/cKK+8jMgH5hXwO5cubHZyqXditc/jxyCYmoLw3s0sViN24rpLjLMkJfTl4WWlOYoZHkAOkFwLsyiOQrvyvWjJoatWKqG0s48Ljj9eetwFbgBlHvICO+dwThdkkVYE8XUTjeQTD7X0ytFEXY01+px4CDd3FpVMWU73kvwdnnmR/7xkemVWrY8CxL1XjItPPAauJdo5I3SZv1ajQfumv78XokxSjvPX8e6BV13U9oolCqqXLxsIygHpFMpzq2Ntp8cffdunkqpn3XW/uWUG/OXZwUex2+un7n0F9roK98HlDlYVffwkbxLsi l03WP7wa i+C4vyeJa047DKy4WzxboFR+nXxzmxwBuOq9MnlyGpHomNSMgKB+YOmiAfCWJXPfZHpFV6AmDFsk3g6vN3IFWsPRfG4LQ1xEAzFIqf0Zeo4bPLarD30UHRx5MbzsmczztxcfixhwF4zBPrZCXyfswg40r/RaickCaPdblFQn0v3ODfi4rNcXjZJ5fZjbUgNq5klujs+s6DJ9nZVTS+adu5C5GjJdvIxvlSk8YdUEV0b6oN9Nftsq3ZW+y0BcmCWqLMD/AjlXzBmawkFK7nzfNjdUnfSTpQ6Ucy7ga3u7dylvZCJJNqhZCuUmcOsgV8HUWGUs5E9yc8KAyLDogTtFIQRdnin43NF/WeerUtxpyv9mgesX35VajuHTxPSR9bEaf1Sp+65hhHvVyNrKwz90Vi/MbWTMY6Y46Rr+twKlcC+zI4j0= 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, May 01, 2025 at 12:18:35PM -0400, Peter Xu wrote: > 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. My reproducer: $ cat uffd-kernel-sig.c #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PAGE_SIZE 4096 #define BUFFER_PAGES 2 void sigusr1_handler(int signum) { printf("SIGUSR1 SIGNAL\n"); } static int setup_userfaultfd(void *addr, size_t len) { int uffd = syscall(SYS_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (uffd == -1) { perror("userfaultfd"); exit(1); } struct uffdio_api ua = { .api = UFFD_API }; if (ioctl(uffd, UFFDIO_API, &ua) == -1) { perror("UFFDIO_API"); exit(1); } struct uffdio_register ur = { .range = { .start = (unsigned long)addr, .len = len }, .mode = UFFDIO_REGISTER_MODE_MISSING }; if (ioctl(uffd, UFFDIO_REGISTER, &ur) == -1) { perror("UFFDIO_REGISTER"); exit(1); } return uffd; } void *signal_sender(void *arg) { pid_t pid = getpid(); usleep(100000); kill(pid, SIGUSR1); return NULL; } int main() { struct sigaction sa; sa.sa_handler = sigusr1_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGUSR1, &sa, NULL) == -1) { perror("sigaction"); exit(1); } size_t buffer_size = BUFFER_PAGES * PAGE_SIZE; void *src_buf = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (src_buf == MAP_FAILED) { perror("mmap src_buf"); exit(1); } if (madvise(src_buf, buffer_size, MADV_DONTNEED) == -1) { perror("madvise"); exit(1); } void *dst_buf = malloc(buffer_size); if (!dst_buf) { perror("malloc dst_buf"); exit(1); } int uffd = setup_userfaultfd(src_buf, buffer_size); pthread_t thread; if (pthread_create(&thread, NULL, signal_sender, NULL) != 0) { perror("pthread_create"); exit(1); } int tmp = open("/tmp/file", O_WRONLY | O_CREAT, 0644); if (tmp < 0) { exit(EXIT_FAILURE); } ssize_t written = write(tmp, src_buf, buffer_size); printf("write returned:%zd\n", written); close(tmp); return 0; } -- Peter Xu