Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6096223rwr; Tue, 9 May 2023 10:07:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ZayoPsOMujfPHjRw7H4rRFVc60Eo0GqcjFvUERtAv1utyS38bGil4LW/BTjP5W6mxMoX7 X-Received: by 2002:a05:6a00:1828:b0:63b:8dcc:84de with SMTP id y40-20020a056a00182800b0063b8dcc84demr17214516pfa.4.1683652076389; Tue, 09 May 2023 10:07:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683652076; cv=none; d=google.com; s=arc-20160816; b=eZ+ZZONGnOAGDvQtpzFMQk44ZxsF+tm6aY4Z5bM6rqOaODGPxZkFTI9mMhfhVcZE9R Nrhf3d/e2XxggHxNDh5njgfBHvvaqQHfrCyn/+gb5T8/XuCGPmdO0HlHy7xlVeJb0Zmp LuAUQf/2lAezvDwVq2BWnQaxv6A/AZrxrytlyIz2/TCgBUs45ApqKUhaQ0QsrdU8ctYv Bn/k24DwrUQ3P+GhQXkesFs3R2e0VWBd1GW2SPyVdCvXotX1/D2K5gA3Epwg5pFeDcm9 NzYypGCY+BSTvQBW8eTaiSQhQ+FI3A+PwfXUeCEZU47FxLhMHfCSfhUNpG/8APlUrysE IZIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=P5i0UV4XUiq/O8euXN5wZ8WzR/MP84BiMxFHt/1Czt4=; b=e8ggl33AACoYVePa0s88qRhV9zo7H+mcSiSmpfOV8oZPhzRaRKVLXDx9mSxajxc5aO XV+9fSpE84gJaiZTn+QbGD0pchkh4TMNYgRQawXBj71oJRdTawl4Os1sHvqQFP5NnsOX lJCWlJcNggPWQNu7T2tDn30sls3CEQxg9NPUztwQF67aUMZEf2yDSFqINfOqXGM0yzrH NO6KArnkEXYhZV55o98pxVOhmgR2QJ8YYT7oqWZQXHrnKDsa452AmDsdBnqpScuD9nZj 83vOz3E8Nxpm28RPUY13G5EkMwuoHPi0mA1L2wadc11j0BO+c1+R/TiZCozwgI+puLkc IYTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=iW5mq41x; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k13-20020aa7998d000000b0063d4595dee9si2851759pfh.46.2023.05.09.10.07.44; Tue, 09 May 2023 10:07:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=iW5mq41x; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234906AbjEIQ5u (ORCPT + 99 others); Tue, 9 May 2023 12:57:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234837AbjEIQ50 (ORCPT ); Tue, 9 May 2023 12:57:26 -0400 Received: from out-61.mta1.migadu.com (out-61.mta1.migadu.com [95.215.58.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A5A92716 for ; Tue, 9 May 2023 09:57:17 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1683651435; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=P5i0UV4XUiq/O8euXN5wZ8WzR/MP84BiMxFHt/1Czt4=; b=iW5mq41x5yiJn7gfvLTGaqQQdNpqEW8JmuzeTOK8Sfw4u+jcMap56jXivpM0QqNJ8IMBqF 3re0jfAERex9kaSEuHDeD0891yfxuIr+YxKFkBDI5xLP7ONOmVUqy+vtWc7eEx3mg/+NBS BLTetoZLlSSytF0SvKiLN6RhTyaI7o8= From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-bcachefs@vger.kernel.org Cc: Kent Overstreet , Kent Overstreet , Jan Kara , "Darrick J . Wong" Subject: [PATCH 06/32] sched: Add task_struct->faults_disabled_mapping Date: Tue, 9 May 2023 12:56:31 -0400 Message-Id: <20230509165657.1735798-7-kent.overstreet@linux.dev> In-Reply-To: <20230509165657.1735798-1-kent.overstreet@linux.dev> References: <20230509165657.1735798-1-kent.overstreet@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kent Overstreet This is used by bcachefs to fix a page cache coherency issue with O_DIRECT writes. Also relevant: mapping->invalidate_lock, see below. O_DIRECT writes (and other filesystem operations that modify file data while bypassing the page cache) need to shoot down ranges of the page cache - and additionally, need locking to prevent those pages from pulled back in. But O_DIRECT writes invoke the page fault handler (via get_user_pages), and the page fault handler will need to take that same lock - this is a classic recursive deadlock if userspace has mmaped the file they're DIO writing to and uses those pages for the buffer to write from, and it's a lock ordering deadlock in general. Thus we need a way to signal from the dio code to the page fault handler when we already are holding the pagecache add lock on an address space - this patch just adds a member to task_struct for this purpose. For now only bcachefs is implementing this locking, though it may be moved out of bcachefs and made available to other filesystems in the future. --------------------------------- The closest current VFS equivalent is mapping->invalidate_lock, which comes from XFS. However, it's not used by direct IO. Instead, direct IO paths shoot down the page cache twice - before starting the IO and at the end, and they're still technically racy w.r.t. page cache coherency. This is a more complete approach: in the future we might consider replacing mapping->invalidate_lock with the bcachefs code. Signed-off-by: Kent Overstreet Cc: Jan Kara Cc: Darrick J. Wong Cc: linux-fsdevel@vger.kernel.org --- include/linux/sched.h | 1 + init/init_task.c | 1 + 2 files changed, 2 insertions(+) diff --git a/include/linux/sched.h b/include/linux/sched.h index 63d242164b..f2a56f64f7 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -869,6 +869,7 @@ struct task_struct { struct mm_struct *mm; struct mm_struct *active_mm; + struct address_space *faults_disabled_mapping; int exit_state; int exit_code; diff --git a/init/init_task.c b/init/init_task.c index ff6c4b9bfe..f703116e05 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -85,6 +85,7 @@ struct task_struct init_task .nr_cpus_allowed= NR_CPUS, .mm = NULL, .active_mm = &init_mm, + .faults_disabled_mapping = NULL, .restart_block = { .fn = do_no_restart_syscall, }, -- 2.40.1