Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5360472rdb; Wed, 13 Dec 2023 06:41:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IEywhf8Ki2AGolhNKiCBD9FHs5aWFInswrsz+f77kwxZ66lV7pHgWpndkO1iYWme1EWTzuj X-Received: by 2002:a17:902:d50d:b0:1d0:bfb7:6700 with SMTP id b13-20020a170902d50d00b001d0bfb76700mr11657010plg.58.1702478493563; Wed, 13 Dec 2023 06:41:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702478493; cv=none; d=google.com; s=arc-20160816; b=08DkyswS2FlPPdkyXw+qY9BoalOlyfinKy3OH6mE8lqXc8QMRfMBw4/gwWoHgQySZt xdUuDm2LJNbrYfSMGgmIGe1mD5DPTMhB2mIldMKKaRUKzTrLFLg/RYu7pkG6s/fatsRR c4aPfOUaIPg4RNHTgQQz2QAlys68dqixmjKfNVdOzt9xEJrnUZjwgRh54jxFFKDs/2oT lZkgG9bINJILrNXTvTzwDGPsfIyEAdFHiMcNw9V878s3SSjIKBIzfjIapPaCss/NB1YY NW9mExYMUpmGBCqNLtrETENBiP+n+JCsbXU7jq7US3+jkGGLMmaMf3VsJfcOkSbqs9sW nlAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=t3mJDSsBd+gLTGFYfrkyu9tBgFHp0rV5RVCzftSRiIQ=; fh=F6+FGML87yHqRev/l5aFDmOtQouYoX3HLtF8iNVpDI4=; b=w5mN5RN+TwR7anDgFT7TiATjhox/QnlQ+cGFTFQni4jTV3LNthdKQnbTeDTO155WFI PrZdnozTiDHfSToEMv4o4qQpEeDALcX3FYqlfztrG4ibptA5Ag+OyoXxmn7IH2X55gvS wiWbxQB9UIZ9k3OxyuVLOWe02WhEaJ14wzjpIVt/B9XI/+T+4iohgFK2DitITah/k1cp KVUAFP8AYTZbXCkS9mGIz5Lppmdy7ZG2GHtevV4qNLVsnmLOrPRf0oOx37CeZpIkx7/e IxT3dR0SDheMjiRwJql8GxSNa0NrY6i9PkJP0E05z9vuFWsZBV2yt/S+SmAyo25HPKBO stiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JRDdaQ2B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id x65-20020a636344000000b005b928e39429si9672537pgb.259.2023.12.13.06.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 06:41:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JRDdaQ2B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id DADA08158300; Wed, 13 Dec 2023 06:41:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442106AbjLMOk7 (ORCPT + 99 others); Wed, 13 Dec 2023 09:40:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442012AbjLMOkk (ORCPT ); Wed, 13 Dec 2023 09:40:40 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37A7EDD for ; Wed, 13 Dec 2023 06:40:46 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d32c5ce32eso22318665ad.0 for ; Wed, 13 Dec 2023 06:40:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702478445; x=1703083245; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=t3mJDSsBd+gLTGFYfrkyu9tBgFHp0rV5RVCzftSRiIQ=; b=JRDdaQ2B8FQu3KlKWYs8F6bw6Hci22Zt6KSu8+h5085vm02ORdC+xT7gMKPvMxmkiN ivnjb+CLoh5sLw1tCoaVyDM7cjXd103faHI1wwzfv2KgmUXJhBX1PtVK4xfa0gBQ5iav C00PPhVfcbO1rBftn8plUb24x2BrGVDnGOunB+43Wn8Lq4lSMDWLF07lWnzF8sL3E25f zP2QpImegtl5gLmoAWdPHjJpGI86k9ZA5imCxpmXBeD3n9kUITL7ywBSVnvc93Vzb/Gl GhsSG0W93y5EiAVv3LrGBOAY1x2u+SkwebyL/eobzovY7Z7gmSetS+YQ7w18Dcn6M22V m+5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702478445; x=1703083245; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t3mJDSsBd+gLTGFYfrkyu9tBgFHp0rV5RVCzftSRiIQ=; b=ZCxeMtbAdFrG4v8fwubEoKVHBpCBu6tPcmqiS1KcuAEUPNeQETPKAG7+OrlBOC4/JR djnwrsA2rK++gWM9C3gYQioSuq0c5sOvQcEGCsx57U6IF51JZl2sFmz4mV/OVp+dB7qw cfF+OSmgHDSWuxe7emHFDkFvIiTPjoDovzSRvBTiXChtu+jUPkZehjHukqcl2UHAVW0m AuyJNKIePGZ+JoKFdjfjtwbjXULvm//0TyzqQGIQnixVg8ZjNfM/J4EUSOCn/w2gejAo LVVEn1o5Bg7cMZGKT1BLi9AryrBjA4tp0Vk/nzB3Hyjxf9gJ9Jl8f1inn2WAEVl0F4NL U8VA== X-Gm-Message-State: AOJu0YyZu8IFlXwbeAGRIKuCTO418gs+aprGwGGcOW2oYwnokNUuOUTo kJc9ITfvveDtjwQoR9SJ+Xk9bvAhpGrsYsIW4Nc= X-Received: by 2002:a17:90b:e0e:b0:28a:dcda:a101 with SMTP id ge14-20020a17090b0e0e00b0028adcdaa101mr1926855pjb.47.1702478445662; Wed, 13 Dec 2023 06:40:45 -0800 (PST) MIME-Version: 1.0 References: <432a89fafce11244287c8af757e73a2eb22a5354.1702339432.git.andreyknvl@google.com> In-Reply-To: From: Andrey Konovalov Date: Wed, 13 Dec 2023 15:40:34 +0100 Message-ID: Subject: Re: [PATCH mm 2/4] kasan: handle concurrent kasan_record_aux_stack calls To: Marco Elver Cc: andrey.konovalov@linux.dev, Andrew Morton , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Tetsuo Handa , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov , syzbot+186b55175d8360728234@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,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 pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 13 Dec 2023 06:41:28 -0800 (PST) On Tue, Dec 12, 2023 at 8:29=E2=80=AFPM Marco Elver wrot= e: > > > - stack_depot_put(alloc_meta->aux_stack[1]); > > + new_handle =3D kasan_save_stack(0, depot_flags); > > + > > + spin_lock_irqsave(&aux_lock, flags); > > This is a unnecessary global lock. What's the problem here? As far as > I can understand a race is possible where we may end up with > duplicated or lost stack handles. Yes, this is the problem. And this leads to refcount underflows in the stack depot code, as we fail to keep precise track of the stack traces. > Since storing this information is best effort anyway, and bugs are > rare, a global lock protecting this is overkill. > > I'd just accept the racyness and use READ_ONCE() / WRITE_ONCE() just > to make sure we don't tear any reads/writes and the depot handles are > valid. This will help with the potential tears but will not help with the refcount issues. > There are other more complex schemes [1], but I think they are > overkill as well. > > [1]: Since a depot stack handle is just an u32, we can have a > > union { > depot_stack_handle_t handles[2]; > atomic64_t atomic_handle; > } aux_stack; > (BUILD_BUG_ON somewhere if sizeof handles and atomic_handle mismatch.) > > Then in the code here create the same union and load atomic_handle. > Swap handle[1] into handle[0] and write the new one in handles[1]. > Then do a cmpxchg loop to store the new atomic_handle. This approach should work. If you prefer, I can do this instead of a spinlo= ck. But we do need some kind of atomicity while rotating the aux handles to make sure nothing gets lost. Thanks!