Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3521446rwi; Fri, 21 Oct 2022 18:10:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Kj0hvPiAkU5BbR4BUPoIgCmcYWIF7nSEFxNew3R0I8LDciKgU9U08dC2b38gpJ3XjmAWY X-Received: by 2002:a17:907:7245:b0:78d:ec9c:e743 with SMTP id ds5-20020a170907724500b0078dec9ce743mr18384559ejc.466.1666401045098; Fri, 21 Oct 2022 18:10:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666401045; cv=none; d=google.com; s=arc-20160816; b=h/cWt4Xd8gnKnTrj6LQ90cxt3pGh4OpERLNxy1YfmDSZkL5Ri8FuBEZpQM1BwPblAx u+Ktvxqsbobrb9VyhBpHX6kburKdcH7c2C92GqOwVkI7NEUO110W63U0o27KpUlggIVp 9QJfIHXckOOhdkpmsAhgnoc9V5wfBQpEa+ZS+ad3ObXnMai2UpG5ftGYFpiE/d31YkVR JfVTZcBclBNmocdXP0OBvAjaRbJ8WLKWiy36BWRjvlQ+CzXrAJ8eXRLt5AXqrt4gpWdq FvONcWSfN+pOpCZQ9u6EVvF7GQ3nKEGFoWOAjs2nM6z1UqW/CdTlDRJUlyCiHGnLX87n YLrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=amHJWELNNBFQ5oGfku8rGjqSoo/TG8P6T1HoY4z+rSA=; b=ZY4wtDWLpkbawAcUBOcssta5hCCFJNOYDOYxErMGH/cVXAf+dYZpnNhkzB/XnlRg4j AABml2N/z4mz1uBE+vCIv0RblAg13+xMmJtifRRRSlmyzdXUUdgwNoMvq/KY1eSuVWcE IlIWL2j6NUtxmVmzqlBcettGRAJ32n9kBO/zCQQLdneG5fFXOUwzBg30AJhmqjh2GZX6 +2Q6Xf+EOVjxpKJqSVFgdrdG4b/h9Ic3Piz9x88MQH5G1TF/aUWKZw4kC82ULYQYpjdk fLGUa+4Ritl8OmCq45T+IUcr/MRk+HEw3ulYE4TSy0ScyuOdgUvOxWAbe+zP8Mh7DyKO VqhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ShXwyvRF; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r8-20020a170906280800b00782e437a368si17380954ejc.160.2022.10.21.18.10.20; Fri, 21 Oct 2022 18:10:45 -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=@redhat.com header.s=mimecast20190719 header.b=ShXwyvRF; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229838AbiJVARw (ORCPT + 99 others); Fri, 21 Oct 2022 20:17:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229615AbiJVARt (ORCPT ); Fri, 21 Oct 2022 20:17:49 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C96402B02DD for ; Fri, 21 Oct 2022 17:17:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666397867; 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=amHJWELNNBFQ5oGfku8rGjqSoo/TG8P6T1HoY4z+rSA=; b=ShXwyvRFgM20zqZxey5KxO3J3NJwc/IG2ppXRMJCwK0Ffks2xU9Fl0FxROo4UOb4o0arOg RQAe1WCy9O5+iq8iDN41WLkUnGHllV6KWhHw4/MyzUqrn+5bOwi857IvmFAh6QfWtNUObM ZzIhawpFB00u3HS7iK/AjOO/RUqwGgQ= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-635-Te_rBxY_MxiW_LgZ7l5WRw-1; Fri, 21 Oct 2022 20:17:46 -0400 X-MC-Unique: Te_rBxY_MxiW_LgZ7l5WRw-1 Received: by mail-qk1-f200.google.com with SMTP id az32-20020a05620a172000b006ee8ae138a7so4705571qkb.4 for ; Fri, 21 Oct 2022 17:17:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=amHJWELNNBFQ5oGfku8rGjqSoo/TG8P6T1HoY4z+rSA=; b=odm28P5DB5gfyVQrrIS8f0PYFeqJUzPSXPO1y0mD1VdkqTP0CnHy+Zp+ewGK8ux7NI O066lo+0TB7GUZl4waQvPrzlq7KwrxMjGUYG6nMF2Le/NlOl7hUVycb9UC5UaWfif8KX 59QFP7Hw40x145BHK8plHWFMryxcJoKy/hQyxOA9mHG0E2Vq60UUeEwOZFvo66SfGWKe F5+UE5JaQnWtmfg86/2JynL7bnsWOGJejpNyyirE5aA8nZL1NBnCW8yevfeBZqwSUitP vJ9m4apI/sVCe7d2MKUyw19smoqWd7+Od2g5yjUnRagsCinNlhhjkiwMG55mcB5FmLXK LXgg== X-Gm-Message-State: ACrzQf2bwfbpkWPcaS9r5eWdjFkhLsob/+ifIkNDrFP0bvOLKa5bV8X6 6ghS6IS5AvPAg8K1mLSHRUM0K3VZhQ3TrigsGBoRIe1gaZvtvjyXs6twSnqwCN9GNEmVjX6pYe9 MZo2uQa+iHEr4J6+xH704lkNJ X-Received: by 2002:a05:620a:2809:b0:6bc:5e0d:d7b1 with SMTP id f9-20020a05620a280900b006bc5e0dd7b1mr16107999qkp.545.1666397865900; Fri, 21 Oct 2022 17:17:45 -0700 (PDT) X-Received: by 2002:a05:620a:2809:b0:6bc:5e0d:d7b1 with SMTP id f9-20020a05620a280900b006bc5e0dd7b1mr16107981qkp.545.1666397865638; Fri, 21 Oct 2022 17:17:45 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id y21-20020a05620a44d500b006eed75805a2sm10874152qkp.126.2022.10.21.17.17.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Oct 2022 17:17:44 -0700 (PDT) Date: Fri, 21 Oct 2022 20:17:43 -0400 From: Peter Xu To: Ira Weiny Cc: Andrew Morton , Matthew Wilcox , kernel test robot , "Fabio M. De Francesco" , lkp@lists.01.org, lkp@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrea Arcangeli Subject: Re: [shmem] 7a7256d5f5: WARNING:possible_recursive_locking_detected Message-ID: References: <202210211215.9dc6efb5-yujie.liu@intel.com> <20221021133041.5811e1fb291eb6aaf122a59c@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 On Fri, Oct 21, 2022 at 05:00:24PM -0700, Ira Weiny wrote: > On Fri, Oct 21, 2022 at 03:48:57PM -0700, Ira wrote: > > On Fri, Oct 21, 2022 at 01:30:41PM -0700, Andrew Morton wrote: > > > On Fri, 21 Oct 2022 14:09:16 +0100 Matthew Wilcox wrote: > > > > > > > On Fri, Oct 21, 2022 at 12:10:17PM +0800, kernel test robot wrote: > > > > > FYI, we noticed WARNING:possible_recursive_locking_detected due to commit (built with gcc-11): > > > > > > > > > > commit: 7a7256d5f512b6c17957df7f59cf5e281b3ddba3 ("shmem: convert shmem_mfill_atomic_pte() to use a folio") > > > > > https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git master > > > > > > > > Ummm. Looks to me like this now occurs because of this part of the > > > > change: > > > > > > > > if (!zeropage) { /* COPY */ > > > > - page_kaddr = kmap_atomic(page); > > > > + page_kaddr = kmap_local_folio(folio, 0); > > > > ret = copy_from_user(page_kaddr, > > > > (const void __user *)src_addr, > > > > PAGE_SIZE); > > > > - kunmap_atomic(page_kaddr); > > > > + kunmap_local(page_kaddr); > > > > > > > > Should I be using __copy_from_user_inatomic() here? > > > > I would say not. I'm curious why copy_from_user() was safe (at least did not > > fail the checkers). :-/ > > > > > > > > Caller __mcopy_atomic() is holding mmap_read_lock(dst_mm) and this > > > copy_from_user() calls > > > might_fault()->might_lock_read(current->mm->mmap_lock). > > > > > > And I guess might_lock_read() gets upset because we're holding another > > > mm's mmap_lock. Which sounds OK to me, unless a concurrent > > > mmap_write_lock() could jam things up. > > > > > > But I cannot see why your patch would suddenly trigger this warning - > > > kmap_local_folio() and kmap_atomic() are basically the same thing. > > > > It is related to your patch but I think what you did made sense on the surface. > > > > On the surface copy_from_user() should not require pagefaults to be disabled. > > But that side affect of kmap_atomic() was being used here because it looks like > > the code is designed to fallback if the fault was not allowed:[1] > > > > mm/shmem.c > > ... > > page_kaddr = kmap_local_folio(folio, 0); > > ret = copy_from_user(page_kaddr, > > (const void __user *)src_addr, > > PAGE_SIZE); > > kunmap_local(page_kaddr); > > > > /* fallback to copy_from_user outside mmap_lock */ > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > if (unlikely(ret)) { > > *pagep = &folio->page; > > ret = -ENOENT; > > /* don't free the page */ > > goto out_unacct_blocks; > > } > > ... > > > > So this is one of those rare places where the kmap_atomic() side effects were > > being depended on... :-( > > > > [1] might_fault() does not actually mean the code completes the fault. > > > > mm/memory.c > > ... > > void __might_fault(const char *file, int line) > > { > > if (pagefault_disabled()) > > return; > > ... > > > > > > > > I see that __mcopy_atomic() is using plain old kmap(), perhaps to work > > > around this? But that's 2015 code and I'm not sure we had such > > > detailed lock checking in those days. > > > > No kmap() can't work around this. That works because the lock is released just > > above that. > > > > mm/userfaultfd.c > > ... > > mmap_read_unlock(dst_mm); > > BUG_ON(!page); > > > > page_kaddr = kmap(page); > > err = copy_from_user(page_kaddr, > > (const void __user *) src_addr, > > PAGE_SIZE); > > kunmap(page); > > ... > > > > So I think the correct solution is below because we want to prevent the page > > fault. > > I was about to get this patch ready to send when I found this: > > commit b6ebaedb4cb1a18220ae626c3a9e184ee39dd248 > Author: Andrea Arcangeli > Date: Fri Sep 4 15:47:08 2015 -0700 > > userfaultfd: avoid mmap_sem read recursion in mcopy_atomic > > If the rwsem starves writers it wasn't strictly a bug but lockdep > doesn't like it and this avoids depending on lowlevel implementation > details of the lock. > > [akpm@linux-foundation.org: delete weird BUILD_BUG_ON()] > Signed-off-by: Andrea Arcangeli > Acked-by: Pavel Emelyanov > ... > > So I wonder if the true fix is something to lockdep? I think lockdep used to complain because we can be taking the same mmap_sem twice in this case (the 2nd one during the useraddr page fault). So to answer the other question - yeah the current->mm and dest_mm can definitely be the same one in this context. > > Regardless I'll send the below patch because it will restore things to a > working order. > > But I'm CC'ing Andrea for comments. Open-code disabling of pagefault sounds okay to me. pagefault_disable() used to be covering the kmap procedure too as done in kmap_atomic(), but frankly I don't know whether there's a real difference. Yeah, let's see whether we can get a confirmation from Andrea. Thanks, -- Peter Xu