From nobody Sat Apr 20 01:38:51 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19B8FC25B06 for ; Sat, 6 Aug 2022 07:44:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241649AbiHFHov (ORCPT ); Sat, 6 Aug 2022 03:44:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241461AbiHFHor (ORCPT ); Sat, 6 Aug 2022 03:44:47 -0400 Received: from sender-of-o53.zoho.in (sender-of-o53.zoho.in [103.117.158.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABE3911C37 for ; Sat, 6 Aug 2022 00:44:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659771854; cv=none; d=zohomail.in; s=zohoarc; b=OyqiYSCCh3C8uVr2eRNnqEcgNmJCAo5kB/fZrYFxYEr3NXXK/vQ8sP0VEmCVEnfGdyv4O69kP8GZoaAFEQ9Bm4trftPckv3JdSgjTAI8nEWlta/GXrcqoBxf2EaKvxMGGBOIP4/rGBOY5EHIX7lCOS4WEZRqRjUNCMPFdlIXKaI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1659771854; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=I7Q4D57L0Fon5Y8OnWeD+j+i4yBdJ+wVG0hPi+xhFQ8=; b=UPI7h69+BeNNjIxPU3XSK6PFbBKYtLl4YacsgEsnk8XnHtUDYNGHup1EHLaK7DmqLblxBpOiw+sRA5AvbqakctBMOR0EO5yX6bZBje+wRmR8YX7YEoCizViax2NwQfmNIXTe3UWBJkJpz4Y3VUfLWt2HLpWNVwuinaQYG52T2rY= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1659771854; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=I7Q4D57L0Fon5Y8OnWeD+j+i4yBdJ+wVG0hPi+xhFQ8=; b=oZ3ucd7nzUTF/dDakhdjR4W3epTJmeattNdaAZavk8/m+LE8bM+hjnMgBcsn1gru IGqn7S3MEl2wXgH5IDjd5UcWLnw0K+OTaIfBm1GQH7cYLa9jDSzlE4huvx8Q2xIJyCO byekvomJwF/3gVdAiFa7dfp8HJ7nvSm+KNGY6pbE= Received: from localhost.localdomain (43.250.158.93 [43.250.158.93]) by mx.zoho.in with SMTPS id 1659771853673529.5982041718509; Sat, 6 Aug 2022 13:14:13 +0530 (IST) From: Siddh Raman Pant To: Eric Biggers , Jonathan Corbet , David Howells , Randy Dunlap , Mauro Carvalho Chehab , Christophe JAILLET , Eric Dumazet Cc: linux-kernel , linux-kernel-mentees Message-ID: Subject: [PATCH v2 1/2] include/linux/watch_queue: Improve documentation Date: Sat, 6 Aug 2022 13:13:41 +0530 X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce kerneldoc-style comments, and document a couple of things explicitly. Signed-off-by: Siddh Raman Pant --- include/linux/watch_queue.h | 102 ++++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index fc6bba20273b..7f8b1f15634b 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -18,57 +18,103 @@ =20 struct cred; =20 +/** + * struct watch_type_filter - Filter on watch type + * + * @type: Type of watch_notification + * @subtype_filter: Bitmask of subtypes to filter on + * @info_filter: Filter on watch_notification::info + * @info_mask: Mask of relevant bits in info_filter + */ struct watch_type_filter { enum watch_notification_type type; - __u32 subtype_filter[1]; /* Bitmask of subtypes to filter on */ - __u32 info_filter; /* Filter on watch_notification::info */ - __u32 info_mask; /* Mask of relevant bits in info_filter */ + __u32 subtype_filter[1]; + __u32 info_filter; + __u32 info_mask; }; =20 +/** + * struct watch_filter - Filter on watch + * + * @rcu: RCU head (in union with type_filter) + * @type_filter: Bitmask of accepted types (in union with rcu) + * @nr_filters: Number of filters + * @filters: Array of watch_type_filter + */ struct watch_filter { union { struct rcu_head rcu; - /* Bitmask of accepted types */ DECLARE_BITMAP(type_filter, WATCH_TYPE__NR); }; - u32 nr_filters; /* Number of filters */ + u32 nr_filters; struct watch_type_filter filters[]; }; =20 +/** + * struct watch_queue - General notification queue + * + * @rcu: RCU head + * @filter: Filter to use on watches + * @pipe: The pipe we're using as a buffer + * @watches: Contributory watches + * @notes: Preallocated notifications + * @notes_bitmap: Allocation bitmap for notes + * @usage: Object usage count + * @lock: To serialize accesses and removes + * @nr_notes: Number of notes + * @nr_pages: Number of pages in notes[] + * @defunct: True when queues closed + */ struct watch_queue { struct rcu_head rcu; struct watch_filter __rcu *filter; - struct pipe_inode_info *pipe; /* The pipe we're using as a buffer */ - struct hlist_head watches; /* Contributory watches */ - struct page **notes; /* Preallocated notifications */ - unsigned long *notes_bitmap; /* Allocation bitmap for notes */ - struct kref usage; /* Object usage count */ + struct pipe_inode_info *pipe; + struct hlist_head watches; + struct page **notes; + unsigned long *notes_bitmap; + struct kref usage; spinlock_t lock; - unsigned int nr_notes; /* Number of notes */ - unsigned int nr_pages; /* Number of pages in notes[] */ - bool defunct; /* T when queues closed */ + unsigned int nr_notes; + unsigned int nr_pages; + bool defunct; }; =20 -/* - * Representation of a watch on an object. +/** + * struct watch - Representation of a watch on an object + * + * @rcu: RCU head (in union with info_id) + * @info_id: ID to be OR'd in to info field (in union with rcu) + * @queue: Queue to post events to + * @queue_node: Link in queue->watches + * @watch_list: Link in watch_list->watchers + * @list_node: The list node + * @cred: Creds of the owner of the watch + * @private: Private data for the watched object + * @id: Internal identifier + * @usage: Object usage count */ struct watch { union { struct rcu_head rcu; - u32 info_id; /* ID to be OR'd in to info field */ + u32 info_id; }; - struct watch_queue __rcu *queue; /* Queue to post events to */ - struct hlist_node queue_node; /* Link in queue->watches */ + struct watch_queue __rcu *queue; + struct hlist_node queue_node; struct watch_list __rcu *watch_list; - struct hlist_node list_node; /* Link in watch_list->watchers */ - const struct cred *cred; /* Creds of the owner of the watch */ - void *private; /* Private data for the watched object */ - u64 id; /* Internal identifier */ - struct kref usage; /* Object usage count */ + struct hlist_node list_node; + const struct cred *cred; + void *private; + u64 id; + struct kref usage; }; =20 -/* - * List of watches on an object. +/** + * struct watch_list - List of watches on an object + * + * @rcu: RCU head + * @watchers: List head + * @release_watch: Function to release watch + * @lock: To protect addition and removal of watches */ struct watch_list { struct rcu_head rcu; @@ -118,8 +164,10 @@ static inline void remove_watch_list(struct watch_list= *wlist, u64 id) } =20 /** - * watch_sizeof - Calculate the information part of the size of a watch re= cord, - * given the structure size. + * watch_sizeof() - Calculate the information part of the size of a watch + * record, given the structure size. + * + * @STRUCT: The structure whose size is to be given */ #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT) =20 --=20 2.35.1 From nobody Sat Apr 20 01:38:51 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1A8BC25B06 for ; Sat, 6 Aug 2022 07:45:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241742AbiHFHpD (ORCPT ); Sat, 6 Aug 2022 03:45:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241672AbiHFHo6 (ORCPT ); Sat, 6 Aug 2022 03:44:58 -0400 Received: from sender-of-o53.zoho.in (sender-of-o53.zoho.in [103.117.158.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19A9911C35 for ; Sat, 6 Aug 2022 00:44:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659771858; cv=none; d=zohomail.in; s=zohoarc; b=ZhewqxYZXZ+4d92N7/6+E+8roDitfj9snck27koQpePAE3WFuk9MjvSwIGz6eWGCFsV02NvYCyXgHjlcFYzpshrM4HYwvhhVFUEknCgVLTEyUrktXLIGER38MjWFJt4+UKcfvW/AQOzj2uYhK+wNJLTdvfB9NkK0w+PTsFa4FNU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1659771858; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=CciC0JA+1Sl6P741HLOLO7oPE18KzPzTWUnJ9g4NcCY=; b=cThpGGll9oucn4GJNKQsynEm1XnwpouK/xSplDvh7EmGcmv1JQV1HiDQYNz1ZyKZF9/qgn0BLQvGxm4z1cPHh3yr4nThvtfxGzQNXC7Mp80DVRyKbCj/uO00qnfCIvwnYQPm/T6c0GvGRLUYueBru+KgtpLIi58OMt2Iq1eVe+E= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1659771858; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=CciC0JA+1Sl6P741HLOLO7oPE18KzPzTWUnJ9g4NcCY=; b=JM4WjGJ0hHN4JfmzzT4r8K6mAuU1dym79SIEZ/Y2OH3I+04GYX6u2zRwNNXlbves kQPJPG2nNPGEzXxrjpAKzpRvNKOc6foAddrDlVhbZngJ8XzVHTBEYCXhRKsbRLe+RlJ X+0l+Ky/Qt8f+EO+ucwhV9nwa+gC5+HS/vz8ZQJ0= Received: from localhost.localdomain (43.250.158.93 [43.250.158.93]) by mx.zoho.in with SMTPS id 165977185695375.74638919740744; Sat, 6 Aug 2022 13:14:16 +0530 (IST) From: Siddh Raman Pant To: Eric Biggers , Jonathan Corbet , David Howells , Randy Dunlap , Mauro Carvalho Chehab , Christophe JAILLET , Eric Dumazet Cc: linux-kernel , linux-kernel-mentees Message-ID: Subject: [PATCH v2 2/2] kernel/watch_queue: NULL the dangling *pipe, and use it for clear check Date: Sat, 6 Aug 2022 13:13:42 +0530 X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" NULL the dangling pipe reference while clearing watch_queue. If not done, a reference to a freed pipe remains in the watch_queue, as this function is called before freeing a pipe in free_pipe_info() (see line 834 of fs/pipe.c). The sole use of wqueue->defunct is for checking if the watch queue has been cleared, but wqueue->pipe is also NULLed while clearing. Thus, wqueue->defunct is superfluous, as wqueue->pipe can be checked for NULL. Hence, the former can be removed. Signed-off-by: Siddh Raman Pant --- include/linux/watch_queue.h | 4 +--- kernel/watch_queue.c | 12 ++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index 7f8b1f15634b..d72ad82a4435 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -55,7 +55,7 @@ struct watch_filter { * * @rcu: RCU head * @filter: Filter to use on watches - * @pipe: The pipe we're using as a buffer + * @pipe: The pipe we're using as a buffer, NULL when queue is cleared/clo= sed * @watches: Contributory watches * @notes: Preallocated notifications * @notes_bitmap: Allocation bitmap for notes @@ -63,7 +63,6 @@ struct watch_filter { * @lock: To serialize accesses and removes * @nr_notes: Number of notes * @nr_pages: Number of pages in notes[] - * @defunct: True when queues closed */ struct watch_queue { struct rcu_head rcu; @@ -76,7 +75,6 @@ struct watch_queue { spinlock_t lock; unsigned int nr_notes; unsigned int nr_pages; - bool defunct; }; =20 /** diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c index a6f9bdd956c3..a70ddfd622ee 100644 --- a/kernel/watch_queue.c +++ b/kernel/watch_queue.c @@ -43,7 +43,7 @@ MODULE_LICENSE("GPL"); static inline bool lock_wqueue(struct watch_queue *wqueue) { spin_lock_bh(&wqueue->lock); - if (unlikely(wqueue->defunct)) { + if (unlikely(!wqueue->pipe)) { spin_unlock_bh(&wqueue->lock); return false; } @@ -105,9 +105,6 @@ static bool post_one_notification(struct watch_queue *w= queue, unsigned int head, tail, mask, note, offset, len; bool done =3D false; =20 - if (!pipe) - return false; - spin_lock_irq(&pipe->rd_wait.lock); =20 mask =3D pipe->ring_size - 1; @@ -603,8 +600,11 @@ void watch_queue_clear(struct watch_queue *wqueue) rcu_read_lock(); spin_lock_bh(&wqueue->lock); =20 - /* Prevent new notifications from being stored. */ - wqueue->defunct =3D true; + /* + * This pipe will get freed by the caller free_pipe_info(). + * Removing this reference also prevents new notifications. + */ + wqueue->pipe =3D NULL; =20 while (!hlist_empty(&wqueue->watches)) { watch =3D hlist_entry(wqueue->watches.first, struct watch, queue_node); --=20 2.35.1