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 E1569C369AB for ; Thu, 24 Apr 2025 14:03:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 250016B009F; Thu, 24 Apr 2025 10:03:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D7AC6B00AD; Thu, 24 Apr 2025 10:03:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 050766B00AE; Thu, 24 Apr 2025 10:03:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D6BF16B009F for ; Thu, 24 Apr 2025 10:03:51 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 73BBA80899 for ; Thu, 24 Apr 2025 14:03:52 +0000 (UTC) X-FDA: 83369105904.25.BE30F72 Received: from mail-qv1-f67.google.com (mail-qv1-f67.google.com [209.85.219.67]) by imf01.hostedemail.com (Postfix) with ESMTP id 274D340018 for ; Thu, 24 Apr 2025 14:03:49 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=SiLy7ab4; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf01.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.219.67 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745503430; a=rsa-sha256; cv=none; b=GslRk954lRb6e/Uy3ggkBcInFFb8eMzHCCXcxBm/4z084e54bU6jHLFjTCGiP/uxvVFIfm DQGrqQBULEkdE43ofxiF7gOV3z3ChEvTkPE+G/XaOnPzpzMOOyymYZ3/JBHiaixw9I4e1z L56VKg/p7NQy2yBK4CJFAh5wmyzjKyw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=SiLy7ab4; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf01.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.219.67 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745503430; 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=HuBoOpzO4HI7+MmYJqWcOQNx+WCqjRzx2t0T6b4RNog=; b=bh2hu9FPhf0CdBfjzxIPW/iwOUoBori8hvybmo8oZdwgRuhJLeKN+MCWB1X6uMOKaC4PWb i2sdCStW2rXDnFdmpQF7/43ALXi8zVgXBoXdpOYfxE3zTF/b3DQlEDyI4JD0blJdfPDE/l UhKX93oZy+jh8/tXQzOXDpcWO6lXzzA= Received: by mail-qv1-f67.google.com with SMTP id 6a1803df08f44-6ecf0e07947so10973796d6.0 for ; Thu, 24 Apr 2025 07:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1745503429; x=1746108229; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=HuBoOpzO4HI7+MmYJqWcOQNx+WCqjRzx2t0T6b4RNog=; b=SiLy7ab4eKgw9HeZNEh+QLJNcKliCEWC8O7UwuuF8qc9wK2Buml7CsscN620Uf6xQ/ ToWYhVhL2WlbA7/Da954yyyRtPilDNG1VjkUqgjJhEdVKM6y6ryZXu6E7FkTE+dHhrKu 8O+mEKMe+TeKy3Cu272exCFcaK/8acUqZqVT0mXFghQLlO79u1FzQQDKHL+hF/8lNbTx TxoD9vTMKZzAhKvJrsnciui9+8LgbuxjvseHzPLw6vgMp6e+GPbcbpMpzg0nvxhA+07Q +fIn8h5Qimk01zUpAlIbBw27O1WW3pqpro69ZRC+vvKCJctT9eCQ4HbqAtF1P5yM3jAY GrZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745503429; x=1746108229; 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=HuBoOpzO4HI7+MmYJqWcOQNx+WCqjRzx2t0T6b4RNog=; b=d+bBb5wA4PHiO/oeWxkA55lsP5brxVW4uC33JDGqO+/mrWG0nduvF4rNuvHrrRd5xR DdYPDgb1QRAvs0aHKlOeDZn8YUTCVh3JA+xWw8TgzM1kAlKI1P4FkFu/oyMHkjUK+xdA eUlebGb6zSdAm7UTv4lM3EAGoqAUN9FZoSk/MeGvozpHSZo+JOfpqEfJd3zn0MxN42Gk H6B2JTYMevqgs2K3skg2h716u5ZcSy+baQLTlbAWgB2TaNF9RzkaE7qFoSLuw/avogK8 pvPETdY8iYpwsqyPTcsrktU8R1KSxNwVkXLqa1EQhQI5KPSVs89XzqR0wx9H2+dLFz06 MbMQ== X-Forwarded-Encrypted: i=1; AJvYcCWEDK4z3f2ocTMk/tjEZaD9C+yhUeha62Ts3YQR1cx2MWjj/oV5Rz+XRMOoJ5cMttNLgKAaV7Mdaw==@kvack.org X-Gm-Message-State: AOJu0YyH419GCS2eUh+1UJTSKm8ojEbI/g1HpKbxrwXr7vB7EkopTn6p rUfvGhujpW+DKhtUgvtJJ5jMj406NSC3MwcR0xUj1ojI61dMWVNmHy1oR58Z2aM= X-Gm-Gg: ASbGnctN+Eq6j4wPeBMRhBYmzyFhXx/pMy9AFevfqvrJzbsvX+JOr8KQUr7Kd9r+gty ONN1V26FG630cOC0zl2qWZTQy1Gi9SBOuCt8ix4LTyB9zjXEbGIQe8bWh0xgBzN8BVLd5K6H5mF ecn17gXSNAugUNxlduYUa+NpEz4qLNBNyj0dy+CTt2Sw38/1rWDFiP8FR5Y76IS/mw+SqFQaQeq JFUL1cHnDZUlx/8RrwQwGMxHR8tVJpZUc+3MVvy5xy5JEnUUPekROgGP2yNZx7pDA/X6ledeo3f mGd0/gN0GC9JbcyLrq/wefGS/2ihmQDpWNzjwzA6W1nrsHncuA== X-Google-Smtp-Source: AGHT+IEIzbE+5QviSUqiQqOIlAq9SVAo+rU1CTcUl+6WL96U3eoCzmI0EQpuZ4GXtAcXFz4jgrHSvw== X-Received: by 2002:a05:6214:509d:b0:6ea:eeb6:c82b with SMTP id 6a1803df08f44-6f4c0f4a7a7mr34487166d6.2.1745503428983; Thu, 24 Apr 2025 07:03:48 -0700 (PDT) Received: from localhost ([2603:7000:c01:2716:365a:60ff:fe62:ff29]) by smtp.gmail.com with UTF8SMTPSA id 6a1803df08f44-6f4c0a74048sm9630576d6.85.2025.04.24.07.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 07:03:48 -0700 (PDT) Date: Thu, 24 Apr 2025 10:03:44 -0400 From: Johannes Weiner To: Jens Axboe Cc: Andrew Morton , Peter Xu , "linux-fsdevel@vger.kernel.org" , Linux-MM Subject: Re: [PATCH] mm/userfaultfd: prevent busy looping for tasks with signals pending Message-ID: <20250424140344.GA840@cmpxchg.org> References: <27c3a7f5-aad8-4f2a-a66e-ff5ae98f31eb@kernel.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <27c3a7f5-aad8-4f2a-a66e-ff5ae98f31eb@kernel.dk> X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 274D340018 X-Stat-Signature: nbthwptjo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/Io2/8nxY/q8FoFo5l5TwMYyCzgvmi+axCQyzG1hb8iXIDtHaKDAnEAJf5oXDUSZp6r88zTwPUJ1vKvrvjD9SUutmryUybbg/thbgZqrFeKMqmEUlApK2ZJMnmZ61mIvGZi3bnVM0gq1Zx7VVOsDL/LUkvGCK8JVD51U9PkI3OzUceltsElfyzHqT8rH/rwcl3m1FhqmlwrnUDIs2DpdW1BPh2Y+ekfzPCeGLuGnDXQs9U9T33rxGdMaohGTNc0xKryWf+PDJrlMO5oWptLw6Du7YDxUkg2a/Ch/ZgBBoZryu07rJcAxOrFAZL0AkcK6XjCGPQEyCR2dofL0hvaSQspaN/iesRUf596ZrtM9J3dbkHyTHR05fhr2g7cEzLFpSfUTMgRSdcUXtCffnV9OftXJtBKmFVntKkQ9tUcNd41KDmk5sWniEuFsxaiuld8X2QS+NfTBndOk/Cc5V9ZH1L6TkT5sujNTBVkZNfTX0S6t9CESigfEJg7c5V6iAIy6omyIMsUrrNzQGGAUFpeBzrzekfwsDCGHgV0YozE78tuf9NVVq04LGJ5EcjCbhjcdNMYIO/rYim/WkHLp2r+FwUMJLTlB5U7/m6XoEQ48WSzZRAebl0YD7/k8jWAr5outnGEwuywGz47m7lg= 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 Wed, Apr 23, 2025 at 05:37:06PM -0600, Jens Axboe wrote: > userfaultfd may use interruptible sleeps to wait on userspace filling > a page fault, which works fine if the task can be reliably put to > sleeping waiting for that. However, if the task has a normal (ie > non-fatal) signal pending, then TASK_INTERRUPTIBLE sleep will simply > cause schedule() to be a no-op. > > For a task that registers a page with userfaultfd and then proceeds > to do a write from it, if that task also has a signal pending then > it'll essentially busy loop from do_page_fault() -> handle_userfault() > until that fault has been filled. Normally it'd be expected that the > task would sleep until that happens. Here's a trace from an application > doing just that: > > handle_userfault+0x4b8/0xa00 (P) > hugetlb_fault+0xe24/0x1060 > handle_mm_fault+0x2bc/0x318 > do_page_fault+0x1e8/0x6f0 Makes sense. There is a fault_signal_pending() check before retrying: static inline bool fault_signal_pending(vm_fault_t fault_flags, struct pt_regs *regs) { return unlikely((fault_flags & VM_FAULT_RETRY) && (fatal_signal_pending(current) || (user_mode(regs) && signal_pending(current)))); } Since it's an in-kernel fault, and the signal is non-fatal, it won't stop looping until the fault is handled. This in itself seems a bit sketchy. You have to hope there is no dependency between handling the signal -> handling the fault inside the userspace components. > do_translation_fault+0x9c/0xd0 > do_mem_abort+0x44/0xa0 > el1_abort+0x3c/0x68 > el1h_64_sync_handler+0xd4/0x100 > el1h_64_sync+0x6c/0x70 > fault_in_readable+0x74/0x108 (P) > iomap_file_buffered_write+0x14c/0x438 > blkdev_write_iter+0x1a8/0x340 > vfs_write+0x20c/0x348 > ksys_write+0x64/0x108 > __arm64_sys_write+0x1c/0x38 > > where the task is looping with 100% CPU time in the above mentioned > fault path. > > Since it's impossible to handle signals, or other conditions like > TIF_NOTIFY_SIGNAL that also prevents interruptible sleeping, from the > fault path, use TASK_UNINTERRUPTIBLE with a short timeout even for vmf > modes that would normally ask for INTERRUPTIBLE or KILLABLE sleep. Fatal > signals will still be handled by the caller, and the timeout is short > enough to hopefully not cause any issues. If this is the first invocation > of this fault, eg FAULT_FLAG_TRIED isn't set, then the normal sleep mode > is used. > > Cc: stable@vger.kernel.org > Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization") When this patch was first introduced, VM_FAULT_RETRY would work only once. The second try would have FAULT_FLAG_ALLOW_RETRY cleared, causing handle_userfault() to return VM_SIGBUS, which would bubble through the fixup table (kernel fault), -EFAULT from iomap_file_buffered_write() and unwind the kernel stack this way. So I'm thinking this is the more likely commit for Fixes: and stable: commit 4064b982706375025628094e51d11cf1a958a5d3 Author: Peter Xu Date: Wed Apr 1 21:08:45 2020 -0700 mm: allow VM_FAULT_RETRY for multiple times > Reported-by: Zhiwei Jiang > Link: https://lore.kernel.org/io-uring/20250422162913.1242057-1-qq282012236@gmail.com/ > Signed-off-by: Jens Axboe Acked-by: Johannes Weiner