Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add spinlock_type.h and use small lock to protect filelist #14801

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions fs/inode/fs_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
FAR struct file *filep;
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);

filep = &list->fl_files[l1][l2];
#ifdef CONFIG_FS_REFCOUNT
Expand Down Expand Up @@ -111,7 +111,7 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
}
#endif

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);
return filep;
}

Expand Down Expand Up @@ -165,7 +165,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
}
while (++i < row);

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);

/* To avoid race condition, if the file list is updated by other threads
* and list rows is greater or equal than temp list,
Expand All @@ -174,7 +174,7 @@ static int files_extend(FAR struct filelist *list, size_t row)

if (orig_rows != list->fl_rows && list->fl_rows >= row)
{
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);

for (j = orig_rows; j < i; j++)
{
Expand All @@ -196,7 +196,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
list->fl_files = files;
list->fl_rows = row;

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);

if (tmp != NULL && tmp != &list->fl_prefile)
{
Expand Down Expand Up @@ -371,6 +371,7 @@ void files_initlist(FAR struct filelist *list)
list->fl_crefs = 1;
list->fl_files = &list->fl_prefile;
list->fl_prefile = list->fl_prefiles;
spin_lock_init(&list->fl_lock);
}

/****************************************************************************
Expand Down Expand Up @@ -589,21 +590,21 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,

/* Find free file */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);

for (; ; i++, j = 0)
{
if (i >= list->fl_rows)
{
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);

ret = files_extend(list, i + 1);
if (ret < 0)
{
return ret;
}

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);
}

do
Expand Down Expand Up @@ -632,7 +633,7 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
}

found:
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);

if (addref)
{
Expand Down
2 changes: 2 additions & 0 deletions include/nuttx/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <nuttx/spawn.h>
#include <nuttx/queue.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock_type.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -491,6 +492,7 @@ struct file

struct filelist
{
spinlock_t fl_lock; /* Manage access to the file list */
uint8_t fl_rows; /* The number of rows of fl_files array */
uint8_t fl_crefs; /* The references to filelist */
FAR struct file **fl_files; /* The pointer of two layer file descriptors array */
Expand Down
46 changes: 2 additions & 44 deletions include/nuttx/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
# include <nuttx/atomic.h>
#endif

#include <nuttx/spinlock_type.h>

#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
Expand All @@ -50,50 +52,6 @@ extern "C"
#define EXTERN extern
#endif

#if defined(CONFIG_RW_SPINLOCK)
typedef int rwlock_t;
# define RW_SP_UNLOCKED 0
# define RW_SP_READ_LOCKED 1
# define RW_SP_WRITE_LOCKED -1
#endif

#ifndef CONFIG_SPINLOCK
# define SP_UNLOCKED 0 /* The Un-locked state */
# define SP_LOCKED 1 /* The Locked state */

typedef uint8_t spinlock_t;
#elif defined(CONFIG_TICKET_SPINLOCK)

union spinlock_u
{
struct
{
unsigned short owner;
unsigned short next;
} tickets;
unsigned int value;
};
typedef union spinlock_u spinlock_t;

# define SP_UNLOCKED (union spinlock_u){{0, 0}}
# define SP_LOCKED (union spinlock_u){{0, 1}}

#else

/* The architecture specific spinlock.h header file must also provide the
* following:
*
* SP_LOCKED - A definition of the locked state value (usually 1)
* SP_UNLOCKED - A definition of the unlocked state value (usually 0)
* spinlock_t - The type of a spinlock memory object.
*
* SP_LOCKED and SP_UNLOCKED must be constants of type spinlock_t.
*/

#include <arch/spinlock.h>

#endif /* CONFIG_SPINLOCK */

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
Expand Down
90 changes: 90 additions & 0 deletions include/nuttx/spinlock_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/****************************************************************************
* include/nuttx/spinlock_type.h
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

#ifndef __INCLUDE_NUTTX_SPINLOCK_TYPE_H
#define __INCLUDE_NUTTX_SPINLOCK_TYPE_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif

#if defined(CONFIG_RW_SPINLOCK)
typedef int rwlock_t;
# define RW_SP_UNLOCKED 0
# define RW_SP_READ_LOCKED 1
# define RW_SP_WRITE_LOCKED -1
#endif

#ifndef CONFIG_SPINLOCK
# define SP_UNLOCKED 0 /* The Un-locked state */
# define SP_LOCKED 1 /* The Locked state */

typedef uint8_t spinlock_t;
#elif defined(CONFIG_TICKET_SPINLOCK)

union spinlock_u
{
struct
{
unsigned short owner;
unsigned short next;
} tickets;
unsigned int value;
};
typedef union spinlock_u spinlock_t;

# define SP_UNLOCKED (union spinlock_u){{0, 0}}
# define SP_LOCKED (union spinlock_u){{0, 1}}

#else

/* The architecture specific spinlock.h header file must also provide the
* following:
*
* SP_LOCKED - A definition of the locked state value (usually 1)
* SP_UNLOCKED - A definition of the unlocked state value (usually 0)
* spinlock_t - The type of a spinlock memory object.
*
* SP_LOCKED and SP_UNLOCKED must be constants of type spinlock_t.
*/

#include <arch/spinlock.h>

#endif /* CONFIG_SPINLOCK */

#undef EXTERN
#if defined(__cplusplus)
}
#endif

#endif /* __INCLUDE_NUTTX_SPINLOCK_TYPE_H */
2 changes: 1 addition & 1 deletion include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* SP_LOCKED and SP_UNLOCKED must constants of type spinlock_t.
*/

# include <arch/spinlock.h>
# include <nuttx/spinlock_type.h>
#endif

/****************************************************************************
Expand Down
Loading