Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp18881878rwd; Wed, 28 Jun 2023 01:52:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6QrJbzJuSpBy4N4yy7hrsFKRbqd3+uc+7Ebd+vayls+u/mER1SOpNKptZGSNbLKCkylXOZ X-Received: by 2002:a17:907:724c:b0:991:f427:2fdf with SMTP id ds12-20020a170907724c00b00991f4272fdfmr6188800ejc.76.1687942355049; Wed, 28 Jun 2023 01:52:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687942355; cv=none; d=google.com; s=arc-20160816; b=NHTsmiu5PvlL9XNPSg3jdHJ8UJ79U6E3o1F1VO8Lj4hXrUoriRv1XaAdUeKXu7ZRT3 BipU+bpBW48l5UEQiXLYJzYvKqlBxKgw7fHXbvVZqJ5lEngFWANXj/M+7Zy51xFarYVw qd3qpqevqSWz2b2LtAI4EAIzbV2JdLCcpOqyUsYDplYpojgKY5mFqCwnGXUhQ60eiP/h lYFq4bCyONawDEnx+z7pKgsGj4dfiSMzjVmhSxmEvXru3KjjN3SEH27W/OOKbNTtD/He 18vZAj4T6skRNuGzFxT5DhCR78auX5FEi8fkSLQuQsvChVfrCOJ5GhkOSj6wmy+lwfq+ FXuQ== 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=bHEtQ2wJjmvauaUesNJbaTs9laDotb9BZAcf/mJEhKc=; fh=XjfgmoRDrILL6w2Fv352UdLWQaGMeBLGa5ELCOloFxk=; b=RHChkypiXQDIiDLSODFnzeiav59YNxvmd4KOBKPt5P3iyPKWWFRaHhm+TyP0uBkm1f bCwe4Nas1cYqdB0EFqT2n8p3jqwFsuJAikcWToSUKq2Hzlup162t64Tc3HGVEbi89+GA 8QMX2mwUZlSHr4kewLAmIoMuGjnfmIlaYY189l7a9B742W2Gvx2hjCeOedH/5cycJ4cM MFnZKt+RYOk9J0lvUr/0yYc6ZCA2XISSXXKqSJ8nvnU5N/iqpmag3il2+yzG6l0cZgyg hNNZv8yXF8qB+VMe6ljhQLzgSeF847jBGA86pByjo1ljK/pyZJ9QbcKNUPkDu0V5ktPL qfnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=UrRITH5F; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mb18-20020a170906eb1200b00991df86ac0fsi3238682ejb.290.2023.06.28.01.52.11; Wed, 28 Jun 2023 01:52:35 -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=@google.com header.s=20221208 header.b=UrRITH5F; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231511AbjF1Hzs (ORCPT + 99 others); Wed, 28 Jun 2023 03:55:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229886AbjF1HyC (ORCPT ); Wed, 28 Jun 2023 03:54:02 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A4D030CF for ; Wed, 28 Jun 2023 00:53:02 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id d75a77b69052e-400aaafa491so26952381cf.1 for ; Wed, 28 Jun 2023 00:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687938781; x=1690530781; 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=bHEtQ2wJjmvauaUesNJbaTs9laDotb9BZAcf/mJEhKc=; b=UrRITH5F5erJnZCvCgYllutnJucSdobURz5+AUMvGBePprupmKDbiaCCgk/uAQrowh 0yJrd3TLSo98iS/yiVcgOnNIqhXz4CeF+gUpN+x43pIAcrEs2uZEy9tu47AuZK0cpAP2 PHIjRRBlXTTiPHi5FIz01BcZ64aXsRDRsesmZH3CTBoOs5UlBt5K4FzTpo9e/QfBaXix YK6hbQvvVQE8l/v3dLGYPV5vUFUcnhG+UaXvS27y/OBjdUKJegQrhGvq8P6RQETub3c5 3IdGdspofLdkPxsHzw3OqaZ/FDDXEHDoKiPegThRJEF2xhdu9GWhP+4vmYCU12war3J9 1vlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687938781; x=1690530781; 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=bHEtQ2wJjmvauaUesNJbaTs9laDotb9BZAcf/mJEhKc=; b=Ya4QZO4UFkM8yk3UZysh+6sJC9Ey4L28rSrllzKBdbNetEH1TI3jiGsTY+0EQGD9DH JuYnJqqY3ZPLNDah4FnAM2LKUORumdbw82pn/inICENXdFx4WxrfPOvoNEEk1zpjPA2E Z/ur/SbvLoJAbVZOVnucMfkyNwVhdCz9E+3oiAjaL7TfawhbbPaVmdG2Uhos38PzLJnP ir851z/xncF51mX+zv/kPwzaZxXcdlvQIXegX1/QIedfK1QIKXLoENycliefFdyIqwn5 XmskCzmH5GYDb8SD41UgelRzXUUTLFx8icfNLuoAk0lr10MFpyQqM3b7F5JJILd3HaMp u29Q== X-Gm-Message-State: AC+VfDyh3R0EymYkns3rZBYxg1k+W5pEIWepr++t9WIjnUA9K3cGCGe9 F9s2tYlonkm7TiMnQCQC1bqxEGneSocwsFtl05UM6UwrpcOJ9hRamoDSvQ== X-Received: by 2002:a5b:4c:0:b0:c17:afbd:71a9 with SMTP id e12-20020a5b004c000000b00c17afbd71a9mr8721564ybp.25.1687938415193; Wed, 28 Jun 2023 00:46:55 -0700 (PDT) MIME-Version: 1.0 References: <20230627-kanon-hievt-bfdb583ddaa6@brauner> <20230627-ausgaben-brauhaus-a33e292558d8@brauner> <20230628-meisennest-redlich-c09e79fde7f7@brauner> In-Reply-To: <20230628-meisennest-redlich-c09e79fde7f7@brauner> From: Suren Baghdasaryan Date: Wed, 28 Jun 2023 00:46:43 -0700 Message-ID: Subject: Re: [PATCH 1/2] kernfs: add kernfs_ops.free operation to free resources tied to the file To: Christian Brauner Cc: Tejun Heo , gregkh@linuxfoundation.org, peterz@infradead.org, lujialin4@huawei.com, lizefan.x@bytedance.com, hannes@cmpxchg.org, mingo@redhat.com, ebiggers@kernel.org, oleg@redhat.com, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL 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 Wed, Jun 28, 2023 at 12:26=E2=80=AFAM Christian Brauner wrote: > > On Tue, Jun 27, 2023 at 08:09:46PM -0700, Suren Baghdasaryan wrote: > > On Tue, Jun 27, 2023 at 6:54=E2=80=AFPM Tejun Heo wrote= : > > > > > > Hello, > > > > > > On Tue, Jun 27, 2023 at 02:58:08PM -0700, Suren Baghdasaryan wrote: > > > > Ok in kernfs_generic_poll() we are using kernfs_open_node.poll > > > > waitqueue head for polling and kernfs_open_node is freed from insid= e > > > > kernfs_unlink_open_file() which is called from kernfs_fop_release()= . > > > > So, it is destroyed only when the last fput() is done, unlike the > > > > ops->release() operation which we are using for destroying PSI > > > > trigger's waitqueue. So, it seems we still need an operation which > > > > would indicate that the file is truly going away. > > > > > > If we want to stay consistent with how kernfs behaves w.r.t. severing= , the > > > right thing to do would be preventing any future polling at severing = and > > > waking up everyone currently waiting, which sounds fine from cgroup b= ehavior > > > POV too. > > > > That's actually what we are currently doing for PSI triggers. > > ->release() is handled by cgroup_pressure_release() which signals the > > waiters, waits for RCU grace period to pass (per > > https://elixir.bootlin.com/linux/latest/source/include/linux/wait.h#L25= 8) > > and then releases all the trigger resources including the waitqueue > > head. However as reported in > > https://lore.kernel.org/all/20230613062306.101831-1-lujialin4@huawei.co= m > > this does not save us from the synchronous polling case: > > > > do_select > > vfs_poll > > cgroup_pressure_release > > psi_trigger_destroy > > wake_up_pollfree(&t->event_wait) -> unblocks vfs_poll > > synchronize_rcu() > > kfree(t) -> frees waitqueue head > > poll_freewait() > > -> uses waitqueue head > > > > > > This happens because we release the resources associated with the file > > while there are still file users (the file's refcount is non-zero). > > And that happens because kernfs can call ->release() before the last > > fput(). > > > > > > > > Now, the challenge is designing an interface which is difficult to ma= ke > > > mistake with. IOW, it'd be great if kernfs wraps poll call so that se= vering > > > is implemented without kernfs users doing anything, or at least make = it > > > pretty obvious what the correct usage pattern is. > > > > > > > Christian's suggestion to rename current ops->release() operation i= nto > > > > ops->drain() (or ops->flush() per Matthew's request) and introduce = a > > > > "new" ops->release() which is called only when the last fput() is d= one > > > > seems sane to me. Would everyone be happy with that approach? > > > > > > I'm not sure I'd go there. The contract is that once ->release() is c= alled, > > > the code backing that file can go away (e.g. rmmod'd). It really shou= ld > > > behave just like the last put from kernfs users' POV. > > > > I 100% agree with the above statement. > > > > > For this specific fix, > > > it's safe because we know the ops is always built into the kernel and= won't > > > go away but it'd be really bad if the interface says "this is a norma= l thing > > > to do". We'd be calling into rmmod'd text pages in no time. > > > > > > So, I mean, even for temporary fix, we have to make it abundantly cle= ar that > > > this is not for usual usage and can only be used if the code backing = the ops > > > is built into the kernel and so on. > > > > I think the root cause of this problem is that ->release() in kernfs > > does not adhere to the common rule that ->release() is called only > > when the file is going away and has no users left. Am I wrong? > > So imho, ultimately this all comes down to rmdir() having special > semantics in kernfs. On any regular filesystem an rmdir() on a directory > which is still referenced by a struct file doesn't trigger an > f_op->release() operation. It's just that directory is unlinked and > you get some sort of errno like ENOENT when you try to create new files > in there or whatever. The actual f_op->release) however is triggered > on last fput(). > > But in essence, kernfs treats an rmdir() operation as being equivalent > to a final fput() such that it somehow magically kills all file > references. And that's just wrong and not supported. Thanks for the explanation, Christian! If kernfs is special and needs different rules for calling f_op->release() then fine, but I need an operation which tells me there are no users of the file so that I can free the resources. What's the best way to do that? > > -- > To unsubscribe from this group and stop receiving emails from it, send an= email to kernel-team+unsubscribe@android.com. >