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 X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FFDAC433E1 for ; Mon, 20 Jul 2020 00:38:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2E09E22D07 for ; Mon, 20 Jul 2020 00:38:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mKUpi/C/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2E09E22D07 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9CA736B0003; Sun, 19 Jul 2020 20:38:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 954888D0002; Sun, 19 Jul 2020 20:38:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F5D26B0006; Sun, 19 Jul 2020 20:38:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0135.hostedemail.com [216.40.44.135]) by kanga.kvack.org (Postfix) with ESMTP id 675396B0003 for ; Sun, 19 Jul 2020 20:38:22 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2A3F8110F22 for ; Mon, 20 Jul 2020 00:38:22 +0000 (UTC) X-FDA: 77056592844.26.ship01_4b17dbf26f20 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id EF84A18021E24 for ; Mon, 20 Jul 2020 00:38:21 +0000 (UTC) X-HE-Tag: ship01_4b17dbf26f20 X-Filterd-Recvd-Size: 5224 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Mon, 20 Jul 2020 00:38:21 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id w2so9495324pgg.10 for ; Sun, 19 Jul 2020 17:38:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:subject:to:cc:mime-version:message-id :content-transfer-encoding; bh=XFq1tbFsgX2AZdMgYvw8N+gQKnN3+VgEwdSA0SX4/tw=; b=mKUpi/C/VrbkHgaAlfwHD+1TzhYa5ul9sEEkCLJouYkaK5jdWeGoRgaXfbw07dzvX2 NzyjGf4e8K0QeJ2EDCqXqSQJIOSUgchdsOZcx6pUC26pIDCvP5XCwEsy4wIRtUaVmzrr kPZGnmnUm1qxmh2p4NkAaXSIxKcrHlAWA1EfQ2ayfOL3U1OuCne6OD72Yug7GE646uuP Mk9QZhIb+UM3SQFn5yCYG0ubBU7qfQjsAnANXBJdGyHYKz4U9e3x9t0aJFNQLYwVTGzN MY3+Hn7teHGvhHaBOLkQmK16Rj4yVCZEc1OdpXfMwapUOQoZyox8ZEh2W2gj5N/T//Hn Vh6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:subject:to:cc:mime-version:message-id :content-transfer-encoding; bh=XFq1tbFsgX2AZdMgYvw8N+gQKnN3+VgEwdSA0SX4/tw=; b=W7MDSN0GuXJ0aAnLLXcwHF4y4lXbrQe0asCpju5EeAYZqcigH9LBu8PGul51BpJNa9 73omM0aOGvd0KAyqyqBMKP86CTAU6GrQZhz770XIfapH6Mly7PG5QdWEEljMvZ1t2Z1t NDbMKIENng9YoKIgtb9VBEmd2f9ZksAHWNKoFV1oCzZ/d4aYIvKYYicr9bWzA9sZ9A0V HBr9SkyvOkBy3GOs3sGH/DbxPU39tamIVDdv8weuhCJ0bZDYJkNDvL7EDQgM3wdJPjNK kUQFgQEWQGcXUQGFntT9dC1Q4lAMJNl2JGYny8Vbe3MH3gVFlIuixjb0Qz+6jJZh4SUY aT4A== X-Gm-Message-State: AOAM532HLtTKk+U6kh6If8LNiCCZfhV3la5pD+0lteT2TnzB4Bqsl4+K YamLxMPrUs+rYdQeW8EMsIw= X-Google-Smtp-Source: ABdhPJwKnuIipkmIajupZTNxeEfnOBQT2VjNliL94btuuxQncHtcH3EQkViU0lPNZOjdJTZ6aogM6g== X-Received: by 2002:a05:6a00:1596:: with SMTP id u22mr17321339pfk.102.1595205500660; Sun, 19 Jul 2020 17:38:20 -0700 (PDT) Received: from localhost (110-174-173-27.tpgi.com.au. [110.174.173.27]) by smtp.gmail.com with ESMTPSA id z11sm14633320pfk.46.2020.07.19.17.38.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jul 2020 17:38:20 -0700 (PDT) Date: Mon, 20 Jul 2020 10:38:15 +1000 From: Nicholas Piggin Subject: io_uring kthread_use_mm / mmget_not_zero possible abuse To: Jens Axboe , "David S. Miller" Cc: linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, sparclinux@vger.kernel.org, linuxppc-dev@lists.ozlabs.org MIME-Version: 1.0 Message-Id: <1595203632.x8vplwce1a.astroid@bobo.none> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: EF84A18021E24 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: When I last looked at this (predating io_uring), as far as I remember it wa= s=20 not permitted to actually switch to (use_mm) an mm user context that was=20 pinned with mmget_not_zero. Those pins were only allowed to look at page=20 tables, vmas, etc., but not actually run the CPU in that mm context. sparc/kernel/smp_64.c depends heavily on this, e.g., void smp_flush_tlb_mm(struct mm_struct *mm) { u32 ctx =3D CTX_HWBITS(mm->context); int cpu =3D get_cpu(); if (atomic_read(&mm->mm_users) =3D=3D 1) { cpumask_copy(mm_cpumask(mm), cpumask_of(cpu)); goto local_flush_and_out; } smp_cross_call_masked(&xcall_flush_tlb_mm, ctx, 0, 0, mm_cpumask(mm)); local_flush_and_out: __flush_tlb_mm(ctx, SECONDARY_CONTEXT); put_cpu(); } If a kthread comes in concurrently between the mm_users test and the=20 mm_cpumask reset, and does mmget_not_zero(); kthread_use_mm() then we have=20 another CPU switched to mm context but not in the mm_cpumask. It's then=20 possible for our thread to schedule on that CPU and not go through a=20 switch_mm (because kthread_unuse_mm will make it lazy, then we can switch=20 back to our user thread and un-lazy it). powerpc has something similar. I don't think this is documented anywhere and certainly isn't checked for=20 unfortunately, so I don't really blame io_uring. The simplest fix is for io_uring to carry mm_users references. If that can'= t=20 be done or we decide to lift the limitation on mmget_not_zero references, w= e=20 can come up with a way to synchronize things. On powerpc for example, we IPI all targets in mm_cpumask before clearing=20 them, so we could disable interrupts while kthread_use_mm does the mm switc= h=20 sequence, and have the IPI handler check that current->mm hasn't been set t= o=20 mm, for example. sparc is a bit harder because it doesn't IPI targets if it thinks it can=20 avoid it. But powerpc found that just doing one IPI isn't a big burden here= =20 so maybe we change sparc to do that too. I would be inclined to fix this=20 mmget_not_zero quirk if we can, unless someone has a very good way to test=20 and enforce it, it'll just happen again. Comments? Thanks, Nick