Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp30341124rwd; Thu, 6 Jul 2023 04:40:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlG4jBwcqskE6gnbGfgWwMokhggKQRhYrEI6oDs4YzxuRizPDl3THmaKiZJHtPiCi9MABe3e X-Received: by 2002:a05:6870:472c:b0:1b0:800:6c01 with SMTP id b44-20020a056870472c00b001b008006c01mr2493500oaq.9.1688643653636; Thu, 06 Jul 2023 04:40:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688643653; cv=none; d=google.com; s=arc-20160816; b=05M50z4WoN8Ei7f9h+rGrwcJaC0TNy9nFP+9J55vrW72e6K1EvmSPh6Ec9R9sGw9u3 WbxAjoROPjC7mHHd73evNKLrDuPxV97oV0lm6iD1KeqqKsn1sxB+Gzho74zuvtGUUiwh mzMBaAGNb+FPn9z2mjJAQIDXU1MFHArNgcRrwG7wFJGg4RScunnKpTSrwNzABFogV5LH LZRRmqDzytIW1tcD2tWGwcWdbRjLOjj/1GeHMeYInRRGkMbpeLexv2ANLt56Ta3ZUr4X tAlOXAGqhEa9GJFMoGTJkXN0NmvjZ11vHVZPu/ojtTW75ja3nualLWag3vmRC0wSG4Fe OUHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=YUOZEoZh3tniSwu0Lc1VH/leg3NuusrIfXtvdOdkp+U=; fh=VJgmlQrJi9YJCzNHXyCofI9+B+LhD0sPbwbQiPf7QIk=; b=pd/K1CCVCu9RvMxfUjBrfwcXmkKTAEscNfsst5eH4Bjh1EET4N5SvIhdjIaHGaAGtA 16IigPJ9kfSNPV+xo/fqsQ8ws5kjaf4bT3Oo4C0YnztjVmiYa/4F3oROn+tO9UYEUoLr /iT8NRsS+N5ev4EOZtQ+2+7b3Fg5MUDdBo+YooTIL8pfr2iIqO9DpNkWF+Eh9dIYSme9 x9mY1ZUiq9rUaVMl5bKCgo1xBID8zwA6egO2fFpBA7Q2kL/+WcD7McAFIenDVI0yCmdE qMrcmII27BJi3veoL7KbxeCCtluOdjtv0fJgMXtW6MxgEFWPnbU1dwPdabrr3YhGLUuw ml1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GAP+5qy6; 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 pg8-20020a17090b1e0800b00263220254e2si1497142pjb.41.2023.07.06.04.40.38; Thu, 06 Jul 2023 04:40:53 -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=GAP+5qy6; 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 S232307AbjGFLcZ (ORCPT + 99 others); Thu, 6 Jul 2023 07:32:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232341AbjGFLcN (ORCPT ); Thu, 6 Jul 2023 07:32:13 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D20491BE8 for ; Thu, 6 Jul 2023 04:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688643080; 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=YUOZEoZh3tniSwu0Lc1VH/leg3NuusrIfXtvdOdkp+U=; b=GAP+5qy64HiGUi/Yu5pzuSNUHxUdy2i1KEhlFHSa2K6iIvdSUDWtwUtVlRRPZ+r/7DFCGg g/8n3HaOrYserd6z5k3RRT+9Kz5AWCCG/1XXdqNUevIu1omw3T43Ckr+7EfBcyj8Hj8BVE yaaO4LkKDIIT4Y7OdRWsYc4f26pBl9E= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-468-fXG2ff8bM7GPqRr24tPn1g-1; Thu, 06 Jul 2023 07:31:19 -0400 X-MC-Unique: fXG2ff8bM7GPqRr24tPn1g-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-62de65b3a5bso8368016d6.2 for ; Thu, 06 Jul 2023 04:31:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688643079; x=1691235079; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YUOZEoZh3tniSwu0Lc1VH/leg3NuusrIfXtvdOdkp+U=; b=bFlggXmLRgh3FBa5boYxXXxM8ajrZu3Th6oZkNaMysLfpvqcgVqKk2R+WsoK+2diHv ZQdUEDUSVmrenSVj216iTcsVtu6UeMFIp09h9v2tt7fNVjF9Yhy+rIGVM3e0cWNuk/iI pKVD/0tafkxnLMhE4Dkp4Hk+KDQal5BB84NWKrkL/ME4lKTSHiT1+CLWroSUiv+jdRPi xrBj3WIv0bcARCzXT1piQBCl4Stg4KcuBcAN/wRPbTrp2pzoid8ZyjdF8LV8CFaHJbgt qVtMkuWyoYQzpf51nnv2wbSZxceosN/2VKrVf4XNXiPYyE6x8clwofLm45DjYyYGT66B N8pQ== X-Gm-Message-State: ABy/qLZWHIrwp5ci0apIcXDipek0hYIevAu0tf7FhRzspXGbyQZ5LHqv 6tqJDyekdR4LYf5WK/T0Sh8PdCGYSglqxEzDGiOTIvaJPG9vNKOvKufTQO4v4RAJC4/F1vEF0pv OlWSQviFc3NqJ+ChitA4E1I0J X-Received: by 2002:a0c:f014:0:b0:636:e56c:eedb with SMTP id z20-20020a0cf014000000b00636e56ceedbmr1572764qvk.34.1688643079066; Thu, 06 Jul 2023 04:31:19 -0700 (PDT) X-Received: by 2002:a0c:f014:0:b0:636:e56c:eedb with SMTP id z20-20020a0cf014000000b00636e56ceedbmr1572731qvk.34.1688643078801; Thu, 06 Jul 2023 04:31:18 -0700 (PDT) Received: from vschneid.remote.csb ([154.57.232.159]) by smtp.gmail.com with ESMTPSA id oo23-20020a05620a531700b007673f8803c3sm624918qkn.96.2023.07.06.04.31.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jul 2023 04:31:18 -0700 (PDT) From: Valentin Schneider To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, x86@kernel.org, Nicolas Saenz Julienne , Steven Rostedt , Masami Hiramatsu , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Paolo Bonzini , Wanpeng Li , Vitaly Kuznetsov , Andy Lutomirski , Frederic Weisbecker , "Paul E. McKenney" , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , Kees Cook , Sami Tolvanen , Ard Biesheuvel , Nicholas Piggin , Juerg Haefliger , Nicolas Saenz Julienne , "Kirill A. Shutemov" , Nadav Amit , Dan Carpenter , Chuang Wang , Yang Jihong , Petr Mladek , "Jason A. Donenfeld" , Song Liu , Julian Pidancet , Tom Lendacky , Dionna Glaze , Thomas =?utf-8?Q?Wei=C3=9Fschuh?= , Juri Lelli , Daniel Bristot de Oliveira , Marcelo Tosatti , Yair Podemsky Subject: Re: [RFC PATCH 11/14] context-tracking: Introduce work deferral infrastructure In-Reply-To: <20230705223956.GD2813335@hirez.programming.kicks-ass.net> References: <20230705181256.3539027-1-vschneid@redhat.com> <20230705181256.3539027-12-vschneid@redhat.com> <20230705223956.GD2813335@hirez.programming.kicks-ass.net> Date: Thu, 06 Jul 2023 12:31:11 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.1 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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 On 06/07/23 00:39, Peter Zijlstra wrote: > On Wed, Jul 05, 2023 at 07:12:53PM +0100, Valentin Schneider wrote: > >> Note: A previous approach by PeterZ [1] used an extra bit in >> context_tracking.state to flag the presence of deferred callbacks to >> execute, and the actual callbacks were stored in a separate atomic >> variable. >> >> This meant that the atomic read of context_tracking.state was sufficient to >> determine whether there are any deferred callbacks to execute. >> Unfortunately, it presents a race window. Consider the work setting >> function as: >> >> preempt_disable(); >> seq = atomic_read(&ct->seq); >> if (__context_tracking_seq_in_user(seq)) { >> /* ctrl-dep */ >> atomic_or(work, &ct->work); >> ret = atomic_try_cmpxchg(&ct->seq, &seq, seq|CT_SEQ_WORK); >> } >> preempt_enable(); >> >> return ret; >> >> Then the following can happen: >> >> CPUx CPUy >> CT_SEQ_WORK \in context_tracking.state >> atomic_or(WORK_N, &ct->work); >> ct_kernel_enter() >> ct_state_inc(); >> atomic_try_cmpxchg(&ct->seq, &seq, seq|CT_SEQ_WORK); >> >> The cmpxchg() would fail, ultimately causing an IPI for WORK_N to be >> sent. Unfortunately, the work bit would remain set, and it can't be sanely >> cleared in case another CPU set it concurrently - this would ultimately >> lead to a double execution of the callback, one as a deferred callback and >> one in the IPI. As not all IPI callbacks are idempotent, this is >> undesirable. > > So adding another atomic is arguably worse. > > The thing is, if the NOHZ_FULL CPU is actually doing context transitions > (SYSCALLs etc..) then everything is fundamentally racy, there is no > winning that game, we could find the remote CPU is in-kernel, send an > IPI, the remote CPU does return-to-user and receives the IPI. > > And then the USER is upset... because he got an IPI. > Yeah, that part is inevitably racy. The thing I was especially worried about was the potential double executions (once in IPI, again in deferred work). It's not /too/ bad as the only two deferred callbacks I'm introducing here are costly-but-stateless, but IMO is a bad foundation. But it seems like we can reuse the existing atomic and squeeze some bits in there, so let's see how that goes :-)