Skip to contentSkip to conversation

SMB3: Add missing locks to protect deferred close file list

PATCH In review Bharath SM 4 participants 6 messages Apr 20, 2023 – Apr 27, 2023

Subsystems (2)
lore
Share & tools
lore

Bharath SM

author
#0 lore raw

PATCH 1/1 SMB3: Add missing locks to protect deferred close file list

From: Bharath SM <[email protected]>

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

---
 fs/cifs/misc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/cifs/misc.c +9 -0
index a0d286ee723d..89bbc12e2ca7 100644
@@ -742,7 +742,10 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
 	list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
 		if (delayed_work_pending(&cfile->deferred)) {
 			if (cancel_delayed_work(&cfile->deferred)) {
+
+				spin_lock(&cifs_inode->deferred_lock);
 				cifs_del_deferred_close(cfile);
+				spin_unlock(&cifs_inode->deferred_lock);
 
 				tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 				if (tmp_list == NULL)
@@ -773,7 +776,10 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
 	list_for_each_entry(cfile, &tcon->openFileList, tlist) {
 		if (delayed_work_pending(&cfile->deferred)) {
 			if (cancel_delayed_work(&cfile->deferred)) {
+
+				spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 				cifs_del_deferred_close(cfile);
+				spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
 				tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 				if (tmp_list == NULL)
@@ -808,7 +814,10 @@ cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
 		if (strstr(full_path, path)) {
 			if (delayed_work_pending(&cfile->deferred)) {
 				if (cancel_delayed_work(&cfile->deferred)) {
+
+					spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 					cifs_del_deferred_close(cfile);
+					spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
 					tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 					if (tmp_list == NULL)
Signature · 1 line
2.34.1
#1 lore raw

Looks good. Acked-by me.

Can you add a "Fixes ..." line to the commit message.

Quoted 52 lines · Bharath SM

From: Bharath SM <[email protected]>

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

---
 fs/cifs/misc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/cifs/misc.c +9 -1
index a0d286ee723d..89bbc12e2ca7 100644
@@ -742,7 +742,10 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
        list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
                if (delayed_work_pending(&cfile->deferred)) {
                        if (cancel_delayed_work(&cfile->deferred)) {
+
+                               spin_lock(&cifs_inode->deferred_lock);
                                cifs_del_deferred_close(cfile);
+                               spin_unlock(&cifs_inode->deferred_lock);
 
                                tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
                                if (tmp_list == NULL)
@@ -773,7 +776,10 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
        list_for_each_entry(cfile, &tcon->openFileList, tlist) {
                if (delayed_work_pending(&cfile->deferred)) {
                        if (cancel_delayed_work(&cfile->deferred)) {
+
+                               spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
                                cifs_del_deferred_close(cfile);
+                               spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
                                tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
                                if (tmp_list == NULL)
@@ -808,7 +814,10 @@ cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
                if (strstr(full_path, path)) {
                        if (delayed_work_pending(&cfile->deferred)) {
                                if (cancel_delayed_work(&cfile->deferred)) {
+
+                                       spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
                                        cifs_del_deferred_close(cfile);
+                                       spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
                                        tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
                                        if (tmp_list == NULL)
--

2.34.1

author
#2 lore raw
Quoted 4 lines · ronnie sahlberg

Looks good. Acked-by me.

Can you add a "Fixes ..." line to the commit message.

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

Fixes ca08d0eac020 ("cifs: Fix memory leak on the deferred close")
Acked-by on v1 1/1
#3 lore raw
In reply to Bharath SM
Quoted 16 lines · Bharath SM

From: Bharath SM <[email protected]>

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

---
 fs/cifs/misc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/cifs/misc.c +0 -0
index a0d286ee723d..89bbc12e2ca7 100644

fs/cifs/misc.c cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode) · line 745

Quoted 4 lines
fs/cifs/misc.c +1 -0
@@ -742,7 +742,10 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
 	list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
 		if (delayed_work_pending(&cfile->deferred)) {
 			if (cancel_delayed_work(&cfile->deferred)) {
+

No need for this extra blank line. Please remove the below ones as well.

With the "Fixes:" tag added as per Ronnie's suggestion,

Quoted 30 lines
fs/cifs/misc.c +8 -0
+				spin_lock(&cifs_inode->deferred_lock);
 				cifs_del_deferred_close(cfile);
+				spin_unlock(&cifs_inode->deferred_lock);
 
 				tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 				if (tmp_list == NULL)
@@ -773,7 +776,10 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
 	list_for_each_entry(cfile, &tcon->openFileList, tlist) {
 		if (delayed_work_pending(&cfile->deferred)) {
 			if (cancel_delayed_work(&cfile->deferred)) {
+
+				spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 				cifs_del_deferred_close(cfile);
+				spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
 				tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 				if (tmp_list == NULL)
@@ -808,7 +814,10 @@ cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
 		if (strstr(full_path, path)) {
 			if (delayed_work_pending(&cfile->deferred)) {
 				if (cancel_delayed_work(&cfile->deferred)) {
+
+					spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 					cifs_del_deferred_close(cfile);
+					spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
 					tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 					if (tmp_list == NULL)
Signature · 1 line
2.34.1
author
#4 lore raw
Quoted 27 lines · Paulo Alcantara
Quoted 16 lines · Bharath SM

From: Bharath SM <[email protected]>

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

---
 fs/cifs/misc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/cifs/misc.c +0 -0
index a0d286ee723d..89bbc12e2ca7 100644

fs/cifs/misc.c cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode) · line 745

Quoted 4 lines
fs/cifs/misc.c +1 -0
@@ -742,7 +742,10 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
      list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
              if (delayed_work_pending(&cfile->deferred)) {
                      if (cancel_delayed_work(&cfile->deferred)) {
+

No need for this extra blank line. Please remove the below ones as well.

Quoted 3 lines

With the "Fixes:" tag added as per Ronnie's suggestion,

Done. Please find updated patch in attachments.

From 0db3e8aeff0973f9cbe6a55030918e1b828e1b6a Mon Sep 17 00:00:00 2001
From: Bharath SM <[email protected]>
Date: Thu, 20 Apr 2023 13:54:33 +0000
Subject: [PATCH] SMB3: Add missing locks to protect deferred close file list

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

Fixes ca08d0eac020 ("cifs: Fix memory leak on the deferred close")
---
 fs/cifs/misc.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/cifs/misc.c +6 -0
index a0d286ee723d..5f874e9c1554 100644
@@ -742,7 +742,9 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
 	list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
 		if (delayed_work_pending(&cfile->deferred)) {
 			if (cancel_delayed_work(&cfile->deferred)) {
+				spin_lock(&cifs_inode->deferred_lock);
 				cifs_del_deferred_close(cfile);
+				spin_unlock(&cifs_inode->deferred_lock);
 
 				tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 				if (tmp_list == NULL)
@@ -773,7 +775,9 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
 	list_for_each_entry(cfile, &tcon->openFileList, tlist) {
 		if (delayed_work_pending(&cfile->deferred)) {
 			if (cancel_delayed_work(&cfile->deferred)) {
+				spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 				cifs_del_deferred_close(cfile);
+				spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
 				tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 				if (tmp_list == NULL)
@@ -808,7 +812,9 @@ cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
 		if (strstr(full_path, path)) {
 			if (delayed_work_pending(&cfile->deferred)) {
 				if (cancel_delayed_work(&cfile->deferred)) {
+					spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 					cifs_del_deferred_close(cfile);
+					spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
 					tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
 					if (tmp_list == NULL)
Signature · 1 line
2.34.1
#5 lore raw
In reply to Paulo Alcantara

added to cifs-2.6.git for-next now (with the 2 acked-bys) that Fixes tag is there

Quoted 61 lines · Paulo Alcantara
Quoted 16 lines · Bharath SM

From: Bharath SM <[email protected]>

cifs_del_deferred_close function has a critical section which modifies the deferred close file list. We must acquire deferred_lock before calling cifs_del_deferred_close function.

---
 fs/cifs/misc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/cifs/misc.c +0 -0
index a0d286ee723d..89bbc12e2ca7 100644

fs/cifs/misc.c cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode) · line 745

Quoted 4 lines
fs/cifs/misc.c +1 -0
@@ -742,7 +742,10 @@ cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
      list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
              if (delayed_work_pending(&cfile->deferred)) {
                      if (cancel_delayed_work(&cfile->deferred)) {
+

No need for this extra blank line. Please remove the below ones as well.

With the "Fixes:" tag added as per Ronnie's suggestion,

Quoted 30 lines
fs/cifs/misc.c +8 -1
+                             spin_lock(&cifs_inode->deferred_lock);
                              cifs_del_deferred_close(cfile);
+                             spin_unlock(&cifs_inode->deferred_lock);
 
                              tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
                              if (tmp_list == NULL)
@@ -773,7 +776,10 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
      list_for_each_entry(cfile, &tcon->openFileList, tlist) {
              if (delayed_work_pending(&cfile->deferred)) {
                      if (cancel_delayed_work(&cfile->deferred)) {
+
+                             spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
                              cifs_del_deferred_close(cfile);
+                             spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
                              tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
                              if (tmp_list == NULL)
@@ -808,7 +814,10 @@ cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
              if (strstr(full_path, path)) {
                      if (delayed_work_pending(&cfile->deferred)) {
                              if (cancel_delayed_work(&cfile->deferred)) {
+
+                                     spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
                                      cifs_del_deferred_close(cfile);
+                                     spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
 
                                      tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
                                      if (tmp_list == NULL)
--

2.34.1

Signature · 3 lines
Thanks,

Steve