Re: [TOMOYO 05/15](repost) Domain transition handler functions.

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <jmorris@...>
Cc: <linux-kernel@...>, <linux-security-module@...>
Date: Monday, October 15, 2007 - 8:09 am

Hello.

James Morris wrote:
OK. I posted new one that uses existing API at http://lkml.org/lkml/2007/10/11/140 .

By the way, what do you think of new primitives shown below?
I'd like to use list_for_each_cookie() and list_add_tail_mb() if acceptable.

----- Start of code -----
/********** Existing API **********/

#include <stdio.h>
#include <stdlib.h>

static inline void mb(void) {;}
static inline void prefetch(void *p) {;}
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
#define list_entry(ptr, type, member) container_of(ptr, type, member)

struct list_head {
	struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &name, &name }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)

/********** Proposed API **********/

/**
 * list_for_each_cookie - iterate over a list with cookie.
 * @pos:        the &struct list_head to use as a loop cursor.
 * @cookie:     the &struct list_head to use as a cookie.
 * @head:       the head for your list.
 *
 * Same with list_for_each except that this primitive uses cookie
 * so that we can continue iteration.
 */
#define list_for_each_cookie(pos, cookie, head) \
	for ((cookie) || ((cookie) = (head)), pos = (cookie)->next; \
	     prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
	     (cookie) = pos, pos = pos->next)

/**
 * list_add_tail_mb - add a new entry with memory barrier.
 * @new: new entry to be added.
 * @head: list head to add it before.
 *
 * Same with list_add_tail_rcu() except that this primitive uses mb()
 * so that we can traverse forwards using list_for_each() and
 * list_for_each_cookie().
 */
static inline void list_add_tail_mb(struct list_head *new,
                                    struct list_head *head)
{
	struct list_head *prev = head->prev;
	struct list_head *next = head;
	new->next = next;
	new->prev = prev;
	mb();
	next->prev = new;
	prev->next = new;
}

/********** Usage example **********/

struct something {
	struct list_head list;
	int v;
};

static LIST_HEAD(something_list);

int main(int argc, char *argv[]) {
	struct something *ptr;
	struct list_head *pos;
	struct list_head *cookie = NULL;
	int i;
	for (i = 0; i < 10; i++) {
		printf("add %d\n", i);
		ptr = malloc(sizeof(*ptr));
		ptr->v = 10 + i;
		list_add_tail_mb(&(ptr->list), &something_list);
	}
	printf("dump\n");
	i = 0;
	do {
		list_for_each_cookie(pos, cookie, &something_list) {
			ptr = list_entry(pos, struct something, list);
			if (++i % 4 == 0) { /* Interrupt such as buffer-full. */
				printf("break\n");
				break;
			}
			printf("%d\n", ptr->v);
		}
	} while (cookie);
	return 0;
}
----- End of code -----

Regards.

-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[TOMOYO 14/15](repost) LSM expansion for TOMOYO Linux., Kentaro Takeda, (Tue Oct 2, 3:39 am)
[TOMOYO 13/15](repost) Conditional permission support., Kentaro Takeda, (Tue Oct 2, 3:39 am)
[TOMOYO 12/15](repost) LSM adapter for TOMOYO., Kentaro Takeda, (Tue Oct 2, 3:38 am)
[TOMOYO 08/15](repost) Argv[0] access control functions., Kentaro Takeda, (Tue Oct 2, 3:35 am)
[TOMOYO 07/15](repost) File access control functions., Kentaro Takeda, (Tue Oct 2, 3:34 am)
[TOMOYO 06/15](repost) Auditing interface., Kentaro Takeda, (Tue Oct 2, 3:33 am)
Re: [TOMOYO 05/15](repost) Domain transition handler functio..., YOSHIFUJI Hideaki / , (Wed Oct 3, 7:43 am)
Re: [TOMOYO 05/15](repost) Domain transition handler functio..., Tetsuo Handa, (Mon Oct 15, 8:09 am)
Sleeping in RCU list traversal, Tetsuo Handa, (Sun Oct 7, 6:38 am)
Re: [TOMOYO 05/15](repost) Domain transition handler functio..., YOSHIFUJI Hideaki / , (Wed Oct 3, 10:32 am)
Re: [TOMOYO 05/15](repost) Domain transition handler functio..., YOSHIFUJI Hideaki / , (Wed Oct 3, 9:11 am)
Re: [TOMOYO 05/15](repost) Domain transition handler functio..., YOSHIFUJI Hideaki / , (Tue Oct 2, 9:00 am)